Skip to content

Commit

Permalink
feat: createGlobal
Browse files Browse the repository at this point in the history
  • Loading branch information
MadCcc committed Jul 18, 2023
1 parent 3870df1 commit da4b2e0
Show file tree
Hide file tree
Showing 6 changed files with 16,915 additions and 3 deletions.
6 changes: 6 additions & 0 deletions docs/api/global-styles.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,9 @@ group: 创建样式
利用 antd v5 的 token 系统,我们可以自行组织实现一个在 Ant Design 中并不存在的 Button 样式。

<code src="../demos/globalStyles/AntdToken.tsx"></code>

## `createStyles` 相似的使用方法

`createGlobal` 同样可以用于插入全局样式,但拥有与 `createStyles` 相似的 api。

<code src="../demos/globalStyles/createGlobal.tsx"></code>
35 changes: 35 additions & 0 deletions docs/demos/globalStyles/createGlobal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { createGlobal, ThemeProvider } from 'antd-style';

const Global = createGlobal(
({ token, css }) => css`
.ant-custom-button-secondary {
color: ${token.colorSuccess};
background: ${token.colorSuccessBg};
height: ${token.controlHeight}px;
border-radius: ${token.borderRadius}px;
padding: 0 ${token.paddingContentHorizontal}px;
:hover {
background: ${token.colorSuccessBgHover};
color: ${token.colorSuccessTextHover};
}
:active {
background: ${token.colorSuccessBorder};
color: ${token.colorSuccessTextActive};
}
border: none;
cursor: pointer;
}
`,
);

export default () => {
return (
<ThemeProvider>
<Global />
<button className="ant-custom-button-secondary">antd 中不存在的按钮</button>
</ThemeProvider>
);
};
21 changes: 19 additions & 2 deletions src/factories/createGlobalStyle.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Global } from '@emotion/react';
import { Global, css as emotionCss } from '@emotion/react';
import { serializeStyles } from '@emotion/serialize';
import { memo } from 'react';

import { CSSStyle, Theme } from '@/types';
import { CSSStyle, FullToken, Theme } from '@/types';

export interface GlobalTheme {
theme: Theme;
Expand All @@ -18,3 +18,20 @@ export const createGlobalStyleFactory =
const theme = useTheme();
return <Global styles={serializeStyles(styles, undefined, { ...props, theme })} />;
});

export const createGlobalFactory =
(useTheme: () => Theme) =>
(
styleFn: ({
token,
css,
}: {
token: FullToken;
css: typeof emotionCss;
}) => ReturnType<typeof emotionCss>,
) => {
return memo(() => {
const token = useTheme();
return <Global styles={styleFn({ token, css: emotionCss })} />;
});
};
5 changes: 4 additions & 1 deletion src/functions/createInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { createContext } from 'react';
import { CacheManager, createCSS, createEmotion, serializeCSS } from '@/core';

import { createEmotionContext } from '@/factories/createEmotionContext';
import { createGlobalStyleFactory } from '@/factories/createGlobalStyle';
import { createGlobalFactory, createGlobalStyleFactory } from '@/factories/createGlobalStyle';
import { createStylishFactory } from '@/factories/createStyish';
import { createStyleProvider } from '@/factories/createStyleProvider';
import { createStylesFactory } from '@/factories/createStyles';
Expand Down Expand Up @@ -99,6 +99,8 @@ export const createInstance = <T = any>(options: CreateOptions<T>) => {

const createGlobalStyle = createGlobalStyleFactory(useTheme);

const createGlobal = createGlobalFactory(useTheme);

const createStylish = createStylishFactory(createStyles);

const ThemeProvider = createThemeProvider({
Expand All @@ -117,6 +119,7 @@ export const createInstance = <T = any>(options: CreateOptions<T>) => {
// ******************** //
createStyles,
createGlobalStyle,
createGlobal,
createStylish,
// ******************** //
// **** 基础样式方法 **** //
Expand Down
1 change: 1 addition & 0 deletions src/functions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const {
createStyles,
createGlobalStyle,
createStylish,
createGlobal,
// **** 基础样式方法 **** //
css,
cx,
Expand Down
Loading

0 comments on commit da4b2e0

Please sign in to comment.