Skip to content

Commit 0720c2a

Browse files
committed
refactor: adjust enableDarkMode - auto means system mode, true equals dark and false light mode
1 parent 3d88c5e commit 0720c2a

File tree

2 files changed

+9
-12
lines changed

2 files changed

+9
-12
lines changed

README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,19 +189,18 @@ 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.
193-
The searchbar automatically reacts to changes to the system mode. If you want to force dark mode you can set the option to `'always'`.
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.
194193

195194
Example:
196195

197196
```javascript
198197
docsSearchBar({
199198
...
200-
enableDarkMode: true
199+
enableDarkMode: 'auto'
201200
})
202201
```
203202

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

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

src/lib/DocsSearchBar.js

Lines changed: 6 additions & 8 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|'always'} [options.enableDarkMode] Allows you to display the searchbar in dark mode. Can be forced with the string 'always'
24+
* @param {boolean|'auto'} [options.enableDarkMode] Allows you to display the searchbar in dark mode. Option 'auto' automatically sets the mode based on the system mode
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 {boolean|'always'} enableDarkMode Wether darkMode is enabled
177+
* @param {boolean|'auto'} enableDarkMode Wether darkMode is enabled
178178
* @returns {void}
179179
*/
180180
static addThemeWrapper(inputSelector, enableDarkMode) {
@@ -185,10 +185,8 @@ class DocsSearchBar {
185185
parent.replaceChild(wrapper, inputElement)
186186
wrapper.appendChild(inputElement)
187187

188-
let isSystemInDarkMode = false
189-
if (inputElement && enableDarkMode === 'always') {
190-
isSystemInDarkMode = true
191-
} else if (inputElement && enableDarkMode && window.matchMedia) {
188+
let isSystemInDarkMode = inputElement && enableDarkMode === true
189+
if (inputElement && enableDarkMode === 'auto' && window.matchMedia) {
192190
const mediaQueryList = window.matchMedia('(prefers-color-scheme: dark)')
193191
isSystemInDarkMode = mediaQueryList.matches
194192

@@ -241,12 +239,12 @@ class DocsSearchBar {
241239
)
242240

243241
if (
244-
args.enableDarkMode !== 'always' &&
242+
args.enableDarkMode !== 'auto' &&
245243
args.enableDarkMode !== false &&
246244
args.enableDarkMode !== true
247245
) {
248246
throw new Error(
249-
`Error: "enableDarkMode" must be either true, false, or 'always'. Supplied value: ${args.enableDarkMode}`,
247+
`Error: "enableDarkMode" must be either true, false, or 'auto'. Supplied value: ${args.enableDarkMode}`,
250248
)
251249
}
252250

0 commit comments

Comments
 (0)