@@ -6,6 +6,11 @@ import {
66 isServer ,
77} from '@qwik.dev/core' ;
88import { themeStorageKey } from '../router-head/theme-script' ;
9+ import {
10+ HiSunOutline ,
11+ HiMoonOutline ,
12+ HiStopCircleOutline ,
13+ } from '@qwikest/icons/heroicons' ;
914
1015type ThemeName = 'dark' | 'light' | 'auto' ;
1116
@@ -23,7 +28,7 @@ export const getTheme = (): ThemeName => {
2328 return theme as ThemeName ;
2429 } else {
2530 // should be 'auto' when no theme is set
26- return 'auto'
31+ return 'auto' ;
2732 }
2833} ;
2934
@@ -59,30 +64,44 @@ export const setTheme = (theme: ThemeName) => {
5964
6065export const ThemeToggle = component$ ( ( ) => {
6166 const themeValue = createSignal ( getTheme ( ) ) ;
62- const onClick$ = event$ ( ( _ : any , e : any ) => {
63- setTheme ( e . value ) ;
64- themeValue . value = e . value ;
67+ const onClick$ = event$ ( ( ) => {
68+ let newTheme = getTheme ( ) ;
69+ console . log ( 'Theme changed to:' , newTheme ) ;
70+ if ( newTheme === 'dark' ) {
71+ newTheme = 'light' ;
72+ setTheme ( newTheme ) ;
73+ } else if ( newTheme === 'light' ) {
74+ newTheme = 'auto' ;
75+ setTheme ( newTheme ) ;
76+ } else {
77+ newTheme = window . matchMedia ( '(prefers-color-scheme: dark)' ) . matches
78+ ? 'light'
79+ : 'dark' ;
80+ setTheme ( newTheme ) ;
81+ }
82+ console . log ( 'New theme set:' , newTheme ) ;
83+
84+ themeValue . value = newTheme ;
6585 } ) ;
6686
6787 return (
68- < div class = "theme-control" >
69- < label for = "theme-select" class = "sr-only" >
70- Choose a theme
71- </ label >
72- < select
73- class = "w-10 rounded-lg border border-border bg-background px-1 py-2 text-sm text-foreground placeholder-muted-foreground outline-none focus:border-ring"
74- onInput$ = { onClick$ }
88+ < >
89+ < button
90+ onClick$ = { onClick$ }
91+ class = "group flex h-8 w-8 items-center justify-center rounded-md bg-background text-foreground hover:opacity-60"
7592 >
76- < option value = "dark" selected = { themeValue . value === 'dark' } >
77- Dark
78- </ option >
79- < option value = "light" selected = { themeValue . value === 'light' } >
80- Light
81- </ option >
82- < option value = "auto" selected = { themeValue . value === 'auto' } >
83- Auto
84- </ option >
85- </ select >
86- </ div >
93+ < div class = "transition-transform duration-200 ease-out group-hover:scale-110 group-active:scale-75" >
94+ { themeValue . value === 'light' && (
95+ < HiSunOutline class = "animate-in zoom-in-50 h-5 w-5 duration-300 ease-out" />
96+ ) }
97+ { themeValue . value === 'dark' && (
98+ < HiMoonOutline class = "animate-in zoom-in-50 h-5 w-5 duration-300 ease-out" />
99+ ) }
100+ { themeValue . value === 'auto' && (
101+ < HiStopCircleOutline class = "animate-in zoom-in-50 h-5 w-5 duration-300 ease-out" />
102+ ) }
103+ </ div >
104+ </ button >
105+ </ >
87106 ) ;
88107} ) ;
0 commit comments