当前位置: 首页 > news >正文

@emotion/css + react+动态主题切换

1.下载插件

npm install --save @emotion/css

2.创建ThemeContext.tsx

// src/ThemeContext.tsx
import React, { createContext, useContext, useState } from "react";

// 定义主题类型
export type Theme = "light" | "dark";

// 定义主题上下文的类型
interface ThemeContextType {
  theme: Theme;
  toggleTheme: () => void;
}

// 创建主题上下文
export const ThemeContext = createContext<ThemeContextType | undefined>(undefined);

// 创建一个主题提供器组件
export const ThemeProvider: React.FC = ({ children }) => {
  const [theme, setTheme] = useState<Theme>("light"); // 默认主题为 'light'

  const toggleTheme = () => {
    setTheme((prevTheme) => (prevTheme === "light" ? "dark" : "light"));
  };

  return <ThemeContext.Provider value={{ theme, toggleTheme }}>{children}</ThemeContext.Provider>;
};

// 创建一个自定义钩子来方便使用主题上下文
export const useTheme = () => {
  const context = useContext(ThemeContext);
  if (!context) {
    throw new Error("useTheme must be used within a ThemeProvider");
  }
  return context;
};

3.创建themeConfig.ts配置主题颜色文件

//themeConfig.ts
import { Theme } from "./ThemeContext";

export const themes = {
  light: {
    backgroundColor: "#ffffff",
    color: "green",
    primaryColor: "#007bff",
    secondaryColor: "#6c757d",
  },
  dark: {
    backgroundColor: "#333333",
    color: "red",
    primaryColor: "#007bff",
    secondaryColor: "#6c757d",
  },
};

export const getTheme = (theme: Theme) => themes[theme];

4.在app.tsx 挂载


import { ThemeProvider } from "@/theme/ThemeContext";

const App = () => {
  return (
    <ThemeProvider>
      <div>App<div>
    </ThemeProvider>
  );
};

export default observer(App);

6.使用直接写样式方式一

// @live
import { useEffect } from "react";
import { css, cx } from "@emotion/css";
import { useTheme } from "@/theme/ThemeContext";
import { getTheme } from "@/theme/themeConfig";
import "./index.less";
const ThemeBox: any = () => {
  const { theme, toggleTheme } = useTheme();
  const currentTheme = getTheme(theme);

  const styles = css`
    background-color: ${currentTheme.color};
  `;
  return (
    <div className={cx("cockpit-contain", styles)} onClick={toggleTheme}>
    </div>
  );
};

export default ThemeBox;

6.使用类名方式二

// @live
import { useEffect } from "react";
import { css, cx } from "@emotion/css";
import { useTheme } from "@/theme/ThemeContext";
import { getTheme } from "@/theme/themeConfig";
import "./index.less";
const ThemeBox: any = () => {
  const { theme, toggleTheme } = useTheme();
  const currentTheme = getTheme(theme);

  const styles = css`
    .background {
      background-color: ${currentTheme.background};
    }
    .boder-color {
      border-color: ${currentTheme.borderColor};
    }
  `;
  return (
    <div className={cx("cockpit-contain", styles)} onClick={toggleTheme}>
        <div className={"background"}></div>
        <div className={"boder-color"}></div>
    </div>
  );
};

export default ThemeBox;

相关文章:

  • Ubuntu-22.04安装ROS2
  • [GESP202503 C++一级题解]:B4258 : 四舍五入
  • 《一本书讲透Elasticsearch:原理、进阶与工程实践》读书笔记
  • 老外讲解用Delphi 12.3作web
  • Linux搭建NFS服务
  • GPIO输出实验,控制LED灯
  • 地图(死亡细胞)
  • 第六章 数学
  • Dynamic WallPaper-壁纸动态-Mac电脑-4K超高清
  • k8s 基础知识:Service + 负载均衡(下)
  • 【Django】教程-2-前端-目录结构介绍
  • 单细胞簇鉴定
  • ubuntu如何安装conda
  • 记录一次渗透测试/常用命令
  • 华为配置篇-ISIS基础实验
  • BKA-CNN-GRU、CNN-GRU、GRU、CNN四模型多变量时序预测(Matlab)
  • 【机器学习】——模型评估与选择
  • 【大模型基础_毛玉仁】4.5 实践与应用--参数高效微调PEFT
  • 6、进程理论和简单进程创建
  • WMS系统功能设计和源码实现(Java开发)
  • 淘宝联盟优惠券网站建设/关键词检索
  • 网站建设网络推广方案/企业管理培训免费课程
  • 泊头网站制作/sem优化软件选哪家
  • 济南建站公司模板/市场营销八大营销模式
  • 工作室 网站 备案/友谊平台
  • 黄平网站制作/成都网站优化排名推广