Skip to content

Commit 9864000

Browse files
Merge #453
453: Automatic dark/light mode toggle, force dark mode r=bidoubiwa a=ColinFrick This adds a new event listener to the color scheme, and `always` as a third option for `enableDarkMode` Any objection against the option value `always`? It could also be `force` or something like that? What do you think? Closes #369 Co-authored-by: Colin Frick <[email protected]> Co-authored-by: colin.frick <[email protected]>
2 parents fd260bb + 68b8463 commit 9864000

File tree

2 files changed

+42
-13
lines changed

2 files changed

+42
-13
lines changed

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,18 +189,23 @@ docsSearchBar({
189189
##### `enableDarkMode` <!-- omit in toc -->
190190

191191
Allows you to display the searchbar in dark mode. It is useful if your website has dark mode support and you also want the searchbar to appear in a dark version.
192-
You can always edit the style of the searchbar to match the style of your website.
192+
You can always edit the style of the searchbar to match the style of your website. When the option `enableDarkMode` is set to `auto`, the searchbar automatically sets the mode to the system mode.
193+
194+
`enableDarkMode` has three possible states:
195+
- `false`: enforce light mode.
196+
- `true`: enforce dark mode.
197+
- `auto`: system mode (light or dark).
193198

194199
Example:
195200

196201
```javascript
197202
docsSearchBar({
198203
...
199-
enableDarkMode: true
204+
enableDarkMode: 'auto'
200205
})
201206
```
202207

203-
Dark mode with `enableDarkMode` set to `true` and system mode set to `dark`:
208+
Dark mode with `enableDarkMode` set to `auto` and system mode set to `dark`:
204209

205210
![docs-searchbar with dark mode](assets/dark-mode.png)
206211

src/lib/DocsSearchBar.js

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { MeiliSearch } from 'meilisearch'
2121
* @param {function} [options.handleSelected] This function is called when a suggestion is selected
2222
* @param {function} [options.enhancedSearchInput] When set to true, a theme is applied to the search box to improve its appearance
2323
* @param {'column'|'simple'} [options.layout] Layout of the search bar
24-
* @param {boolean} [options.enableDarkMode] Allows you to display the searchbar in dark mode
24+
* @param {boolean|'auto'} [options.enableDarkMode] Allows you to enforce, light theme, dark theme, or auto mode on the searchbar.
2525
* @return {Object}
2626
*/
2727
const usage = `Usage:
@@ -174,7 +174,7 @@ class DocsSearchBar {
174174
* Wraps input selector in a docs-searchbar-js div
175175
* @function addThemeWrapper
176176
* @param {string} inputSelector Selector of the input element
177-
* @param {bool} enableDarkMode Wether darkMode is enabled
177+
* @param {boolean|'auto'} enableDarkMode Allows you to enforce, light theme, dark theme, or auto mode on the searchbar.
178178
* @returns {void}
179179
*/
180180
static addThemeWrapper(inputSelector, enableDarkMode) {
@@ -185,14 +185,28 @@ class DocsSearchBar {
185185
parent.replaceChild(wrapper, inputElement)
186186
wrapper.appendChild(inputElement)
187187

188-
const isSystemInDarkMode =
189-
window.matchMedia &&
190-
window.matchMedia('(prefers-color-scheme: dark)').matches
188+
let isSystemInDarkMode = Boolean(enableDarkMode)
189+
if (enableDarkMode === 'auto' && window.matchMedia) {
190+
const mediaQueryList = window.matchMedia('(prefers-color-scheme: dark)')
191+
isSystemInDarkMode = mediaQueryList.matches
192+
193+
const listener = function (e) {
194+
if (document.body.contains(wrapper)) {
195+
wrapper.setAttribute('data-ds-theme', e.matches ? 'dark' : 'light')
196+
} else if (mediaQueryList.removeEventListener) {
197+
mediaQueryList.removeEventListener('change', listener)
198+
} else if (mediaQueryList.removeListener) {
199+
mediaQueryList.removeListener(listener)
200+
}
201+
}
191202

192-
wrapper.setAttribute(
193-
'data-ds-theme',
194-
inputElement && enableDarkMode && isSystemInDarkMode ? 'dark' : 'light',
195-
)
203+
if (mediaQueryList.addEventListener) {
204+
mediaQueryList.addEventListener('change', listener)
205+
} else if (mediaQueryList.addListener) {
206+
mediaQueryList.addListener(listener)
207+
}
208+
}
209+
wrapper.setAttribute('data-ds-theme', isSystemInDarkMode ? 'dark' : 'light')
196210
}
197211

198212
/**
@@ -225,9 +239,19 @@ class DocsSearchBar {
225239
true,
226240
)
227241

242+
if (
243+
args.enableDarkMode !== 'auto' &&
244+
args.enableDarkMode !== false &&
245+
args.enableDarkMode !== true
246+
) {
247+
throw new Error(
248+
`Error: "enableDarkMode" must be either true, false, or 'auto'. Supplied value: ${args.enableDarkMode}`,
249+
)
250+
}
251+
228252
DocsSearchBar.typeCheck(
229253
args,
230-
['debug', 'enableDarkMode', 'enhancedSearchInput'],
254+
['debug', 'enhancedSearchInput'],
231255
'boolean',
232256
false,
233257
)

0 commit comments

Comments
 (0)