@@ -2,52 +2,63 @@ import {useCallback, useMemo, useRef} from 'react';
22import type { CSSProperties } from 'react' ;
33import type { ThemeType } from 'css-vars-hook' ;
44
5+ import type { UnitType } from '@/lib/UnitType' ;
6+
57import {
68 createStyleObject ,
79 getRootVariable ,
810 removeRootVariable ,
911 setRootVariable ,
1012} from '../utils' ;
1113import type { HookInterface } from './HookInterfaceType' ;
12- import type { UnitType } from '../UnitType' ;
1314
1415/**
1516 * @private
1617 * Logic for root theme handling such as updates and CSS style creation
1718 */
1819export const useRootTheme = (
19- theme : ThemeType
20+ theme : ThemeType ,
21+ id : string
2022) : HookInterface & { style : CSSProperties } => {
2123 const themeRef = useRef ( theme ) ;
2224
23- const setTheme = useCallback ( ( nextTheme : ThemeType ) => {
24- Object . keys ( nextTheme ) . forEach ( key => {
25- setRootVariable ( key , nextTheme [ key ] ) ;
26- } ) ;
25+ const setTheme = useCallback (
26+ ( nextTheme : ThemeType ) => {
27+ Object . keys ( nextTheme ) . forEach ( key => {
28+ setRootVariable ( id ) ( key , nextTheme [ key ] ) ;
29+ } ) ;
2730
28- themeRef . current = nextTheme ;
29- } , [ ] ) ;
31+ themeRef . current = nextTheme ;
32+ } ,
33+ [ id ]
34+ ) ;
3035
3136 const getTheme = useCallback ( ( ) => themeRef . current , [ ] ) ;
3237
3338 const getVariable = useCallback (
34- ( variableName : string ) => getRootVariable ( variableName ) ,
35- [ ]
39+ ( variableName : string ) => getRootVariable ( id ) ( variableName ) ,
40+ [ id ]
41+ ) ;
42+ const setVariable = useCallback (
43+ ( variableName : string , value : UnitType ) => {
44+ setRootVariable ( id ) ( variableName , value ) ;
45+ themeRef . current = {
46+ ...themeRef . current ,
47+ [ variableName ] : value ,
48+ } ;
49+ } ,
50+ [ id ]
3651 ) ;
37- const setVariable = useCallback ( ( variableName : string , value : UnitType ) => {
38- setRootVariable ( variableName , value ) ;
39- themeRef . current = {
40- ...themeRef . current ,
41- [ variableName ] : value ,
42- } ;
43- } , [ ] ) ;
4452
45- const removeVariable = useCallback ( ( variableName : string ) => {
46- removeRootVariable ( variableName ) ;
47- const nextTheme = { ...themeRef . current } ;
48- delete nextTheme [ variableName ] ;
49- themeRef . current = nextTheme ;
50- } , [ ] ) ;
53+ const removeVariable = useCallback (
54+ ( variableName : string ) => {
55+ removeRootVariable ( id ) ( variableName ) ;
56+ const nextTheme = { ...themeRef . current } ;
57+ delete nextTheme [ variableName ] ;
58+ themeRef . current = nextTheme ;
59+ } ,
60+ [ id ]
61+ ) ;
5162
5263 const style = useMemo ( ( ) => createStyleObject ( themeRef . current ) , [ ] ) ;
5364
0 commit comments