Skip to content

Commit

Permalink
chore: rm antd internal func
Browse files Browse the repository at this point in the history
  • Loading branch information
zombieJ committed Sep 4, 2023
1 parent 3487688 commit 4c5b820
Show file tree
Hide file tree
Showing 12 changed files with 62 additions and 12 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

## About

🚨 Sample code in `styles/` use antd internal `genComponentStyleHook` to generate style hook.
It's only to simplify the dependency of this repo which is **not recommend** to use internal API in real world.
Sample code in `styles/styleUtil` copy the antd `genComponentStyleHook` function.
You can use any other css-in-js solution to generate style hook like `Emotion` or `styled-components`.

## Usage
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"@types/react-dom": "^16.9.0"
},
"dependencies": {
"@ant-design/cssinjs": "^1.16.1",
"@ant-design/cssinjs": "^1.17.0",
"@babel/runtime": "^7.18.3",
"@ctrl/tinycolor": "^3.6.0",
"classnames": "^2.3.2",
Expand Down
2 changes: 1 addition & 1 deletion src/Text/style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
type GenerateStyle,
genComponentStyleHook,
type FullToken,
} from 'antd/es/theme/internal';
} from '../styles/styleUtil';

import { background } from '../styles/gradientUtil';
import { DOT_PREFIX } from '../constant';
Expand Down
2 changes: 1 addition & 1 deletion src/styles/buttonStyle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
type GenerateStyle,
genComponentStyleHook,
type FullToken,
} from 'antd/es/theme/internal';
} from './styleUtil';

import {
getAnimationBackground,
Expand Down
2 changes: 1 addition & 1 deletion src/styles/cardStyle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
type GenerateStyle,
genComponentStyleHook,
type FullToken,
} from 'antd/es/theme/internal';
} from './styleUtil';

import { getBorderStyle } from './gradientUtil';
import { DOT_PREFIX } from '../constant';
Expand Down
2 changes: 1 addition & 1 deletion src/styles/checkboxStyle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
type GenerateStyle,
genComponentStyleHook,
type FullToken,
} from 'antd/es/theme/internal';
} from './styleUtil';

import { background, getBorderStyle } from './gradientUtil';
import { DOT_PREFIX } from '../constant';
Expand Down
2 changes: 1 addition & 1 deletion src/styles/dividerStyle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
type GenerateStyle,
genComponentStyleHook,
type FullToken,
} from 'antd/es/theme/internal';
} from './styleUtil';

import { background } from './gradientUtil';
import { DOT_PREFIX } from '../constant';
Expand Down
2 changes: 1 addition & 1 deletion src/styles/radioStyle.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { genComponentStyleHook } from 'antd/es/theme/internal';
import { genComponentStyleHook } from './styleUtil';

import { genStyle } from './checkboxStyle';

Expand Down
2 changes: 1 addition & 1 deletion src/styles/spinStyle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
type GenerateStyle,
genComponentStyleHook,
type FullToken,
} from 'antd/es/theme/internal';
} from './styleUtil';

import { background } from './gradientUtil';
import { DOT_PREFIX } from '../constant';
Expand Down
51 changes: 51 additions & 0 deletions src/styles/styleUtil.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
export * from 'antd/es/theme/internal';
import { theme as Theme, ConfigProvider, GlobalToken } from 'antd';
import { CSSInterpolation, useStyleRegister } from '@ant-design/cssinjs';
import {
FullToken,
OverrideComponent,
} from 'antd/es/theme/util/genComponentStyleHook';
import React from 'react';

// Copy from antd `genComponentStyleHook`, you can use any other CSS-in-JS library instead.
export function genComponentStyleHook<ComponentName extends OverrideComponent>(
componentNames: [ComponentName, string],
styleFn: (token: GlobalToken) => CSSInterpolation,
) {
const concatComponent = componentNames.join('-');

return (prefixCls: string): void => {
const { theme, token, hashId } = Theme.useToken();
const { getPrefixCls, iconPrefixCls } = React.useContext(
ConfigProvider.ConfigContext,
);
const rootPrefixCls = getPrefixCls();

// Shared config
const sharedConfig: Omit<Parameters<typeof useStyleRegister>[0], 'path'> = {
theme,
token,
hashId,
};

useStyleRegister(
{ ...sharedConfig, path: [concatComponent, prefixCls, iconPrefixCls] },
() => {
const componentCls = `.${prefixCls}`;

const mergedToken = {
...token,
componentCls,
prefixCls,
iconCls: `.${iconPrefixCls}`,
antCls: `.${rootPrefixCls}`,
};

const styleInterpolation = styleFn(
mergedToken as any as FullToken<ComponentName>,
);
return styleInterpolation;
},
);
};
}
2 changes: 1 addition & 1 deletion src/styles/switchStyle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
type GenerateStyle,
genComponentStyleHook,
type FullToken,
} from 'antd/es/theme/internal';
} from './styleUtil';

import { background } from './gradientUtil';
import { DOT_PREFIX } from '../constant';
Expand Down
2 changes: 1 addition & 1 deletion src/styles/tagStyle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
type GenerateStyle,
genComponentStyleHook,
type FullToken,
} from 'antd/es/theme/internal';
} from './styleUtil';

import { getBackground, getBorderStyle } from './gradientUtil';

Expand Down

0 comments on commit 4c5b820

Please sign in to comment.