Skip to content
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

fix: genStyleUtils doesn't work when realToken is undefined #17

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/hooks/useToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,17 @@ export interface UseTokenReturn<
DesignToken extends TokenType,
> {
token: GlobalToken<CompTokenMap, AliasToken>;
realToken?: GlobalToken<CompTokenMap, AliasToken>;
theme?: Theme<DesignToken, AliasToken>;
components?: TokenMapWithTheme<CompTokenMap, DesignToken, AliasToken>;
hashId?: string;
components?: TokenMapWithTheme<CompTokenMap, DesignToken, AliasToken>;
hashed?: string | boolean;
/**
* @warring dev only: css var token object. If you use `useToken` in production, you should use `token` instead.
*/
realToken?: GlobalToken<CompTokenMap, AliasToken>;
/**
* @warring dev only: css var options object. If you use `useToken` in production, do not use it.
*/
cssVar?: {
prefix?: string;
key?: string;
Expand Down
37 changes: 29 additions & 8 deletions src/util/genStyleUtils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
TokenMap,
TokenMapKey,
UseComponentStyleResult,
GlobalToken,
} from '../interface';

import type AbstractCalculator from './calc/calculator';
Expand Down Expand Up @@ -91,12 +92,10 @@

export type GetResetStyles<AliasToken extends TokenType> = (token: AliasToken) => CSSInterpolation;

export type GetCompUnitless<CompTokenMap extends TokenMap, AliasToken extends TokenType> = <
C extends TokenMapKey<CompTokenMap>,
>(
component: C | [C, string],
export type GetCompUnitless<CompTokenMap extends TokenMap, AliasToken extends TokenType> = (
component?: TokenMapKey<CompTokenMap> | [TokenMapKey<CompTokenMap>, string],
) => {
[key in ComponentTokenKey<CompTokenMap, AliasToken, C>]: boolean;
[key in keyof AliasToken]?: boolean;
};

function genStyleUtils<
Expand Down Expand Up @@ -221,7 +220,12 @@
const { unitless: compUnitless, injectStyle = true, prefixToken, ignore } = options;

const CSSVarRegister: React.FC<Readonly<CSSVarRegisterProps>> = ({ rootCls, cssVar = {} }) => {
const { realToken } = useToken();
const {
/**
* @warring dev only, detail see {@link UseTokenReturn.realToken}
*/
realToken = {} as GlobalToken<CompTokenMap, AliasToken>,
} = useToken();

Check warning on line 228 in src/util/genStyleUtils.tsx

View check run for this annotation

Codecov / codecov/patch

src/util/genStyleUtils.tsx#L228

Added line #L228 was not covered by tests
useCSSVarRegister(
{
path: [component],
Expand Down Expand Up @@ -257,7 +261,12 @@
};

const useCSSVar = (rootCls: string) => {
const { cssVar } = useToken();
const {
/**
* @warring dev only, detail see {@link UseTokenReturn.cssVar}
*/
cssVar,
} = useToken();

return [
(node: React.ReactElement): React.ReactElement =>
Expand Down Expand Up @@ -311,7 +320,19 @@

// Return new style hook
return (prefixCls: string, rootCls: string = prefixCls): UseComponentStyleResult => {
const { theme, realToken, hashId, token, cssVar } = useToken();
const {
theme,
hashId,
token,
/**
* @warring dev only, detail see {@link UseTokenReturn.realToken}
*/
realToken = {} as GlobalToken<CompTokenMap, AliasToken>,
/**
* @warring dev only, detail see {@link UseTokenReturn.cssVar}
*/
cssVar,
} = useToken();

const { rootPrefixCls, iconPrefixCls } = usePrefix();
const csp = useCSP();
Expand Down
Loading