antd 黑暗模式 (v5 react)

 不知道为啥 🤷 官网根本没写怎么切换 

1. 开启这个东西,是用configprovide


import { ConfigProvider, theme } from 'antd';


<ConfigProvider
      theme={{
        algorithm: isDark ? theme.darkAlgorithm : theme.defaultAlgorithm,
      }}
    >

2. 然后一些默认的,会自己定义好,没定义的,就需要自己写

观察切换后,模式有了什么改变

好吧,它没改变,这个东西难道要自己写。。,,

 // 正确初始化状态(建议使用布尔值)
  const [isDark, setIsDark] = useState(false);

  // 简洁的切换方法
  const toggleTheme = () => {
    setIsDark((prev) => !prev);
    if (!isDark) {
      document.body.setAttribute('dark-theme', 'dark');
    } else {
      document.body.removeAttribute('dark-theme');
    }
  };

发现body上加了dark-theme,那么就应该以body为区分,做一些事情

body[tomato-theme='dark'] .content-inner{
  background-color: yellow;
}

要么单独定义

要么就用一套通用的,在root里把这件事做了,用的时候只用变量

:root {
  --color-primary-white: green;
}


:root body[dark-theme="dark"]{
    --color-primary-white: yellow;
  }

可以看到,只要使用  

background-color: var(--color-primary-white);

就有效了

参考

https://juejin.cn/post/7301959466399465526

https://juejin.cn/post/7242284648021164091?from=search-suggest

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值