Theming
Grauity provides a powerful theming system that supports multiple themes (light and dark) with scoped theme switching. The theming system is built on styled-components and provides CSS variables for easy customization.Overview
The theming system consists of two main components:- GrauityThemeProvider: The root theme provider that wraps your entire application
- NSThemeScope: A component for applying different themes to specific sections of your app
All Grauity components automatically adapt to the active theme using CSS variables and theme context.
GrauityThemeProvider
TheGrauityThemeProvider is the foundation of Grauity’s theming system. It should wrap your root-level element (e.g., in your App component) to enable theming throughout your application.
Basic Usage
Props
Configuration object containing theme definitions for light and dark themes.
The initial theme to apply at the root level.
Your application content.
Theme Configuration
Grauity comes with pre-defined light and dark theme configurations that you can use or customize.Default Theme Objects
- Light Theme
- Dark Theme
The
NS_LIGHT_THEME_CONFIG constant (exported from ui/themes/lightThemeConstants.ts:3) defines all light theme values:Custom Theme Configuration
You can provide your own theme configuration by passing a customthemeConfig prop:
Scoped Theming with NSThemeScope
TheNSThemeScope component (defined in ui/elements/ThemeScope/ThemeScope.tsx:21) allows you to apply different themes to specific sections of your application without affecting the global theme.
Basic Usage
Props
Explicitly set the theme for this scope.
Invert the current theme (light becomes dark, dark becomes light).
The HTML element or component to render as.
Additional CSS classes to apply.
Content to render within the themed scope.
NSThemeScope provides a
scopedTheme prop in styled-components that you can access via props.theme.scopedTheme to detect the locally scoped theme.Legacy Theme Classes
Grauity also supports legacy theme classes that can be applied directly to the body element or any container:.grauity-theme-light- Applies light theme.grauity-theme-dark- Applies dark theme
ThemeContext (see ui/themes/ThemeContext.tsx:106) and applied to document.body when using the NSThemeWrapper component.
System Preference Detection
TheNSThemeWrapper component can automatically detect and respond to the user’s system color scheme preference:
usePreferredColorScheme is enabled, Grauity listens to the prefers-color-scheme media query and automatically switches themes when the system preference changes.
Theme Context and Hooks
For advanced use cases, you can access theme context directly:useThemeScope hook:
Best Practices
Use Theme Tokens
Always use CSS variables and theme tokens instead of hardcoded colors for automatic theme switching.
Scope Themes Sparingly
Use NSThemeScope only when necessary. Most apps should use a single global theme.
Test Both Themes
Always test your components in both light and dark themes to ensure proper contrast and readability.
Respect User Preference
Consider using
usePreferredColorScheme to respect users’ system-level theme preferences.Related
- Design Tokens - Learn about the CSS variables used in themes
- Colors - View all available color tokens
- NSThemeScope - Advanced scoped theming component