Overview
NSThemeScope is a powerful component that enables you to apply different themes (light or dark) to specific sections of your application. It creates a scoped theme context that can be nested and inherited, allowing for complex theme hierarchies.
Key Features
Theme Isolation
Apply different themes to specific UI sections without affecting the rest of your app
Nesting Support
Nest ThemeScope components to create complex theme hierarchies
Theme Inversion
Automatically invert the parent theme with a simple prop
Custom Components
Render any HTML element or React component as the theme container
Import
Basic Usage
Props
The theme to apply within this scope. If not provided, it will inherit from the parent scope. Takes precedence over the
invert prop.If true, applies the inverse of the parent theme. If the parent is light, this scope becomes dark, and vice versa.
The HTML element (like
'div', 'section') or React component to render as the root of this theme scope. If a React component is used, it must accept a className prop and apply it at the outermost HTML element for the theme to work correctly.Additional CSS class names to apply to the root element.
The content to render within this theme scope.
Advanced Examples
Nested Theme Scopes
Create complex theme hierarchies by nesting multipleNSThemeScope components:
Using Custom Components
Render the theme scope using a custom component or styled element:Alternating Themes
Create an alternating theme pattern using theinvert prop:
useThemeScope Hook
TheuseThemeScope hook allows you to access the current theme context from any child component within a NSThemeScope.
Usage
Return Values
The current scoped theme name.
The CSS class name used for applying the theme (e.g.,
grauity-theme-light or grauity-theme-dark).Styled Components Integration
NSThemeScope automatically provides theme information to styled-components via the theme context. You can access the scoped theme in your styled components:
Theme Inheritance
When
applyTheme is not specified and invert is false, the component inherits the theme from its parent NSThemeScope. If there is no parent, it defaults to 'light'.Use Cases
Modal and Overlay Theming
Modal and Overlay Theming
Apply different themes to modals and overlays that appear above your main content:
Card-Based Layouts
Card-Based Layouts
Create visually distinct cards with alternating themes:
Navigation and Sidebar Theming
Navigation and Sidebar Theming
Best Practices
- Use
applyThemewhen you need explicit control over the theme - Use
invertwhen you want to create contrast relative to the parent - Ensure custom components passed to
asaccept and apply theclassNameprop - Consider using
useThemeScopehook for theme-aware conditional logic