Dark Mode
Add dark mode support to your application with smooth transitions and system preference detection.
The
nach-themes library provides a simple and elegant way to implement dark mode in your React applications, with support for system preferences, smooth transitions, and click animations.Installation
Install the package via pnpm:
npm install install nach-themesQuick Start
1. Configure your CSS
First, ensure you have both light and dark theme variables defined in your CSS:
2. Wrap your app with ThemeProvider
Create a providers component to wrap your application:
Then use it in your root layout:
3. Create a theme toggle button
Features
System Theme Detection
By default, the theme provider respects the user's system preference:
The library automatically detects changes in system preferences and updates the theme accordingly when set to
'system'.Available Themes
The
useTheme hook provides access to three theme modes:'light'- Light mode'dark'- Dark mode'system'- Follows system preference
Smooth View Transitions
The library includes built-in support for the View Transitions API, creating smooth animated transitions between themes. When you pass a click event to
setTheme, it creates a circular reveal animation from the click position:Without the click event, it falls back to a standard cross-fade transition:
Resolved Theme
Get the actual theme being applied, even when set to
'system':Advanced Usage
Theme Toggle with Loading State
Handle the hydration state to prevent layout shifts:
The
useMounted hook can be implemented as:Theme Dropdown
Create a more sophisticated theme selector:
Programmatic Theme Changes
Access theme information anywhere in your app:
TypeScript Support
The library is fully typed. All hooks and components have complete TypeScript definitions:
Found something to improve?
Notice a bug, typo, or missing detail on this page? Help us make the documentation better by opening a GitHub issue.