如何拿到一个预设算法下的主题变量
#42602
Replies: 2 comments 1 reply
-
@pcyanglei |
Beta Was this translation helpful? Give feedback.
0 replies
-
你可以通过 import { GlobalToken, theme } from 'antd'
import { createTheme } from '@ant-design/cssinjs'
const darkToken: GlobalToken = createTheme(theme.darkAlgorithm).getDerivativeToken(theme.defaultSeed); // 得到 darkToken 完整示例代码如下: import * as React from "react";
import { ConfigProvider, GlobalToken, theme } from 'antd'
import { createTheme } from '@ant-design/cssinjs'
const darkToken: GlobalToken = createTheme(theme.darkAlgorithm).getDerivativeToken(theme.defaultSeed);
function Block() {
const { token } = theme.useToken(); // 消费上下文 token
return (
<>
<div style={{
width: 600,
// backgroundColor: 'red',
backgroundColor: token.colorBgContainer,
color: token.colorText,
}}>
<code>current: colorBgContainer: {token.colorBgContainer}</code>
<br />
<code>current: colorText: {token.colorText}</code>
<hr />
<code>dark: colorBgContainer: {darkToken.colorBgContainer}</code>
<br />
<code>dark: colorText: {darkToken.colorText}</code>
</div>
</>
);
}
function App() {
const [isDark, setIsDark] = React.useState(false)
function toggleTheme() {
setIsDark(!isDark)
}
return (
<ConfigProvider theme={{
algorithm: isDark ? theme.darkAlgorithm : theme.defaultAlgorithm
}}>
<button type="button" onClick={toggleTheme}>
{isDark ? 'dark' : 'light'}
</button>
<Block />
</ConfigProvider>
)
}
export default App |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
主题设置为暗黑模式:
当我使用 Design Token进行消费时
此时header的背景色是默认light模式算法下的背景色,而我想要得到当前设置的dark模式算法下的背景色
Beta Was this translation helpful? Give feedback.
All reactions