1.2.6 • Published 1 year ago

react-hook-theme v1.2.6

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

react-hook-theme

Switch between dark and light mode in your React application with ease.

Toggle

Features

  • Provides the theme context to components
  • Hook to retrieve and change the current theme
  • Optional toggle to switch between dark and light mode
  • Automatic detection of users' browser settings
  • Optionally persists the selected theme to local storage
  • Ready for Next.js

Example

Check out the CodeSandbox for a working example.

Another example is also included in the example folder of the repository.

Installation

yarn add react-hook-theme

# or

npm install react-hook-theme

Basic usage

Provider

Wrap the application in the ThemeProvider. Optionally provide settings via the options prop.

/*
 * index.tsx
 */

import { ThemeProvider } from 'react-hook-theme';

// ...
<ThemeProvider
    options={{
        theme: 'dark',
        save: true,
    }}
>
    <App />
</ThemeProvider>;
// ...

Options Prop

NameTypeRequiredDescriptionDefault
themeThemeThe default theme as a fallbackdark
savebooleanSave theme to local storage when changedfalse

Styling of Dark / Light Mode

Adjust the styling of your app by utilizing css variables:

/*
 * style.css
 */

:root {
    --background: #fff;
    --primary: #000;
}

[data-theme='light'] {
    --background: #fff;
    --primary: #000;
}

[data-theme='dark'] {
    --background: #282c34;
    --primary: #fff;
}

// ...

body {
    background-color: var(--background);
    color: var(--primary);
}

Toggle

Optionally use the toggle component to render a switch to change between dark and light mode.

/*
 * App.tsx
 */

import { Toggle } from 'react-hook-theme';

// styling
import 'react-hook-theme/dist/styles/style.css';

// ...
<header>
    <Toggle />
</header>;

Props

NameTypeRequiredDescriptionDefault
idstringSets the html id of the input elementrht-toggle

Hooks

Use the provided useTheme hook to access or update the current theme

/*
 * App.tsx
 */

import { useTheme } from 'react-hook-theme';

// ...
const { theme, setTheme } = useTheme();

Return

Object NameTypeDescription
themeThemeThe current theme
setTheme(theme: Theme) => voidUpdate the current theme
optionsThemeOptionsConfiguration of hook
1.2.6

1 year ago

1.2.5

1 year ago

1.2.4

1 year ago

1.2.3

1 year ago

1.2.2

1 year ago

1.2.1

1 year ago

1.2.0

1 year ago

1.1.0

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago