Simple, lightweight theme management for Preact applications
- 📦 Tiny bundle size (~413B GZipped)
- 🤌 Simple API
- No FOUC (in cases where preact isn't loaded on network)
- Based on @dumbjs/persona
# npm
npm install @preachjs/themes
# yarn
yarn add @preachjs/themes
# pnpm
pnpm add @preachjs/themesimport { ThemeProvider, useTheme } from '@preachjs/themes'
const Main = () => {
return (
<ThemeProvider>
<App />
</ThemeProvider>
)
}
const App = () => {
const { theme, setTheme } = useTheme('system')
return (
<>
<p>Current Theme: {theme}</p>
<button
onClick={() => {
setTheme('light')
}}
>
Light
</button>
<button
onClick={() => {
setTheme('dark')
}}
>
Dark
</button>
<button
onClick={() => {
setTheme('system')
}}
>
System
</button>
</>
)
}The ThemeProvider component accepts the following props:
defaultTheme: The initial theme to use ('light','dark', or'system'). Defaults to'system'.storageKey: The key used to persist theme preference in localStorage. Defaults to'theme'.
<ThemeProvider defaultTheme="dark" storageKey="my-app-theme">
<App />
</ThemeProvider>Three theme options are available:
light: Forces light themedark: Forces dark themesystem: Automatically switches between light and dark based on system preferences
React hook that provides theme management functionality.
Returns:
theme: Current theme value ('light','dark', or'system')setTheme: Function to update the theme
Example:
const { theme, setTheme } = useTheme('system')Context provider component that must wrap your application.
Props:
defaultTheme: Initial theme (optional)storageKey: localStorage key for persistence (optional)children: React nodes
Example:
<ThemeProvider defaultTheme="system">
<App />
</ThemeProvider>For Advanced use cases like multiple themes and different default states, you can write your own context and use @dumbjs/persona that abstracts the browser APIs to make it simpler for you to use.