Debian Codegraph @

docs/user_guide/light-dark.rst

raw
1
2 .. _manage-themes:
3
4 Light and dark themes
5 =====================
6
7 You can change the major background/foreground colors of this theme using
8 built-in "dark" and "light" modes.
9 These are controlled by a button in the navigation header, with the following options:
10
11 - A ``light`` theme with a bright background and dark text / UI elements
12 - A ``dark`` theme with a dark background and light text / UI elements
13 - ``auto``: the documentation theme will follow the system default that you have set
14
15
16 Configure default theme mode
17 ----------------------------
18
19 By default, visitors to your documentation will use the theme mode ``auto``.
20 This will choose a theme based on the user's system settings, and default to
21 ``light`` if no system setting is found.
22
23 If you wish to use a different default theme mode, set the ``default_mode`` configuration to one of ``auto``, ``dark``, ``light``.
24 For example:
25
26 .. code-block:: python
27
28 html_context = {
29 # ...
30 "default_mode": "light"
31 }
32
33 For more information, see :ref:`manage-themes`.
34
35 .. tip::
36
37 To completely remove the theme management, configure ``default_mode`` to the value you want in your documentation (``light`` or ``dark``) and then remove the theme-switcher from the ``navbar_end`` section of the header navbar configuration:
38
39 .. code-block:: python
40
41 html_theme_options {
42 ...
43 # Note we have omitted `theme-switcher` below
44 "navbar_end": ["navbar-icon-links"]
45 }
46
47 Customize the CSS of light and dark themes
48 ------------------------------------------
49
50 .. danger::
51
52 Theming is still a beta feature, so the variables related to color theming are likely to change in the future. No backward compatibility is guaranteed when customization is done.
53
54
55 To customize the CSS of page elements in a theme-dependent manner, use the ``html[data-theme='<THEME>']`` CSS selector.
56 For example to define a different background color for both the light and dark themes:
57
58 .. code-block:: css
59
60 /* anything related to the light theme */
61 html[data-theme="light"] {
62
63 /* whatever you want to change */
64 background-color: white;
65 }
66
67 /* anything related to the dark theme */
68 html[data-theme="dark"] {
69
70 /* whatever you want to change */
71 background-color: black;
72 }
73
74 A complete list of the colors used in this theme can be found in the :doc:`CSS style section <styling>`.
75
76 Theme-dependent images and content
77 ----------------------------------
78
79 It is possible to use different content for light and dark modes so that the content only shows up when a particular theme is active.
80 This is useful if your content depends on the theme's style, such as a PNG image with a light or dark background.
81
82 There are **two CSS helper classes** to specify items on the page as theme-specific.
83 These are:
84
85 - :code:`only-dark`: Only display an element when the dark theme is active.
86 - :code:`only-light` Only display an element when the light theme is active.
87
88 For example, the following page content defines two images, each of which uses a different one of the classes above.
89 Change the theme and a new image should be displayed.
90
91 .. tab-set::
92
93 .. tab-item:: rst
94
95 .. code-block:: rst
96
97 .. image:: https://picsum.photos/id/9/200/200
98 :class: only-dark
99
100 .. image:: https://picsum.photos/id/1025/200/200
101 :class: only-light
102
103 .. tab-item:: markdown
104
105 .. code-block:: md
106
107 ```{image} https://picsum.photos/id/9/200/200
108 :class: only-dark
109 ```
110
111 ```{image} https://picsum.photos/id/1025/200/200
112 :class: only-light
113 ```
114
115 .. image:: https://picsum.photos/id/9/200/200
116 :class: only-dark
117
118 .. image:: https://picsum.photos/id/1025/200/200
119 :class: only-light
120
121 Images and content that work in both themes
122 -------------------------------------------
123
124 When the **dark theme** is activated, images that do not support dark mode will
125 automatically have a white background added to ensure the image contents are
126 visible, and their brightness will be reduced by a filter.
127
128 If your image is suitable for the dark theme, add the CSS class
129 :code:`only-dark` as noted above. If your image is suitable for both light and
130 dark themes, add the CSS class :code:`dark-light` to make your image
131 theme-agnostic.
132
133 For example, here's an image without adding this helper class.
134 Change to the dark theme and a grey background will be present.
135
136 .. tab-set::
137
138 .. tab-item:: rst
139
140 .. code-block:: rst
141
142 .. image:: https://picsum.photos/id/237/200/200
143 :class: p-2
144
145 .. tab-item:: markdown
146
147 .. code-block:: md
148
149 ```{image} https://picsum.photos/id/237/200/200
150 :class: p-2
151 ```
152
153
154 .. image:: https://picsum.photos/id/237/200/200
155 :class: p-2
156
157 Here's the same image with this class added:
158
159 .. tab-set::
160
161 .. tab-item:: rst
162
163 .. code-block:: rst
164
165 .. image:: https://picsum.photos/id/237/200/200
166 :class: dark-light
167
168 .. tab-item:: markdown
169
170 .. code-block:: md
171
172 ```{image} https://picsum.photos/id/237/200/200
173 :class: dark-light p-2
174 ```
175
176 .. image:: https://picsum.photos/id/237/200/200
177 :class: dark-light p-2
178
179 Define custom JavaScript to react to theme changes
180 --------------------------------------------------
181
182 You can define a JavaScript event hook that will run your code any time the theme changes.
183 This is useful if you need to change elements of your page that cannot be defined by CSS rules.
184 For example, to change an image source (e.g., logo) whenever the ``data-theme`` changes, a snippet like this can be used:
185
186 .. tab-set::
187
188 .. tab-item:: rst
189
190 .. code-block:: rst
191
192 .. raw:: html
193
194 <script type="text/javascript">
195 var observer = new MutationObserver(function(mutations) {
196 const dark = document.documentElement.dataset.theme == 'dark';
197 document.getElementsByClassName('mainlogo')[0].src = dark ? '_static/my_logo_dark.svg' : "_static/my_logo_light.svg";
198 })
199 observer.observe(document.documentElement, {attributes: true, attributeFilter: ['data-theme']});
200 </script>
201 <link rel="preload" href="_static/my_logo_dark.svg" as="image">
202
203 .. image:: _static/my_logo_light.svg
204 :alt: My Logo
205 :class: logo, mainlogo
206 :align: center
207
208 .. tab-item:: markdown
209
210 .. code-block:: md
211
212 <script type="text/javascript">
213 var observer = new MutationObserver(function(mutations) {
214 const dark = document.documentElement.dataset.theme == 'dark';
215 document.getElementsByClassName('mainlogo')[0].src = dark ? '_static/my_logo_dark.svg' : "_static/my_logo_light.svg";
216 })
217 observer.observe(document.documentElement, {attributes: true, attributeFilter: ['data-theme']});
218 </script>
219 <link rel="preload" href="_static/my_logo_dark.svg" as="image">
220
221 ```{image} _static/my_logo_light.svg
222 :alt: My Logo
223 :class: logo, mainlogo
224 :align: center
225 ```
226
227 The JavaScript reacts to ``data-theme`` changes to alter ``img``, and the ``link`` is used to preload the dark image.
228 See the `MutationObserver documentation <https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver>`_ for more information.