Skip to content

Commit e8cd3dc

Browse files
committed
refactor: remove all token interface, use generic instead
1 parent 72e9741 commit e8cd3dc

19 files changed

+114
-2292
lines changed

src/context/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ export { useMergedConfigContext } from './config-provider';
22
export { useMergedThemeContext } from './theme-provider';
33

44
export type { GetConfigProviderContext } from './config-provider';
5-
export type { GetThemeProviderContext, DesignTokenProviderProps } from './theme-provider';
5+
export type { GetThemeProviderContext } from './theme-provider';

src/context/theme-provider.ts

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,30 @@
11
import React from 'react';
2-
import type { Theme } from '@ant-design/cssinjs';
2+
import type { Theme, TokenType } from '@ant-design/cssinjs';
33

4-
import defaultSeedToken from '../themes/seed';
4+
import type { OverrideTokenMap, TokenMap } from '../interface';
55

6-
import type { AliasToken, OverrideToken, MapToken, SeedToken, GlobalToken } from '../interface';
7-
8-
type ComponentsToken<CompTokenMap extends Object> = {
9-
[key in keyof OverrideToken<CompTokenMap>]?: OverrideToken<CompTokenMap>[key] & {
10-
theme?: Theme<SeedToken, MapToken>;
6+
export type TokenMapWithTheme<
7+
CompTokenMap extends TokenMap,
8+
DesignToken extends TokenType,
9+
AliasToken extends TokenType,
10+
> = {
11+
[key in keyof OverrideTokenMap<CompTokenMap>]?: OverrideTokenMap<CompTokenMap>[key] & {
12+
theme?: Theme<DesignToken, AliasToken>;
13+
};
1114
};
12-
};
1315

14-
export interface DesignTokenProviderProps<CompTokenMap extends Object> {
15-
token: GlobalToken<CompTokenMap>;
16+
export interface DesignTokenProviderProps<
17+
CompTokenMap extends TokenMap,
18+
DesignToken extends TokenType,
19+
AliasToken extends TokenType,
20+
> {
21+
token: OverrideTokenMap<CompTokenMap>;
22+
realToken?: OverrideTokenMap<CompTokenMap>;
1623
/** Just merge `token` & `override` at top to save perf */
17-
override: { override: Partial<AliasToken> } & ComponentsToken<CompTokenMap>;
18-
realToken?: GlobalToken<CompTokenMap>;
24+
override: { override: OverrideTokenMap<CompTokenMap> };
25+
theme?: Theme<DesignToken, AliasToken>;
26+
components?: TokenMapWithTheme<CompTokenMap, DesignToken, AliasToken>;
1927
hashId?: string;
20-
theme?: Theme<SeedToken, MapToken>;
21-
components?: ComponentsToken<CompTokenMap>;
2228
hashed?: string | boolean;
2329
cssVar?: {
2430
prefix?: string;
@@ -28,17 +34,27 @@ export interface DesignTokenProviderProps<CompTokenMap extends Object> {
2834

2935
// To ensure snapshot stable. We disable hashed in test env.
3036
export const DefaultThemeProviderContextConfig = {
31-
token: defaultSeedToken,
32-
override: { override: defaultSeedToken },
37+
token: {},
38+
override: { override: {} },
3339
hashed: true,
3440
};
3541

36-
export type GetThemeProviderContext<CompTokenMap extends Object> = () => [React.Context<DesignTokenProviderProps<CompTokenMap>>];
42+
export type GetThemeProviderContext<
43+
CompTokenMap extends TokenMap,
44+
DesignToken extends TokenType,
45+
AliasToken extends TokenType,
46+
> = () => [React.Context<DesignTokenProviderProps<CompTokenMap, DesignToken, AliasToken>>];
3747

38-
export function useMergedThemeContext<CompTokenMap extends Object> (getThemeProviderContext?: GetThemeProviderContext<CompTokenMap>) {
39-
const DefaultThemeProviderContext = React.createContext<DesignTokenProviderProps<CompTokenMap>>(
40-
DefaultThemeProviderContextConfig as DesignTokenProviderProps<CompTokenMap>
41-
);
48+
export function useMergedThemeContext<
49+
CompTokenMap extends TokenMap,
50+
DesignToken extends TokenType,
51+
AliasToken extends TokenType,
52+
>(getThemeProviderContext?: GetThemeProviderContext<CompTokenMap, DesignToken, AliasToken>) {
53+
const DefaultThemeProviderContext = React.createContext<DesignTokenProviderProps<CompTokenMap, DesignToken, AliasToken>>({
54+
token: {},
55+
override: { override: {} },
56+
hashed: true,
57+
});
4258

4359
const [ThemeProviderContext = {}] = getThemeProviderContext?.() ?? [];
4460

0 commit comments

Comments
 (0)