最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to use antd theme tokens in my custom component? - Stack Overflow

programmeradmin2浏览0评论

I want to use antd theme tokens to style my custom ponent? May be there is an emotion-like way:

// style.ts
export default createStyles((token) => ({
  foo: {
    color: token.colorPrimary,
  }
})

// index.tsx
import styles from './style';

const FooBar = () => {
  return (
    <div className={styles.foo}>
      FooBar
    </div>
  );
};

Or maybe there is a better way to do it? There is practically nothing in the docs except useToken and use in style directly.

I want to use antd theme tokens to style my custom ponent? May be there is an emotion-like way:

// style.ts
export default createStyles((token) => ({
  foo: {
    color: token.colorPrimary,
  }
})

// index.tsx
import styles from './style';

const FooBar = () => {
  return (
    <div className={styles.foo}>
      FooBar
    </div>
  );
};

Or maybe there is a better way to do it? There is practically nothing in the docs except useToken and use in style directly.

Share Improve this question asked May 24, 2023 at 8:45 Maxim LaninMaxim Lanin 4,54127 silver badges33 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

You can import theme from antd and use it as a hook:

import { theme as antdTheme } from 'antd'

function MyComponent() {
  const { useToken } = antdTheme
  const { token: theme } = useToken() 

  // now use it
  return (<div 
    style={{
      color: theme.colorPrimary
    }}>
      styled div
  </div>)
}

Or you can create a styles file (keeping your styles separated from your ponents/functions) and use it like this:

// styles.ts
import { CSSProperties } from 'react'
import { theme as antdTheme } from 'antd'

// if you use TS you must declare types here
type StylesType = {
  DivStyle: CSSProperties
  OtherComponentStyle: CSSProperties
}

function Styles(): StylesType {
  const { useToken } = antdTheme
  const { token: theme } = useToken()

  const DivStyle = {
    color: theme.colorPrimary
  }

  ... other ponents


  return {
    DivStyle,
    ... other ponents
  }
}

export default

And use it like this:

// MyComponent.tsx
import Styles from 'path/to/styles'

function MyComponent() {
  const S = Styles()

  // now use it
  return (
    <div 
      style={S.DivStyle}
    >
        styled div
    </div>
  )
}
How to use antd theme tokens to customize ponents?

Antd has very useful theme token. customize like your design system. we can avoid styling manually too.and we can control the ponents by theme token. very usefully.

for override theme token, you can check it docs here ant theme token override

pls check it out my example

example

export const theme = Object.freeze({
  // apply ant design theme
  token: {
    // primary color
    colorPrimary: "#5C469C",
    colorLink: "green",
    // hover color
    colorPrimaryHover: "#5C469C",
    colorLinkActive: "#00AA67",
    colorLinkHover: "#00AA67",
    wireframe: false,
    fontSize: 14,
    borderRadius: 4,
    sizeStep: 4,
    fontFamily: `'Nunito', 'Segoe UI', 'Roboto', 'Oxygen',
    'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
    sans-serif`,
    lineHeight: 1.5714285714285714,
    colorError: "#ED0131"
  },
  ponents: {
    Radio: {
      // orange color
      colorPrimary: "orange"
    },
    Input: {
      controlHeight: 48,
      borderRadius: 4
    },
    Button: {
      controlHeight: 60,
      borderRadius: 10
    },
    Select: {
      controlHeight: 48,
      borderRadius: 4
    }
  }
});

<ConfigProvider theme={theme}>
   <div className="App">
    <h1>Hello CodeSandbox</h1>
    <h2>Start editing to see some magic happen!</h2>
   </div>
   <Form>
    <Form.Item>
      <Input />
    </Form.Item>
    <Form.Item>
     <Radio.Group>
       <Radio value={true}>Yes</Radio>
       <Radio value={false}>No</Radio>
     </Radio.Group>
    </Form.Item>
    <Button type="primary">Send</Button>
    <Button type="ghost">Send</Button>
  </Form>
</ConfigProvider>
发布评论

评论列表(0)

  1. 暂无评论