Skip to content

Commit c82adfc

Browse files
authored
Merge pull request plone#1443 from plone/color-mode-bootstrap5
add documentation for switching color themes
2 parents d573c35 + c65e43f commit c82adfc

File tree

3 files changed

+174
-12
lines changed

3 files changed

+174
-12
lines changed

docs/classic-ui/static-resources.md

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ myst:
44
"description": "Static resources in Plone 6"
55
"property=og:description": "Static resources in Plone 6"
66
"property=og:title": "Static resources in Plone 6"
7-
"keywords": "Plone, static, resources"
7+
"keywords": "Plone, static, resources, JavaScript, CSS"
88
---
99

1010
(classic-ui-static-resources-label)=
@@ -16,12 +16,13 @@ For this, we need to register static resources.
1616

1717

1818
(classic-ui-static-resources-registering-label)=
19-
## Registering javascript and css
19+
20+
## Registering JavaScript and CSS
2021

2122
To register a static resource in Plone 6, we need to use the `plone.base.interfaces.resources.IBundleRegistry` interface.
2223

23-
The following example registers a Javascript resource in `browser/profiles/default/registry` of your Plone 6 project.
24-
The js files have to be in the `browser/static` folder of your Plone 6 project.
24+
The following example registers a JavaScript resource in `browser/profiles/default/registry` of your Plone 6 project.
25+
The JavaScript files have to be in the `browser/static` folder of your Plone 6 project.
2526

2627
```xml
2728
<registry>
@@ -47,7 +48,7 @@ You can register a CSS resource in the same way.
4748
</registry>
4849
```
4950

50-
Registering a js file and a css file in the same bundle is also possible.
51+
Registering a JavaScript file and a CSS file in the same bundle is also possible.
5152

5253
```xml
5354
<registry>
@@ -62,33 +63,37 @@ Registering a js file and a css file in the same bundle is also possible.
6263
</registry>
6364
```
6465

66+
6567
(classic-ui-static-resources-available-attributeslabel)=
6668

6769
## Available attributes
6870

6971
The following attributes are available for registering a static resource:
7072

7173
`enabled`
72-
: Whether the bundle is enabled or not. If it is disabled, the bundle will not be loaded.
74+
: Whether the bundle is enabled or not.
75+
If it is disabled, the bundle will not be loaded.
7376

7477
`jscompilation`
75-
: The path to the compiled js file.
78+
: The path to the compiled JavaScript file.
7679

7780
`csscompilation`
78-
: The path to the compiled css file.
81+
: The path to the compiled CSS file.
7982

8083
`depends`
81-
: A list of bundles that this bundle depends on.
84+
: A list of bundles that this bundle depends on.
8285

8386
`load_async`
84-
: Whether the bundle should be loaded asynchronously or not. *Only JS*
87+
: Whether the bundle should be loaded asynchronously or not.
88+
*Only JavaScript*
8589

8690
`load_defer`
87-
: Whether the bundle should be loaded deferred or not. *Only JS*
91+
: Whether the bundle should be loaded deferred or not.
92+
*Only JavaScript*
8893

8994

9095
(classic-ui-static-resources-loading-order-label)=
9196

9297
## Loading order of resources
9398

94-
`depends` is used to define the loading order of resources, by specifying the name of the depending bundle.
99+
`depends` is used to define the loading order of resources, by specifying the name of the depending bundle.
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
---
2+
myst:
3+
html_meta:
4+
"description": "Color modes in Plone Classic UI"
5+
"property=og:description": "Color modes in Plone Classic UI"
6+
"property=og:title": "Color modes in Plone Classic UI"
7+
"keywords": "Plone, Classic UI, theming, color modes, Bootstrap, Twitter"
8+
---
9+
10+
(color-modes-label)=
11+
12+
# Color modes
13+
14+
Bootstrap 5.3 has introduced [color modes](https://getbootstrap.com/docs/5.3/customize/color-modes/).
15+
This chapter is a guide for how to implement color modes in Plone 6.1.
16+
17+
```{versionadded} Plone 6.1
18+
```
19+
20+
21+
22+
(preferred-color-modes-label)=
23+
24+
## Preferred color modes
25+
26+
You will need to add some JavaScript functionality to set the Bootstrap theme to the user's preferred color scheme.
27+
Add the JavaScript file to the `browser/static` folder of your Plone 6.1 project.
28+
Register it in the `browser/profiles/default/registry` of your Plone 6.1 project.
29+
See {ref}`classic-ui-static-resources-registering-label` for more information.
30+
31+
```js
32+
(() => {
33+
'use strict'
34+
35+
// Set Bootstrap Theme to the preferred color scheme
36+
const setPreferredTheme = () => {
37+
if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
38+
document.documentElement.setAttribute('data-bs-theme', 'dark')
39+
} else {
40+
document.documentElement.setAttribute('data-bs-theme', 'light')
41+
}
42+
}
43+
44+
window.addEventListener('DOMContentLoaded', () => {
45+
setPreferredTheme()
46+
})
47+
})()
48+
```
49+
50+
51+
(toggle-button-label)=
52+
53+
## Toggle button
54+
55+
To switch color themes, corresponding elements with `data-bs-theme-value` attributes must be added to the DOM.
56+
Default Bootstrap 5.3 color themes include `light`, `dark`, and `auto`.
57+
If you want to add a theme toggler to your site, you can use the following example.
58+
59+
```html
60+
<div class="btn-group btn-group-sm">
61+
<button class="btn btn-secondary" data-bs-theme-value="light">
62+
Light
63+
</button>
64+
<button class="btn btn-secondary" data-bs-theme-value="dark">
65+
Dark
66+
</button>
67+
</div>
68+
```
69+
70+
71+
(register-the-toggle-button-label)=
72+
73+
## Register the toggle button
74+
75+
You will need to add some JavaScript functionality to the toggler.
76+
The following code snippet is based on the [Bootstrap 5.3 documentation](https://getbootstrap.com/docs/5.3/customize/color-modes/#javascript).
77+
78+
Add the JavaScript file to the `browser/static` folder of your Plone 6.1 project.
79+
Register it in the `browser/profiles/default/registry` of your Plone 6.1 project.
80+
See {ref}`classic-ui-static-resources-registering-label` for more information.
81+
82+
```js
83+
(() => {
84+
'use strict'
85+
86+
const storedTheme = localStorage.getItem('theme')
87+
88+
const getPreferredTheme = () => {
89+
if (storedTheme) {
90+
return storedTheme
91+
}
92+
return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'
93+
}
94+
95+
const setTheme = function (theme) {
96+
if (theme === 'auto' && window.matchMedia('(prefers-color-scheme: dark)').matches) {
97+
document.documentElement.setAttribute('data-bs-theme', 'dark')
98+
} else {
99+
document.documentElement.setAttribute('data-bs-theme', theme)
100+
}
101+
}
102+
103+
const showActiveTheme = theme => {
104+
const btnToActive = document.querySelector(`[data-bs-theme-value="${theme}"]`)
105+
106+
document.querySelectorAll('[data-bs-theme-value]').forEach(element => {
107+
element.classList.remove('active')
108+
})
109+
btnToActive.classList.add('active')
110+
}
111+
112+
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', () => {
113+
if (storedTheme !== 'light' || storedTheme !== 'dark') {
114+
setTheme(getPreferredTheme())
115+
}
116+
})
117+
118+
window.addEventListener('DOMContentLoaded', () => {
119+
setTheme(getPreferredTheme())
120+
showActiveTheme(getPreferredTheme())
121+
122+
document.querySelectorAll('[data-bs-theme-value]')
123+
.forEach(toggle => {
124+
toggle.addEventListener('click', () => {
125+
const theme = toggle.getAttribute('data-bs-theme-value')
126+
localStorage.setItem('theme', theme)
127+
setTheme(theme)
128+
showActiveTheme(theme)
129+
})
130+
})
131+
})
132+
})()
133+
```
134+
135+
136+
(customize-single-elements-label)=
137+
138+
## Customize single elements
139+
140+
Elements can be assigned a static theme using the `data-bs-theme` attribute.
141+
When set to a value, the element will be rendered in the given theme, overriding the global theme.
142+
See the following example.
143+
144+
```html
145+
<form data-bs-theme='light'>
146+
<div class="mb-3">
147+
<label for="exampleInputEmail1" class="form-label">Email address</label>
148+
<input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp">
149+
</div>
150+
<button type="submit" class="btn btn-primary">Submit</button>
151+
</form>
152+
```
153+
154+
## Using color modes in Plone 6.0
155+
156+
To use color modes in Plone 6.0, manually include `barceloneta=3.1.0` and `plone.staticresources=2.1.0`, as described in {ref}`manage-add-an-add-on`.

docs/classic-ui/theming/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@ barceloneta
2323
diazo
2424
from-scratch
2525
through-the-web
26+
color-mode
2627
```

0 commit comments

Comments
 (0)