From 741cfbf5b359fbc1a38f676c517f09b5529d9423 Mon Sep 17 00:00:00 2001 From: MadCcc Date: Tue, 31 Oct 2023 14:43:31 +0800 Subject: [PATCH] fix: css var prefix should affect style hash (#155) --- src/hooks/useCSSVarRegister.ts | 9 ++-- src/hooks/useCacheToken.tsx | 2 +- .../__snapshots__/css-variables.spec.tsx.snap | 2 +- tests/css-variables.spec.tsx | 49 ++++++++++++++++++- 4 files changed, 54 insertions(+), 8 deletions(-) diff --git a/src/hooks/useCSSVarRegister.ts b/src/hooks/useCSSVarRegister.ts index 3967dca..b14a2f6 100644 --- a/src/hooks/useCSSVarRegister.ts +++ b/src/hooks/useCSSVarRegister.ts @@ -37,15 +37,14 @@ const useCSSVarRegister = >( const { cache: { instanceId }, container, - autoClear, } = useContext(StyleContext); const { _tokenKey: tokenKey } = token; - const stylePath = [...config.path, key, scope]; + const stylePath = [...config.path, key, scope, tokenKey]; const cache = useGlobalCache>( CSS_VAR_PREFIX, - [...stylePath, tokenKey], + stylePath, () => { const originToken = fn(); const [mergedToken, cssVarsStr] = transformToken(originToken, key, { @@ -57,8 +56,8 @@ const useCSSVarRegister = >( const styleId = uniqueHash(stylePath, cssVarsStr); return [mergedToken, cssVarsStr, styleId, key]; }, - ([, , styleId], fromHMR) => { - if ((fromHMR || autoClear) && isClientSide) { + ([, , styleId]) => { + if (isClientSide) { removeCSS(styleId, { mark: ATTR_MARK }); } }, diff --git a/src/hooks/useCacheToken.tsx b/src/hooks/useCacheToken.tsx index 058019f..e4b7fae 100644 --- a/src/hooks/useCacheToken.tsx +++ b/src/hooks/useCacheToken.tsx @@ -209,7 +209,7 @@ export default function useCacheToken< recordCleanToken(themeKey); const hashId = cssVar - ? `${hashPrefix}-${hash(salt)}` + ? `${hashPrefix}-${hash(`${salt}${cssVar.prefix ?? ''}`)}` : `${hashPrefix}-${hash(tokenKey)}`; mergedDerivativeToken._hashId = hashId; // Not used diff --git a/tests/__snapshots__/css-variables.spec.tsx.snap b/tests/__snapshots__/css-variables.spec.tsx.snap index dc43828..f1a7fc8 100644 --- a/tests/__snapshots__/css-variables.spec.tsx.snap +++ b/tests/__snapshots__/css-variables.spec.tsx.snap @@ -1,3 +1,3 @@ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html -exports[`CSS Variables > support ssr 1`] = `""`; +exports[`CSS Variables > support ssr 1`] = `""`; diff --git a/tests/css-variables.spec.tsx b/tests/css-variables.spec.tsx index c0698f1..ff7d770 100644 --- a/tests/css-variables.spec.tsx +++ b/tests/css-variables.spec.tsx @@ -60,6 +60,7 @@ type DesignTokenProviderProps = { hashed?: string | boolean; cssVar?: { key: string; + prefix?: string; }; }; @@ -110,7 +111,7 @@ function useToken(): [DerivativeToken, string, string, DerivativeToken] { >(theme, [defaultDesignToken, rootDesignToken], { salt: typeof hashed === 'string' ? hashed : '', cssVar: cssVar && { - prefix: 'rc', + prefix: cssVar.prefix ?? 'rc', key: cssVar.key, unitless: { lineHeight: true, @@ -403,4 +404,50 @@ describe('CSS Variables', () => { expect(extractStyle(cache)).toMatchSnapshot(); }); + + it('css var prefix should regenerate component style', () => { + const { rerender } = render( + + + , + ); + + let styles = Array.from(document.head.querySelectorAll('style')); + expect(styles.length).toBe(3); + expect( + styles.some((style) => style.textContent?.includes('var(--app-')), + ).toBe(true); + expect( + styles.some((style) => style.textContent?.includes('var(--bank-')), + ).toBe(false); + + rerender( + + + , + ); + + styles = Array.from(document.head.querySelectorAll('style')); + expect(styles.length).toBe(4); + expect( + styles.some((style) => style.textContent?.includes('var(--app-')), + ).toBe(true); + expect( + styles.some((style) => style.textContent?.includes('var(--bank-')), + ).toBe(true); + }); });