Skip to content

Commit 9481c3d

Browse files
authored
Use dataclasses instead of extra_classes (#597)
* Use dataclasses instead of extra_classes * Fix CSS * Uncomment proper json * Fix docs' * Fix up the CSS of the dropdown * Docs update
1 parent 3ebd74f commit 9481c3d

File tree

5 files changed

+81
-11
lines changed

5 files changed

+81
-11
lines changed

docs/_static/custom.css

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/* Background of stsable should be green */
2+
#version_switcher a[data-version-name*="stable"] {
3+
background-color: #e2ffe2;
4+
}
5+
6+
/* Color of explicit versions should be orange */
7+
#version_switcher
8+
a:not([data-version-name*="stable"]):not([data-version-name="dev"]) {
9+
background-color: #f8f9fa;
10+
color: grey;
11+
}
12+
13+
/* If we're on a dev version, make the switcher button orange */
14+
#version_switcher_button[data-active-version-name*="dev"] {
15+
background-color: rgb(255 138 62);
16+
}

docs/_static/switcher.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
[
22
{
33
"name": "dev",
4-
"version": "latest",
5-
"extra_classes": ["dev"]
4+
"version": "latest"
65
},
76
{
87
"name": "0.8.0 (stable)",

docs/conf.py

+5
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@
145145
"demo/no-sidebar": [], # Test what page looks like with no sidebar items
146146
}
147147

148+
myst_heading_anchors = 2
148149

149150
html_context = {
150151
"github_user": "pandas-dev",
@@ -161,3 +162,7 @@
161162
# relative to this directory. They are copied after the builtin static files,
162163
# so a file named "default.css" will overwrite the builtin "default.css".
163164
html_static_path = ["_static"]
165+
166+
167+
def setup(app):
168+
app.add_css_file("custom.css")

docs/user_guide/configuring.rst

+51-4
Original file line numberDiff line numberDiff line change
@@ -386,9 +386,6 @@ each can have the following fields:
386386
switcher.
387387
- ``name``: an optional name to display in the switcher dropdown instead of the
388388
version string (e.g., "latest", "stable", "dev", etc).
389-
- ``extra_classes``: an optional list of classes to add to the switcher
390-
button for a given version (e.g., ``["dev", "rc"]``). These classes are only
391-
added when the version is active.
392389
- ``url``: an optional URL. If provided, it links the version to ``url``
393390
instead of ``switcher['url_template']``.
394391

@@ -403,7 +400,6 @@ Here is an example JSON file:
403400
},
404401
{
405402
"version": "2.1rc1",
406-
"extra_classes": ["dev", "rc"]
407403
},
408404
{
409405
"version": "2.0"
@@ -542,6 +538,57 @@ to insert a version switcher at the top of the left sidebar, while still
542538
keeping the default navigation below it. See :doc:`sections` for more
543539
information.
544540

541+
Style the switcher buttons
542+
--------------------------
543+
544+
You may apply styles via CSS to any of the switcher buttons to control their look and feel.
545+
Each button has two `HTML dataset entries <https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/dataset>`_
546+
that you can use to apply CSS rules to subsets of buttons. These entries are:
547+
548+
.. code-block::
549+
550+
data-version
551+
data-version-name
552+
553+
For example, the link for an entry with ``version="0.2"``,
554+
and ``name="My version"`` would have metadata like so:
555+
556+
.. code-block:: html
557+
558+
<a data-version-name="My version" data-version="0.2" class="<classes...>">
559+
560+
You can create CSS rules that select this metadata like so:
561+
562+
.. code-block:: scss
563+
564+
// Style all links with a specific subset of versions
565+
#version_switcher a[data-version="0.2"],
566+
#version_switcher a[data-version="0.3"] {
567+
background-color: red;
568+
}
569+
// Style all links with `stable` in the version name
570+
#version_switcher a[data-version-name*="stable"] {
571+
background-color: green;
572+
}
573+
574+
In addition, the parent button of the dropdown list contains similar metadata
575+
about the **current version**. This could be used to style the entire dropdown
576+
a certain color based on the active version.
577+
578+
For example, if you wanted to style the dropdown button orange if it was a ``dev``
579+
version, you could use the following CSS selector:
580+
581+
.. code-block:: scss
582+
583+
// If the active version has the name "dev", style it orange
584+
#version_switcher_button[data-active-version-name*="dev"] {
585+
background-color: rgb(255 138 62);
586+
}
587+
588+
.. seealso::
589+
590+
See the `MDN documentation on dataset properties <https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/dataset>`_
591+
for more information on using and styling with these properties.
545592

546593
Add an Edit this Page button
547594
============================

src/pydata_sphinx_theme/theme/pydata_sphinx_theme/_templates/version-switcher.html

+8-5
Original file line numberDiff line numberDiff line change
@@ -66,18 +66,21 @@
6666
// trying to redirect, and if not, will redirect to the homepage
6767
// for that version of the docs.
6868
node.onclick = checkPageExistsAndRedirect;
69+
// Add dataset values for the version and name in case people want
70+
// to apply CSS styling based on this information.
71+
node.dataset["versionName"] = entry.name;
72+
node.dataset["version"] = entry.version;
73+
6974
$("#version_switcher_menu").append(node);
7075
// replace dropdown button text with the preferred display name of
7176
// this version, rather than using sphinx's {{ version }} variable.
7277
// also highlight the dropdown entry for the currently-viewed
7378
// version's entry
7479
if (entry.version == "{{ theme_switcher.get('version_match') }}") {
7580
node.classList.add("active");
76-
$("#version_switcher_button").text(entry.name);
77-
// Add extra class to the button for the selected entry
78-
if ("extra_classes" in entry) {
79-
$("#version_switcher_menu")[0].classList.add(...entry.extra_classes);
80-
}
81+
let btn = document.getElementById("version_switcher_button");
82+
btn.innerText = btn.dataset["activeVersionName"] = entry.name;
83+
btn.dataset["activeVersion"] = entry.version;
8184
}
8285
});
8386
});

0 commit comments

Comments
 (0)