Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dark theme and theme switcher #540

Merged
merged 59 commits into from
Apr 9, 2022
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
07e416b
docs: exclude files created by jupyter
12rambau Jan 5, 2022
932844e
set up js for theme change
12rambau Jan 5, 2022
2469f07
demo with only background color change
12rambau Jan 5, 2022
f0cccbe
set-up all the dark theme colors
12rambau Jan 7, 2022
9850073
overwrite the pygment css file
12rambau Jan 7, 2022
338a1b5
add default_theme parameter
12rambau Jan 7, 2022
a1d9865
small update to documentation
12rambau Jan 7, 2022
6f50e5a
color support for ethical add
12rambau Jan 7, 2022
df23c55
missing alpha channel in css
12rambau Jan 7, 2022
84338de
docs: fix badly written css comments
12rambau Jan 18, 2022
5e3f3a5
Merge branch 'master' into dark-theme
12rambau Jan 18, 2022
81a2117
Update src/pydata_sphinx_theme/__init__.py
12rambau Feb 11, 2022
bf6044b
use typescript comment structure
12rambau Feb 11, 2022
c59735c
make the switch as a btn
12rambau Feb 11, 2022
25a8e1e
use lighter section headers
12rambau Feb 12, 2022
03a84ee
replace all colors with rgba defined colors
12rambau Feb 13, 2022
951a316
add comments on pygments theme switch
12rambau Feb 13, 2022
7342f64
move ethical-ads to its own scss file
12rambau Feb 13, 2022
06ab359
use theme options to control pygments styling
12rambau Feb 14, 2022
3b36764
change theme switch color
12rambau Feb 14, 2022
9ecd5e4
remind that pygments_style is overwritten
12rambau Feb 14, 2022
31f7c5e
add comment to js theme management functions
12rambau Feb 14, 2022
5b1452d
refactor cycleTheme
12rambau Feb 14, 2022
ecdf638
prevent flickering when switching theme
12rambau Feb 14, 2022
8eb2c1e
set the theme-switcher as a template
12rambau Feb 14, 2022
36445bc
document the css trick
12rambau Feb 16, 2022
3e07469
document the css trick
12rambau Feb 16, 2022
a474db1
change colors to improve accecibility
12rambau Feb 16, 2022
c822508
change colors to improve accecibility
12rambau Feb 16, 2022
2c28826
Merge branch 'master' into dark-theme
12rambau Feb 16, 2022
e107a14
apply pre-commit checks
12rambau Feb 16, 2022
5bb58c3
Merge branch 'dark-theme' of github.com:sepal-contrib/pydata-sphinx-t…
12rambau Feb 16, 2022
fec703b
add the theme switcher to the tests
12rambau Feb 16, 2022
06c7e13
quote Furo theme in comments
12rambau Feb 17, 2022
34b3934
add the css trick to all version modified widgets
12rambau Feb 17, 2022
17409bf
include sidebar and topic in the color scheme
12rambau Feb 17, 2022
9b13e31
refactor navbar-toggler
12rambau Feb 17, 2022
8413144
support for version btn
12rambau Feb 18, 2022
1406d1f
fix blockquote colors
12rambau Feb 18, 2022
ba3e59a
listen to the span instead of the a tag
12rambau Feb 18, 2022
82c2e79
use the same color for hov and focus
12rambau Feb 18, 2022
a2aaf68
typo
12rambau Mar 9, 2022
0f9f477
typo
12rambau Mar 9, 2022
80cb20e
set the theme switcher in the navbar-end
12rambau Mar 9, 2022
e4c68a4
set back the theme-switcher in pydata documentation
12rambau Mar 9, 2022
43ceb27
Merge branch 'master' into dark-theme
12rambau Mar 12, 2022
4b6f568
replace orange and green
12rambau Mar 12, 2022
1bff226
use color in the gihub action report
12rambau Mar 12, 2022
5220fb9
test the theme switcher
12rambau Mar 12, 2022
ba04a68
remove the theme switcher container from the test
12rambau Mar 12, 2022
48d3d6b
typo
12rambau Mar 12, 2022
1a45cc7
docs: disclaimer on colors
12rambau Mar 12, 2022
02f185c
docs: typo
12rambau Mar 12, 2022
df22fc2
Merge pull request #1 from sepal-contrib/master
12rambau Mar 27, 2022
27e7bd6
reduce minscore for lighthouse SEO expectations
12rambau Mar 28, 2022
5ef0ac3
Cleaning up the colors for accessibility
choldgraf Mar 29, 2022
45860d7
Warning about stability
choldgraf Mar 29, 2022
decac66
Merge pull request #2 from pydata/dark-theme
12rambau Mar 30, 2022
45d65c3
Update src/pydata_sphinx_theme/assets/styles/_navbar.scss
12rambau Apr 8, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This pattern also affects html_static_path and html_extra_path.
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"]
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store", "**.ipynb_checkpoints"]

