|
3 | 3 |
|
4 | 4 | This page covers how to use and configure / customize `sphinx-togglebutton`.
|
5 | 5 |
|
6 |
| -There are two main ways to use `sphinx-togglebutton`: |
| 6 | +There are three main ways to use `sphinx-togglebutton`: |
7 | 7 |
|
| 8 | +- Wrap arbitrary objects in a toggle button via a CSS selector |
8 | 9 | - Collapse admonitions with the `dropdown` class
|
9 | 10 | - Make arbitrary chunks of content "toggle-able" with the `toggle` directive
|
10 | 11 |
|
11 |
| -Both are described below |
| 12 | +Each is described below |
12 | 13 |
|
13 |
| -(dropdown-admonitions)= |
14 |
| -## Collapse admonitions with the `dropdown` class |
15 |
| - |
16 |
| -Making dropdown admonitions allows you to insert extra information in your document |
17 |
| -without forcing the user to see that content. For example: |
18 |
| - |
19 |
| -:::{admonition} What could be inside this warning? |
20 |
| -:class: warning, dropdown |
| 14 | +(use:css-selector)= |
| 15 | +## Collapse a block of content with a CSS selector |
21 | 16 |
|
22 |
| -A whale of a joke! |
| 17 | +You can hide any content and display a toggle button to show it by using certain CSS classes. |
| 18 | +`sphinx-togglebutton` will wrap elements with these classes in a `<details>` block like so: |
23 | 19 |
|
24 |
| -```{image} https://media.giphy.com/media/FaKV1cVKlVRxC/giphy.gif |
| 20 | +```html |
| 21 | +<details> |
| 22 | + <summary>Click to show</summary> |
| 23 | + <!-- Element that had a CSS class selector --> |
| 24 | +</details> |
25 | 25 | ```
|
26 |
| -::: |
27 |
| - |
28 |
| -Create a dropdown admonition by adding the `dropdown` class to an admonition directive. |
29 |
| -For example, like so: |
30 |
| - |
31 |
| -::::{tab-set} |
32 | 26 |
|
33 |
| -:::{tab-item} MyST |
| 27 | +:::{admonition} example |
| 28 | +:class: tip |
| 29 | +This MyST Markdown: |
34 | 30 |
|
35 | 31 | ````md
|
36 |
| - |
37 |
| -```{note} |
38 |
| -:class: dropdown |
39 |
| - |
40 |
| -My note |
| 32 | +```{image} https://media.giphy.com/media/FaKV1cVKlVRxC/giphy.gif |
| 33 | +:class: toggle |
41 | 34 | ```
|
42 | 35 | ````
|
43 |
| - |
44 |
| -::: |
45 |
| - |
46 |
| -:::{tab-item} reStructuredText |
47 |
| - |
48 |
| -```rst |
49 |
| -.. note:: |
50 |
| - :class: dropdown |
51 |
| -
|
52 |
| - My note |
| 36 | +results in: |
| 37 | +```{image} https://media.giphy.com/media/FaKV1cVKlVRxC/giphy.gif |
| 38 | +:class: toggle |
53 | 39 | ```
|
54 |
| - |
55 | 40 | :::
|
56 |
| -:::: |
57 |
| - |
58 |
| -You can use a custom admonition title and apply the style of a "built-in" |
59 |
| -admonition (e.g., `note`, `warning`, etc) with the `admonition::` directive: |
60 |
| - |
61 |
| -::::{tab-set} |
62 | 41 |
|
63 |
| -:::{tab-item} MyST |
| 42 | +### Configure the CSS selector used to insert toggle buttons |
64 | 43 |
|
65 |
| -````md |
66 |
| -```{admonition} Here's my title |
67 |
| -:class: dropdown, warning |
| 44 | +By default, `sphinx-togglebutton` will use this selector: |
68 | 45 |
|
69 |
| -My note |
70 | 46 | ```
|
71 |
| -```` |
72 |
| - |
73 |
| -::: |
74 |
| - |
75 |
| -:::{tab-item} reStructuredText |
76 |
| - |
77 |
| -```rst |
78 |
| -.. admonition:: Here's my title |
79 |
| - :class: dropdown, warning |
80 |
| -
|
81 |
| - My note |
| 47 | +.toggle, .admonition.dropdown |
82 | 48 | ```
|
83 | 49 |
|
84 |
| -::: |
85 |
| -:::: |
86 |
| - |
87 |
| -Creates: |
| 50 | +However, you can customize this behavior with the `togglebutton_selector` configuration value. |
| 51 | +To specify the selector to use, pass a valid CSS selector as a string: |
88 | 52 |
|
89 |
| -:::{admonition} Here's my title |
90 |
| -:class: dropdown, warning |
| 53 | +:::{admonition} example |
| 54 | +:class: tip |
| 55 | +Configure `sphinx-togglebutton` to look for a `.toggle-this-element` class and an element with ID `#my-special-id` **instead of** `.toggle` and `.admonition.dropdown`. |
91 | 56 |
|
92 |
| -My custom admonition! |
| 57 | +```python |
| 58 | +sphinx_togglebutton_selector = ".toggle-this-element, #my-special-id" |
| 59 | +``` |
93 | 60 | :::
|
94 | 61 |
|
95 |
| -To show the content by default, add a `toggle-shown` class as well. |
96 |
| - |
97 |
| -:::{tab-set-code} |
| 62 | +(dropdown-admonitions)= |
| 63 | +## Collapse admonitions with the `dropdown` class |
98 | 64 |
|
99 |
| -````markdown |
100 |
| -```{note} |
101 |
| -:class: dropdown, toggle-shown |
| 65 | +`sphinx-togglebutton` treats admonitions as a special case if they are selected. |
| 66 | +If a Sphinx admonition matches the toggle button selector, then its title will be displayed with a button to reveal its content. |
102 | 67 |
|
103 |
| -This is my note. |
| 68 | +:::{admonition} example |
| 69 | +:class: tip |
| 70 | +````md |
| 71 | +```{admonition} This will be shown |
| 72 | +:class: dropdown |
| 73 | +And this will be hidden! |
104 | 74 | ```
|
105 | 75 | ````
|
106 |
| - |
107 |
| -```rst |
108 |
| -.. note:: |
109 |
| - :class: dropdown, toggle-shown |
110 |
| -
|
111 |
| - This is my note. |
| 76 | +results in |
| 77 | +```{admonition} This will be shown |
| 78 | +:class: dropdown |
| 79 | +And this will be hidden! |
112 | 80 | ```
|
113 |
| - |
114 | 81 | :::
|
115 | 82 |
|
116 |
| -This will generate the following block: |
| 83 | +This works for any kind of Sphinx admoniton: |
117 | 84 |
|
118 | 85 | :::{note}
|
119 |
| -:class: dropdown, toggle-shown |
| 86 | +:class: dropdown |
| 87 | +A note! |
| 88 | +::: |
120 | 89 |
|
121 |
| -This is my note. |
| 90 | +:::{warning} |
| 91 | +:class: dropdown |
| 92 | +A warning! |
122 | 93 | :::
|
123 | 94 |
|
| 95 | + |
124 | 96 | (toggle-directive)=
|
125 |
| -## Toggle any content with the toggle directive |
| 97 | +## Use the `{toggle}` directive to toggle blocks of content |
126 | 98 |
|
127 | 99 | To add toggle-able content, use the **toggle directive**. This directive
|
128 | 100 | will wrap its content in a toggle-able container. You can call it like so:
|
@@ -169,12 +141,37 @@ It results in the following:
|
169 | 141 | Here is my toggle-able content!
|
170 | 142 | :::
|
171 | 143 |
|
172 |
| -## Control the togglebutton hover text |
| 144 | +## Change the button hint text |
173 | 145 |
|
174 |
| -You can control the "hint" text that is displayed next to togglebuttons when |
175 |
| -their content is collapsed. To do so, use the following configuration variable |
176 |
| -in your `conf.py` file: |
| 146 | +You can control the "hint" text that is displayed next to togglebuttons. |
| 147 | +To do so, use the following configuration variable in your `conf.py` file: |
177 | 148 |
|
178 | 149 | ```python
|
179 |
| -togglebutton_hint = "My text" |
| 150 | +togglebutton_hint = "Displayed when the toggle is closed." |
| 151 | +togglebutton_hint_hide = "Displayed when the toggle is open." |
| 152 | +``` |
| 153 | + |
| 154 | +## Change the toggle icon color |
| 155 | + |
| 156 | +You can apply some extra styles to the toggle button to achieve the look you want. |
| 157 | +This is particularly useful if the color of the toggle button does not contrast with the background of an admonition. |
| 158 | + |
| 159 | +To style the toggle button, [add a custom CSS file to your documentation](https://docs.readthedocs.io/en/stable/guides/adding-custom-css.html) and include a custom CSS selector like so: |
| 160 | + |
| 161 | +```scss |
| 162 | +// Turn the color red... |
| 163 | +// ...with admonition toggle buttons |
| 164 | +button.toggle-button { |
| 165 | + color: red; |
| 166 | +} |
| 167 | + |
| 168 | +// ...with content block toggle buttons |
| 169 | +.toggle-button summary { |
| 170 | + color: red; |
| 171 | +} |
180 | 172 | ```
|
| 173 | + |
| 174 | +## Printing behavior with toggle buttons |
| 175 | + |
| 176 | +When you print the screen while using `sphinx-togglebutton`, the toggle-able content will not show up. |
| 177 | +To reveal it for printing, you must manually un-toggle the items and then print. |
0 commit comments