Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/marcus-rise/react-theme

react theme toggle hook
https://github.com/marcus-rise/react-theme

hook react theme

Last synced: 25 days ago
JSON representation

react theme toggle hook

Awesome Lists containing this project

README

        

# React theme

Handle system color scheme and user preferences.

https://react-theme.marcus-rise.dev

## Install

using `npm`

```bash
npm install @marcus-rise/react-theme
```

or using `yarn`

```bash
yarn add @marcus-rise/react-theme
```

## Usage

First of all, we need to initialize `ThemeProvider` context, and after this we can get access with
hook `useTheme`.

```tsx
import {ThemeProvider, useTheme} from "@marcus-rise/react-theme";

const ThemeToggle = () => {
const {isDarkTheme, preferences, toggleTheme} = useTheme();

return (
<>
toggle


preferences: {preferences ?? "system default"}


isDarkTheme: {isDarkTheme ? "yes" : "no"}
>
);
};

const App = () => (



);

export {App};
```

## API

### Context provider `ThemeProvider`

To set custom localStorage key set `preferencesStorageKey` property for user preferences

```tsx

```

To save user preferences in cookies, provide `cookiesKey` prop as a cookie key string

```tsx

```

### Hook `useTheme`

- `isDarkTheme` is a`boolean` what color scheme is selected, basing on user preferences and system
settings
- `preferences` is a `string` form `enum`

```ts
enum ThemePreference {
DARK = "dark",
LIGHT = "light",
}
```

- `toggleTheme` toggle theme from system default to light, from light to dark and from dark to
system default
- `setTheme` set theme (dark or light)
- `resetThemeToSystem` set theme to system default