Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: root selector with single & #193

Merged
merged 3 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"classnames": "^2.3.1",
"csstype": "^3.1.3",
"rc-util": "^5.35.0",
"stylis": "^4.0.13"
"stylis": "^4.3.3"
},
"devDependencies": {
"@ctrl/tinycolor": "^3.4.0",
Expand Down
3 changes: 3 additions & 0 deletions src/hooks/useStyleRegister.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,9 @@ export const parseStyle = (
if (mergedKey.startsWith('@')) {
// 略过媒体查询,交给子节点继续插入 hashId
subInjectHash = true;
} else if (mergedKey === '&') {
// 抹掉 root selector 上的单个 &
mergedKey = injectSelectorHash('', hashId, hashPriority);
} else {
// 注入 hashId
mergedKey = injectSelectorHash(key, hashId, hashPriority);
Expand Down
34 changes: 34 additions & 0 deletions tests/index.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -777,4 +777,38 @@ describe('csssinjs', () => {
});
});
});

it('hash & nest style', () => {
const genHashStyle = (): CSSInterpolation => ({
'&': {
a: {
color: 'red',
},
},
});

const Holder = () => {
const [token, hashId] = useCacheToken<DerivativeToken>(theme, [], {
salt: 'test',
});

useStyleRegister({ theme, token, hashId, path: ['holder'] }, () => [
genHashStyle(),
]);

return <div className={classNames('box', hashId)} />;
};

const { unmount } = render(<Holder />);

const styles = Array.from(document.head.querySelectorAll('style'));
expect(styles).toHaveLength(1);

const style = styles[0];
expect(style.innerHTML).toContain(
':where(.css-dev-only-do-not-override-1ldpa3u) a{color:red;}',
);

unmount();
});
});
Loading