# -- Extension options -------------------------------------------------------

Expand Down
14 changes: 13 additions & 1 deletion docs/user_guide/configuring.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ in your ``conf.py`` file. This is a dictionary with ``key: val`` pairs that
you can configure in various ways. This page describes the options available to you.

Configure project logo
==============================
======================

To add a logo that's placed at the left of your nav bar, put a logo file under your
doc path's _static folder, and use the following configuration:
Expand All @@ -31,6 +31,18 @@ If you'd like it to link to another page or use an external link instead, use th

.. _icon-links:

Configure default theme
=======================

The theme mode can be changed by the user. By default landing on the documentation will switch the mode to ``auto``. You can specified this value to be one of ``auto``, ``dark``, ``light``.

.. code-block:: python

html_context= {
...
"default_mode": "auto"
}

Configure icon links
====================

Expand Down
32 changes: 31 additions & 1 deletion docs/user_guide/customizing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,35 @@ To add a custom stylesheet, follow these steps:

When you build your documentation, this stylesheet should now be activated.

.. _manage-themes:

Manage themes
=============

Pydata sphinx theme embed 3 different theming mode:

- ``auto``: the documentation theme will follow the one provided by your computer
- ``dark```: the documentation is displayed with the dark theme
- ``light``: the documentation is displayed with the light theme

In order to customize the display of any of the theme element you need to encaspulate your modifications in the approriate css rules:

.. code-block:: css

/* anything related to the light theme */
body[data-theme="light"] {

/* whatever you want to change */
background: white;
}

/* anything related to the dark theme */
body[data-theme="dark"] {

/* whatever you want to change */
background: black;
}

.. _css-variables:

CSS Theme variables
Expand Down Expand Up @@ -58,7 +87,8 @@ In order to change a variable, follow these steps:

Note that these are `CSS variables <css-variable-help_>`_ and not
`SASS variables <https://sass-lang.com/documentation/variables>`_.
The theme is defined with CSS variables, not SASS variables!
The theme is defined with CSS variables, not SASS variables! refer to the previous section if
12rambau marked this conversation as resolved.
Show resolved Hide resolved
you desire a differnet behaviour between the light and dark theme
12rambau marked this conversation as resolved.
Show resolved Hide resolved

For a complete list of the theme variables that you may override, see the
`theme variables defaults CSS file <pydata-css-variables_>`_:
Expand Down
46 changes: 46 additions & 0 deletions src/pydata_sphinx_theme/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from sphinx.environment.adapters.toctree import TocTree
from sphinx.errors import ExtensionError
from sphinx.util import logging
from pygments.formatters import HtmlFormatter

from .bootstrap_html_translator import BootstrapHTML5Translator

Expand Down Expand Up @@ -501,6 +502,50 @@ def get_edit_url():
context["theme_show_toc_level"] = int(context.get("theme_show_toc_level", 1))


# -----------------------------------------------------------------------------
12rambau marked this conversation as resolved.
Show resolved Hide resolved
# handle pygment css
# -----------------------------------------------------------------------------
12rambau marked this conversation as resolved.
Show resolved Hide resolved
def _get_styles(formatter, prefix):
"""
Get styles out of a formatter, where everything has the correct prefix.
"""

for line in formatter.get_linenos_style_defs():
yield f"{prefix} {line}"
yield from formatter.get_background_style_defs(prefix)
yield from formatter.get_token_style_defs(prefix)


def get_pygments_stylesheet():
"""
Generate the theme-specific pygments.css.
There is no way to tell Sphinx how the theme handles modes
"""
light_formatter = HtmlFormatter(style="tango")
12rambau marked this conversation as resolved.
Show resolved Hide resolved
dark_formatter = HtmlFormatter(style="native")

lines = []

light_prefix = 'body[data-theme="light"] .highlight'
lines.extend(_get_styles(light_formatter, prefix=light_prefix))

dark_prefix = 'body[data-theme="dark"] .highlight'
lines.extend(_get_styles(dark_formatter, prefix=dark_prefix))

return "\n".join(lines)


# cannot deal with pygments and modes so weoverwrite the pygment css file
12rambau marked this conversation as resolved.
Show resolved Hide resolved
def _overwrite_pygments_css(app, exception=None):

if exception is not None:
return

assert app.builder
with open(os.path.join(app.builder.outdir, "_static", "pygments.css"), "w") as f:
f.write(get_pygments_stylesheet())


# -----------------------------------------------------------------------------


Expand All @@ -521,6 +566,7 @@ def setup(app):
app.connect("html-page-context", setup_edit_url)
app.connect("html-page-context", add_toctree_functions)
app.connect("html-page-context", update_templates)
app.connect("build-finished", _overwrite_pygments_css)

# Include templates for sidebar
app.config.templates_path.append(os.fsdecode(theme_path / "_templates"))
Expand Down
75 changes: 75 additions & 0 deletions src/pydata_sphinx_theme/assets/scripts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import "bootstrap";

import "../styles/index.scss";

////////////////////////////////////////////////////////////////////////////////
// TOC interactivity
////////////////////////////////////////////////////////////////////////////////

12rambau marked this conversation as resolved.
Show resolved Hide resolved
function addTOCInteractivity() {
// TOC sidebar - add "active" class to parent list
//
Expand All @@ -31,6 +35,10 @@ function addTOCInteractivity() {
});
}

////////////////////////////////////////////////////////////////////////////////
// Scroll
////////////////////////////////////////////////////////////////////////////////

// Navigation sidebar scrolling to active page
function scrollToActive() {
var sidebar = document.querySelector("div.bd-sidebar");
Expand Down Expand Up @@ -71,7 +79,74 @@ function scrollToActive() {
});
}

////////////////////////////////////////////////////////////////////////////////
// Theme interaction
////////////////////////////////////////////////////////////////////////////////
var prefersDark = window.matchMedia("(prefers-color-scheme: dark)");

function autoTheme(e) {
document.body.dataset.theme = prefersDark.matches ? "dark" : "light";
}

function setTheme(mode) {
if (mode !== "light" && mode !== "dark" && mode !== "auto") {
console.error(`Got invalid theme mode: ${mode}. Resetting to auto.`);
mode = "auto";
}

// get the theme
var colorScheme = prefersDark.matches ? "dark" : "light";
document.body.dataset.mode = mode;
document.body.dataset.theme = mode == "auto" ? colorScheme : mode;

// save mode
localStorage.setItem("theme", mode);
console.log(`Changed to ${mode} mode.`);

// add a listener if set on auto
prefersDark.onchange = mode == "auto" ? autoTheme : "";
}

function cycleTheme() {
12rambau marked this conversation as resolved.
Show resolved Hide resolved
const defaultMode = document.body.dataset.defaultMode || "auto";
const currentTheme = localStorage.getItem("theme") || defaultMode;

if (prefersDark.matches) {
12rambau marked this conversation as resolved.
Show resolved Hide resolved
// Auto (dark) -> Light -> Dark
if (currentTheme === "auto") {
setTheme("light");
} else if (currentTheme == "light") {
setTheme("dark");
} else {
setTheme("auto");
}
} else {
// Auto (light) -> Dark -> Light
if (currentTheme === "auto") {
setTheme("dark");
} else if (currentTheme == "dark") {
setTheme("light");
} else {
setTheme("auto");
}
}
}

