Theming
ThemeToggle
A component that allows users to switch between light and dark modes.
ThemeToggle
The ThemeToggle
component provides an animated button that allows users to toggle between light and dark themes.
Import
Features
- Smooth animation when toggling between themes
- Automatically displays the correct icon based on current theme
- Can be used in controlled or uncontrolled mode
- Automatically integrates with the app's theme context
Props
Prop | Type | Default | Description |
---|---|---|---|
value | boolean | Current theme state | Optional controlled mode value |
onChange | (value: boolean) => void | Toggle theme | Optional callback for controlled mode |
Basic Usage
Controlled Mode
The component can also be used in controlled mode, where you manage the state yourself:
Implementation Details
The ThemeToggle
component uses React Native's Animated
API to create a smooth transition effect when switching between themes. It displays a sun icon for dark mode and a moon icon for light mode.
Under the hood, it uses the useTheme
hook from the ThemeContext to access and modify the current theme state. When pressed, it animates a scale and rotation effect before changing the icon.