--- name: naive-ui-theming description: Customize and apply themes in Naive UI including dark mode, theme variables, and creating themed components metadata: author: jiaiyan version: 1.0.0 --- # Naive UI Theming Learn how to customize themes, apply dark mode, and create themed components in Naive UI. ## When to Use Use this skill when you need to: - Implement dark/light mode switching - Customize theme colors and variables - Create themed components that respond to theme changes - Access theme variables in your components ## Prerequisites - Basic understanding of Naive UI setup - Vue 3 Composition API knowledge - CSS custom properties (CSS variables) understanding ## Basic Usage ### Theme Switching Use `n-config-provider` to control the theme of all descendant components: ```vue ``` ### Using n-element for Theme-Aware Styling The `n-element` component allows you to apply theme-aware styles using CSS variables: ```vue ``` ### Using Theme Variables with useThemeVars Access theme variables programmatically using the `useThemeVars` composable: ```vue ``` ## API Reference ### n-config-provider Theme Props | Property | Type | Default | Description | |----------|------|---------|-------------| | `theme` | `object \| null` | `null` | Theme object (use `darkTheme` for dark mode, `null` for light) | | `theme-overrides` | `object` | - | Custom theme overrides | ### Available Theme Variables Common theme variables accessible via `useThemeVars()`: | Variable | Description | |----------|-------------| | `primaryColor` | Primary color | | `primaryColorHover` | Primary color on hover | | `primaryColorPressed` | Primary color when pressed | | `successColor` | Success state color | | `warningColor` | Warning state color | | `errorColor` | Error state color | | `infoColor` | Info state color | | `textColorBase` | Base text color | | `textColor1` | Primary text color | | `textColor2` | Secondary text color | | `textColor3` | Tertiary text color | | `borderColor` | Border color | | `borderRadius` | Border radius | ### n-element Props | Property | Type | Default | Description | |----------|------|---------|-------------| | `tag` | `string` | `'div'` | HTML tag to render | ## Common Patterns ### Persistent Theme with Local Storage ```vue ``` ### Custom Theme Overrides ```vue ``` ### Theme-Aware Component ```vue ``` ## Best Practices 1. **Use CSS Variables**: Leverage CSS custom properties for smooth theme transitions 2. **Wrap at Root Level**: Place `n-config-provider` at the top of your component tree 3. **Persist User Preference**: Store theme preference in localStorage for better UX 4. **Test Both Themes**: Always test your components in both light and dark modes 5. **Use Transitions**: Add CSS transitions for smooth theme switching animations 6. **Override Selectively**: Only override the theme variables you need to customize ## CSS Variable Reference Naive UI provides numerous CSS variables that automatically update with theme changes: ```css /* Common theme variables */ --primary-color --primary-color-hover --primary-color-pressed --success-color --warning-color --error-color --info-color /* Text colors */ --text-color-base --text-color-1 --text-color-2 --text-color-3 /* Background colors */ --body-color --card-color --modal-color /* Border */ --border-color --border-radius /* Animation */ --cubic-bezier-ease-in-out --cubic-bezier-ease-out --cubic-bezier-ease-in ``` Use these variables in your styles for consistent theming: ```vue ```