Skip to content

Commit 0947f47

Browse files
committed
fix(root id): fix root id
1 parent 83a33ea commit 0947f47

File tree

9 files changed

+62
-58
lines changed

9 files changed

+62
-58
lines changed

src/lib/LocalTheme/LocalRoot.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import type {HTMLAttributes, ReactNode} from 'react';
22
import {createElement, forwardRef, useEffect, useMemo} from 'react';
33
import type {ThemeType} from 'css-vars-hook';
44

5-
import {createStyleObject} from '../utils';
6-
import type {DataAttributes, LibraryProps} from '../NativeProps';
5+
import {createStyleObject} from '@/lib/utils';
6+
import type {DataAttributes, LibraryProps} from '@/lib/NativeProps';
77

88
/**
99
* @public

src/lib/LocalTheme/useLocalTheme.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import {useCallback, useRef, useMemo} from 'react';
22
import type {ThemeType} from 'css-vars-hook';
33

4-
import {setCSSVariable} from '../utils';
4+
import {setCSSVariable} from '@/lib/utils';
5+
import type {UnitType} from '@/lib/UnitType';
6+
57
import type {LocalRootProps} from './LocalRoot';
68
import {LocalRoot} from './LocalRoot';
7-
import type {UnitType} from '../UnitType';
89

910
/**
1011
* @public

src/lib/NativeProps.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ export type DataAttributeKey = `data-${string}`;
55
export type DataAttributes = Record<DataAttributeKey, string>;
66

77
export type LibraryProps<TElement = HTMLDivElement> = AriaAttributes & {
8-
id?: string;
8+
/** Provide an id for Provider component */
9+
id?: HTMLAttributes<TElement>['id'];
910
/**
1011
* Set native ARIA role attribute
1112
* @see https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Roles

src/lib/RootTheme/HookInterfaceType.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type {ThemeType} from 'css-vars-hook';
22

3-
import type {UnitType} from '../UnitType';
3+
import type {UnitType} from '@/lib/UnitType';
44

55
/**
66
* @public

src/lib/RootTheme/RootThemeProvider.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import type {FC, ReactNode} from 'react';
2-
import {useMemo, useEffect} from 'react';
2+
import {useMemo, useEffect, useId} from 'react';
33
import type {ThemeType} from 'css-vars-hook';
44

5-
import {ROOT_ID} from '../config';
65
import type {DataAttributes, LibraryProps} from '../NativeProps';
76
import {RootContext} from './RootContext';
87
import {useRootTheme} from './useRootTheme';
@@ -24,11 +23,13 @@ export const RootThemeProvider: FC<RootThemeProviderProps> = ({
2423
children,
2524
theme,
2625
className,
27-
id = ROOT_ID,
26+
id,
2827
...nativeProps
2928
}) => {
29+
const backupId = useId();
30+
const rootId = id ? id : backupId;
3031
const {setTheme, style, getTheme, getVariable, setVariable, removeVariable} =
31-
useRootTheme(theme);
32+
useRootTheme(theme, rootId);
3233

3334
const {Provider} = RootContext;
3435

@@ -43,7 +44,7 @@ export const RootThemeProvider: FC<RootThemeProviderProps> = ({
4344

4445
return (
4546
<Provider value={actions}>
46-
<div {...nativeProps} id={id} className={className} style={style}>
47+
<div {...nativeProps} id={rootId} className={className} style={style}>
4748
{children}
4849
</div>
4950
</Provider>

src/lib/RootTheme/useRootTheme.ts

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,63 @@ import {useCallback, useMemo, useRef} from 'react';
22
import type {CSSProperties} from 'react';
33
import type {ThemeType} from 'css-vars-hook';
44

5+
import type {UnitType} from '@/lib/UnitType';
6+
57
import {
68
createStyleObject,
79
getRootVariable,
810
removeRootVariable,
911
setRootVariable,
1012
} from '../utils';
1113
import 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
*/
1819
export 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

src/lib/config.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/lib/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,5 @@ export {useRootTheme, RootThemeProvider} from './RootTheme';
22
export type {RootThemeProviderProps} from './RootTheme';
33
export {useLocalTheme} from './LocalTheme';
44
export type {LocalRootProps} from './LocalTheme';
5-
export {ROOT_ID} from './config';
65
export type {UnitType} from './UnitType';
76
export type {LibraryProps, DataAttributes, DataAttributeKey} from './NativeProps';

src/lib/utils.ts

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import type {MutableRefObject, CSSProperties} from 'react';
22
import type {ThemeType} from 'css-vars-hook';
33

4-
import {ROOT_ID} from './config';
5-
import type {UnitType} from './UnitType';
4+
import type {UnitType} from '@/lib/UnitType';
65

76
const normalizeUnit = (unit: UnitType) => {
87
if (typeof unit === 'string') {
@@ -53,35 +52,32 @@ export const createStyleObject = (theme: ThemeType): CSSProperties => {
5352
return result;
5453
};
5554

56-
/** @function
57-
* @name getRootElement
58-
* @description Get theme root element in an SSR-safe way
59-
*/
60-
export const getRootElement = (): HTMLElement => document.getElementById(ROOT_ID)!;
61-
6255
/** @function
6356
* @name setRootVariable
6457
* @description Set CSS variable on theme root element
6558
*/
66-
export const setRootVariable = (variableName: string, value: UnitType) => {
67-
const root = getRootElement();
68-
root?.style?.setProperty?.(`--${variableName}`, normalizeUnit(value));
69-
};
59+
export const setRootVariable =
60+
(id: string) => (variableName: string, value: UnitType) => {
61+
const root = document.getElementById(id)!;
62+
root?.style?.setProperty?.(`--${variableName}`, normalizeUnit(value));
63+
};
7064

7165
/** @function
7266
* @name getRootVariable
7367
* @description Get CSS variable on theme root element
7468
*/
75-
export const getRootVariable = (variableName: string): string => {
76-
const root = getRootElement();
77-
return root?.style?.getPropertyValue?.(`--${variableName}`);
78-
};
69+
export const getRootVariable =
70+
(id: string) =>
71+
(variableName: string): string => {
72+
const root = document.getElementById(id)!;
73+
return root?.style?.getPropertyValue?.(`--${variableName}`);
74+
};
7975

8076
/** @function
8177
* @name removeRootVariable
8278
* @description Remove CSS variable from theme root element
8379
*/
84-
export const removeRootVariable = (variableName: string) => {
85-
const root = getRootElement();
80+
export const removeRootVariable = (id: string) => (variableName: string) => {
81+
const root = document.getElementById(id)!;
8682
root?.style?.removeProperty?.(`--${variableName}`);
8783
};

0 commit comments

Comments
 (0)