From Less variables to design tokens in Ant Design 5

Ant Design 4 was themed with Less.

Ant Design 4 was themed with Less. You overrode variables like @primary-color at build time, turned on javascriptEnabled in less-loader, and added babel-plugin-import so each component pulled in its own styles. Changing the theme at runtime, say for a dark mode toggle, meant shipping two compiled stylesheets or reaching for an experimental mode.

Ant Design 5, released in November 2022, drops Less completely. Styles are generated in the browser by a CSS-in-JS engine, and the theme is a JavaScript object of design tokens. Here’s how the new model works, and what to watch for when moving over.

The basic switch

import { ConfigProvider, Button } from 'antd';

export function App() {
  return (
    <ConfigProvider
      theme={{
        token: {
          colorPrimary: '#00b96b',
          borderRadius: 2,
        },
      }}
    >
      <Button type="primary">Save</Button>
    </ConfigProvider>
  );
}

There’s no build step, no Less, and no plugin. Each component injects the styles it needs the first time it renders. Most of the old variables have a token with a similar name:

Ant Design 4 (Less) Ant Design 5 (token)
@primary-color colorPrimary
@border-radius-base borderRadius
@font-size-base fontSize
@font-family fontFamily

Seeds, and the tokens derived from them

The big idea is that you set a few seed tokens, and antd derives the rest. Set colorPrimary to #00b96b, and you also get a hover colour, an active colour, background tints and borders, all calculated from it. Reading the theme back with theme.useToken(), that one seed gave me colorPrimaryHover: #20c77c, without writing it anywhere.

There are three layers:

  • Seed tokens: the inputs, like colorPrimary, borderRadius, fontSize.
  • Map tokens: values derived from seeds, like colorPrimaryHover or borderRadiusLG.
  • Alias tokens: names for how things are used, like colorText or colorBgContainer.

Override a seed when you want the change to ripple through everything. Override a derived token only when one specific shade is wrong.

Dark mode is an algorithm

The derivation step is a function called an algorithm, and swapping it is how you get dark or compact themes:

import { ConfigProvider, theme } from 'antd';

<ConfigProvider
  theme={{
    algorithm: isDark ? theme.darkAlgorithm : theme.defaultAlgorithm,
    token: { colorPrimary: '#00b96b' },
  }}
>

Algorithms combine, too: algorithm: [theme.darkAlgorithm, theme.compactAlgorithm].

One thing surprised me. With the dark algorithm, token.colorPrimary didn’t come back as #00b96b. It came back as #03a05e, adjusted to sit better on a dark background. That’s usually what you want. If your brand colour must be exact in both modes, an algorithm is just a function, so you can wrap the dark one and put the seed back:

const darkKeepBrand = (seed, map) => ({
  ...theme.darkAlgorithm(seed, map),
  colorPrimary: seed.colorPrimary,
});

The hover and active shades still come from the dark algorithm, so check they still look right next to the exact colour.

Theming one component

You can override tokens for a single component type:

<ConfigProvider
  theme={{
    token: { colorPrimary: '#00b96b' },
    components: {
      Button: { colorPrimary: '#722ed1' },
    },
  }}
>

And ConfigProviders nest, so one section of the page can have its own theme without affecting the rest.

Using tokens in your own components

Your own components should look like they belong. In Ant Design 4, you’d import the Less variables into your own stylesheets. In 5, you read the tokens in JavaScript:

import { theme } from 'antd';

function Panel({ children }) {
  const { token } = theme.useToken();
  return (
    <section
      style={{
        background: token.colorBgContainer,
        borderRadius: token.borderRadiusLG,
        padding: token.paddingLG,
        color: token.colorText,
      }}
    >
      {children}
    </section>
  );
}

Because it’s a hook, the panel follows the theme automatically, dark mode included.

Overriding styles got easier

Ant Design 5 wraps its hashed class in :where(), so the generated selectors look like this:

:where(.css-dev-only-do-not-override-93yflw).ant-btn { ... }

:where() has zero specificity, so a plain .ant-btn rule in your own CSS wins without !important. (Don’t target that hashed class. It changes between builds, as its name hints.)

The catch is old browsers. If you still support a browser without :where(), wrap the app in StyleProvider from @ant-design/cssinjs with hashPriority=“high”. The selectors then become plain .css-…ant-btn, at the cost of higher specificity.

Migration checklist

  • Remove every Less import of antd styles, babel-plugin-import, and the javascriptEnabled option you only needed for antd.
  • Add import ‘antd/dist/reset.css’ once, for the base reset that used to come with the global stylesheet.
  • Turn your Less overrides into a theme object. Start with seeds.
  • Replace moment with dayjs in date pickers. Ant Design 5 uses dayjs.
  • If you render on the server, extract the styles with createCache and extractStyle from @ant-design/cssinjs, or the first paint arrives unstyled.

The new system is more JavaScript at runtime, and you can feel that on very large pages. In exchange, the theme is just data: you can switch it at runtime, nest it, read it in your own components, and keep it in one file instead of a build config.