Skip to content

Commit ec5b294

Browse files
authored
ENH: Enhance toggle button design and layout (#32)
1 parent 5cd606a commit ec5b294

File tree

10 files changed

+343
-247
lines changed

10 files changed

+343
-247
lines changed

docs/conf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,9 @@
7474
# a list of builtin themes.
7575
#
7676
html_theme = "sphinx_book_theme"
77-
# html_theme = "sphinx_rtd_theme"
77+
# html_theme = "sphinx_rtd_theme" # These are just for testing
7878
# html_theme = "alabaster"
79+
# html_theme = "furo"
7980

8081
html_theme_options = {
8182
"repository_url": "https://github.com/executablebooks/sphinx-togglebutton",
@@ -102,7 +103,6 @@
102103
#
103104
# html_sidebars = {}
104105

105-
106106
# -- Options for HTMLHelp output ---------------------------------------------
107107

108108
# Output file base name for HTML help builder.

docs/index.md

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,58 @@
11
# `sphinx-togglebutton`
22

33
A small sphinx extension to add "toggle button" elements to sections of your page.
4-
This allows you to:
4+
For example:
55

6-
- Collapse admonitions (notes, warnings, etc) so that their content is hidden
7-
until users click a toggle button. See {ref}`dropdown-admonitions`.
8-
- Collapse arbitrary chunks of content on your page with a `toggle` directive.
9-
See {ref}`toggle-directive`.
6+
## Collapse admonitions
107

11-
:::{admonition} For example, click the "+" button to the right:
8+
You can collapse admonitions (notes, warnings, etc) so that their content is hidden until users click the admonition title.
9+
10+
:::{admonition} Example: click this title to toggle the content
11+
:class: dropdown
12+
You can toggle any admonition to hide its content behind a user click!
13+
Do so by adding a `dropdown` class to the admonition, like this:
14+
15+
````
16+
```{note}
1217
:class: dropdown
1318
14-
Here's a toggled note! You can put all kinds of stuff in here!
19+
Some content
20+
```
21+
````
1522
:::
1623

17-
You can also add a toggle button to arbitrary chunks of content.
18-
For example, click the toggle button to the right just below.
24+
See {ref}`dropdown-admonitions` for more information.
25+
26+
## Hide any content behind a toggle button
27+
28+
You can also hide arbitrary content behind a toggle button.
29+
When users press the button, they will see the content.
30+
For example:
1931

2032
::::{toggle}
21-
:::{admonition} Wow!
22-
:class: tip
2333

24-
It's a code block!
34+
This is a toggled content block!
35+
It was added like this:
2536

26-
```python
27-
a = "wow, very python"
37+
````
38+
```{toggle}
39+
This is a toggled content block!
2840
```
29-
:::
41+
````
42+
3043
::::
3144

32-
See {ref}`usage` for more information.
45+
You can either do this with a `{toggle}` directive, or by adding a `toggle` CSS class to any elements you'd like hidden behind a toggle button.
3346

34-
:::{admonition} Check out sphinx-panels as well!
47+
See [](use:css-selector) for more details.
48+
49+
:::{admonition} Check out sphinx-design as well!
3550
:class: tip
3651

3752
For a bootstrap-based "dropdown" directive that uses pure CSS, check out
38-
[Sphinx Panels](https://sphinx-panels.readthedocs.io/en/latest/#dropdown-usage)
53+
[Sphinx Design](https://sphinx-design.readthedocs.io/en/latest/dropdowns.html)
3954
:::
4055

41-
```{toctree}
42-
use
43-
reference
44-
```
45-
4656
## Installation
4757

4858
You can install `sphinx-togglebutton` with `pip`:
@@ -65,3 +75,10 @@ extensions = [
6575
```
6676

6777
See {ref}`usage` for information about how to use `sphinx-togglebutton`.
78+
79+
80+
```{toctree}
81+
:maxdepth: 2
82+
use
83+
reference/index
84+
```
File renamed without changes.

docs/use.md

Lines changed: 84 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -3,126 +3,98 @@
33

44
This page covers how to use and configure / customize `sphinx-togglebutton`.
55

6-
There are two main ways to use `sphinx-togglebutton`:
6+
There are three main ways to use `sphinx-togglebutton`:
77

8+
- Wrap arbitrary objects in a toggle button via a CSS selector
89
- Collapse admonitions with the `dropdown` class
910
- Make arbitrary chunks of content "toggle-able" with the `toggle` directive
1011

11-
Both are described below
12+
Each is described below
1213

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
2116

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:
2319

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>
2525
```
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}
3226

33-
:::{tab-item} MyST
27+
:::{admonition} example
28+
:class: tip
29+
This MyST Markdown:
3430

3531
````md
36-
37-
```{note}
38-
:class: dropdown
39-
40-
My note
32+
```{image} https://media.giphy.com/media/FaKV1cVKlVRxC/giphy.gif
33+
:class: toggle
4134
```
4235
````
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
5339
```
54-
5540
:::
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}
6241

63-
:::{tab-item} MyST
42+
### Configure the CSS selector used to insert toggle buttons
6443

65-
````md
66-
```{admonition} Here's my title
67-
:class: dropdown, warning
44+
By default, `sphinx-togglebutton` will use this selector:
6845

69-
My note
7046
```
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
8248
```
8349

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:
8852

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`.
9156

92-
My custom admonition!
57+
```python
58+
sphinx_togglebutton_selector = ".toggle-this-element, #my-special-id"
59+
```
9360
:::
9461

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
9864

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.
10267

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!
10474
```
10575
````
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!
11280
```
113-
11481
:::
11582

116-
This will generate the following block:
83+
This works for any kind of Sphinx admoniton:
11784

11885
:::{note}
119-
:class: dropdown, toggle-shown
86+
:class: dropdown
87+
A note!
88+
:::
12089

121-
This is my note.
90+
:::{warning}
91+
:class: dropdown
92+
A warning!
12293
:::
12394

95+
12496
(toggle-directive)=
125-
## Toggle any content with the toggle directive
97+
## Use the `{toggle}` directive to toggle blocks of content
12698

12799
To add toggle-able content, use the **toggle directive**. This directive
128100
will wrap its content in a toggle-able container. You can call it like so:
@@ -169,12 +141,37 @@ It results in the following:
169141
Here is my toggle-able content!
170142
:::
171143

172-
## Control the togglebutton hover text
144+
## Change the button hint text
173145

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:
177148

178149
```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+
}
180172
```
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.

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
license="MIT License",
2525
packages=find_packages(),
2626
package_data={
27-
"sphinx_togglebutton": ["_static/togglebutton.css_t", "_static/togglebutton.js"]
27+
"sphinx_togglebutton": ["_static/togglebutton.css", "_static/togglebutton.js", "_static/togglebutton-chevron.svg"]
2828
},
2929
install_requires=["setuptools", "wheel", "sphinx", "docutils"],
3030
extras_require={"sphinx": ["matplotlib", "myst_nb", "sphinx_book_theme", "sphinx_design"]},

sphinx_togglebutton/__init__.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ def st_static_path(app):
1111
app.config.html_static_path.append(static_path)
1212

1313

14-
def add_to_context(app, config):
14+
def initialize_js_assets(app, config):
1515
# Update the global context
16-
config.html_context.update({"togglebutton_hint": config.togglebutton_hint})
16+
app.add_js_file(None, body=f"let toggleHintShow = '{config.togglebutton_hint}';")
17+
app.add_js_file(None, body=f"let toggleHintHide = '{config.togglebutton_hint_hide}';")
18+
app.add_js_file("togglebutton.js")
1719

1820

1921
# This function reads in a variable and inserts it into JavaScript
@@ -56,11 +58,11 @@ def setup(app):
5658
# Tell Sphinx about this configuration variable
5759
app.add_config_value("togglebutton_selector", ".toggle, .admonition.dropdown", "html")
5860
app.add_config_value("togglebutton_hint", "Click to show", "html")
59-
app.add_js_file("togglebutton.js")
61+
app.add_config_value("togglebutton_hint_hide", "Click to hide", "html")
6062

6163
# Run the function after the builder is initialized
6264
app.connect("builder-inited", insert_custom_selection_config)
63-
app.connect("config-inited", add_to_context)
65+
app.connect("config-inited", initialize_js_assets)
6466
app.add_directive("toggle", Toggle)
6567
return {
6668
"version": __version__,

0 commit comments

Comments
 (0)