1.0.8 • Published 1 year ago

@haydenbleasel/use-theme v1.0.8

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

useTheme

useTheme is a tiny, SSR-friendly hook for handling themes with system preference detection. It is designed to work with Tailwind's darkMode: 'class' setting by adding a dark or light class to the html element when the user's system preference is set. It also persists to localStorage so that the user's preference is remembered.

Installation

yarn add @haydenbleasel/use-theme

Usage

import useTheme from '@haydenbleasel/use-theme';

const App = () => {
  const [theme, setTheme] = useTheme();

  return (
    <div>
      <h1>Current theme: {theme}</h1>
      <button onClick={() => setTheme('light')}>Light</button>
      <button onClick={() => setTheme('dark')}>Dark</button>
      <button onClick={() => setTheme(null)}>System Default</button>
    </div>
  );
};
1.0.8

1 year ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago