Skip to content

Commit

Permalink
Merge pull request #21 from ant-design/feat-layer
Browse files Browse the repository at this point in the history
feat: pass layer
  • Loading branch information
zombieJ authored Sep 14, 2024
2 parents 199ce2d + fb070d6 commit b665980
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/util/genStyleUtils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import type { UseCSP } from '../hooks/useCSP';
import type { UsePrefix } from '../hooks/usePrefix';
import type { UseToken } from '../hooks/useToken';

type LayerConfig = Parameters<typeof useStyleRegister>[0]['layer'];

export interface StyleInfo {
hashId: string;
prefixCls: string;
Expand Down Expand Up @@ -115,6 +117,7 @@ function genStyleUtils<
resetFont?: boolean,
) => CSSObject;
getCompUnitless?: GetCompUnitless<CompTokenMap, AliasToken>;
layer?: LayerConfig;
}) {
// Dependency inversion for preparing basic config.
const {
Expand Down Expand Up @@ -309,6 +312,10 @@ function genStyleUtils<
const [component] = cells;
const concatComponent = cells.join('-');

const mergedLayer = config.layer || {
name: 'antd',
};

// Return new style hook
return (prefixCls: string, rootCls: string = prefixCls): UseComponentStyleResult => {
const { theme, realToken, hashId, token, cssVar } = useToken();
Expand Down Expand Up @@ -342,9 +349,7 @@ function genStyleUtils<
hashId,
nonce: () => csp.nonce!,
clientOnly: options.clientOnly,
layer: {
name: 'antd',
},
layer: mergedLayer,

// antd is always at top of styles
order: options.order || -999,
Expand Down
27 changes: 27 additions & 0 deletions tests/genStyleUtils.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { render, renderHook } from '@testing-library/react';

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

interface TestCompTokenMap {
TestComponent: object;
Expand All @@ -23,6 +24,10 @@ describe('genStyleUtils', () => {
}),
useCSP: jest.fn().mockReturnValue({ nonce: 'nonce' }),
getResetStyles: jest.fn().mockReturnValue([]),
layer: {
name: 'test',
dependencies: ['parent'],
},
};

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

beforeEach(() => {
// Clear head style
const head = document.head;
head.innerHTML = '';
});

describe('genStyleHooks', () => {
it('should generate style hooks', () => {
const component = 'TestComponent';
Expand Down Expand Up @@ -89,4 +100,20 @@ describe('genStyleUtils', () => {
expect(getByTestId('test-root')).toHaveTextContent('test-prefix');
});
});

it('layer', () => {
const StyledComponent = genSubStyleComponent(
'TestComponent',
() => ({}),
() => ({}),
);

render(
<StyleProvider cache={createCache()} layer>
<StyledComponent prefixCls="test" />
</StyleProvider>,
);

expect(document.head.innerHTML).toContain('@layer parent,test;');
});
});

0 comments on commit b665980

Please sign in to comment.