Dosu LogoDosu Logo
Ask
Join our Discord
Ant DesignPublic
Ant Design
DocumentsAnt Design
migrate-less-component.en-US
migrate-less-component.en-US
Type
External
Status
Published
Created
Jun 12, 2026
Updated
Jun 12, 2026
Source
docs/guide/migrate-less-component.en-US.md

Migrating Component Code#

The component demo below is from the ProCard's Static component in procomponents.

Migration Steps#

1. Replace Entry Point#

Change the style file from index.less to style.ts, and add the hooks const { styles } = useStyles() import. Combine the styles and the default container class in the JSX layer. This way, the JSX layer code is updated.

// index.tsx
- import './index.less';
+ import useStyles from './style.ts';

const Statistic: React.FC = () => {
+ const { styles, cx } = useStyles();

  //...

  return (
- <div className={classString} style={style}>
+ <div className={cx(styles, classString)} style={style}>
  // The following code remains unchanged
  //...
  )}

2. Replace Style Code#

Change the style code from less syntax to createStyles syntax. First, rename index.less to style.ts, then add the following at the top:

+ import { createStyles } from 'antd-style';

+ export const useStyles = createStyles(({css})=>{

    return css`
    `
})

Then place the original code inside the css function.

import { createStyles } from 'antd-style';

export const useStyles = createStyles(({css})=>{

    return css`
+ .@{pro-card-statistic-prefix-cls} {
+ // ...
+ }
    `
})

3. Adjust Hierarchy#

Since we use css`` to provide scope, the nested level styles need to be adjusted as follows:

import { createStyles } from 'antd-style';

export const useStyles = createStyles(({css})=>{

    return css`
- .@{pro-card-statistic-prefix-cls} {
+ & .@{pro-card-statistic-prefix-cls} {
      // ...
     }
+ .@{pro-card-statistic-prefix-cls} {
+ & + & {
+ // ...
+ }
+ }
    `
})

4. Replace Less Variables with Tokens and JS Variables#

import { createStyles } from 'antd-style';

export const useStyles = createStyles(({ css, prefixCls })=>{
    const prefix = `${prefixCls}-pro-card-statistic`;

    return css`
- .@{pro-card-statistic-prefix-cls} {
+ & .${prefix} {
- font-size: @font-size-base;
+ font-size: ${token.fontSize}px;
     }
- .@{pro-card-statistic-prefix-cls} {
+ .${prefix} {
      & + & {

- .@{ant-prefix}-statistic-title {
- color: @text-color;
+ .${prefixCls}-statistic-title {
+ color: ${token.colorText};
      }

+ }
    `
})

The effect after migration is shown below. It can be seen that after a relatively low migration cost, the new component writing method already supports the ability to respond to themes by default and gains all the dynamic capabilities after cssinjs transformation.

New Component Writing Scheme#

See: Component Development

Documents
Masonry Server-Side Rendering
2026-03-17-doctor-enhancements
2026-03-17-doctor-enhancements-design
2026-03-20-bug-fixes-round-2
2026-03-23-bug-report-commands
2026-03-23-bug-report-commands-design
2026-03-24-antd-mcp-server-design
2026-03-24-doctor-ecosystem-peerdeps
2026-03-24-doctor-ecosystem-peerdeps-design
2026-03-26-env-command
2026-03-26-env-command-design
2026-04-02-antd-v3-support-design
2026-04-02-antd-v3-support-plan
2026-04-17-opt-out-auto-report-design
2026-05-26-upgrade-command-design
AGENTS
CHANGELOG
CHANGELOG
CHANGELOG
CHANGELOG.zh-CN
CLAUDE
HISTORY
HISTORY
README
README
README
README
README
README
README
README
README
README
README.zh-CN
README.zh-CN
RFC-createStaticStyles
SKILL
antd-based-components.en-US
antd-based-components.zh-CN
antd-override.en-US
antd-override.zh-CN
babel-plugin.en-US
babel-plugin.zh-CN
bug_report
changelog.en-US
changelog.zh-CN
clay.en-US
clay.zh-CN
color-panel
compare.en-US
compare.zh-CN
components-usage.en-US
components-usage.zh-CN
controlled
create-instance.en-US
create-instance.zh-CN
create-static-styles.en-US
create-static-styles.zh-CN
create-styles.en-US
create-styles.en-US
create-styles.zh-CN
create-styles.zh-CN
create-stylish.en-US
create-stylish.zh-CN
css-in-js-intro.en-US
css-in-js-intro.zh-CN
cssinjs-compiler-difference.en-US
cssinjs-compiler-difference.zh-CN
custom-theme.en-US
custom-theme.zh-CN
custom-token-types.en-US
custom-token-types.zh-CN
design-v6
feature_request
fix-switch-theme-fouc.en-US
fix-switch-theme-fouc.zh-CN
genStyleUtils
global-styles.en-US
global-styles.zh-CN
index
index
index
index
index
index
index.en-US
index.en-US
index.zh-CN
index.zh-CN
index.zh-CN
intro
intro
introModal
introModal
mac-select.en-US
mac-select.zh-CN
migrate-less-application.en-US
migrate-less-application.zh-CN
migrate-less-codemod.en-US
migrate-less-codemod.zh-CN
migrate-less-component.en-US
migrate-less-component.zh-CN
mirgration-less-global-style.en-US
mirgration-less-global-style.zh-CN
motivationHelp
motivationHelp
nest-element-style.en-US
nest-element-style.zh-CN
overview
performance-comparsion.en-US
performance-comparsion.zh-CN
performance.en-US
performance.zh-CN
previewer-token-panel
proposalHelp
proposalHelp
question
reproHelp
reproHelp
reproModal
reproModal
setup-styled.en-US
setup-styled.zh-CN
spec
ssr.en-US
ssr.zh-CN
static-message.en-US
static-message.zh-CN
stepsHelp
stepsHelp
strategy.en-US
strategy.zh-CN
style-provider.en-US
style-provider.zh-CN
styled.en-US
styled.en-US
styled.zh-CN
styled.zh-CN
stylish.en-US
stylish.zh-CN
switch-theme.en-US
switch-theme.zh-CN
theme
theme-provider.en-US
theme-provider.zh-CN
use-responsive.en-US
use-responsive.zh-CN
use-theme-mode.en-US
use-theme-mode.zh-CN
use-theme.en-US
use-theme.zh-CN