Skip to content

Commit b7415d0

Browse files
committed
docs(header): make moon/sun icons switch current applied theme
1 parent 99e840e commit b7415d0

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

apps/website/src/components/header/header.tsx

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { component$, useComputed$, useSignal, useStyles$ } from '@builder.io/qwik';
1+
import { $, component$, useComputed$, useSignal, useStyles$ } from '@builder.io/qwik';
22
// eslint-disable-next-line @nx/enforce-module-boundaries
33
import { version as headlessVersion } from '../../../../../packages/kit-headless/package.json';
44
// eslint-disable-next-line @nx/enforce-module-boundaries
@@ -129,6 +129,23 @@ export default component$(({ showVersion = false }: HeaderProps) => {
129129

130130
const { theme, setTheme } = useTheme();
131131

132+
const switchLightDark = $((input: string | string[]): string | string[] | undefined => {
133+
const switchWord = (word: string): string => {
134+
if (word.includes('light')) {
135+
return word.replace('light', 'dark');
136+
} else if (word.includes('dark')) {
137+
return word.replace('dark', 'light');
138+
}
139+
return word;
140+
};
141+
142+
if (typeof input === 'string') {
143+
return switchWord(input);
144+
} else if (Array.isArray(input)) {
145+
return input.map((item) => switchWord(item));
146+
}
147+
});
148+
132149
return (
133150
<div
134151
class={cn(
@@ -185,9 +202,7 @@ export default component$(({ showVersion = false }: HeaderProps) => {
185202
<button
186203
type="button"
187204
aria-label="Toggle dark mode"
188-
onClick$={() => {
189-
theme?.includes('light') ? setTheme('dark') : setTheme('light');
190-
}}
205+
onClick$={async () => setTheme(await switchLightDark(theme || 'light'))}
191206
>
192207
<div class="hidden dark:block">
193208
<MoonIcon />

0 commit comments

Comments
 (0)