1 Star 0 Fork 0

KwooShung / React-Themes

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

@kwooshung/react-themes

超方便的主题管理组件,可自行设置主题颜色,可任意增加自定义主题,可自行设计模板,所有数据主题共享同一个状态,支持本地存储设置,可根据系统判断浅色调或暗色调。

GitHub License GitHub Release Date - Published_At GitHub last commit GitHub code size in bytes GitHub top language GitHub pull requests GitHub issues Github Stars NPM Version Npm.js Downloads/Week Github CI/CD codecov Maintainability Gitee Repo

English | 中文

为什么开发它?

  • 现如今的网站,要是没有个 “更换主题” 的功能,能叫现代化网站?最次也得有个 “暗黑模式” 吧?

解决了什么痛点?

  • 每次开发一个网站,都要自己写一套主题管理,都非常麻烦;
  • 为什么不用其他第三方组件?
    • 与其自家的UI组件库高度耦合;
    • 功能太过于复杂,不够轻量;
    • 轻量的组件库,功能相对过于简单;

为什么使用它?

  • 无头,没有样式,可自定义主题切换按钮或列表;
  • 支持自定义主题颜色,默认支持 'light''dark' 两种主题;
  • 使用方便,操作简单,学习成本低,灵活性高;
  • 状态共享,所有数据主题共享同一个状态;
  • ES6 的现代特性实现;
  • TypeScript 编写,类型安全;
  • 可按需引入,esm 模块化,天生支持 树摇(tree-shaking),不用担心打包后的体积;
  • 当然本项目也提供了 commonjs 规范的 cjs 版本;
  • 测试覆盖率 100%

在线演示

安装

npm

npm install @kwooshung/react-themes

yarn

yarn add @kwooshung/react-themes

pnpm

pnpm add @kwooshung/react-themes

使用方法

ThemesProvider 包裹的组件,都可以通过 Themes 获取到主题统一的状态。

ThemesProvider

import { ThemesProvider } from '@kwooshung/react-themes';
import Switch from './Switch';

/**
 * @zh 主题列表
 * @en Theme list
 */
const ThemeList = ['light', 'dark', 'blue', 'green'];

/**
 * @zh 全局布局
 * @en Global layout
 */
const Layout = () => {
  return (
    <ThemesProvider def='auto' list={ThemeList}>
      {/* <OtherComponents /> */}
      <Switch />
      {/* <OtherComponents /> */}
    </ThemesProvider>
  );
};

export default App;

Themes

import { IThemesContext, Themes } from '@kwooshung/react-themes';

const Demo = () => {
  const themes = (context: IThemesContext): ReactNode => {
    return (
      //在这里写你的主题切换按钮或列表,通过 `context` 可以获取到主题状态;
    );
  };

  return <Themes>{themes}</Themes>;
};

export default Switch;

API

接口类型定义,也可阅览 interfaces.d.ts 源文件;

IThemesProps

属性 类型 默认值 描述
children (themeContext: IThemesContext) => ReactNode - 子组件渲染函数

IThemesProviderProps

属性 类型 默认值 描述
def string auto 默认主题值,默认 auto
list string[] - 主题列表

IThemesContext

属性 类型 默认值 描述
value string auto 当前主题值,默认 auto
name string auto 当前主题名,除了 auto 以外,和 value 一样,如果 value = auto,若系统是暗色调,则 name=dark
setTheme (theme: string) => void - 主题列表
addThemes (themes: string string[]) => void 添加主题
getValueTheme () => string - 获取当前主题值
getNameTheme () => string - 获取当前主题名
getAvailableThemes () => string[] - 获取可用主题列表
MIT License Copyright (c) 2024 KwooShung Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

超方便的主题管理组件,可自行设置主题颜色,可任意增加自定义主题,可自行设计模板,所有数据主题共享同一个状态,支持本地存储设置,可根据系统判断浅色调或暗色调。 展开 收起
JavaScript 等 5 种语言
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/kwooshung/React-Themes.git
git@gitee.com:kwooshung/React-Themes.git
kwooshung
React-Themes
React-Themes
main

搜索帮助

53164aa7 5694891 3bd8fe86 5694891