Skip to content

Commit 9e73e2e

Browse files
authored
ENHANCE: Improve hiding behavior for togglebuttons (#33)
1 parent ec5b294 commit 9e73e2e

File tree

5 files changed

+66
-7
lines changed

5 files changed

+66
-7
lines changed

docs/conf.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@
8888

8989
myst_enable_extensions = ["colon_fence"]
9090

91+
# To test behavior in JS
92+
# togglebutton_hint = "test show"
93+
# togglebutton_hint_hide = "test hide"
94+
# togglebutton_open_on_print = False
95+
9196
# Add any paths that contain custom static files (such as style sheets) here,
9297
# relative to this directory. They are copied after the builtin static files,
9398
# so a file named "default.css" will overwrite the builtin "default.css".

docs/use.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,5 +173,10 @@ button.toggle-button {
173173

174174
## Printing behavior with toggle buttons
175175

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.
176+
By default `sphinx-togglebutton` will **open all toggle-able content when you print**.
177+
It will close them again when the printing operation is complete.
178+
To disable this behavior, use the following configuration in `conf.py`:
179+
180+
```python
181+
togglebutton_open_on_print = False
182+
```

sphinx_togglebutton/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ def initialize_js_assets(app, config):
1515
# Update the global context
1616
app.add_js_file(None, body=f"let toggleHintShow = '{config.togglebutton_hint}';")
1717
app.add_js_file(None, body=f"let toggleHintHide = '{config.togglebutton_hint_hide}';")
18+
open_print = str(config.togglebutton_open_on_print).lower()
19+
app.add_js_file(None, body=f"let toggleOpenOnPrint = '{open_print}';")
1820
app.add_js_file("togglebutton.js")
1921

2022

@@ -59,6 +61,7 @@ def setup(app):
5961
app.add_config_value("togglebutton_selector", ".toggle, .admonition.dropdown", "html")
6062
app.add_config_value("togglebutton_hint", "Click to show", "html")
6163
app.add_config_value("togglebutton_hint_hide", "Click to hide", "html")
64+
app.add_config_value("togglebutton_open_on_print", True, "html")
6265

6366
# Run the function after the builder is initialized
6467
app.connect("builder-inited", insert_custom_selection_config)

sphinx_togglebutton/_static/togglebutton.css

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
transition: opacity .3s, height .3s;
88
}
99

10-
/* Overrides for admonition toggles */
11-
1210
/* Toggle buttons inside admonitions so we see the title */
1311
.toggle.admonition {
1412
position: relative;
@@ -53,10 +51,8 @@ button.toggle-button {
5351
@media (min-width: 768px) {
5452
button.toggle-button.toggle-button-hidden:before {
5553
content: attr(data-toggle-hint); /* This will be filled in by JS */
56-
position: absolute;
5754
font-size: .8em;
58-
left: -6.5em;
59-
bottom: .4em;
55+
align-self: center;
6056
}
6157
}
6258

@@ -91,6 +87,7 @@ details.toggle-details summary {
9187
display: flex;
9288
align-items: center;
9389
width: fit-content;
90+
cursor: pointer;
9491
list-style: none;
9592
border-radius: .4em;
9693
border: 1px solid #ccc;
@@ -99,6 +96,14 @@ details.toggle-details summary {
9996
font-size: .9em;
10097
}
10198

99+
details.toggle-details summary:hover {
100+
background: #f6f6f6;
101+
}
102+
103+
details.toggle-details summary:active {
104+
background: #eee;
105+
}
106+
102107
details.toggle-details[open] summary {
103108
margin-bottom: .5em;
104109
}
@@ -115,3 +120,11 @@ details.toggle-details[open] summary ~ * {
115120
from {opacity: 0%;}
116121
to {opacity: 100%;}
117122
}
123+
124+
/* Print rules - we hide all toggle button elements at print */
125+
@media print {
126+
/* Always hide the summary so the button doesn't show up */
127+
details.toggle-details summary {
128+
display: none;
129+
}
130+
}

sphinx_togglebutton/_static/togglebutton.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,36 @@ const sphinxToggleRunWhenDOMLoaded = cb => {
137137
}
138138
sphinxToggleRunWhenDOMLoaded(addToggleToSelector)
139139
sphinxToggleRunWhenDOMLoaded(initToggleItems)
140+
141+
/** Toggle details blocks to be open when printing */
142+
if (toggleOpenOnPrint == "true") {
143+
window.addEventListener("beforeprint", () => {
144+
// Open the details
145+
document.querySelectorAll("details.toggle-details").forEach((el) => {
146+
el.dataset["togglestatus"] = el.open;
147+
el.open = true;
148+
});
149+
150+
// Open the admonitions
151+
document.querySelectorAll(".admonition.toggle.toggle-hidden").forEach((el) => {
152+
console.log(el);
153+
el.querySelector("button.toggle-button").click();
154+
el.dataset["toggle_after_print"] = "true";
155+
});
156+
});
157+
window.addEventListener("afterprint", () => {
158+
// Re-close the details that were closed
159+
document.querySelectorAll("details.toggle-details").forEach((el) => {
160+
el.open = el.dataset["togglestatus"] == "true";
161+
delete el.dataset["togglestatus"];
162+
});
163+
164+
// Re-close the admonition toggle buttons
165+
document.querySelectorAll(".admonition.toggle").forEach((el) => {
166+
if (el.dataset["toggle_after_print"] == "true") {
167+
el.querySelector("button.toggle-button").click();
168+
delete el.dataset["toggle_after_print"];
169+
}
170+
});
171+
});
172+
}

0 commit comments

Comments
 (0)