Theme Implementation
Practical guide for implementing the theme system in components, including provider setup and hooks.
Theme Implementation Guide
This guide provides detailed implementation guidance for the application's theme system. It covers setup, management, and best practices for theme implementation.
Theming Quickstart
Need to get up and running quickly? Here's the minimal code you need to implement theming.
Overview
This guide provides practical implementation details for the theme system in our React Native application. It covers theme definition, provider setup, typings, and component integration patterns. The focus is on code examples and best practices for day-to-day theme usage.
Quick Start
Add the ThemeProvider to your application root:
Purpose & Scope
This document provides implementation guidance for:
- Setting up the theme context and provider
- Creating and organizing theme files
- Using themes in React Native components
- Creating responsive and adaptive theming
- Performance optimization techniques
This guide is intended for developers who need to work with the theme system in day-to-day development.
Platform-Specific Theme Customization
Mobile-Specific Theming
React Native applications often need platform-specific styling adjustments. Here's how to implement effective mobile theme customization:
For better maintainability, use a dedicated helper for platform-specific values:
Safe Area Handling
Mobile devices have notches, home indicators and other UI elements that require content padding. Use the theme system to handle these consistently:
Customizing themes based on platform is crucial for meeting platform-specific design guidelines while maintaining a cohesive experience.
Performance Considerations
Performance Benchmarks
In our testing, theme switching should meet the following performance benchmarks:
| Operation | Target Duration | Notes |
|---|---|---|
| Initial theme load | < 50ms | From cached source |
| Remote theme load | < 250ms | First load with caching |
| Theme switch | < 100ms | Including UI updates |
| Dynamic color access | < 1ms | Single theme property |
These benchmarks provide targets to maintain a responsive user experience. If your implementation exceeds these times, consider the optimization strategies below.
Optimization Strategies
-
Memoize theme-dependent values
-
Pre-calculate derived values
Theme management performance is critical for maintaining a responsive user experience, especially during theme switching.
Prerequisites
Before implementing themes, ensure you are familiar with:
- Theme Management - Core concepts and architecture
- React Context API for state management
- React Native's StyleSheet API
- TypeScript type definitions
Implementation Details
Theme Definition
Themes are defined in core/shared/styles/theme.ts with TypeScript typings:
Typography Definition
Typography styles are defined in a separate file to maintain clear organization:
Theme Provider Implementation
The ThemeProvider component leverages React Context to provide theme values to the component tree:
Using Themes in Components
Components can consume the theme through the provided hooks:
Usage Patterns
Responsive Theme Utilities
Create responsive styling utilities to adapt to different screen sizes:
Theme Switcher Component
Create a component to allow users to switch between themes:
Best Practices & Security
Performance Optimization
-
(Do ✅) Memoize theme values
-
(Do ✅) Use StyleSheet.create for static styles
-
(Don't ❌) Create style objects in render
Troubleshooting
Common Issues and Solutions
| Issue | Possible Cause | Solution |
|---|---|---|
| Theme not applying to components | Component not wrapped in ThemeProvider | Ensure the component is a descendant of ThemeProvider |
| Error: "useTheme must be used within a ThemeProvider" | Using useTheme outside provider scope | Check component hierarchy or add ThemeProvider higher in the tree |
| Inconsistent colors across app | Multiple theme definitions | Ensure all theme values come from the central theme system |
| Theme not updating on mode change | Missing dependencies in useMemo | Add all theme dependencies to dependency arrays |
| Slow performance with themes | Creating style objects in render | Move style creation outside render or use StyleSheet.create |
Migration Considerations
When migrating from hardcoded styles to the theme system:
-
Audit existing styles
- Identify all hardcoded colors, spacing values, and text styles
- Map them to corresponding theme values
-
Incremental migration approach
- Start with core components used throughout the app
- Work outward to more specialized components
- Update one component family at a time
-
Unit test coverage
- Add tests for theme-dependent rendering
- Verify components render correctly in both light and dark modes
-
Design system alignment
- Ensure the theme values match the design system specifications
- Create a theme validator utility if needed
Related Documents
Core References
- Theme Management - Core concepts and architecture overview
- White-Label Themes - Brand-specific theming
Architecture Documents
- App Initialization - Learn how themes integrate with app startup
- Core Architecture - Overall system design
- Project Structure - Project organization including theme files
UI Components
- component-patterns - Best practices for themed components
- Assets & Media Overview - Working with themed assets
Theme Architecture
Reference document explaining core theming concepts and system design.
White-Label Implementation
Guide for implementing brand-specific theming.
Summary
Implementing the theme system in your components involves:
- Setting up the ThemeProvider at the application root
- Using the useTheme hook to access theme values in components
- Creating well-structured, maintainable styles that derive from theme values
- Optimizing performance by avoiding inline style creation
- Supporting accessibility with proper contrast and text scaling
By following these patterns consistently, you ensure a cohesive visual experience across the application and make it easier to implement brand-specific theming.