Skip to content

Commit

Permalink
fix: cascade layer is not applied when there is no style content (#207)
Browse files Browse the repository at this point in the history
* test: add unit test

* fix: cascade layer is not applied when there is no style content
  • Loading branch information
Wxh16144 authored Dec 4, 2024
1 parent 4beb19f commit b67bf66
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/hooks/useStyleRegister.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,11 @@ export const parseStyle = (
if (!root) {
styleStr = `{${styleStr}}`;
} else if (layer) {
styleStr = `@layer ${layer.name} {${styleStr}}`;

// fixme: https://github.com/thysultan/stylis/pull/339
if (styleStr) {
styleStr = `@layer ${layer.name} {${styleStr}}`;
}

if (layer.dependencies) {
effectStyle[`@layer ${layer.name}`] = layer.dependencies
Expand Down
30 changes: 30 additions & 0 deletions tests/layer.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,34 @@ describe('layer', () => {
const styles = Array.from(document.head.querySelectorAll('style'));
expect(styles[0].innerHTML.trim()).toEqual('@layer shared,button;');
});

// TODO: try fix, If stylis is fixed, this case should not be needed here.
// https://github.com/thysultan/stylis/pull/339
// https://github.com/ant-design/ant-design/issues/51867
it('empty braces (#51867)', () => {
const theme = createTheme(() => ({}));
const Demo = () => {
useStyleRegister(
{
theme,
token: { _tokenKey: 'test' },
path: ['shared'],
layer: {
name: 'shared',
},
},
() => [],
);
return null;
};

render(
<StyleProvider layer cache={createCache()}>
<Demo />
</StyleProvider>,
);

const styles = Array.from(document.head.querySelectorAll('style'));
expect(styles[0].innerHTML.trim()).toEqual('');
});
});

0 comments on commit b67bf66

Please # to comment.