@@ -2,6 +2,7 @@ import { useMemo, useState } from 'react';
22import { THEME_MODE } from '@tdesign/common-js/common' ;
33import getColorTokenColor from '@tdesign/common-js/utils/getColorTokenColor' ;
44import useMutationObservable from './useMutationObserver' ;
5+ import { canUseDocument } from '../_util/dom' ;
56
67const DEFAULT_OPTIONS = {
78 debounceTime : 250 ,
@@ -28,17 +29,22 @@ function useVariables<T extends Record<string, string>>(
2829 variables : T ,
2930 targetElement ?: HTMLElement ,
3031) : Record < keyof T , string > {
31- if ( typeof window !== undefined && ! targetElement ) {
32+ const [ , forceUpdate ] = useState < Record < string , never > > ( { } ) ;
33+
34+ // @ts -expect-error
35+ if ( ! canUseDocument ) return { } ;
36+
37+ if ( ! targetElement ) {
3238 // eslint-disable-next-line no-param-reassign
3339 targetElement = document ?. documentElement ;
3440 }
35- const [ , forceUpdate ] = useState < Record < string , never > > ( { } ) ;
3641
3742 // 确保 variables 参数有效
3843 if ( ! variables || Object . keys ( variables ) . length === 0 ) {
3944 throw new Error ( 'useVariables: variables parameter cannot be empty' ) ;
4045 }
4146
47+ // eslint-disable-next-line react-hooks/rules-of-hooks
4248 const refs = useMemo ( ( ) => {
4349 const values = { } as Record < keyof T , string > ;
4450
@@ -71,6 +77,7 @@ function useVariables<T extends Record<string, string>>(
7177 }
7278 } ;
7379
80+ // eslint-disable-next-line react-hooks/rules-of-hooks
7481 useMutationObservable (
7582 targetElement ,
7683 ( mutationsList ) => {
0 commit comments