Skip to content

Automatic dark/light mode toggle, force dark mode #453

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,18 +189,23 @@ docsSearchBar({
##### `enableDarkMode` <!-- omit in toc -->

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.
You can always edit the style of the searchbar to match the style of your website.
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.

`enableDarkMode` has three possible states:
- `false`: enforce light mode.
- `true`: enforce dark mode.
- `auto`: system mode (light or dark).

Example:

```javascript
docsSearchBar({
...
enableDarkMode: true
enableDarkMode: 'auto'
})
```

Dark mode with `enableDarkMode` set to `true` and system mode set to `dark`:
Dark mode with `enableDarkMode` set to `auto` and system mode set to `dark`:

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

Expand Down
44 changes: 34 additions & 10 deletions src/lib/DocsSearchBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { MeiliSearch } from 'meilisearch'
* @param {function} [options.handleSelected] This function is called when a suggestion is selected
* @param {function} [options.enhancedSearchInput] When set to true, a theme is applied to the search box to improve its appearance
* @param {'column'|'simple'} [options.layout] Layout of the search bar
* @param {boolean} [options.enableDarkMode] Allows you to display the searchbar in dark mode
* @param {boolean|'auto'} [options.enableDarkMode] Allows you to enforce, light theme, dark theme, or auto mode on the searchbar.
* @return {Object}
*/
const usage = `Usage:
Expand Down Expand Up @@ -174,7 +174,7 @@ class DocsSearchBar {
* Wraps input selector in a docs-searchbar-js div
* @function addThemeWrapper
* @param {string} inputSelector Selector of the input element
* @param {bool} enableDarkMode Wether darkMode is enabled
* @param {boolean|'auto'} enableDarkMode Allows you to enforce, light theme, dark theme, or auto mode on the searchbar.
* @returns {void}
*/
static addThemeWrapper(inputSelector, enableDarkMode) {
Expand All @@ -185,14 +185,28 @@ class DocsSearchBar {
parent.replaceChild(wrapper, inputElement)
wrapper.appendChild(inputElement)

const isSystemInDarkMode =
window.matchMedia &&
window.matchMedia('(prefers-color-scheme: dark)').matches
let isSystemInDarkMode = Boolean(enableDarkMode)
if (enableDarkMode === 'auto' && window.matchMedia) {
const mediaQueryList = window.matchMedia('(prefers-color-scheme: dark)')
isSystemInDarkMode = mediaQueryList.matches

const listener = function (e) {
if (document.body.contains(wrapper)) {
wrapper.setAttribute('data-ds-theme', e.matches ? 'dark' : 'light')
} else if (mediaQueryList.removeEventListener) {
mediaQueryList.removeEventListener('change', listener)
} else if (mediaQueryList.removeListener) {
mediaQueryList.removeListener(listener)
}
}

wrapper.setAttribute(
'data-ds-theme',
inputElement && enableDarkMode && isSystemInDarkMode ? 'dark' : 'light',
)
if (mediaQueryList.addEventListener) {
mediaQueryList.addEventListener('change', listener)
} else if (mediaQueryList.addListener) {
mediaQueryList.addListener(listener)
}
}
wrapper.setAttribute('data-ds-theme', isSystemInDarkMode ? 'dark' : 'light')
}

/**
Expand Down Expand Up @@ -225,9 +239,19 @@ class DocsSearchBar {
true,
)

if (
args.enableDarkMode !== 'auto' &&
args.enableDarkMode !== false &&
args.enableDarkMode !== true
) {
throw new Error(
`Error: "enableDarkMode" must be either true, false, or 'auto'. Supplied value: ${args.enableDarkMode}`,
)
}

DocsSearchBar.typeCheck(
args,
['debug', 'enableDarkMode', 'enhancedSearchInput'],
['debug', 'enhancedSearchInput'],
'boolean',
false,
)
Expand Down