generated from react-component/trigger
-
Notifications
You must be signed in to change notification settings - Fork 10
feat: add extraCssVarPrefixCls option to inject CSS vars for additional prefix classes #36
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5180ba3
feat: add extraCssVarPrefixCls option to inject CSS vars for addition…
zombieJ afd8e6c
chore: revert
zombieJ 485b231
fix: fix extraCssVarPrefixCls test to properly verify CSS variable in…
zombieJ b85034f
refactor: optimize extraCssVarPrefixCls to use comma-separated selectors
zombieJ 552fbe1
revert: inline tokenGenerator back to original structure
zombieJ 80d1cbd
chore: relax @ant-design/cssinjs version to caret range
zombieJ 2204e7c
Update src/util/genStyleUtils.ts
zombieJ File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| # Action Items for @ant-design/cssinjs | ||
|
|
||
| ## 优化 CSS 变量注入以减少冗余 | ||
|
|
||
| ### 当前问题 | ||
| 在 `@rc-component/util` 的 `genStyleUtils` 中新增了 `extraCssVarPrefixCls` 选项,允许为多个 prefixCls 注册 CSS 变量。当前实现需要为每个 scope 调用一次 `useCSSVarRegister`,导致: | ||
|
|
||
| 1. **代码冗余**:多个 scope 会生成相同值的 CSS 变量 | ||
| 2. **性能开销**:大量 DOM 操作会带来性能问题 | ||
|
|
||
| ### 当前生成的 CSS | ||
| ```css | ||
| .xxx-hashId { --btn-color: #1890ff; } | ||
| .xxx-hashId.ant-btn-compact { --btn-color: #1890ff; } | ||
| .xxx-hashId.ant-btn-large { --btn-color: #1890ff; } | ||
| ``` | ||
|
|
||
| ### 建议的优化方案 | ||
| 支持在 `useCSSVarRegister` 的 config 中传入多个 scope,生成逗号分隔的选择器: | ||
|
|
||
| ```css | ||
| .xxx-hashId, .xxx-hashId.ant-btn-compact, .xxx-hashId.ant-btn-large { | ||
| --btn-color: #1890ff; | ||
| } | ||
| ``` | ||
|
|
||
| ### API 变更建议 | ||
| ```typescript | ||
| // 当前 API | ||
| useCSSVarRegister( | ||
| { ..., scope: 'ant-btn-compact' }, // 单个 scope | ||
| tokenGenerator | ||
| ); | ||
|
|
||
| // 建议的 API(兼容现有) | ||
| useCSSVarRegister( | ||
| { ..., scope: 'ant-btn-compact' }, // 仍然支持单个 scope | ||
| tokenGenerator | ||
| ); | ||
| useCSSVarRegister( | ||
| { ..., scopes: ['ant-btn', 'ant-btn-compact', 'ant-btn-large'] }, // 新增支持数组的 scopes | ||
| tokenGenerator | ||
| ); | ||
| ``` | ||
|
|
||
| ### 需要修改的文件 | ||
| - `lib/hooks/useCSSVarRegister.js` - 接受 `scopes` 数组并修改 `serializeCSSVar` 生成逻辑 | ||
| - `lib/util/css-variables.js` - 修改 `serializeCSSVar` 支持多个 scope 生成逗号分隔选择器 | ||
| - 对应的 TypeScript 类型定义文件 | ||
|
|
||
| ### 注意事项 | ||
| 1. 保持向后兼容,`scope` 仍然可用 | ||
| 2. 这属于性能优化,不是破坏性变更 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| import React from 'react'; | ||
| import { render } from '@testing-library/react'; | ||
| import { createCache, StyleProvider } from '@ant-design/cssinjs'; | ||
| import { genStyleUtils } from '../src'; | ||
|
|
||
| interface TestTokenMap { | ||
| TestComponent: { | ||
| colorPrimary?: string; | ||
| fontSize?: number; | ||
| }; | ||
| } | ||
|
|
||
| describe('extraCssVarPrefixCls', () => { | ||
| const mockConfig = { | ||
| usePrefix: jest.fn().mockReturnValue({ | ||
| rootPrefixCls: 'ant', | ||
| iconPrefixCls: 'anticon', | ||
| }), | ||
| useToken: jest.fn().mockReturnValue({ | ||
| theme: { | ||
| id: 'test', | ||
| } as any, | ||
| realToken: { | ||
| colorPrimary: '#1890ff', | ||
| fontSize: 14, | ||
| TestComponent: { | ||
| colorPrimary: '#ff0000', | ||
| fontSize: 16, | ||
| }, | ||
| }, | ||
| hashId: 'css-dev-only-do-not-override-abc123', | ||
| token: { | ||
| colorPrimary: '#1890ff', | ||
| fontSize: 14, | ||
| TestComponent: { | ||
| colorPrimary: '#ff0000', | ||
| fontSize: 16, | ||
| }, | ||
| }, | ||
| cssVar: { | ||
| prefix: 'ant', | ||
| key: 'test', | ||
| }, | ||
| }), | ||
| useCSP: jest.fn().mockReturnValue({ nonce: 'nonce' }), | ||
| getResetStyles: jest.fn().mockReturnValue([]), | ||
| layer: { | ||
| name: 'test', | ||
| dependencies: ['parent'], | ||
| }, | ||
| }; | ||
|
|
||
| const { genStyleHooks } = genStyleUtils<TestTokenMap, any, any>(mockConfig); | ||
|
|
||
| beforeEach(() => { | ||
| document.head.innerHTML = ''; | ||
| }); | ||
|
|
||
| it('should inject CSS vars for extraCssVarPrefixCls', () => { | ||
| const hooks = genStyleHooks( | ||
| 'TestComponent', | ||
| (token) => ({ | ||
| [`${token.componentCls}`]: { | ||
| color: token.colorPrimary, | ||
| fontSize: token.fontSize, | ||
| }, | ||
| }), | ||
| () => ({ | ||
| colorPrimary: '#ff0000', | ||
| fontSize: 16, | ||
| }), | ||
| { | ||
| extraCssVarPrefixCls: ['custom-a', 'custom-b'], | ||
| }, | ||
| ); | ||
|
|
||
| const TestComponent = () => { | ||
| const [hashId, cssVarCls] = hooks('test-prefix'); | ||
| const className = [hashId, cssVarCls].filter(Boolean).join(' '); | ||
| return <div className={className}>{hashId}</div>; | ||
| }; | ||
|
|
||
| render( | ||
| <StyleProvider cache={createCache()}> | ||
| <TestComponent /> | ||
| </StyleProvider>, | ||
| ); | ||
|
|
||
| const totalStyle = Array.from(document.querySelectorAll('style')) | ||
| .map((el) => el.textContent) | ||
| .join('\n'); | ||
|
|
||
| expect(totalStyle).toContain('.custom-a'); | ||
| expect(totalStyle).toContain('.custom-b'); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.