react + antd实现动态切换主题功能(适用于antd5.x版本)

news/2024/7/15 17:49:10 标签: react.js, 前端, antd

前言

在之前的几篇文章中(React + Antd实现动态切换主题功能之二(默认主题与暗黑色主题切换)、React + Antd实现动态切换主题功能)介绍了antd实现动态切换主题功能,文章里介绍的方法在antd5.x版本却不适用,因为and5.x版本弃用了less,改用了css-in-js方案(https://ant-design.antgroup.com/docs/react/migration-v5-cn#技术调整)

antd5x_2">antd5.x版本中实现方案

在5.x版本中,如果想实现动态主题切换(默认主题切换与暗色主题),有一种更加简单的方案,就是借助于ConfigProvider(https://ant-design.antgroup.com/components/config-provider-cn)。
参考ConfigProvider的API(ConfigProvider Api),会发现其中有一个theme的属性,再根据介绍中跳转至定制主题,仔细阅读会惊喜的发现以下说明:

通过修改算法可以快速生成风格迥异的主题,5.0 版本中默认提供三套预设算法,分别是默认算法 theme.defaultAlgorithm、暗色算法 theme.darkAlgorithm 和紧凑算法 theme.compactAlgorithm。你可以通过修改 ConfigProvider 中 theme 属性的 algorithm 属性来切换算法。

不难发现介绍中的默认算法 theme.defaultAlgorithm、暗色算法 theme.darkAlgorithm 分别是我们所需要的默认主题和暗色主题。并且官网中也给出了一小段代码,展示了如何设置ConfigProvider.theme属性配置:

import { Button, ConfigProvider, theme } from 'antd';
import React from 'react';

const App: React.FC = () => (
  <ConfigProvider
    theme={{
      algorithm: theme.darkAlgorithm,
    }}
  >
    <Button />
  </ConfigProvider>
);

export default App;

按照上面的实例,我们只需要依样画葫芦,动态修改ConfigProvider.theme属性即可。
代码如下:

import React, { useState } from "react";
import "./index.css";
import {
  Button,
  Calendar,
  Card,
  DatePicker,
  Empty,
  Layout,
  Radio,
  Space,
  ConfigProvider,
  theme
} from "antd";

import type { RadioChangeEvent } from "antd";
import { DownloadOutlined } from "@ant-design/icons";

const App: React.FC = () => {
  const [value, setValue] = useState("default");
  const onChange = (e: RadioChangeEvent) => {
    console.log("radio checked", e.target.value);
    setValue(e.target.value);
  };
  return (
    // default则使用theme.defaultAlgorithm, dark则使用theme.darkAlgorithm
    <ConfigProvider
      theme={{
        algorithm:
          value === "default" ? theme.defaultAlgorithm : theme.darkAlgorithm
      }}
    >
      <Layout>
        <Layout.Content>
          <Radio.Group onChange={onChange} value={value}>
            <Radio value={"default"}>default</Radio>
            <Radio value={"dark"}>dark</Radio>
          </Radio.Group>
          <br />
          <Space>
            <DatePicker />
            <Empty />
            <Card
              title="Default size card"
              extra={<a href="#">More</a>}
              style={{ width: 300 }}
            >
              <p>Card content</p>
              <p>Card content</p>
              <p>Card content</p>
            </Card>
          </Space>
          <br />
          <Space>
            <Radio.Group>
              <Radio.Button value="large">Large</Radio.Button>
              <Radio.Button value="default">Default</Radio.Button>
              <Radio.Button value="small">Small</Radio.Button>
            </Radio.Group>
            <br />
            <br />
            <Button type="primary">Primary</Button>
            <Button>Default</Button>
            <Button type="dashed">Dashed</Button>
            <br />
            <Button type="link">Link</Button>
            <br />
            <Button type="primary" icon={<DownloadOutlined />} />
            <Button type="primary" shape="circle" icon={<DownloadOutlined />} />
            <Button type="primary" shape="round" icon={<DownloadOutlined />} />
            <Button type="primary" shape="round" icon={<DownloadOutlined />}>
              Download
            </Button>
            <Button type="primary" icon={<DownloadOutlined />}>
              Download
            </Button>
          </Space>
          <br />
          <Space>
            <Calendar fullscreen={false} />
          </Space>
        </Layout.Content>
      </Layout>
    </ConfigProvider>
  );
};

export default App;

最终实现效果如下:
默认主题和暗黑主题切换效果

写在最后

至此antd5.x版本下默认主题与暗黑主题动态切换效果已经大功告成,具体实现代码可以移步至codesandbox:https://codesandbox.io/s/antd5-x-change-style-87pldy?file=/demo.tsx
有想法的朋友欢迎在评论区进行交流,也欢迎大家点赞、收藏。


http://www.niftyadmin.cn/n/425134.html

相关文章

属性和方法

类的属性 变量&#xff1a;1&#xff0c;按照数据类型来分基本数据类型&#xff0c;引用数据类型 2&#xff0c;按照变量在类中声明的位置不同&#xff1a;成员变量&#xff08;属性&#xff09;、局部属性&#xff08;方法内&#xff0c;构造器内&#xff0c;代码块内等&…

私有云和公有云是什么?有什么区别?

作者&#xff1a;Insist-- 个人主页&#xff1a;insist--个人主页 作者会持续更新网络知识和python基础知识&#xff0c;期待你的关注 目录 一、私有云和公有云是什么&#xff1f; 1、私有云是什么&#xff1f; 2、公有云是什么&#xff1f; 二、举个例子 1、私有云 2、公…

u-view2.0 的placeholder-style设置失效, 解决办法

设置placeholder-style属性发现怎么设置都无效&#xff0c;此时更有趣的事情发生了&#xff1b;&#xff08;使用了uview-ui v1&#xff0c;情况都一样&#xff09; 查了一些资料&#xff0c;原因是因为动态创建元素的原因&#xff0c; 解决办法&#xff1a;用v-if去控制显隐…

GitHub 下载某个程序的特定版本(代码)

git clone 下载源码 git tag 列出所有版本号 git checkout 某版本号  你当前文件夹下的源码会变成这个版本号的源码。 但可能遇到错误&#xff1a; error: Your local changes to the following files would be overwritten by checkout : xxxx  Please commit your ch…

AutoCV第十课:3D基础

3D基础 前言 手写 AI 推出的全新保姆级从零手写自动驾驶 CV 课程&#xff0c;链接。记录下个人学习笔记&#xff0c;仅供自己参考。 本次课程我们来学习下 nuScenes 数据集的可视化。 课程大纲可看下面的思维导图。 1. nuScenes数据集 明确下我们本次学习的目的&#xff1a;将…

CentOS添加永久路由方式

CentOS添加永久路由方式 1. CentOS7添加永久路由2. CentOS8添加永久路由 1. CentOS7添加永久路由 ● 方式一&#xff1a;   执行以下命令打开路由配置文件进行编辑&#xff1a; vi /etc/sysconfig/network-scripts/route-<interface>这里的<interface>是指您要添…

一些原理图设计最佳实践

要画出清晰、可读性好和整洁的电路原理图&#xff0c;应该遵守以下一般规范&#xff1a; 使用专业的绘图软件&#xff1a;使用专业的电路设计软件&#xff0c;如KiCad、Eagle、Altium Designer、OrCAD等&#xff0c;这些软件提供了丰富的元件库和绘图工具&#xff0c;可以轻松创…

ATTCK v13版本战术介绍——凭证访问(二)

一、引言 在前几期文章中我们介绍了ATT&CK中侦察、资源开发、初始访问、执行、持久化、提权、防御规避战术&#xff0c;本期我们为大家介绍ATT&CK 14项战术中凭证访问战术第7-12种子技术&#xff0c;后续会介绍凭证访问其他子技术&#xff0c;敬请关注。 二、ATT&…