-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
34d3b21
commit efaaa3d
Showing
3 changed files
with
81 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
packages/theme/README.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# React theme | ||
|
||
Handle system color scheme and user preferences. | ||
|
||
## 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 ( | ||
<> | ||
<button onClick={toggleTheme}>toggle</button> | ||
<br/> | ||
<span>preferences: {preferences}</span> | ||
<br/> | ||
<span>isDarkTheme: {isDarkTheme ? "yes" : "no"}</span> | ||
</> | ||
) | ||
} | ||
|
||
const App = () => ( | ||
<ThemeProvider> | ||
<ThemeToggle/> | ||
</ThemeProvider> | ||
) | ||
``` | ||
|
||
## API | ||
|
||
### Context provider `ThemeProvider` | ||
|
||
To set custom localStorage key set `preferencesStorageKey` property for user preferences | ||
|
||
```tsx | ||
<ThemeProvider preferencesStorageKey={"OPTIONAL_APP_THEME_STORAGE_KEY"}> | ||
``` | ||
|
||
### 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", | ||
SYSTEM = "system", | ||
} | ||
``` | ||
|
||
you can import this enum directly | ||
|
||
```ts | ||
import {ThemeProvider} from "@marcus-rise/react-theme"; | ||
``` | ||
|
||
- `toggleTheme` is a `function`, that toggle preferences from `system` -> `light` -> `dark` | ||
|