1- import React , { useState } from 'react'
2- import { Button } from './ui/button'
3- import { createRandomTheme } from '../lib/create-theme-config'
4- import { useTheme } from 'next-themes'
5- import { getComputedHSLColor , saveTheme , setColorsProperties } from '../lib/utils'
6- import { Dices , Lock , UnLock } from './icons' ;
7- import { ReadonlyThemeWithHSLColor , SystemThemes , themeModes } from '../lib/theme'
1+ import React , { useEffect , useState } from "react" ;
2+ import { Button } from "./ui/button" ;
3+ import { createRandomTheme } from "../lib/create-theme-config" ;
4+ import { useTheme } from "next-themes" ;
5+ import {
6+ getComputedHSLColor ,
7+ saveTheme ,
8+ setColorsProperties ,
9+ } from "../lib/utils" ;
10+ import { Dices , Lock , UnLock } from "./icons" ;
11+ import {
12+ SystemThemes ,
13+ themeModes ,
14+ ThemeWithHSLColor ,
15+ } from "../lib/theme" ;
16+ import { themeEmittor } from "../lib/emittors" ;
817
918function RandomBtn ( ) {
10- const { resolvedTheme= "" + undefined , systemTheme= "dark" } = useTheme ( ) ;
11- const [ lockPrimary , setLockPrimary ] = useState ( true ) ;
12- const onClickHandler = ( ) => {
13- const themes = createRandomTheme ( lockPrimary ? getComputedHSLColor ( "--primary" ) : undefined )
14- let theme ;
19+ const {
20+ resolvedTheme : NresolvedTheme = "" + undefined ,
21+ systemTheme : NsystemTheme = "dark" ,
22+ } = useTheme ( ) ;
23+ const [ resolvedTheme , setResolvedTheme ] = useState < string > ( ) ;
24+ const [ systemTheme , setSystemTheme ] = useState < string > ( ) ;
25+ useEffect ( ( ) => {
26+ setResolvedTheme ( NresolvedTheme ) ;
27+ setSystemTheme ( NsystemTheme ) ;
28+ } , [ NresolvedTheme , NsystemTheme ] ) ;
29+ const [ lockPrimary , setLockPrimary ] = useState ( true ) ;
30+ const onClickHandler = ( ) => {
31+ const themes = createRandomTheme (
32+ lockPrimary ? getComputedHSLColor ( "--primary" ) : undefined
33+ ) ;
34+ let theme ;
1535
16- if ( SystemThemes . includes ( resolvedTheme as any ) ) {
17- theme = themes [ resolvedTheme as themeModes ] as ReadonlyThemeWithHSLColor [ ] ;
18- SystemThemes . forEach ( theme => saveTheme ( resolvedTheme , themes [ theme ] ) ) // save both themes
19- } else {
20- theme = themes [ systemTheme ] as ReadonlyThemeWithHSLColor [ ] ;
21- saveTheme ( resolvedTheme , theme )
22- }
23- setColorsProperties ( theme )
36+ if ( SystemThemes . includes ( resolvedTheme as any ) ) {
37+ theme = themes [
38+ resolvedTheme as themeModes
39+ ] as ThemeWithHSLColor [ ] ;
40+ SystemThemes . forEach ( ( theme ) => saveTheme ( theme , themes [ theme ] ) ) ; // save both themes
41+ } else {
42+ theme = themes [ systemTheme as themeModes ] as ThemeWithHSLColor [ ] ;
43+ saveTheme ( resolvedTheme , theme ) ;
2444 }
25- const LockIcon = lockPrimary ? Lock : UnLock
45+ themeEmittor . applyTheme ( theme )
46+ // themeEmittor.e.setState(theme)
47+ // setColorsProperties(theme);
48+ } ;
49+ const LockIcon = lockPrimary ? Lock : UnLock ;
2650 return (
27- < Button onClick = { onClickHandler } title = { 'Generate Random theme, Primary is ' + ( lockPrimary ? "locked" : "not locked" ) } variant = { "colorbtn" } className = 'cursor-pointer' >
28- < Dices className = 'size-5' /> Randomize < LockIcon onClick = { ( e ) => console . log ( setLockPrimary ( ! lockPrimary ) , lockPrimary , e . stopPropagation ( ) ) } aria-label = { ( lockPrimary ?'unlock' : 'lock' ) + ' primary color' } className = 'ml-auto size-6 hover:opacity-80 rounded-md hover:bg-background hover:fill-foreground p-0.5 fill-current' />
51+ < Button
52+ onClick = { onClickHandler }
53+ title = {
54+ "Generate Random theme, Primary is " +
55+ ( lockPrimary ? "locked" : "not locked" )
56+ }
57+ variant = { "colorbtn" }
58+ className = "cursor-pointer"
59+ >
60+ < Dices className = "size-5" /> Randomize{ " " }
61+ < LockIcon
62+ onClick = { ( e ) =>
63+ console . log (
64+ setLockPrimary ( ! lockPrimary ) ,
65+ lockPrimary ,
66+ e . stopPropagation ( )
67+ )
68+ }
69+ aria-label = { ( lockPrimary ? "unlock" : "lock" ) + " primary color" }
70+ className = "ml-auto size-6 hover:opacity-80 rounded-md hover:bg-background hover:fill-foreground p-0.5 fill-current"
71+ />
2972 </ Button >
30- )
73+ ) ;
3174}
3275
33- export default RandomBtn
76+ export default RandomBtn ;
0 commit comments