Skip to content

Commit b665980

Browse files
authored
Merge pull request #21 from ant-design/feat-layer
feat: pass layer
2 parents 199ce2d + fb070d6 commit b665980

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

src/util/genStyleUtils.tsx

+8-3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ import type { UseCSP } from '../hooks/useCSP';
2727
import type { UsePrefix } from '../hooks/usePrefix';
2828
import type { UseToken } from '../hooks/useToken';
2929

30+
type LayerConfig = Parameters<typeof useStyleRegister>[0]['layer'];
31+
3032
export interface StyleInfo {
3133
hashId: string;
3234
prefixCls: string;
@@ -115,6 +117,7 @@ function genStyleUtils<
115117
resetFont?: boolean,
116118
) => CSSObject;
117119
getCompUnitless?: GetCompUnitless<CompTokenMap, AliasToken>;
120+
layer?: LayerConfig;
118121
}) {
119122
// Dependency inversion for preparing basic config.
120123
const {
@@ -309,6 +312,10 @@ function genStyleUtils<
309312
const [component] = cells;
310313
const concatComponent = cells.join('-');
311314

315+
const mergedLayer = config.layer || {
316+
name: 'antd',
317+
};
318+
312319
// Return new style hook
313320
return (prefixCls: string, rootCls: string = prefixCls): UseComponentStyleResult => {
314321
const { theme, realToken, hashId, token, cssVar } = useToken();
@@ -342,9 +349,7 @@ function genStyleUtils<
342349
hashId,
343350
nonce: () => csp.nonce!,
344351
clientOnly: options.clientOnly,
345-
layer: {
346-
name: 'antd',
347-
},
352+
layer: mergedLayer,
348353

349354
// antd is always at top of styles
350355
order: options.order || -999,

tests/genStyleUtils.test.tsx

+27
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { render, renderHook } from '@testing-library/react';
33

44
import { genStyleUtils } from '../src';
55
import type { CSSVarRegisterProps, SubStyleComponentProps } from '../src/util/genStyleUtils';
6+
import { createCache, StyleProvider } from '@ant-design/cssinjs';
67

78
interface TestCompTokenMap {
89
TestComponent: object;
@@ -23,6 +24,10 @@ describe('genStyleUtils', () => {
2324
}),
2425
useCSP: jest.fn().mockReturnValue({ nonce: 'nonce' }),
2526
getResetStyles: jest.fn().mockReturnValue([]),
27+
layer: {
28+
name: 'test',
29+
dependencies: ['parent'],
30+
},
2631
};
2732

2833
const { genStyleHooks, genSubStyleComponent, genComponentStyleHook } = genStyleUtils<
@@ -31,6 +36,12 @@ describe('genStyleUtils', () => {
3136
object
3237
>(mockConfig);
3338

39+
beforeEach(() => {
40+
// Clear head style
41+
const head = document.head;
42+
head.innerHTML = '';
43+
});
44+
3445
describe('genStyleHooks', () => {
3546
it('should generate style hooks', () => {
3647
const component = 'TestComponent';
@@ -89,4 +100,20 @@ describe('genStyleUtils', () => {
89100
expect(getByTestId('test-root')).toHaveTextContent('test-prefix');
90101
});
91102
});
103+
104+
it('layer', () => {
105+
const StyledComponent = genSubStyleComponent(
106+
'TestComponent',
107+
() => ({}),
108+
() => ({}),
109+
);
110+
111+
render(
112+
<StyleProvider cache={createCache()} layer>
113+
<StyledComponent prefixCls="test" />
114+
</StyleProvider>,
115+
);
116+
117+
expect(document.head.innerHTML).toContain('@layer parent,test;');
118+
});
92119
});

0 commit comments

Comments
 (0)