BNA

BNA UI
26

Mode Toggle

An animated button component for switching between light and dark themes.

Installation

pnpm dlx bna-ui add mode-toggle

Usage

import { ModeToggle } from '@/components/ui/mode-toggle';
<ModeToggle />

Examples

Default

API Reference

ModeToggle

An animated button that toggles between light and dark themes.

PropTypeDefaultDescription
styleViewStyle-Additional styles to apply to the button.

The component uses the Button component internally, so it inherits button styling and behavior.

Animation Details

Icon Transition

  • Scale Animation: Icons scale down to 0, change, then scale back to 1
  • Duration: 150ms for each scale phase (300ms total)
  • Icons: Sun for light mode, Moon for dark mode

Sun Rotation

  • Rotation: 180° rotation when switching to light mode
  • Duration: 300ms
  • Effect: Creates a spinning sun rays effect
  • Timing: Only rotates when switching to sun icon

Theme Integration

The ModeToggle requires:

  • useModeToggle hook for theme switching logic
  • Theme context that provides isDark state
  • toggleMode function to change themes

Example theme hook:

const { toggleMode, isDark } = useModeToggle();

Performance

The component is optimized for smooth animations:

  • Uses react-native-reanimated for 60fps animations
  • Animations run on the UI thread
  • Minimal re-renders with useSharedValue
  • Efficient icon switching with runOnJS

Accessibility

The ModeToggle maintains accessibility:

  • Uses semantic button component
  • Screen readers announce theme changes
  • Maintains proper focus behavior
  • Works with keyboard navigation
  • Respects system accessibility settings

Integration Example

// App.tsx
import { ModeToggle } from '@/components/ui/mode-toggle';
import { ThemeProvider } from '@/context/theme';
 
export default function App() {
  return (
    <ThemeProvider>
      <View style={styles.header}>
        <Text>My App</Text>
        <ModeToggle />
      </View>
      {/* Rest of your app */}
    </ThemeProvider>
  );
}

Dependencies

The component requires these utilities to function:

  • useModeToggle: Hook for theme switching logic
  • Button: Base button component
  • Icon: Themed icon wrapper
  • Theme context with isDark and toggleMode