Skip to content

Commit c06da68

Browse files
MadCcczombieJkimteayon
authored
chore: master merge next (#236)
* feat: remove compatibility under React v18 (#210) * 2.0.0-alpha.0 * 2.0.0-alpha.2 * feat: rm style tokenKey (#213) * feat: rm style tokenKey * test: fix test cases * chore: fix tsc * feat: remove wrapSSR * docs: update examples * feat: css var support hash (#214) * feat: css var support hash * chore: remove useless comment * chore: fix tsc * test: add test case * chore: rm only * chore: add useMemo back (#215) * docs: add first render demo (#216) * 2.0.0-alpha.3 * fix: should not clean token when token change (#217) * 2.0.0-alpha.4 * perf: cache effect (#218) * chore: bump father-plugin (#219) * chore: bump version to 2.0.0-alpha.5 * fix: Keep render order in ssr (#225) * chore: record updateTime * test: add test case * chore: bump father version * chore: bump version to 2.0.0-alpha.6 * feat: extractStyle support once (#228) * feat: support autoPrefix (#230) * chore: bump version to 2.0.0-alpha.7 * fix: not use func of autoPrefix * chore: fix lint * chore: bump version to 2.0.0-alpha.8 * fix: fix the issue of failed verification when content is cssvar (#235) * fix: content * fix: content * feat: 更新文档 * feat: 更新文档 * feat: 更新测试用例 * feat: 更新测试用例 * chore: fix lint * chore: bump version to 2.0.0-alpha.9 * test: update test case --------- Co-authored-by: 二货爱吃白萝卜 <smith3816@gmail.com> Co-authored-by: Mickey <951203214@qq.com>
2 parents e7fd8a3 + a4da2e9 commit c06da68

38 files changed

Lines changed: 1012 additions & 1028 deletions
File renamed without changes.

docs/demo/auto-clear.md

Lines changed: 0 additions & 8 deletions
This file was deleted.

docs/demo/first-render.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
title: First Render
3+
nav:
4+
title: Demo
5+
path: /demo
6+
---
7+
8+
<code src="../examples/first-render.tsx"></code>

docs/examples/auto-clear.tsx

Lines changed: 0 additions & 37 deletions
This file was deleted.

docs/examples/autoPrefix.tsx

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import {
2+
autoPrefixTransformer,
3+
createTheme,
4+
StyleProvider,
5+
useStyleRegister,
6+
} from '@ant-design/cssinjs';
7+
import React from 'react';
8+
9+
const Demo = () => {
10+
useStyleRegister(
11+
{ theme: createTheme(() => ({})), token: {}, path: ['.auto-prefix-box'] },
12+
() => ({
13+
'.auto-prefix-box': {
14+
width: '200px',
15+
height: '200px',
16+
backgroundColor: '#f0f0f0',
17+
border: '1px solid #d9d9d9',
18+
borderRadius: '8px',
19+
// Properties that will get vendor prefixes
20+
transform: 'translateX(50px) scale(1.1)',
21+
transition: 'all 0.3s ease',
22+
userSelect: 'none',
23+
display: 'flex',
24+
flexDirection: 'column',
25+
alignItems: 'center',
26+
justifyContent: 'center',
27+
backdropFilter: 'blur(10px)',
28+
'&:hover': {
29+
transform: 'translateX(50px) scale(1.2)',
30+
backgroundColor: '#e6f7ff',
31+
},
32+
},
33+
}),
34+
);
35+
36+
return (
37+
<div className="auto-prefix-box">
38+
<h3>Auto Prefix Demo</h3>
39+
<p>Hover to see effect</p>
40+
<p style={{ fontSize: '12px', color: '#666' }}>
41+
Check DevTools to see vendor prefixes in CSS
42+
</p>
43+
</div>
44+
);
45+
};
46+
47+
export default () => (
48+
<StyleProvider transformers={[autoPrefixTransformer]}>
49+
<Demo />
50+
</StyleProvider>
51+
);

docs/examples/components/Button.tsx

Lines changed: 54 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,32 @@
1-
import type { CSSInterpolation, CSSObject } from '@ant-design/cssinjs';
2-
import { unit, useStyleRegister } from '@ant-design/cssinjs';
1+
import type {
2+
CSSInterpolation,
3+
CSSObject} from '@ant-design/cssinjs';
4+
import {
5+
unit,
6+
useCSSVarRegister,
7+
useStyleRegister,
8+
} from '@ant-design/cssinjs';
39
import classNames from 'classnames';
410
import React from 'react';
511
import type { DerivativeToken } from './theme';
612
import { useToken } from './theme';
713

14+
interface ButtonToken extends DerivativeToken {
15+
buttonPadding: string;
16+
buttonBorderBottomWidth: string;
17+
}
18+
819
// 通用框架
920
const genSharedButtonStyle = (
1021
prefixCls: string,
11-
token: DerivativeToken,
22+
token: ButtonToken,
1223
): CSSInterpolation => ({
1324
[`.${prefixCls}`]: {
1425
borderColor: token.borderColor,
1526
borderWidth: token.borderWidth,
1627
borderRadius: token.borderRadius,
1728
lineHeight: token.lineHeight,
29+
padding: token.buttonPadding,
1830

1931
cursor: 'pointer',
2032

@@ -33,7 +45,7 @@ const genSharedButtonStyle = (
3345
// 实心底色样式
3446
const genSolidButtonStyle = (
3547
prefixCls: string,
36-
token: DerivativeToken,
48+
token: ButtonToken,
3749
postFn: () => CSSObject,
3850
): CSSInterpolation => [
3951
genSharedButtonStyle(prefixCls, token),
@@ -47,11 +59,12 @@ const genSolidButtonStyle = (
4759
// 默认样式
4860
const genDefaultButtonStyle = (
4961
prefixCls: string,
50-
token: DerivativeToken,
62+
token: ButtonToken,
5163
): CSSInterpolation =>
5264
genSolidButtonStyle(prefixCls, token, () => ({
5365
backgroundColor: token.componentBackgroundColor,
5466
color: token.textColor,
67+
borderWidth: token.buttonBorderBottomWidth,
5568

5669
'&:hover': {
5770
borderColor: token.primaryColor,
@@ -62,7 +75,7 @@ const genDefaultButtonStyle = (
6275
// 主色样式
6376
const genPrimaryButtonStyle = (
6477
prefixCls: string,
65-
token: DerivativeToken,
78+
token: ButtonToken,
6679
): CSSInterpolation =>
6780
genSolidButtonStyle(prefixCls, token, () => ({
6881
backgroundColor: token.primaryColor,
@@ -77,7 +90,7 @@ const genPrimaryButtonStyle = (
7790
// 透明按钮
7891
const genGhostButtonStyle = (
7992
prefixCls: string,
80-
token: DerivativeToken,
93+
token: ButtonToken,
8194
): CSSInterpolation => [
8295
genSharedButtonStyle(prefixCls, token),
8396
{
@@ -103,23 +116,46 @@ const Button = ({ className, type, ...restProps }: ButtonProps) => {
103116
const prefixCls = 'ant-btn';
104117

105118
// 【自定义】制造样式
106-
const [theme, token, hashId, cssVarKey] = useToken();
119+
const [theme, token, hashId, cssVarKey, realToken] = useToken();
107120

108121
// default 添加默认样式选择器后可以省很多冲突解决问题
109122
const defaultCls = `${prefixCls}-default`;
110123
const primaryCls = `${prefixCls}-primary`;
111124
const ghostCls = `${prefixCls}-ghost`;
112125

113-
// 全局注册,内部会做缓存优化
114-
const wrapSSR = useStyleRegister(
115-
{ theme, token, hashId, path: [prefixCls] },
116-
() => [
117-
genDefaultButtonStyle(defaultCls, token),
118-
genPrimaryButtonStyle(primaryCls, token),
119-
genGhostButtonStyle(ghostCls, token),
120-
],
126+
const [cssVarToken] = useCSSVarRegister(
127+
{
128+
path: [prefixCls],
129+
key: cssVarKey,
130+
token: realToken,
131+
prefix: prefixCls,
132+
unitless: {
133+
lineHeight: true,
134+
},
135+
ignore: {
136+
lineHeightBase: true,
137+
},
138+
scope: prefixCls,
139+
hashId,
140+
},
141+
() => ({
142+
buttonPadding: '4px 8px',
143+
buttonBorderBottomWidth: `calc(${token.borderWidth} * 2)`,
144+
}),
121145
);
122146

147+
const mergedToken: any = {
148+
...token,
149+
...cssVarToken,
150+
};
151+
152+
// 全局注册,内部会做缓存优化
153+
useStyleRegister({ theme, token, hashId, path: [prefixCls] }, () => [
154+
genDefaultButtonStyle(defaultCls, mergedToken),
155+
genPrimaryButtonStyle(primaryCls, mergedToken),
156+
genGhostButtonStyle(ghostCls, mergedToken),
157+
]);
158+
123159
const typeCls =
124160
(
125161
{
@@ -128,11 +164,11 @@ const Button = ({ className, type, ...restProps }: ButtonProps) => {
128164
} as any
129165
)[type as any] || defaultCls;
130166

131-
return wrapSSR(
167+
return (
132168
<button
133169
className={classNames(prefixCls, typeCls, hashId, className, cssVarKey)}
134170
{...restProps}
135-
/>,
171+
/>
136172
);
137173
};
138174

docs/examples/components/Spin.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ const Spin = ({ className, ...restProps }: SpinProps) => {
4141
const [theme, token, hashId] = useToken();
4242

4343
// 全局注册,内部会做缓存优化
44-
const wrapSSR = useStyleRegister(
44+
useStyleRegister(
4545
{ theme, token, hashId, path: [prefixCls] },
4646
() => [genSpinStyle(prefixCls, token)],
4747
);
4848

49-
return wrapSSR(
50-
<div className={classNames(prefixCls, hashId, className)} {...restProps} />,
49+
return (
50+
<div className={classNames(prefixCls, hashId, className)} {...restProps} />
5151
);
5252
};
5353

docs/examples/components/theme.tsx

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { CSSObject, Theme } from '@ant-design/cssinjs';
22
import { createTheme, useCacheToken } from '@ant-design/cssinjs';
33
import { TinyColor } from '@ctrl/tinycolor';
4-
import React from 'react';
4+
import React, { PropsWithChildren } from 'react';
55

66
export type GetStyle = (prefixCls: string, token: DerivativeToken) => CSSObject;
77

@@ -61,32 +61,68 @@ export const DesignTokenContext = React.createContext<{
6161
token: defaultDesignToken,
6262
});
6363

64+
export const DesignTokenProvider: React.FC<
65+
PropsWithChildren<{
66+
value?: {
67+
token?: Partial<DesignToken>;
68+
hashed?: string | boolean;
69+
cssVar?: {
70+
key?: string;
71+
prefix?: string;
72+
};
73+
};
74+
}>
75+
> = ({ children, value }) => {
76+
const { token, hashed, cssVar } = value || {};
77+
const themeKey = React.useId();
78+
const cssVarKey = `css-var-${themeKey.replace(/:/g, '')}`;
79+
return (
80+
<DesignTokenContext.Provider
81+
value={{
82+
token,
83+
hashed,
84+
cssVar: {
85+
...cssVar,
86+
key: cssVar?.key || cssVarKey,
87+
},
88+
}}
89+
>
90+
{children}
91+
</DesignTokenContext.Provider>
92+
);
93+
};
94+
6495
export function useToken(): [
6596
Theme<any, any>,
6697
DerivativeToken,
6798
string,
68-
string | undefined,
99+
string,
100+
DerivativeToken,
69101
] {
70102
const {
71103
token: rootDesignToken = {},
72104
hashed,
73-
cssVar,
105+
cssVar: ctxCssVar,
74106
} = React.useContext(DesignTokenContext);
75107
const theme = React.useContext(ThemeContext);
76108

77-
const [token, hashId] = useCacheToken<DerivativeToken, DesignToken>(
78-
theme,
79-
[defaultDesignToken, rootDesignToken],
80-
{
81-
salt: typeof hashed === 'string' ? hashed : '',
82-
cssVar: cssVar && {
83-
prefix: 'rc',
84-
key: cssVar.key,
85-
unitless: {
86-
lineHeight: true,
87-
},
109+
const cssVar = {
110+
key: ctxCssVar?.key || 'css-var-root',
111+
};
112+
113+
const [token, hashId, actualToken] = useCacheToken<
114+
DerivativeToken,
115+
DesignToken
116+
>(theme, [defaultDesignToken, rootDesignToken], {
117+
salt: typeof hashed === 'string' ? hashed : '',
118+
cssVar: {
119+
prefix: 'rc',
120+
key: cssVar?.key || 'css-var-root',
121+
unitless: {
122+
lineHeight: true,
88123
},
124+
hashed: !!hashed,
89125
},
90-
);
91-
return [theme, token, hashed ? hashId : '', cssVar?.key];
126+
});
127+
return [theme, token, hashed ? hashId : '', cssVar.key, actualToken];
92128
}

0 commit comments

Comments
 (0)