generated from arvinxx/npm-template
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feat: 简化 ThemeProvider 注入 styled 的接口配置
BREAKING CHANGES: 移除 styled.useTheme 的用法,需要调整为 styled.ThemeContext,且 styled 的ThemeProvider 可以无需注入
- Loading branch information
Showing
7 changed files
with
95 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { StyledConfig, StyledThemeProvider } from '@/types'; | ||
|
||
/** | ||
* 创建一个 styled api 的 ThemeProvider | ||
* 如果用户有设定 ThemeProvider,就使用用户的,否则使用 ThemeContext.Provider | ||
* @param styledConfig | ||
*/ | ||
export const createStyledThemeProvider = (styledConfig: StyledConfig): StyledThemeProvider => { | ||
if (styledConfig.ThemeProvider) return styledConfig.ThemeProvider; | ||
|
||
const { ThemeContext } = styledConfig; | ||
return (props) => ( | ||
<ThemeContext.Provider value={props.theme}>{props.children}</ThemeContext.Provider> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,17 @@ | ||
import { ThemeProvider, useTheme } from '@emotion/react'; | ||
import { ThemeContext, ThemeProvider } from '@emotion/react'; | ||
import { Context } from 'react'; | ||
|
||
import { createStyledThemeProvider } from '@/factories/createStyledThemeProvider'; | ||
import { StyledConfig, StyledThemeProvider, Theme } from '@/types'; | ||
|
||
export let DEFAULT_THEME_PROVIDER = ThemeProvider as StyledThemeProvider; | ||
export let DEFAULT_USE_THEME = useTheme as () => Theme; | ||
export let DEFAULT_THEME_CONTEXT = ThemeContext as Context<Theme>; | ||
|
||
export const setupStyled = (config: StyledConfig) => { | ||
if (config?.ThemeProvider) { | ||
DEFAULT_THEME_PROVIDER = config.ThemeProvider; | ||
} | ||
if (config.useTheme) { | ||
DEFAULT_USE_THEME = config.useTheme; | ||
if (!config.ThemeContext) { | ||
throw 'ThemeContext is required. Please check your config.'; | ||
} | ||
|
||
DEFAULT_THEME_CONTEXT = config.ThemeContext; | ||
DEFAULT_THEME_PROVIDER = createStyledThemeProvider(config); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,16 @@ | ||
import { Theme } from 'antd-style/src'; | ||
import { FC, ReactNode } from 'react'; | ||
import { Context, FC, ReactNode } from 'react'; | ||
import { Theme } from './theme'; | ||
|
||
export interface StyledConfig { | ||
/** | ||
* styled 对象所对应的 ThemeContext | ||
* @requires | ||
*/ | ||
ThemeContext: Context<any>; | ||
/** | ||
* 可以注入相应 styled 方法的 ThemeProvider,或其他自己定义的ThemeProvider | ||
*/ | ||
ThemeProvider?: StyledThemeProvider; | ||
useTheme?: () => any; | ||
} | ||
|
||
export type StyledThemeProvider = FC<{ theme: Theme; children: ReactNode }>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters