Skip to content

Commit 9bccf22

Browse files
sneakers-the-ratpre-commit-ci[bot]bsipocz
authored
Use admonitions to style collapsible cells :) (#656)
* Use admonitions to style collapsible cells :) * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Brigitta Sipőcz <bsipocz@gmail.com>
1 parent 4b7a0ed commit 9bccf22

File tree

2 files changed

+59
-39
lines changed

2 files changed

+59
-39
lines changed

myst_nb/sphinx_.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -492,10 +492,14 @@ def add_to_app(cls, app: Sphinx):
492492

493493
def visit_HideCellInput(self: SphinxTranslator, node: HideCodeCellNode):
494494
classes = " ".join(node["classes"])
495-
self.body.append(f'<details class="hide {classes}">\n')
495+
self.body.append(f'<details class="admonition hide {classes}">\n')
496496
self.body.append('<summary aria-label="Toggle hidden content">\n')
497-
self.body.append(f'<span class="collapsed">{escape(node["prompt_show"])}</span>\n')
498-
self.body.append(f'<span class="expanded">{escape(node["prompt_hide"])}</span>\n')
497+
self.body.append(
498+
f'<p class="collapsed admonition-title">{escape(node["prompt_show"])}</p>\n'
499+
)
500+
self.body.append(
501+
f'<p class="expanded admonition-title">{escape(node["prompt_hide"])}</p>\n'
502+
)
499503
self.body.append("</summary>\n")
500504

501505

myst_nb/static/mystnb.css

Lines changed: 52 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ div.container.cell {
4040
}
4141

4242
/* Input cells */
43-
div.cell div.cell_input,
44-
div.cell details.above-input>summary {
43+
div.cell > div.cell_input {
4544
padding-left: 0em;
4645
padding-right: 0em;
4746
border: var(--mystnb-source-border-width) var(--mystnb-source-border-color) solid;
@@ -90,57 +89,69 @@ div.cell_output div.output>div.highlight {
9089
border: 1px solid var(--mystnb-traceback-border-color);
9190
}
9291

93-
/* Collapsible cell content */
94-
div.cell details.above-input div.cell_input {
95-
border-top-left-radius: 0;
96-
border-top-right-radius: 0;
97-
border-top: var(--mystnb-source-border-width) var(--mystnb-source-border-color) dashed;
92+
/* --- Collapsible cell content --- */
93+
94+
/*
95+
encourage summary container to blend in with its parent.
96+
p.admonition-title should hold the title styles.
97+
*/
98+
div.cell details.hide summary {
99+
border-left: unset;
100+
padding: inherit;
101+
margin: inherit;
102+
background-color: inherit;
98103
}
99104

100-
div.cell div.cell_input.above-output-prompt {
101-
border-bottom-left-radius: 0;
102-
border-bottom-right-radius: 0;
105+
/* Neighboring input/output elements - spacing, borders */
106+
div.cell details.hide.above-input + details.below-input,
107+
div.cell div.cell_input + details.below-input
108+
{
109+
margin-top: 0;
110+
}
111+
112+
div.cell details.hide.above-input:has(+ details.below-input),
113+
div.cell div.cell_input:has(+ details.below-input)
114+
{
115+
margin-bottom: 0;
103116
}
104117

105-
div.cell details.above-input>summary {
118+
div.cell:has(> *:nth-child(2)) div.cell_input:first-child,
119+
div.cell:has(> *:nth-child(2)) details:first-child
120+
{
106121
border-bottom-left-radius: 0;
107122
border-bottom-right-radius: 0;
108-
border-bottom: var(--mystnb-source-border-width) var(--mystnb-source-border-color) dashed;
109-
padding-left: 1em;
110-
margin-bottom: 0;
111123
}
112124

113-
div.cell details.above-output>summary {
114-
background-color: var(--mystnb-source-bg-color);
115-
padding-left: 1em;
116-
padding-right: 0em;
117-
border: var(--mystnb-source-border-width) var(--mystnb-source-border-color) solid;
118-
border-radius: var(--mystnb-source-border-radius);
119-
border-left-color: var(--mystnb-source-margin-color);
120-
border-left-width: medium;
125+
div.cell:has(> *:nth-child(2)) div.cell_input:last-child,
126+
div.cell:has(> *:nth-child(2)) details:last-child
127+
{
128+
border-top-left-radius: 0;
129+
border-top-right-radius: 0;
121130
}
122131

123-
div.cell details.below-input>summary {
124-
background-color: var(--mystnb-source-bg-color);
125-
padding-left: 1em;
126-
padding-right: 0em;
127-
border: var(--mystnb-source-border-width) var(--mystnb-source-border-color) solid;
128-
border-top: none;
129-
border-bottom-left-radius: var(--mystnb-source-border-radius);
130-
border-bottom-right-radius: var(--mystnb-source-border-radius);
131-
border-left-color: var(--mystnb-source-margin-color);
132-
border-left-width: medium;
132+
/* intra-label styles for collapsibles */
133+
div.cell.container details.hide.above-input>summary,
134+
div.cell.container details.hide.below-input>summary,
135+
div.cell.container details.hide.above-output>summary
136+
{
137+
display: block;
138+
border-left: none;
133139
}
134140

135-
div.cell details.hide>summary>span {
136-
opacity: var(--mystnb-hide-prompt-opacity);
141+
div.cell details.hide>summary>p.admonition-title {
142+
display: list-item;
143+
margin-bottom: 0;
137144
}
138145

139-
div.cell details.hide[open]>summary>span.collapsed {
146+
div.cell details.hide:not([open]) {
147+
padding-bottom: 0;
148+
}
149+
150+
div.cell details.hide[open]>summary>p.collapsed {
140151
display: none;
141152
}
142153

143-
div.cell details.hide:not([open])>summary>span.expanded {
154+
div.cell details.hide:not([open])>summary>p.expanded {
144155
display: none;
145156
}
146157

@@ -159,6 +170,11 @@ div.cell details.hide[open]>summary~* {
159170
animation: collapsed-fade-in 0.3s ease-in-out;
160171
}
161172

173+
/* Clear conflicting styles for details and admonitions set by some themes */
174+
div.cell details.admonition summary::before {
175+
content: unset;
176+
}
177+
162178
/* Math align to the left */
163179
.cell_output .MathJax_Display {
164180
text-align: left !important;

0 commit comments

Comments
 (0)