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
Theme Switching Button #1223
Comments
Still has issue in just-the-docs/just-the-docs#1223.
Today, after some other attempts, I think there might be one solution. If we can put dark and light themes under the same |
Finally, I implemented the theme change with CSS and JS. Maybe this feature can be added to JTD with some changes. Basically, in the overrided <head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=Edge">
<script>
if (localStorage.getItem('theme') === 'dark') document.documentElement.classList.add('dark-mode');
</script>
<script type="text/javascript" src="{{ '/assets/js/theme-switch.js' | relative_url }}"></script>
<link rel="stylesheet" href="{{ '/assets/css/just-the-docs-switchable.css' | relative_url }}">
... The window.addEventListener("DOMContentLoaded", function() {
const toggleDarkMode = document.getElementById("theme-toggle");
if (localStorage.getItem('theme') === 'dark') {
setTheme('dark');
} else {
setTheme('light');
}
jtd.addEvent(toggleDarkMode, 'click', function(){
const currentTheme = getTheme();
const newTheme = currentTheme === 'dark' ? 'light' : 'dark';
localStorage.setItem('theme', newTheme);
setTheme(newTheme);
});
function getTheme() {
return document.documentElement.classList.contains('dark-mode') ? 'dark' : 'light';
}
function setTheme(theme) {
if (theme === 'dark') {
toggleDarkMode.innerHTML = `<svg width='18px' height='18px'><use href="#svg-moon"></use></svg>`;
document.documentElement.classList.add('dark-mode');
document.documentElement.classList.remove('light-mode');
} else {
toggleDarkMode.innerHTML = `<svg width='18px' height='18px'><use href="#svg-sun"></use></svg>`;
document.documentElement.classList.add('light-mode');
document.documentElement.classList.remove('dark-mode');
}
}
}); Finally, I combine the Site live at: https://mmcesim.org |
Loving the dark theme! @Teddy-van-Jerry 🔥 |
Thanks! The dark theme is already provided in JTD and can be configured in |
Hi @Teddy-van-Jerry, thanks for opening an issue! This is one of our most requested features, and there's a handful of (stale) PRs that have tried to implement this in some way. I'd be happy to review a PR with your proposed solution and work with you to merge it upstream! (I am alright implementing a toggle and automatic theme switch in piecemeal, rather than all at once) Related issues and PRs:
|
Hi @Teddy-van-Jerry , this is a beautiful implementation. One thing I would suggest is reversing the symbols: logically, and how others have it implemented, if I am on a light theme and want to switch to dark, I should click on a moon icon. And vice versa. How about we stay consistent and logical? Example: https://al-folio.github.io |
How could i add this to my site but using the default light and dark modes? |
Sorry guys, I am busy with some other research projects recently and do not have enough time to look into this carefully. Well, I also want to use the system's default, and it can be done with some minor modifications to look into the system settings. However, my employed method in this issue is not decent enough. Actually, a better way is to use CSS variables, as in many projects. To achieve that, we need to make a bunch of modifications to the source. I am not sure if there is a better way with SASS as in the current project, but my previous search told me that it can be very difficult. |
Is your feature request related to a problem? Please describe.
I hope to implement a theme-switching button so that users can choose a dark or light theme. Currently, the option provided by JTD only allows setting the site to be either dark or light.
Describe the solution you'd like
The theme can be changed by JavaScript:
The current solution:
header_custom.html
: (I have noticed that this location is not very mobile-friendly.)Overriding
head.html
:body
, so I override_layouts/default.html
to have the followingscript
:However, there is an issue that in the dark theme, it flickers on refresh. I am not experienced in this, and the solution here at StackOverflow cannot solve the issue. I am not sure what is wrong, so can it be related to
jtd.setTheme(theme);
so that the theme is applied only after the whole page loads?Describe alternatives you've considered
I am currently implementing it as mentioned above, overriding
_includes
files. But the main idea is the same.Additional context
The current site is live at mmcesim.org
(with the flickering issue). The repo is mmcesim/mmcesim.org.The text was updated successfully, but these errors were encountered: