admin 管理员组文章数量: 1086019
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 badges2 Answers
Reset to default 3You 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>
本文标签: javascriptHow to use antd theme tokens in my custom componentStack Overflow
版权声明:本文标题:javascript - How to use antd theme tokens in my custom component? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744089512a2531765.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论