|
| 1 | +# Customizing the Appearance of Your Documentation Pages |
| 2 | + |
| 3 | +Customize the look and feel of your documentation webpages. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +By default, rendered documentation webpages produced by DocC come with a default |
| 8 | +visual styling. If you wish, you may make adjustments to this styling by adding |
| 9 | +an optional `theme-settings.json` file to the root of your documentation catalog |
| 10 | +with some configuration. This file is used to customize various things like |
| 11 | +colors and fonts. You can even make changes to the way that specific elements |
| 12 | +appear, like buttons, code listings, and asides. Additionally, some metadata for |
| 13 | +the website can be configured in this file, and it can also be used to opt in or |
| 14 | +opt out of certain default rendering behavior. |
| 15 | + |
| 16 | +If you're curious about the full capabilities of the `theme-settings.json` file, |
| 17 | +you can check out the latest Open API specification for it [here][1], which |
| 18 | +documents all valid settings that could be present. |
| 19 | + |
| 20 | +Let's walk through some basic examples of customizing the appearance of |
| 21 | +documentation now. |
| 22 | + |
| 23 | +### Colors |
| 24 | + |
| 25 | +DocC utilizes [CSS custom properties][2] (CSS variables) to define the various |
| 26 | +colors that are used throughout the HTML elements of rendered documentation. Any |
| 27 | +key/value in the `theme.color` object of `theme-settings.json` will either |
| 28 | +update an existing color property used in the renderer or add a new one that |
| 29 | +could be referenced by existing ones. |
| 30 | + |
| 31 | +**Example** |
| 32 | + |
| 33 | +As a simple example, the following configuration would update the background |
| 34 | +fill color for the "documentation intro" element to `"rebeccapurple"` by |
| 35 | +changing the value of the CSS property to `--color-documentation-intro-fill: |
| 36 | +"rebeccapurple"`. |
| 37 | + |
| 38 | +```json |
| 39 | +{ |
| 40 | + "theme": { |
| 41 | + "color": { |
| 42 | + "documentation-intro-fill": "rebeccapurple" |
| 43 | + } |
| 44 | + } |
| 45 | +} |
| 46 | +``` |
| 47 | + |
| 48 | + |
| 49 | + |
| 50 | +**Example** |
| 51 | + |
| 52 | +Note that any valid CSS color value could be used. Here is a more advanced |
| 53 | +example which demonstrates how specialized dark and light versions of the main |
| 54 | +background fill and nav colors could be defined as varying lightness percentages |
| 55 | +of a green hue. |
| 56 | + |
| 57 | +```json |
| 58 | +{ |
| 59 | + "theme": { |
| 60 | + "color": { |
| 61 | + "hue-green": "120", |
| 62 | + "fill": { |
| 63 | + "dark": "hsl(var(--color-hue-green), 100%, 10%)", |
| 64 | + "light": "hsl(var(--color-hue-green), 100%, 90%)" |
| 65 | + }, |
| 66 | + "fill-secondary": { |
| 67 | + "dark": "hsl(var(--color-hue-green), 100%, 20%)", |
| 68 | + "light": "hsl(var(--color-hue-green), 100%, 80%)" |
| 69 | + }, |
| 70 | + "nav-solid-background": "var(--color-fill)" |
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | +``` |
| 75 | + |
| 76 | + |
| 77 | + |
| 78 | + |
| 79 | + |
| 80 | +As a general rule, the default color properties provided by DocC assumes a |
| 81 | +naming convention where "fill" colors are used for backgrounds and "figure" |
| 82 | +colors are used for foreground colors, like text. |
| 83 | + |
| 84 | +> Tip: |
| 85 | +> For a more complete example of a fully customized documentation website, you |
| 86 | +> can check out [this fork][3] of the DocC documentation that is using this |
| 87 | +> [theme-settings.json file][4]. |
| 88 | +
|
| 89 | +### Fonts |
| 90 | + |
| 91 | +By default, there are two main fonts used by DocC: one for monospaced text in |
| 92 | +code voice and another for all other text content. With `theme-settings.json`, |
| 93 | +you can customize the CSS `font-family` lists for these however you choose. |
| 94 | + |
| 95 | +**Example** |
| 96 | + |
| 97 | +```json |
| 98 | +{ |
| 99 | + "theme": { |
| 100 | + "typography": { |
| 101 | + "html-font": "\"Comic Sans MS\", \"Comic Sans\", cursive", |
| 102 | + "html-font-mono": "\"Courier New\", Courier, monospace" |
| 103 | + } |
| 104 | + } |
| 105 | +} |
| 106 | +``` |
| 107 | + |
| 108 | + |
| 109 | + |
| 110 | +### Borders |
| 111 | + |
| 112 | +Another aspect of the renderer that you may wish to modify is the way that it |
| 113 | +applies border to certain elements. With `theme-settings.json`, you can change |
| 114 | +the default border radius for all elements and also individually update the |
| 115 | +border styles for asides, buttons, code listings, and tutorial steps. |
| 116 | + |
| 117 | +**Example** |
| 118 | + |
| 119 | +```json |
| 120 | +{ |
| 121 | + "theme": { |
| 122 | + "aside": { |
| 123 | + "border-radius": "6px", |
| 124 | + "border-style": "double", |
| 125 | + "border-width": "3px" |
| 126 | + }, |
| 127 | + "border-radius": "0", |
| 128 | + "button": { |
| 129 | + "border-radius": "8px", |
| 130 | + "border-style": "solid", |
| 131 | + "border-width": "2px" |
| 132 | + }, |
| 133 | + "code": { |
| 134 | + "border-radius": "2px", |
| 135 | + "border-style": "dashed", |
| 136 | + "border-width": "2px" |
| 137 | + }, |
| 138 | + "tutorial-step": { |
| 139 | + "border-radius": "12px" |
| 140 | + } |
| 141 | + } |
| 142 | +} |
| 143 | +``` |
| 144 | + |
| 145 | + |
| 147 | + |
| 148 | + |
| 149 | + |
| 150 | + |
| 152 | + |
| 153 | +### Icons |
| 154 | + |
| 155 | +It is also possible to use `theme-settings.json` to specify custom icons to use |
| 156 | +in place of the default ones provided by DocC. There are a few basic steps to |
| 157 | +follow in order to override an icon: |
| 158 | + |
| 159 | +1. Find the identifier that maps to the icon you want to override. |
| 160 | +2. Add this value as an `id` attribute to the new SVG icon. |
| 161 | +3. Associate a relative or absolute URL to the new SVG icon with the identifier |
| 162 | + in `theme-settings.json`. |
| 163 | + |
| 164 | +> Note: |
| 165 | +> You can find all the possible identifier values for every valid icon kind in |
| 166 | +> the Open API [specification][1] for `theme-settings.json`. |
| 167 | +
|
| 168 | +**Example** |
| 169 | + |
| 170 | +As a simple example, here is how you could update the icon used for articles in |
| 171 | +the sidebar to be a simple box: |
| 172 | + |
| 173 | +1. Find the identifier for article icons: `"article"` |
| 174 | +2. Add the `id="article"` attribute to the new SVG icon: `simple-box.svg` |
| 175 | + ```svg |
| 176 | + <svg viewBox="0 0 14 14" xmlns="http://www.w3.org/2000/svg" id="article"> |
| 177 | + <rect x="2" y="2" width="10" height="10" stroke="currentColor" fill="none"></rect> |
| 178 | + </svg> |
| 179 | + ``` |
| 180 | +3. Configure `theme-settings.json` to map the URL for the new article icon |
| 181 | + ```json |
| 182 | + { |
| 183 | + "theme": { |
| 184 | + "icons": { |
| 185 | + "article": "/images/simple-box.svg" |
| 186 | + } |
| 187 | + } |
| 188 | + } |
| 189 | + ``` |
| 190 | + |
| 191 | + |
| 193 | + |
| 194 | +### Metadata |
| 195 | + |
| 196 | +You can specify some text that will be used in the HTML `<title>` element for |
| 197 | +all webpages in `theme-settings.json` in place of the default "Documentation" |
| 198 | +text. |
| 199 | + |
| 200 | +**Example** |
| 201 | + |
| 202 | +```json |
| 203 | +{ |
| 204 | + "meta": { |
| 205 | + "title": "Custom Title", |
| 206 | + } |
| 207 | +} |
| 208 | +``` |
| 209 | + |
| 210 | +With that configuration, the title of this page would change from "Customizing |
| 211 | +Your Documentation Appearance | Documentation" to "Customizing Your Documentation |
| 212 | +Appearance | **Custom Title**". |
| 213 | + |
| 214 | +### Feature Flags |
| 215 | + |
| 216 | +From time to time, there may be instances of various experimental UI features |
| 217 | +that can be enabled or disabled using `theme-settings.json`. |
| 218 | + |
| 219 | +**Example** |
| 220 | + |
| 221 | +As of the time of writing this, there is only one available "quick navigation" |
| 222 | +feature that is disabled by default and can be enabled with the following |
| 223 | +configuration. Note that this flag may be dropped in the future and new ones may |
| 224 | +be added as necessary for other features. |
| 225 | + |
| 226 | +```json |
| 227 | +{ |
| 228 | + "features": { |
| 229 | + "docs": { |
| 230 | + "quickNavigation": { |
| 231 | + "enable": true |
| 232 | + } |
| 233 | + } |
| 234 | + } |
| 235 | +} |
| 236 | +``` |
| 237 | + |
| 238 | +[1]: https://github.com/apple/swift-docc/blob/main/Sources/SwiftDocC/SwiftDocC.docc/Resources/ThemeSettings.spec.json |
| 239 | +[2]: https://drafts.csswg.org/css-variables/ |
| 240 | +[3]: https://mportiz08.github.io/swift-docc/documentation/docc |
| 241 | +[4]: https://mportiz08.github.io/swift-docc/theme-settings.json |
| 242 | + |
| 243 | +<!-- Copyright (c) 2022 Apple Inc and the Swift Project authors. All Rights Reserved. --> |
0 commit comments