Skip to content

Commit b53a804

Browse files
committed
✨ feat: 增加 createStyles 的使用方法
1 parent 4ea5e43 commit b53a804

File tree

4 files changed

+143
-1
lines changed

4 files changed

+143
-1
lines changed

src/createStyles.ts

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// =========== emotion css 方案 =========== //
2+
// 写成 useStyles 的方式
3+
// 适用场景:单纯用于部分样式的,没必要抽成组件的话,使用这种写法
4+
// 可支持将样式部分独立到 style.ts 文件中
5+
import { useMemo } from 'react';
6+
7+
import type { AntdStylish } from './stylish';
8+
import { useInternalStylish } from './stylish';
9+
import { AntdToken } from './types';
10+
import { useToken } from './useToken';
11+
12+
export interface CreateStylesTheme {
13+
token: AntdToken;
14+
stylish: AntdStylish;
15+
}
16+
17+
/**
18+
* 业务应用中创建样式基础写法
19+
*/
20+
export function createStyles<Props, ReturnStyle>(
21+
createStyleFn: (theme: CreateStylesTheme, props?: Props) => ReturnStyle,
22+
) {
23+
return (props?: Props): ReturnStyle => {
24+
const token = useToken();
25+
const stylish = useInternalStylish();
26+
27+
return useMemo(() => createStyleFn({ token, stylish }, props), [token, props]);
28+
};
29+
}
30+
31+
export { css, cx } from '@emotion/css';

src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export * from './createStyles';
12
export * from './styled';
23
export * from './types';
34
export * from './useToken';

src/styled.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export interface ThemeProviderProps {
2020
*/
2121
customToken?: Record<string, any>;
2222
/**
23-
* 自定义 stylish 可以自行扩展和新增自己需要的符合样式
23+
* 自定义 stylish 可以自行扩展和新增自己需要的复合样式
2424
*/
2525
customStylish?: Record<string, string>;
2626
}

src/stylish/index.ts

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import { useToken } from '@/useToken';
2+
import { css } from '@emotion/css';
3+
import { transparentize } from 'polished';
4+
import { useMemo } from 'react';
5+
6+
export interface AntdStylish {
7+
defaultButton: string;
8+
9+
textInfo: string;
10+
textDefault: string;
11+
12+
containerBgHover: string;
13+
containerBgL2: string;
14+
15+
dataInputContainer: string;
16+
dataInputContainerFocused: string;
17+
backgroundBlur: string;
18+
}
19+
20+
/**
21+
* 一组统一封装好的 antd 标准样式
22+
*/
23+
export const useInternalStylish = (): AntdStylish => {
24+
const token = useToken();
25+
26+
return useMemo(() => {
27+
const containerBgHover = css`
28+
cursor: pointer;
29+
transition: 150ms background-color ease-in-out;
30+
&:hover {
31+
background: ${token.colorFillQuaternary};
32+
}
33+
`;
34+
const dataInputContainerHover = css`
35+
color: ${token.colorText};
36+
background-color: ${token.colorFillTertiary};
37+
border-color: transparent;
38+
`;
39+
const dataInputContainerFocused = css`
40+
color: ${token.colorText} !important;
41+
background-color: ${token.colorFillQuaternary} !important;
42+
border-color: ${token.colorPrimary} !important;
43+
box-shadow: none;
44+
`;
45+
46+
const defaultButtonBase = css`
47+
color: ${token.colorTextSecondary};
48+
background: ${token.colorFillQuaternary};
49+
border-color: transparent;
50+
`;
51+
52+
return {
53+
defaultButton: css`
54+
${defaultButtonBase};
55+
56+
&:hover {
57+
color: ${token.colorText} !important;
58+
background: ${token.colorFillSecondary} !important;
59+
border-color: transparent !important;
60+
}
61+
&:focus {
62+
${defaultButtonBase};
63+
border-color: ${token.colorPrimary} !important;
64+
}
65+
`,
66+
67+
textInfo: css`
68+
color: ${token.colorTextSecondary};
69+
&:hover {
70+
color: ${token.colorText};
71+
}
72+
`,
73+
textDefault: css`
74+
color: ${token.colorTextSecondary};
75+
`,
76+
77+
containerBgHover: css`
78+
cursor: pointer;
79+
transition: 150ms background-color ease-in-out;
80+
81+
&:hover {
82+
background: ${token.colorFillQuaternary};
83+
}
84+
`,
85+
containerBgL2: css`
86+
${containerBgHover};
87+
border-radius: 4px;
88+
background: ${token.colorFillQuaternary};
89+
90+
&:hover {
91+
background: ${token.colorFillTertiary};
92+
}
93+
`,
94+
dataInputContainerFocused,
95+
dataInputContainer: css`
96+
&:hover {
97+
${dataInputContainerHover}
98+
}
99+
&:focus {
100+
${dataInputContainerFocused}
101+
}
102+
`,
103+
104+
backgroundBlur: css`
105+
background: ${transparentize(0.4)(token.colorBgElevated)};
106+
backdrop-filter: blur(10px);
107+
`,
108+
};
109+
}, [token]);
110+
};

0 commit comments

Comments
 (0)