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

Better NoJs Support around ThemeToggle #98

Closed
ohchase opened this issue Jan 25, 2025 · 2 comments
Closed

Better NoJs Support around ThemeToggle #98

ohchase opened this issue Jan 25, 2025 · 2 comments

Comments

@ohchase
Copy link

ohchase commented Jan 25, 2025

I am using this template but would like to provide a no javascript version of my site by default

When configuring the main config with an explicit theme I would have expected the themetoggle javascript resource wouldn't be included

[extra]
theme = "dark"

Relevant theme logic in the header.html template where themetoggle.js is linked:

{% set theme = config.extra.theme | default(value="toggle") %}
{% if theme == "dark" %}
<link rel="stylesheet" type="text/css" href="{{ get_url(path='theme/dark.css') }}" />
{% elif theme == "light" %}
<link rel="stylesheet" type="text/css" href={{ get_url(path="theme/light.css") }} />
{% elif theme == "auto" %}
<link rel="stylesheet" type="text/css" href={{ get_url(path="theme/light.css") }} />
<link rel="stylesheet" type="text/css" href="{{ get_url(path='theme/dark.css') }}" media="(prefers-color-scheme: dark)" />
{% elif theme == "toggle" %}
<link rel="stylesheet" type="text/css" href={{ get_url(path="theme/light.css") }} />
<link id="darkModeStyle" rel="stylesheet" type="text/css" href="{{ get_url(path='theme/dark.css') }}" />
{% endif %}
<!-- Set the correct theme in the script -->
<script src={{ get_url(path="js/themetoggle.js") }}></script>
{% if theme == "dark" %}
<script>setTheme("dark");</script>
{% elif theme == "light" %}
<script>setTheme("light");</script>
{% elif theme == "auto" %}
<script>
if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
setTheme("dark");
} else {
setTheme("light");
}
</script>
{% else %}
<script>setTheme(getSavedTheme());</script>
{% endif %}

When setting an explicit theme the only reason themetoggle.js is included is to set a local storage attribute

function setTheme(mode) {
localStorage.setItem("theme-storage", mode);
}

I would like to figure out a way to make the template not use any javascript if you set a constant theme coloring.
Not sure if this is possible, but going to be working on trying to get this working in a fork.

One thought was setting the default to "light" and depending on the user's web browser client/viewer to auto dark mode site

@not-matthias
Copy link
Owner

When configuring the main config with an explicit theme I would have expected the themetoggle javascript resource wouldn't be included
Good catch!

I pushed an update that should fix this.

@ohchase
Copy link
Author

ohchase commented Feb 16, 2025

@not-matthias @andresterba thank you very much! fantastic theme and greatly appreciate it having more options for NoJS support now!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants