Skip to content

Commit

Permalink
✨ feat: 调整 stylish 的实现逻辑
Browse files Browse the repository at this point in the history
BREAKING CHANGES: stylish 将不再能直接作为 className 使用,需要通过 css 包裹才能生效,将 stylish 的定位变得更加精准 —— 可复用的样式片段
  • Loading branch information
arvinxx committed Jan 22, 2023
1 parent a1a5f6a commit 1822b38
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
15 changes: 10 additions & 5 deletions src/containers/ThemeProvider/TokenContainer.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ThemeProvider as Provider } from '@emotion/react';
import { css as reactCss, ThemeProvider as Provider } from '@emotion/react';
import { SerializedStyles } from '@emotion/serialize';
import { ReactElement, useMemo } from 'react';

import { css, cx } from '@/functions';
import { useThemeMode } from '@/hooks';
import { useAntdTheme } from '@/hooks/useAntdTheme';
import type { ThemeProviderProps } from './type';
Expand Down Expand Up @@ -40,14 +40,19 @@ const TokenContainer: <T, S>(props: TokenContainerProps<T, S>) => ReactElement |
stylish: antdStylish,
appearance,
isDarkMode,
css,
cx,
css: reactCss,
});
}
return stylishOrGetStylish;
}, [stylishOrGetStylish, token, customToken, antdStylish, appearance]);

const stylish = { ...customStylish, ...antdStylish };
const stylish = useMemo(() => {
const merged = { ...customStylish, ...antdStylish };

return Object.fromEntries(
Object.entries<SerializedStyles>(merged).map(([key, value]) => [key, value.styles]),
);
}, [customStylish, antdStylish]);

const theme: Theme = {
...token,
Expand Down
5 changes: 2 additions & 3 deletions src/functions/createStyles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type {
ThemeAppearance,
} from '@/types';

import { type CSSObject, type Emotion } from './css';
import { type CSSObject } from './css';

export interface CreateStylesTheme extends CommonStyleUtils {
token: FullToken;
Expand All @@ -24,10 +24,9 @@ export interface CreateStylesTheme extends CommonStyleUtils {
/**
* 最终返回 styles 对象的类型定义
*/
export interface ReturnStyles<T extends StyleInputType> {
export interface ReturnStyles<T extends StyleInputType> extends Pick<CommonStyleUtils, 'cx'> {
styles: ReturnStyleToUse<T>;
theme: Omit<Theme, 'prefixCls'>;
cx: Emotion['cx'];
prefixCls: string;
}

Expand Down
14 changes: 12 additions & 2 deletions src/types/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@ import { ThemeConfig } from 'antd/es/config-provider/context';
import { AliasToken } from 'antd/es/theme/interface';

import { Emotion } from '@/functions';
import { CSSInterpolation, SerializedStyles } from '@emotion/serialize';
import { ThemeAppearance, ThemeMode } from './appearance';

export interface EmotionReactCss {
(template: TemplateStringsArray, ...args: Array<CSSInterpolation>): SerializedStyles;
(...args: Array<CSSInterpolation>): SerializedStyles;
}

export interface CommonStyleUtils {
cx: Emotion['cx'];
css: Emotion['css'];
}

export interface ThemeContextState {
appearance: ThemeAppearance;
themeMode: ThemeMode;
Expand All @@ -32,12 +39,15 @@ export interface CustomTokenParams extends AppearanceState {

export type GetCustomToken<T> = (theme: CustomTokenParams) => T;

export interface CustomStylishParams extends CommonStyleUtils, AppearanceState {
export interface CustomStylishParams extends AppearanceState {
token: FullToken;
stylish: AntdStylish;
css: EmotionReactCss;
}

export type GetCustomStylish<S> = (theme: CustomStylishParams) => S;
export type GetCustomStylish<S> = (theme: CustomStylishParams) => {
[T in keyof S]: SerializedStyles;
};

export type GetAntdThemeConfig = (appearance: ThemeAppearance) => ThemeConfig | undefined;

Expand Down

0 comments on commit 1822b38

Please sign in to comment.