Skip to content

Commit f1418c1

Browse files
committed
More tweaks
1 parent 1ce96e7 commit f1418c1

File tree

8 files changed

+148
-71
lines changed

8 files changed

+148
-71
lines changed

docs/_data/config-header.yml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ end:
3939
icon: fab fa-github-square
4040
title: GitHub
4141
url: https://github.com/executablebooks
42-
- type: button
43-
icon: fas fa-comments
44-
title: Forum
45-
url: https://github.com/orgs/executablebooks/discussions
42+
- type: button
43+
icon: fas fa-comments
44+
outline: true
45+
content: Discussion
46+
classes: text-primary
47+
url: https://github.com/orgs/executablebooks/discussions

docs/_static/custom.css

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
:root {
2-
--sd-color-primary: #f37726;
2+
/* WONT WORK UNTIL THE NEXT PYDATA THEME IS RELEASED
3+
--sbt-color-primary: #f37726;
4+
--sd-color-primary: var(--sbt-color-primary);
5+
*/
36
}

src/sphinx_book_theme/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,8 @@ def render_component(component):
7676
output = COMPONENT_FUNCS[kind](app, context, **component_copy)
7777
except Exception as exc:
7878
msg = f"Component render failure for:\n{component}\n\n"
79-
msg += f"Exception: {exc}"
8079
SPHINX_LOGGER.warn(msg)
81-
return
80+
raise exc
8281
return output
8382

8483
context["theme_render_component"] = render_component

src/sphinx_book_theme/_components.py

