Skip to content

Commit b67bf66

Browse files
authored
fix: cascade layer is not applied when there is no style content (#207)
* test: add unit test * fix: cascade layer is not applied when there is no style content
1 parent 4beb19f commit b67bf66

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/hooks/useStyleRegister.tsx

+5-1
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,11 @@ export const parseStyle = (
333333
if (!root) {
334334
styleStr = `{${styleStr}}`;
335335
} else if (layer) {
336-
styleStr = `@layer ${layer.name} {${styleStr}}`;
336+
337+
// fixme: https://github.com/thysultan/stylis/pull/339
338+
if (styleStr) {
339+
styleStr = `@layer ${layer.name} {${styleStr}}`;
340+
}
337341

338342
if (layer.dependencies) {
339343
effectStyle[`@layer ${layer.name}`] = layer.dependencies

tests/layer.spec.tsx

+30
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,34 @@ describe('layer', () => {
6666
const styles = Array.from(document.head.querySelectorAll('style'));
6767
expect(styles[0].innerHTML.trim()).toEqual('@layer shared,button;');
6868
});
69+
70+
// TODO: try fix, If stylis is fixed, this case should not be needed here.
71+
// https://github.com/thysultan/stylis/pull/339
72+
// https://github.com/ant-design/ant-design/issues/51867
73+
it('empty braces (#51867)', () => {
74+
const theme = createTheme(() => ({}));
75+
const Demo = () => {
76+
useStyleRegister(
77+
{
78+
theme,
79+
token: { _tokenKey: 'test' },
80+
path: ['shared'],
81+
layer: {
82+
name: 'shared',
83+
},
84+
},
85+
() => [],
86+
);
87+
return null;
88+
};
89+
90+
render(
91+
<StyleProvider layer cache={createCache()}>
92+
<Demo />
93+
</StyleProvider>,
94+
);
95+
96+
const styles = Array.from(document.head.querySelectorAll('style'));
97+
expect(styles[0].innerHTML.trim()).toEqual('');
98+
});
6999
});

0 commit comments

Comments
 (0)