# React-Themes
**Repository Path**: kwooshung/React-Themes
## Basic Information
- **Project Name**: React-Themes
- **Description**: A super convenient theme management component, you can set the theme color by yourself, add custom themes at will, and design templates by yourself. All data themes share the same state, support local storage settings, and can determine light or dark colors according to the system.
- **Primary Language**: JavaScript
- **License**: MIT
- **Default Branch**: main
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2024-08-21
- **Last Updated**: 2024-08-26
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# @kwooshung/react-themes
> 超方便的主题管理组件,可自行设置主题颜色,可任意增加自定义主题,可自行设计模板,所有数据主题共享同一个状态,支持本地存储设置,可根据系统判断浅色调或暗色调。
[](LICENSE)







[](https://www.npmjs.com/package/@kwooshung/react-themes)
[](https://www.npmjs.com/package/@kwooshung/react-themes)
[](https://github.com/kwooshung/React-Themes/actions/)
[](https://codecov.io/gh/kwooshung/React-Themes)
[](https://codeclimate.com/github/kwooshung/React-Themes/maintainability)
[](https://gitee.com/kwooshung/React-Themes/)
    English | 
    中文
      当前主题: {themeName}
      
        {getAvailableThemes().map((theme) => (
          -  setTheme(theme)}>
            {theme}
          ))}
 
  );
};
export default ThemeSwitcher;
```
# API
接口类型定义,也可阅览 **[interfaces.d.ts](./src/themes/interfaces.d.ts)** 源文件;
## IThemesProviderProps
| 属性         | 类型     | 默认值                      | 描述                                                 |
| ------------ | -------- | --------------------------- | ---------------------------------------------------- |
| defaultValue | string   | `auto`                      | 默认主题值                                           |
| list         | string[] | `['light', 'dark']`         | 主题列表                                             |
| saveKey      | string   | -                           | 可选,保存主题到cookie的键,如果设置则保存           |
| saveExpired  | number   | `365 * 24 * 60 * 60 * 1000` | 可选,保存到cookie的过期时间,单位为毫秒,默认为一年 |
## useThemesContext
`useThemesContext` 是一个 Hook,用于获取当前主题的上下文,必须在 `ThemesProvider` 内部使用。
```typescript
const { themeValue, themeName, setTheme, addTheme, getAvailableThemes } = useThemesContext(); // 返回值为 `IThemesContext` 接口类型
```
## IThemesContext
| Properties         | Type                                 | Default Value | Description                                                                                                                                          |
| ------------------ | ------------------------------------ | ------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------- |
| themeValue         | string                               | `auto`        | Current theme value, defaults to `auto`                                                                                                              |
| themeName          | string                               | `auto`        | Current theme name. Except for `auto`, it is the same as `themeValue`. If `themeValue = auto`, and the system is in dark mode, then `themeName=dark` |
| setTheme           | (theme: string) => void              | -             | Set the current theme                                                                                                                                |
| addTheme           | (themes: string \| string[]) => void | -             | Add a theme                                                                                                                                          |
| getAvailableThemes | () => string[]                       | -             | Get the list of available themes                                                                                                                     |