Lines changed: 69 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,66 @@
1616
def component_button(
1717
app,
1818
context,
19-
content="",
20-
title="",
21-
icon="",
22-
image="",
23-
url="",
24-
onclick="",
25-
button_id="",
26-
label_for="",
27-
id="",
28-
tooltip_placement="",
19+
content=None,
20+
title=None,
21+
icon=None,
22+
image=None,
23+
outline=None,
24+
id=None,
25+
tooltip_placement=None,
26+
url=None,
27+
onclick=None,
28+
button_id=None,
29+
label_for=None,
2930
attributes={},
3031
classes=[],
3132
):
33+
"""Render a clickable button.
34+
35+
There are three possible actions that will be triggered,
36+
corresponding to different kwargs having values.
37+
38+
Meta Parameters
39+
---------------
40+
app: An instance of sphinx.Application
41+
context: A Sphinx build context dictionary
42+
43+
General parameters
44+
------------------
45+
content: Content to populate inside the button.
46+
title: A tooltip / accessibility-friendly title.
47+
icon: A tiny square icon. A set of FontAwesome icon classes, or path to an image.
48+
image: A larger image of any aspect ratio. A path to a local or remote image.
49+
button_id: The ID to be added to this button.
50+
outline: Whether to outline the button.
51+
tooltip_placement: Whether the tooltip will be to the left, right, top, or bottom.
52+
attributes: A dictionary of any key:val attributes to add to the button.
53+
classes: A list of CSS classes to add to the button.
54+
55+
Action-specific parameters
56+
--------------------------
57+
url: The URL to which a button will direct when clicked.
58+
onclick: JavaScript that will be called when a person clicks.
59+
label_for: The input this label should trigger when clicked (button is a label).
60+
"""
61+
# Set up attributes and classes that will be used to create HTML attributes at end
3262
attributes = attributes.copy()
33-
attributes.update({"type": "button", "class": ["btn", "icon-button"]})
34-
attributes["class"].extend(classes.copy())
63+
attributes.update({"type": "button"})
64+
65+
# Update classes with custom added ones
66+
default_classes = ["btn", "icon-button"]
67+
if classes:
68+
if isinstance(classes, str):
69+
classes = [classes]
70+
else:
71+
classes = []
72+
classes.extend(default_classes)
73+
74+
# Give an outline if desired.
75+
if outline:
76+
classes.append("btn-outline")
77+
78+
# Checks for proper arguments
3579
btn_content = ""
3680
if url and onclick:
3781
raise Exception("Button component cannot have both url and onclick specified.")
@@ -62,33 +106,23 @@ def component_button(
62106
"""
63107

64108
if not content:
65-
attributes["class"].append("icon-button-no-content")
109+
classes.append("icon-button-no-content")
66110
else:
67-
btn_content += content
111+
btn_content += f'<span class="btn__content-container">{content}</span>'
68112

69113
if button_id:
70114
attributes["id"] = button_id
71115

72-
attributes["aria-label"] = title
73-
74-
# Handle tooltips
75-
title = context["translate"](title)
76-
tooltip_placement = "bottom" if not tooltip_placement else tooltip_placement
77-
78-
# If we're already using data-toggle, wrap the button content in a span.
79-
# This lets us use another data-toggle.
80-
if "data-toggle" in attributes:
81-
btn_content = f"""
82-
<span data-toggle="tooltip" data-placement="{tooltip_placement}" title="{title}">
83-
{btn_content}
84-
</span>
85-
""" # noqa
86-
else:
116+
# Handle tooltips if a title is given
117+
if title:
118+
title = context["translate"](title)
119+
tooltip_placement = "bottom" if not tooltip_placement else tooltip_placement
120+
attributes["aria-label"] = title
87121
attributes["data-placement"] = tooltip_placement
88122
attributes["title"] = title
89123

90124
# Convert all the options for the button into a string of HTML attributes
91-
attributes["class"] = " ".join(attributes["class"])
125+
attributes["class"] = " ".join(classes)
92126
attributes_str = " ".join([f'{key}="{val}"' for key, val in attributes.items()])
93127

94128
# Generate the button HTML
@@ -163,13 +197,17 @@ def component_dropdown(
163197
dropdown_id = "menu-dropdown-"
164198
dropdown_id += hashlib.md5(dropdown_items.encode("utf-8")).hexdigest()[:5]
165199

166-
# Generate the button HTML
200+
# Generate the dropdown button HTML
167201
dropdown_attributes = {
168202
"data-toggle": "dropdown",
169203
"aria-haspopup": "true",
170204
"aria-expanded": "false",
171205
"type": "button",
172206
}
207+
if "title" in kwargs:
208+
SPHINX_LOGGER.warn("Cannot use title / tooltip with dropdown menu. Removing.")
209+
kwargs.pop("title")
210+
173211
html_button = component_button(
174212
app,
175213
context,

src/sphinx_book_theme/assets/styles/components/_buttons.scss

Lines changed: 62 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,6 @@
1313
}
1414
}
1515

16-
// TODO NEW RULES THAT I NEED TO INCORPORATE ABOVE and in other places
17-
// Used when there's a solo icon with no text, so make the icon a bit bigger
18-
19-
div.dropdown-menu button.btn {
20-
display: flex;
21-
align-items: center;
22-
}
23-
2416
.icon-button {
2517
padding: 0.2em 0.2em;
2618
margin-bottom: 0;
@@ -39,32 +31,73 @@ div.dropdown-menu button.btn {
3931
font-size: 1.3rem;
4032
}
4133

42-
.dropdown-menu {
43-
// Copied from dropdown menu style above
44-
border-radius: $box-border-radius;
45-
box-shadow: 0px 3px 10px 0px rgba(0, 0, 0, 0.25);
34+
div.dropdown {
35+
// Control the dropdown menu display
4636

47-
button {
48-
padding-left: 0.5rem;
49-
width: 100%;
50-
border-radius: 0;
51-
52-
&:hover {
53-
background-color: #eee;
54-
}
37+
// First, overwrite the Bootstrap default so we can make nicer animations
38+
// These make it "displayed" but hidden
39+
.dropdown-menu {
40+
display: block;
41+
visibility: hidden;
42+
opacity: 0;
43+
transition: visibility 50ms ease-out, opacity 150ms ease-out 50ms;
5544
}
56-
a:hover {
57-
text-decoration: none;
45+
46+
// On hover, we display the contents via visibility and opacity
47+
&:hover div.dropdown-menu,
48+
div.dropdown-menu:hover {
49+
visibility: visible;
50+
opacity: 1;
51+
// Remove the delay when hovering, which makes it appear instantly, but delay in disappear
52+
transition-delay: 0ms;
5853
}
5954

60-
span.btn__icon-container {
61-
display: flex;
62-
margin-right: 0.5rem;
55+
// Other styling on the dropdown menu
56+
.dropdown-menu {
57+
// Copied from dropdown menu style above
58+
border-radius: $box-border-radius;
59+
box-shadow: 0px 3px 10px 0px rgba(0, 0, 0, 0.25);
60+
61+
button {
62+
display: flex;
63+
align-items: center;
64+
padding-left: 0.5rem;
65+
width: 100%;
66+
border-radius: 0;
67+
68+
&:hover {
69+
background-color: #eee;
70+
}
71+
}
6372

64-
img,
65-
i {
66-
width: 1.2rem; // Slightly wider to make the font match the images
67-
font-size: 1rem;
73+
a:hover {
74+
text-decoration: none;
6875
}
76+
77+
span.btn__icon-container {
78+
display: flex;
79+
margin-right: 0.5rem;
80+
81+
img,
82+
i {
83+
width: 1.2rem; // Slightly wider to make the font match the images
84+
font-size: 1rem;
85+
}
86+
}
87+
}
88+
}
89+
90+
// Extra spacing when we have both icons and content
91+
span.btn__icon-container ~ .btn__content-container {
92+
margin-left: 0.5em;
93+
}
94+
95+
// Outline buttons need extra spacing
96+
button.btn-outline {
97+
outline: 1px solid $non-content-grey;
98+
padding: 0.3rem 0.5rem;
99+
100+
&:hover {
101+
background-color: #ededed;
69102
}
70103
}

src/sphinx_book_theme/assets/styles/sections/_header-article.scss

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,16 @@
2222
.header-article__right {
2323
display: flex;
2424
align-items: center;
25-
gap: 0.5rem;
2625
}
2726

2827
.header-article__right {
2928
margin-left: auto;
29+
30+
button,
31+
label,
32+
div {
33+
padding-left: 0.25rem;
34+
}
3035
}
3136
}
3237
}

src/sphinx_book_theme/header_buttons/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ def add_header_buttons(app, pagename, templatename, context, doctree):
118118
header_buttons.append(
119119
{
120120
"type": "dropdown",
121-
"title": "Source repositories",
122121
"icon": "fab fa-github",
123122
"items": repo_buttons,
124123
"side": "right",
@@ -171,7 +170,6 @@ def add_header_buttons(app, pagename, templatename, context, doctree):
171170
header_buttons.append(
172171
{
173172
"type": "dropdown",
174-
"title": "Download this page",
175173
"icon": "fas fa-download",
176174
"items": download_buttons,
177175
"side": "right",

src/sphinx_book_theme/header_buttons/launch.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,6 @@ def add_launch_buttons(
180180
header_buttons.append(
181181
{
182182
"type": "dropdown",
183-
"title": "Launch interactive content",
184183
"icon": "fas fa-rocket",
185184
"items": launch_buttons_list,
186185
"side": "right",

0 commit comments

Comments
 (0)