function setupTheme() {
// setup at least one time
const defaultMode = document.body.dataset.defaultMode || "auto";
const currentTheme = localStorage.getItem("theme") || defaultMode;
setTheme(currentTheme);
12rambau marked this conversation as resolved.
Show resolved Hide resolved

// Attach event handlers for toggling themes
const btnList = document.getElementsByClassName("theme-switch");
Array.from(btnList).forEach((btn) => {
btn.addEventListener("click", cycleTheme);
});
}

// This is equivalent to the .ready() function as described in
// https://api.jquery.com/ready/
$(setupTheme);
$(scrollToActive);
$(addTOCInteractivity);
5 changes: 3 additions & 2 deletions src/pydata_sphinx_theme/assets/styles/_admonitions.scss
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ div.admonition,
border-left: 0.2rem solid;
border-color: rgba(var(--pst-color-admonition-default), 1);
border-radius: 0.2rem;
box-shadow: 0 0.2rem 0.5rem rgba(0, 0, 0, 0.05),
0 0 0.0625rem rgba(0, 0, 0, 0.1);
box-shadow: 0 0.2rem 0.5rem rgba(var(--pst-color-admonition-shadow), 1),
0 0 0.0625rem rgba(var(--pst-color-admonition-shadow), 1);
transition: color 250ms, background-color 250ms, border-color 250ms;
background-color: rgba(var(--pst-color-admonition-background), 1);

// Last item should have no spacing since we'll control that w/ padding
*:last-child {
Expand Down
29 changes: 28 additions & 1 deletion src/pydata_sphinx_theme/assets/styles/_api.scss
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ table.field-list {
th.field-name {
padding: 1px 8px 1px 5px;
white-space: nowrap;
background-color: rgb(238, 238, 238);
background-color: rgba(var(--pst-color-background-up), 1);
}

/* italic font for parameter types */
Expand Down Expand Up @@ -87,3 +87,30 @@ table.field-list {
.sig-name {
color: rgba(var(--pst-color-inline-code), 1);
}

// change target color for dark theme
dt:target {
background-color: rgba(var(--pst-color-target), 1);
}

// adapt ethical add to the theme
12rambau marked this conversation as resolved.
Show resolved Hide resolved
.ethical-sidebar a,
.ethical-sidebar a:visited,
.ethical-sidebar a:hover,
.ethical-sidebar a:active,
.ethical-footer a,
.ethical-footer a:visited,
.ethical-footer a:hover,
.ethical-footer a:active {
color: rgba(var(--pst-color-add-text), 1);
}

.ethical-sidebar,
.ethical-footer {
background-color: rgba(var(--pst-color-add-background), 1);
border: 1px solid rgba(var(--pst-color-add-border), 1);
border-radius: 5px;
color: rgba(var(--pst-color-add-text), 1);
font-size: 14px;
line-height: 20px;
}
10 changes: 5 additions & 5 deletions src/pydata_sphinx_theme/assets/styles/_base.scss
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ html {
body {
padding-top: calc(var(--pst-header-height) + 20px);

background-color: white;
background-color: rgba(var(--pst-color-background), 1);
font-family: var(--pst-font-family-base);
font-weight: 400;
line-height: 1.65;
Expand All @@ -20,7 +20,7 @@ p {

/* section header in docstring pages */
&.rubric {
border-bottom: 1px solid rgb(201, 201, 201);
border-bottom: 1px solid rgba(var(--pst-color-border), 1);
}
}

Expand Down Expand Up @@ -97,7 +97,7 @@ small,

hr {
border: 0;
border-top: 1px solid #e5e5e5;
border-top: 1px solid rgba(var(pst-color-border), 1);
}

pre,
Expand All @@ -117,9 +117,9 @@ pre {
background-color: rgba(var(--pst-color-preformatted-background), 1);
color: rgba(var(--pst-color-preformatted-text), 1);
line-height: 1.2em;
border: 1px solid rgb(201, 201, 201);
border: 1px solid rgba(var(--pst-color-preformatted-border), 1);
border-radius: 0.2rem;
box-shadow: 1px 1px 1px #d8d8d8;
box-shadow: 1px 1px 1px rgba(var(--pst-color-preformatted-shadow), 1);
}

// Override bootstrap by restoring the basic theme default.
Expand Down
Loading