Skip to content

Commit 35f041c

Browse files
Wxh16144rikkayoru
andauthored
fix: removeCSS not work with container (#211)
* fix: `removeCSS` not work with `container` (cherry picked from commit 2fb097e) * test: add unit test * fixup! fix: `removeCSS` not work with `container` * ⚡️improve perf * Revert "fixup! fix: `removeCSS` not work with `container`" This reverts commit bb54111. * fixup! fix: `removeCSS` not work with `container` --------- Co-authored-by: Derek <[email protected]>
1 parent bed0452 commit 35f041c

File tree

4 files changed

+48
-11
lines changed

4 files changed

+48
-11
lines changed

src/hooks/useCSSVarRegister.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ const useCSSVarRegister = <V, T extends Record<string, V>>(
5858
},
5959
([, , styleId]) => {
6060
if (isClientSide) {
61-
removeCSS(styleId, { mark: ATTR_MARK });
61+
removeCSS(styleId, { mark: ATTR_MARK, attachTo: container });
6262
}
6363
},
6464
([, cssVarsStr, styleId]) => {

src/hooks/useCacheToken.tsx

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,13 @@ const TOKEN_THRESHOLD = 0;
9292
function cleanTokenStyle(tokenKey: string, instanceId: string) {
9393
tokenKeys.set(tokenKey, (tokenKeys.get(tokenKey) || 0) - 1);
9494

95-
const tokenKeyList = Array.from(tokenKeys.keys());
96-
const cleanableKeyList = tokenKeyList.filter((key) => {
97-
const count = tokenKeys.get(key) || 0;
98-
99-
return count <= 0;
100-
});
95+
const cleanableKeyList = new Set<string>();
96+
tokenKeys.forEach((value, key) => {
97+
if (value <= 0) cleanableKeyList.add(key);
98+
})
10199

102100
// Should keep tokens under threshold for not to insert style too often
103-
if (tokenKeyList.length - cleanableKeyList.length > TOKEN_THRESHOLD) {
101+
if (tokenKeys.size - cleanableKeyList.size > TOKEN_THRESHOLD) {
104102
cleanableKeyList.forEach((key) => {
105103
removeStyleTags(key, instanceId);
106104
tokenKeys.delete(key);

src/hooks/useStyleRegister.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,6 @@ export const parseStyle = (
333333
if (!root) {
334334
styleStr = `{${styleStr}}`;
335335
} else if (layer) {
336-
337336
// fixme: https://github.com/thysultan/stylis/pull/339
338337
if (styleStr) {
339338
styleStr = `@layer ${layer.name} {${styleStr}}`;
@@ -462,7 +461,7 @@ export default function useStyleRegister(
462461
// Remove cache if no need
463462
([, , styleId], fromHMR) => {
464463
if ((fromHMR || autoClear) && isClientSide) {
465-
removeCSS(styleId, { mark: ATTR_MARK });
464+
removeCSS(styleId, { mark: ATTR_MARK, attachTo: container });
466465
}
467466
},
468467

tests/index.spec.tsx

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { render } from '@testing-library/react';
22
import classNames from 'classnames';
3+
import type { ReactElement, ReactNode } from 'react';
34
import * as React from 'react';
4-
import { ReactElement, ReactNode, StrictMode } from 'react';
5+
import { StrictMode } from 'react';
56
import { describe, expect } from 'vitest';
67
import type { CSSInterpolation, DerivativeFunc } from '../src';
78
import {
@@ -426,6 +427,45 @@ describe('csssinjs', () => {
426427
expect(container.querySelectorAll('style')).toHaveLength(1);
427428
});
428429

430+
// https://github.com/ant-design/cssinjs/issues/189
431+
it('should cleanup style when unmount', () => {
432+
const container = document.createElement('div');
433+
434+
const CssVarBox = () => {
435+
const [token] = useCacheToken<DerivativeToken>(
436+
theme,
437+
[{ primaryColor: 'red' }],
438+
{
439+
salt: 'test',
440+
},
441+
);
442+
443+
useCSSVarRegister(
444+
{
445+
key: 'color-2',
446+
path: ['cssinjs-cleanup-style-when-unmount'],
447+
token,
448+
},
449+
() => ({
450+
token: token.primaryColor,
451+
}),
452+
);
453+
454+
return null;
455+
};
456+
457+
const { unmount } = render(
458+
<StyleProvider cache={createCache()} container={container} autoClear>
459+
<Box />
460+
<CssVarBox />
461+
</StyleProvider>,
462+
);
463+
464+
expect(container.querySelectorAll('style')).toHaveLength(2);
465+
unmount();
466+
expect(container.querySelectorAll('style')).toHaveLength(0);
467+
});
468+
429469
describe('nonce', () => {
430470
function test(name: string, nonce: string | (() => string)) {
431471
it(name, () => {

0 commit comments

Comments
 (0)