Skip to content

Commit 77b9f34

Browse files
authored
feat: support custom hashPriority (#7)
* feat: support custom hashPriority * feat: code optimize * feat: code optimize * feat: code optimize
1 parent 610aae0 commit 77b9f34

File tree

5 files changed

+44
-11
lines changed

5 files changed

+44
-11
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
"typescript": "^4.0.0"
5656
},
5757
"dependencies": {
58-
"@ant-design/cssinjs": "^1.6.1",
58+
"@ant-design/cssinjs": "^1.8.1",
5959
"@babel/runtime": "^7.18.3",
6060
"@rc-component/portal": "^1.1.0",
6161
"antd": "^5.3.0",

src/index.tsx

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
} from '@ant-design/cssinjs';
77
import * as antd from 'antd';
88
import { renderToString } from 'react-dom/server';
9-
import type { ExtractStyleParams } from './interface';
9+
import type { CustomRender } from './interface';
1010
const blackList: string[] = [
1111
'ConfigProvider',
1212
'Drawer',
@@ -18,8 +18,6 @@ const blackList: string[] = [
1818
'Tour',
1919
];
2020

21-
const styleTagReg = /<style[^>]*>([\s\S]*?)<\/style>/g;
22-
2321
const defaultNode = () => (
2422
<>
2523
{Object.keys(antd)
@@ -44,7 +42,7 @@ const defaultNode = () => (
4442
</>
4543
);
4644

47-
export function extractStyle(customTheme?: ExtractStyleParams): string {
45+
export function extractStyle(customTheme?: CustomRender): string {
4846
const cache = createCache();
4947
renderToString(
5048
<StyleProvider cache={cache}>
@@ -53,8 +51,7 @@ export function extractStyle(customTheme?: ExtractStyleParams): string {
5351
);
5452

5553
// Grab style from cache
56-
const styleText = extStyle(cache);
57-
54+
const styleText = extStyle(cache, true);
5855

59-
return styleText.replace(styleTagReg, '$1');
56+
return styleText;
6057
}

src/interface.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export type ExtractStyleParams = (node: JSX.Element) => JSX.Element;
1+
export type CustomRender = (node: JSX.Element) => JSX.Element;

tests/__snapshots__/index.test.tsx.snap

Lines changed: 4 additions & 2 deletions
Large diffs are not rendered by default.

tests/index.test.tsx

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { StyleProvider } from '@ant-design/cssinjs';
12
import { extractStyle } from '../src/index';
23
import { ConfigProvider } from 'antd';
34

@@ -23,4 +24,37 @@ describe('Static-Style-Extract', () => {
2324
expect(cssText).toContain(testGreenColor);
2425
expect(cssText).toMatchSnapshot();
2526
});
27+
it('with custom hashPriority', () => {
28+
const cssText = extractStyle(
29+
(node) => (
30+
<StyleProvider hashPriority='high'>
31+
<ConfigProvider
32+
theme={{
33+
token: {
34+
colorPrimary: testGreenColor,
35+
},
36+
}}
37+
>
38+
{node}
39+
</ConfigProvider>
40+
</StyleProvider>
41+
)
42+
);
43+
expect(cssText).toContain(testGreenColor);
44+
expect(cssText).not.toContain(':where');
45+
expect(cssText).toMatchSnapshot();
46+
47+
const cssText2 = extractStyle((node) => (
48+
<ConfigProvider
49+
theme={{
50+
token: {
51+
colorPrimary: testGreenColor,
52+
},
53+
}}
54+
>
55+
{node}
56+
</ConfigProvider>
57+
));
58+
expect(cssText2).toContain(':where');
59+
});
2660
});

0 commit comments

Comments
 (0)