ant-design/components/theme/internal.ts
Wuxh 7f189d56e5
refactor: merge preset colors (#39742)
* style: remove redundant code

* feat(style): add preset colour style generation method

(cherry picked from commit 1266e42ba27accb48a6544e3eddaa4a94daef00c)

* feat: uniform preset colour generation css selector method

(cherry picked from commit 5af87e8d5ffcfd23e755946167f200c0565f8222)

* chore: merge preset colors

(cherry picked from commit 05040dfb703f60a3ea1715326748c508acbf41a6)

* chore: update

(cherry picked from commit 241b40a1361469487a6a3e8f1ad07a25d250463d)

* chore: remove Badge preset inverse colors

* chore: remove fix

删除的这部分其实一个 Bug,但是为了给 PR 减负(尽可能单一, 后面新开一个 PR 来修复这个问题

* suggestions accepted

Update components/style/presetColor.tsx

Co-authored-by: MadCcc <1075746765@qq.com>

Co-authored-by: MadCcc <1075746765@qq.com>
2023-01-06 11:06:46 +08:00

91 lines
2.4 KiB
TypeScript

import type { CSSInterpolation, Theme } from '@ant-design/cssinjs';
import { createTheme, useCacheToken, useStyleRegister } from '@ant-design/cssinjs';
import React from 'react';
import version from '../version';
import type {
AliasToken,
GlobalToken,
MapToken,
OverrideToken,
PresetColorType,
PresetColorKey,
SeedToken,
} from './interface';
import { PresetColors } from './interface';
import defaultDerivative from './themes/default';
import defaultSeedToken from './themes/seed';
import formatToken from './util/alias';
import type { FullToken } from './util/genComponentStyleHook';
import genComponentStyleHook from './util/genComponentStyleHook';
import statisticToken, { merge as mergeToken, statistic } from './util/statistic';
const defaultTheme = createTheme(defaultDerivative);
export {
// colors
PresetColors,
// Statistic
statistic,
statisticToken,
mergeToken,
// hooks
useStyleRegister,
genComponentStyleHook,
};
export type {
SeedToken,
AliasToken,
PresetColorType,
PresetColorKey,
// FIXME: Remove this type
AliasToken as DerivativeToken,
FullToken,
};
// ================================ Context =================================
// To ensure snapshot stable. We disable hashed in test env.
export const defaultConfig = {
token: defaultSeedToken,
hashed: true,
};
export const DesignTokenContext = React.createContext<{
token: Partial<AliasToken>;
theme?: Theme<SeedToken, MapToken>;
components?: OverrideToken;
hashed?: string | boolean;
}>(defaultConfig);
// ================================== Hook ==================================
export function useToken(): [Theme<SeedToken, MapToken>, GlobalToken, string] {
const {
token: rootDesignToken,
hashed,
theme,
components,
} = React.useContext(DesignTokenContext);
const salt = `${version}-${hashed || ''}`;
const mergedTheme = theme || defaultTheme;
const [token, hashId] = useCacheToken<GlobalToken, SeedToken>(
mergedTheme,
[defaultSeedToken, rootDesignToken],
{
salt,
override: { override: rootDesignToken, ...components },
formatToken,
},
);
return [mergedTheme, token, hashed ? hashId : ''];
}
export type UseComponentStyleResult = [(node: React.ReactNode) => React.ReactElement, string];
export type GenerateStyle<
ComponentToken extends object = AliasToken,
ReturnType = CSSInterpolation,
> = (token: ComponentToken) => ReturnType;