Socket
Socket
Sign inDemoInstall

react-hook-theme

Package Overview
Dependencies
5
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    react-hook-theme

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


Version published
Weekly downloads
134
increased by10.74%
Maintainers
1
Install size
20.6 kB
Created
Weekly downloads
 

Readme

Source

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

Keywords

FAQs

Last updated on 07 Feb 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc