Skip to content

Commit a678e15

Browse files
authored
docs: add dark light switcher for patternhub (#4261)
1 parent 424537a commit a678e15

File tree

2 files changed

+52
-8
lines changed

2 files changed

+52
-8
lines changed

showcases/patternhub/components/default-page.tsx

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
import { useRouter } from 'next/router';
22
import dynamic from 'next/dynamic';
3-
import { Fragment, type PropsWithChildren, useEffect, useState } from 'react';
3+
import {
4+
Fragment,
5+
type PropsWithChildren,
6+
useCallback,
7+
useEffect,
8+
useState
9+
} from 'react';
410
import hljs from 'highlight.js';
511
import Link from 'next/link';
612
import {
713
DBBrand,
8-
DBButton,
14+
DBSwitch,
15+
DBTooltip,
916
DBHeader,
1017
DBPage,
1118
DBSection,
@@ -20,6 +27,9 @@ import {
2027
import Navigation from './navigation';
2128
import VersionSwitcher from './version-switcher';
2229

30+
const preferDark = '(prefers-color-scheme: dark)';
31+
const colorModeKey = 'db-ux-mode';
32+
2333
const DefaultPage = ({
2434
children,
2535
noNavigation
@@ -38,6 +48,25 @@ const DefaultPage = ({
3848
const [breadcrumb, setBreadcrumb] = useState<NavigationItem[]>();
3949
const router = useRouter();
4050

51+
const [mode, setMode] = useState<boolean>(
52+
localStorage.getItem(colorModeKey) === null
53+
? globalThis.matchMedia?.(preferDark).matches
54+
: localStorage.getItem(colorModeKey) === 'dark'
55+
);
56+
57+
const setColorMode = useCallback((dark: boolean) => {
58+
localStorage.setItem(colorModeKey, dark ? 'dark' : 'light');
59+
setMode(dark);
60+
}, []);
61+
62+
useEffect(() => {
63+
globalThis
64+
.matchMedia(preferDark)
65+
.addEventListener('change', (event) => {
66+
setColorMode(event.matches);
67+
});
68+
}, []);
69+
4170
useEffect(() => {
4271
hljs.configure({
4372
languages: [
@@ -106,6 +135,7 @@ const DefaultPage = ({
106135
)}
107136
{router.isReady && !fullscreen && (
108137
<DBPage
138+
data-mode={mode ? 'dark' : 'light'}
109139
fadeIn
110140
variant="fixed"
111141
header={
@@ -118,12 +148,20 @@ const DefaultPage = ({
118148
</DBBrand>
119149
}
120150
primaryAction={
121-
<DBButton
122-
icon="magnifying_glass"
123-
variant="ghost"
124-
noText>
125-
Search
126-
</DBButton>
151+
<DBSwitch
152+
checked={mode}
153+
visualAid
154+
icon="sun"
155+
iconAfter="moon"
156+
showLabel={false}
157+
onChange={() => {
158+
setColorMode(!mode);
159+
}}>
160+
<DBTooltip>
161+
Switch color scheme (light/dark)
162+
</DBTooltip>
163+
Switch color scheme (light/dark)
164+
</DBSwitch>
127165
}
128166
secondaryAction={<VersionSwitcher />}>
129167
<Navigation />

showcases/patternhub/styles/globals.scss

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,3 +633,9 @@ h6 {
633633
}
634634
}
635635
}
636+
637+
.db-switch {
638+
> .db-tooltip {
639+
min-inline-size: variables.$db-sizing-2xl;
640+
}
641+
}

0 commit comments

Comments
 (0)