IconContext API, allowing you to set default styles for all icons within a component tree.
IconContext Provider
TheIconContext.Provider lets you configure default props for all icons in its subtree.
Basic Usage
Available Context Options
TheIconContext interface supports the following properties:
Global Styling Patterns
- Color Theme
- Size Standardization
- Class-Based Styling
Set a consistent color scheme:
Inline Global Styling
Use thestyle property in IconContext for inline CSS:
Nesting Contexts
You can nest multipleIconContext.Provider components to create hierarchical styles:
Overriding Context Styles
Individual icon props always override context values:1
Context sets defaults
The
IconContext.Provider establishes base styles2
Icons inherit styles
All icons within the provider inherit these defaults
3
Props override context
Individual icon props take precedence over context values
CSS Class Merging
When both context and individual icons haveclassName, they are merged:
Vertical Alignment Fix
From version 3+,
vertical-align: middle is not applied by default. Use IconContext to add it globally:Advanced: Dynamic Theming
Create theme-aware icon styling:SVG Attributes via Context
Set default SVG attributes for all icons:Best Practices
Use IconContext for consistency
Use IconContext for consistency
Apply
IconContext.Provider at the root or layout level to ensure consistent icon styling across your application.Prefer className over inline styles
Prefer className over inline styles
Using
className with CSS files provides better performance and maintainability than inline styles.Document your icon conventions
Document your icon conventions
Establish and document icon sizing and color conventions for your team to maintain visual consistency.
Test context boundaries
Test context boundaries
Be mindful of where your
IconContext.Provider boundaries are, especially in component libraries or shared components.Next Steps
Customizing Icons
Learn about individual icon customization
Performance
Optimize bundle size and rendering performance