-
Notifications
You must be signed in to change notification settings - Fork 39
fix: undefined error when consuming cssvar #200
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: alpha
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ import { serializeCSS } from '@/core'; | |
| import { useAntdTheme, useThemeMode } from '@/hooks'; | ||
| import { StyledThemeProvider, Theme } from '@/types'; | ||
|
|
||
| import { useCustomToken } from '@/hooks/useCustomToken'; | ||
| import type { ThemeProviderProps } from './type'; | ||
|
|
||
| interface TokenContainerProps<T, S = Record<string, string>> | ||
|
|
@@ -25,7 +26,7 @@ const TokenContainer: <T, S>(props: TokenContainerProps<T, S>) => ReactElement | | |
| }) => { | ||
| const themeState = useThemeMode(); | ||
| const { appearance, isDarkMode } = themeState; | ||
| const { stylish: antdStylish, ...token } = useAntdTheme(); | ||
| const { stylish: antdStylish, cssVar, ...token } = useAntdTheme(); | ||
|
|
||
| // 获取默认的自定义 token | ||
| const defaultCustomToken = useMemo(() => { | ||
|
|
@@ -47,6 +48,13 @@ const TokenContainer: <T, S>(props: TokenContainerProps<T, S>) => ReactElement | | |
| return { ...defaultCustomToken, ...customTokenOrFn }; | ||
| }, [defaultCustomToken, customTokenOrFn, token, appearance]); | ||
|
|
||
| const { token: cssVariableToken, cssVarCls, hashId } = useCustomToken(customToken as any); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| const mergedCssVar = useMemo( | ||
| () => ({ ...cssVar, ...cssVariableToken }), | ||
| [cssVar, cssVariableToken], | ||
| ); | ||
|
|
||
| // 获取 stylish | ||
| const customStylish = useMemo(() => { | ||
| if (!stylishOrGetStylish) return {}; | ||
|
|
@@ -68,12 +76,17 @@ const TokenContainer: <T, S>(props: TokenContainerProps<T, S>) => ReactElement | | |
| const theme: Theme = { | ||
| ...token, | ||
| ...(customToken as any), | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| cssVar: mergedCssVar, | ||
| stylish: stylish as any, | ||
| ...themeState, | ||
| prefixCls, | ||
| }; | ||
|
|
||
| return <StyledThemeProvider theme={theme}>{children}</StyledThemeProvider>; | ||
| return ( | ||
| <StyledThemeProvider theme={theme}> | ||
| <div className={[cssVarCls, hashId].filter(Boolean).join(' ')}>{children}</div> | ||
| </StyledThemeProvider> | ||
| ); | ||
| }; | ||
|
|
||
| export default TokenContainer; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import { CustomToken } from '@/types'; | ||
| import { useCacheToken } from '@ant-design/cssinjs'; | ||
| import { useToken as useInternalToken } from 'antd/lib/theme/internal'; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| import React from 'react'; | ||
|
|
||
| function useCssVarConf() { | ||
| const [theme, realToken, hashId, token, cssVar, zeroRuntime] = useInternalToken(); | ||
| const themeKey = React.useId(); | ||
|
|
||
| const mergeCssVar = { | ||
| ...cssVar, | ||
| key: `antd-style-${cssVar?.key}-${themeKey.replace(/:/g, '')}`, | ||
| }; | ||
|
|
||
| // copied from https://github.com/ant-design/ant-design/blob/4e8d79581e4ec2b4a4cf7abf2e1ee4e939ba1a90/components/theme/util/genStyleUtils.ts#L25-L27 | ||
| return { theme, realToken, hashId, token, cssVar: mergeCssVar, zeroRuntime }; | ||
| } | ||
|
|
||
| export function useCustomToken(customToken: CustomToken) { | ||
| const { theme, cssVar } = useCssVarConf(); | ||
|
|
||
| const [token, hashId, realToken] = useCacheToken<CustomToken>(theme, [], { | ||
| salt: 'antd-style', | ||
| override: customToken || {}, | ||
| getComputedToken: (_, override) => override as any, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Casting |
||
| cssVar, | ||
| }); | ||
|
|
||
| return { | ||
| token, | ||
| hashId, | ||
| realToken, | ||
| cssVarCls: cssVar.key ?? '', | ||
| }; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The non-null assertion
tokenName!is unsafe becausetokenNameis an optional property. IftokenNameisundefined, this will cause a runtime error. A safer approach is to conditionally access the property only whentokenNameis defined.