made theme switching better

This commit is contained in:
Worble 2019-05-06 22:16:37 +01:00
parent dc3d52a7cb
commit df5fc9890b
4 changed files with 20 additions and 18 deletions

View file

@ -12,6 +12,7 @@
- [Brand](#brand) - [Brand](#brand)
- [Search](#search) - [Search](#search)
- [Title](#title) - [Title](#title)
- [Theming](#theming)
- [Original](#original) - [Original](#original)
## Installation ## Installation
@ -149,14 +150,14 @@ In extra, setting `zulma_theme` to a valid value will change the current colour
- pulse - pulse
- simplex - simplex
All valid themes can also be found under the `extras.zulma_themes` variable in the `config.toml`. Choosing no theme will set default as the theme. Setting an invalid theme value will cause the site to render improperly. All valid themes can also be found under the `extra.zulma_themes` variable in the `theme.toml`. Choosing no theme will set default as the theme. Setting an invalid theme value will cause the site to render improperly.
```toml ```toml
[extra] [extra]
zulma_theme = "darkly" zulma_theme = "darkly"
``` ```
Additionally, in extra, you can also set the `zulma_allow_theme_selection` boolean. Setting this to true will allow a menu in the footer to allow users to select their own theme. This requires javascript to be enabled to function; if the page detects javascript is disabled on the clients machine, it will hide itself. Additionally, in extra, you can also set the `zulma_allow_theme_selection` boolean. Setting this to `true` will allow a menu in the footer to allow users to select their own theme. This option will store their theme choice in their localstorage and apply it on every page, assuming `zulma_allow_theme_selection` is still true. This requires javascript to be enabled to function; if the page detects javascript is disabled on the clients machine, it will hide itself.
```toml ```toml
[extra] [extra]

View file

@ -1,29 +1,5 @@
const THEME_KEY = "ZULMA_THEME"; const THEME_KEY = "ZULMA_THEME";
function changeTheme(themeName) {
let alternates = [];
document.querySelectorAll('link.stylesheet').forEach(element => {
if (element.id === themeName) {
element.media = '';
}
else {
alternates.push(element);
}
});
alternates.forEach(element => {
element.media = 'none';
});
saveTheme(themeName);
}
function saveTheme(themeName) {
localStorage.setItem(THEME_KEY, themeName);
}
window.addEventListener('DOMContentLoaded', () => {
let theme = localStorage.getItem(THEME_KEY); let theme = localStorage.getItem(THEME_KEY);
if (theme) { if (theme) {
changeTheme(theme); changeTheme(theme);
@ -33,8 +9,30 @@ window.addEventListener('DOMContentLoaded', () => {
} }
}); });
} }
function changeTheme(themeName) {
let alternates = [];
document.querySelectorAll('link.stylesheet').forEach(element => {
if (element.id === themeName) {
element.disabled = false;
}
else {
alternates.push(element);
}
}); });
alternates.forEach(element => {
element.disabled = true;
});
saveTheme(themeName);
}
function saveTheme(themeName) {
localStorage.setItem(THEME_KEY, themeName);
}
window.addEventListener('load', () => { window.addEventListener('load', () => {
document.getElementById('theme-select').onchange = function () { document.getElementById('theme-select').onchange = function () {
changeTheme(this.value); changeTheme(this.value);

View file

@ -28,10 +28,10 @@
{% if config.build_search_index %} {% if config.build_search_index %}
<script defer type="text/javascript" src="{{ get_url(path="elasticlunr.min.js") }}"></script> <script defer type="text/javascript" src="{{ get_url(path="elasticlunr.min.js") }}"></script>
<script defer type="text/javascript" src="{{ get_url(path="search_index.en.js") }}"></script> <script defer type="text/javascript" src="{{ get_url(path="search_index.en.js") }}"></script>
{% endif %}
{% if config.extra.zulma_allow_theme_selection %} {% if config.extra.zulma_allow_theme_selection %}
<script type="text/javascript" src="{{ get_url(path="js/switchcss.js") }}"></script> <script type="text/javascript" src="{{ get_url(path="js/switchcss.js") }}"></script>
{% endif %} {% endif %}
{% endif %}
{% endblock js %} {% endblock js %}
{% block extra_head %} {% block extra_head %}
@ -52,6 +52,7 @@
</head> </head>
<body> <body>
{% block body%}
{% block content %} {% block content %}
<!-- START NAV --> <!-- START NAV -->
{% block header %} {% block header %}
@ -87,6 +88,8 @@
{% if config.build_search_index %} {% if config.build_search_index %}
<script type="text/javascript" src="{{ get_url(path="js/search.js") }}"></script> <script type="text/javascript" src="{{ get_url(path="js/search.js") }}"></script>
{% endif %} {% endif %}
{% endblock body%}
</body> </body>
</html> </html>

View file

@ -136,7 +136,7 @@
<link id="{{theme}}" class="stylesheet" rel="stylesheet" <link id="{{theme}}" class="stylesheet" rel="stylesheet"
href="{{ get_url(path=theme ~ ".css", trailing_slash=false) }}" /> href="{{ get_url(path=theme ~ ".css", trailing_slash=false) }}" />
{% else %} {% else %}
<link id="{{theme}}" class="stylesheet" media="none" rel="stylesheet" <link id="{{theme}}" class="alternate stylesheet" disabled rel="stylesheet"
href="{{ get_url(path=theme ~ ".css", trailing_slash=false) }}" /> href="{{ get_url(path=theme ~ ".css", trailing_slash=false) }}" />
{% endif %} {% endif %}
{% endfor %} {% endfor %}