|
| 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`. |
0 commit comments