Theming
Learn how to switch between themes and customize colors, typography, spacing, and more using Unistyles.
The theming system in CraftReactNative is built on Unistyles, providing a centralized approach to styling with support for multiple theme variants and dynamic theme switching.
Available themes
CraftReactNative comes with 4 pre-configured themes:
- lightTeal - Light mode with teal color scheme
- darkTeal - Dark mode with teal color scheme
- lightOrange - Light mode with orange color scheme
- darkOrange - Dark mode with orange color scheme
The theme configuration is defined in the craftrn-ui/themes folder and integrated with Unistyles for seamless access across your entire application.
What themes can customize
Themes in CraftReactNative aren't limited to colors. Each theme can customize:
- Colors: Background, text, and interactive element colors
- Typography: Font sizes, line heights, and font weights for all text variants
- Spacing: Padding, margins, and gaps between elements
- Border Radius: Corner rounding for cards, buttons, and other elements
This makes themes powerful for accessibility and personalization. For example, you could create:
- Large Text Theme: Increased font sizes for better readability
- Bold Theme: Heavier font weights for users who prefer stronger text
- Compact Theme: Reduced spacing for more content density
- High Contrast Theme: Stronger color contrasts for visual accessibility
Initial setup
During installation, the Unistyles configuration is set up automatically. The unistyles.ts file configures all available themes:
Make sure to import this file in your app's entry point:
Switching themes
Using UnistylesRuntime
The UnistylesRuntime provides methods to programmatically switch themes:
Example: Theme toggle component
Here's a complete example of a theme switcher component using the recommended StyleSheet.create approach:
Listening to theme changes
Recommended: When using StyleSheet.create, styles automatically update when the theme changes without causing component re-renders. This is the most performant approach:
Note: If you need to access the theme name or other runtime values, use UnistylesRuntime.themeName directly instead of the useUnistyles hook to avoid unnecessary re-renders.
System theme detection
You can configure Unistyles to automatically follow the system theme:
Or use Unistyles' built-in adaptive theme feature:
Customizing themes
To create your own theme variant, extend the theme configuration:
- Create a new theme object in
craftrn-ui/themes/config.ts - Add it to the Unistyles configuration in
craftrn-ui/themes/unistyles.ts - Update the TypeScript types to include your new theme
Example: Custom color theme
Example: Large text theme for accessibility
Create a theme with larger text for better readability:
Example: Bold theme
Create a theme with bolder text for users who prefer stronger typography:
Example: Compact theme
Create a theme with reduced spacing for more content density:
Best practices
- Use semantic tokens: Always use theme tokens (e.g.,
theme.colors.interactivePrimary,theme.textVariants.body1) instead of hardcoded values - Test all themes: Ensure your components work correctly in all theme variants
- Persist user preference: Save the user's theme choice and restore it on app launch
- Respect system settings: Consider using adaptive themes to match user's system preferences
- Consider accessibility: Offer themes with larger text or bolder fonts for users who need them
- Maintain consistency: When creating custom themes, keep the same semantic structure as the base themes
- Smooth transitions: Unistyles handles theme transitions automatically, but you can add custom animations if needed
Accessibility
All themes in CraftReactNative are designed with accessibility in mind. Color contrasts are designed following WCAG guidelines, aiming to ensure that text remains readable and interactive elements are clearly distinguishable across all theme variants and modes.
Beyond colors, themes can be customized for additional accessibility needs:
- Larger text sizes for users with visual impairments
- Bolder font weights for improved readability
- Increased spacing for easier touch targets
- Reduced motion through theme-controlled animation preferences
Consider offering accessibility-focused theme variants alongside your standard themes to make your app usable by a wider audience.