Skip to content

Commit 5500d6f

Browse files
authored
fix(Watermark): fix watermark in ssr (#3764)
* fix(Watermark): fix watermark in ssr * fix(Watermark): fix watermark in ssr
1 parent 55e33f8 commit 5500d6f

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

packages/components/hooks/useVariables.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { useMemo, useState } from 'react';
22
import { THEME_MODE } from '@tdesign/common-js/common';
33
import getColorTokenColor from '@tdesign/common-js/utils/getColorTokenColor';
44
import useMutationObservable from './useMutationObserver';
5+
import { canUseDocument } from '../_util/dom';
56

67
const 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

Comments
 (0)