欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 艺术 > @emotion/css + react+动态主题切换

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

2025/4/2 14:12:33 来源:https://blog.csdn.net/m0_56590688/article/details/146577628  浏览:    关键词:@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;

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词