Skip to content

Commit a65d322

Browse files
committed
extend color mode documentation
1 parent 4a1c698 commit a65d322

File tree

1 file changed

+71
-51
lines changed

1 file changed

+71
-51
lines changed

docs/classic-ui/theming/color-mode.md

Lines changed: 71 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,39 @@
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.
1+
# Color Modes
2+
Bootstrap 5.3 has introduced [Color Modes](https://getbootstrap.com/docs/5.3/customize/color-modes/).
3+
Here is a small guide how to implement color themes in Plone 6.
4+
5+
## Preferred Color Modes
6+
You will need to add some Javascript functionality to set the Bootstrap theme to the user's preferred color scheme.
7+
Add the Javascript file to the `browser/static` folder of your Plone 6 project and register it in the `browser/profiles/default/registry` of your Plone 6 project.
8+
See [Registering Javascript and CSS](classic-ui-static-resources-registering-label) for more information.
9+
10+
```js
11+
(() => {
12+
'use strict'
13+
14+
// Set Bootstrap Theme to the preferred color scheme
15+
const setPreferredTheme = () => {
16+
if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
17+
document.documentElement.setAttribute('data-bs-theme', 'dark')
18+
} else {
19+
document.documentElement.setAttribute('data-bs-theme', 'light')
20+
}
21+
}
22+
23+
window.addEventListener('DOMContentLoaded', () => {
24+
setPreferredTheme()
25+
})
26+
})()
27+
```
528

629
## 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:
30+
In order to switch color themes, corresponding elements with `data-bs-theme-value`
31+
attributes must be added to the DOM.
32+
Default Bootstrap 5.3 color themes include `light`, `dark` and `auto`.
33+
If you want to add a theme toggler to your site, you can use the following example:
934

1035
```html
11-
<div class="btn-group">
36+
<div class="btn-group btn-group-sm">
1237
<button class="btn btn-secondary" data-bs-theme-value="light">
1338
Light
1439
</button>
@@ -18,74 +43,69 @@ If you want to add a theme toggler to your site, you can use the following eleme
1843
</div>
1944
```
2045

21-
## Javascript
46+
## Registering the Toggle Button
47+
48+
You will need to add some Javascript functionality to the toggler.
49+
The following code snippet is based on the [Bootstrap 5.3 documentation](https://getbootstrap.com/docs/5.3/customize/color-modes/#javascript).
2250

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.
51+
Add the Javascript file to the `browser/static` folder of your Plone 6 project and register it in the `browser/profiles/default/registry` of your Plone 6 project.
2652
See [Registering Javascript and CSS](classic-ui-static-resources-registering-label) for more information.
2753

2854
```js
29-
document.addEventListener('DOMContentLoaded', () => {
55+
(() => {
3056
'use strict'
3157

32-
const storedTheme = localStorage.getItem('theme');
58+
const storedTheme = localStorage.getItem('theme')
3359

3460
const getPreferredTheme = () => {
35-
if (storedTheme) {
36-
return storedTheme;
37-
}
38-
39-
return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
61+
if (storedTheme) {
62+
return storedTheme
63+
}
64+
return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'
4065
}
4166

4267
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);
68+
if (theme === 'auto' && window.matchMedia('(prefers-color-scheme: dark)').matches) {
69+
document.documentElement.setAttribute('data-bs-theme', 'dark')
70+
} else {
71+
document.documentElement.setAttribute('data-bs-theme', theme)
72+
}
5173
}
5274

53-
setTheme(getPreferredTheme());
54-
5575
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-
});
76+
const btnToActive = document.querySelector(`[data-bs-theme-value="${theme}"]`)
6177

62-
btnToActive.classList.add('active');
78+
document.querySelectorAll('[data-bs-theme-value]').forEach(element => {
79+
element.classList.remove('active')
80+
})
81+
btnToActive.classList.add('active')
6382
}
6483

6584
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', () => {
66-
if (storedTheme !== 'light' || storedTheme !== 'dark') {
67-
setTheme(getPreferredTheme());
68-
}
69-
});
85+
if (storedTheme !== 'light' || storedTheme !== 'dark') {
86+
setTheme(getPreferredTheme())
87+
}
88+
})
7089

71-
showActiveTheme(getPreferredTheme());
90+
window.addEventListener('DOMContentLoaded', () => {
91+
setTheme(getPreferredTheme())
92+
showActiveTheme(getPreferredTheme())
7293

73-
document.querySelectorAll('[data-bs-theme-value]')
94+
document.querySelectorAll('[data-bs-theme-value]')
7495
.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-
});
96+
toggle.addEventListener('click', () => {
97+
const theme = toggle.getAttribute('data-bs-theme-value')
98+
localStorage.setItem('theme', theme)
99+
setTheme(theme)
100+
showActiveTheme(theme)
101+
})
102+
})
103+
})
104+
})()
84105
```
85106

86107
## Customize single elements
87-
88-
You can customize single elements with the `data-bs-theme` attribute.
108+
Elements can be assigned a static theme using the `data-bs-theme` attribute.
89109
When set to a value the element will be rendered in the given theme, despite the global theme.
90110
For example:
91111

0 commit comments

Comments
 (0)