|
| 1 | +# Color theme toggler for Bootstrap 5 |
| 2 | +With Bootstrap 5.3 a color-theme toggler is possible. |
| 3 | +This is not provided by default and needs some custom code. |
| 4 | +Here is a small guide how to implement it into Plone 6. |
| 5 | + |
| 6 | +## Toggle Button |
| 7 | +Add elements with `data-bs-theme-value`. |
| 8 | +If you want to add a theme toggler to your site, you can use the following elements: |
| 9 | + |
| 10 | +```html |
| 11 | +<div class="btn-group"> |
| 12 | + <button class="btn btn-secondary" data-bs-theme-value="light"> |
| 13 | + Light |
| 14 | + </button> |
| 15 | + <button class="btn btn-secondary" data-bs-theme-value="dark"> |
| 16 | + Dark |
| 17 | + </button> |
| 18 | +</div> |
| 19 | +``` |
| 20 | + |
| 21 | +## Javascript |
| 22 | + |
| 23 | +The following Javascript is needed to make the toggler work. |
| 24 | +It is based on the [Bootstrap 5.3 documentation](https://getbootstrap.com/docs/5.3/customize/color-modes/). |
| 25 | +The Javascript is added to the `browser/static` folder of your Plone 6 project and registered in the `browser/profiles/default/registry` of your Plone 6 project. |
| 26 | +See [Registering Javascript and CSS](classic-ui-static-resources-registering-label) for more information. |
| 27 | + |
| 28 | +```js |
| 29 | +document.addEventListener('DOMContentLoaded', () => { |
| 30 | + 'use strict' |
| 31 | + |
| 32 | + const storedTheme = localStorage.getItem('theme'); |
| 33 | + |
| 34 | + const getPreferredTheme = () => { |
| 35 | + if (storedTheme) { |
| 36 | + return storedTheme; |
| 37 | + } |
| 38 | + |
| 39 | + return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'; |
| 40 | + } |
| 41 | + |
| 42 | + const setTheme = function (theme) { |
| 43 | + if (theme === 'auto' && window.matchMedia('(prefers-color-scheme: dark)').matches) { |
| 44 | + document.documentElement.setAttribute('data-bs-theme', 'dark'); |
| 45 | + } else { |
| 46 | + document.documentElement.setAttribute('data-bs-theme', theme); |
| 47 | + } |
| 48 | + const e = new CustomEvent('data-bs-theme-changed'); |
| 49 | + e.theme = theme; |
| 50 | + document.dispatchEvent(e); |
| 51 | + } |
| 52 | + |
| 53 | + setTheme(getPreferredTheme()); |
| 54 | + |
| 55 | + const showActiveTheme = theme => { |
| 56 | + const btnToActive = document.querySelector(`[data-bs-theme-value="${theme}"]`) |
| 57 | + |
| 58 | + document.querySelectorAll('[data-bs-theme-value]').forEach(element => { |
| 59 | + element.classList.remove('active'); |
| 60 | + }); |
| 61 | + |
| 62 | + btnToActive.classList.add('active'); |
| 63 | + } |
| 64 | + |
| 65 | + window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', () => { |
| 66 | + if (storedTheme !== 'light' || storedTheme !== 'dark') { |
| 67 | + setTheme(getPreferredTheme()); |
| 68 | + } |
| 69 | + }); |
| 70 | + |
| 71 | + showActiveTheme(getPreferredTheme()); |
| 72 | + |
| 73 | + document.querySelectorAll('[data-bs-theme-value]') |
| 74 | + .forEach(toggle => { |
| 75 | + toggle.addEventListener('click', (e) => { |
| 76 | + e.preventDefault(); |
| 77 | + const theme = toggle.getAttribute('data-bs-theme-value'); |
| 78 | + localStorage.setItem('theme', theme); |
| 79 | + setTheme(theme); |
| 80 | + showActiveTheme(theme); |
| 81 | + }); |
| 82 | + }); |
| 83 | +}); |
| 84 | +``` |
| 85 | + |
| 86 | +## Customize single elements |
| 87 | + |
| 88 | +You can customize single elements with the `data-bs-theme` attribute. |
| 89 | +When set to a value the element will be rendered in the given theme, despite the global theme. |
| 90 | +For example: |
| 91 | + |
| 92 | +```html |
| 93 | +<form data-bs-theme='light'> |
| 94 | + <div class="mb-3"> |
| 95 | + <label for="exampleInputEmail1" class="form-label">Email address</label> |
| 96 | + <input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp"> |
| 97 | + </div> |
| 98 | + <button type="submit" class="btn btn-primary">Submit</button> |
| 99 | +</form> |
| 100 | +``` |
0 commit comments