Overview
TheIconContext is a React Context that allows you to configure default props for all icons in a component subtree. This is useful for setting consistent styling, sizing, or other attributes across multiple icons without repeating props on each icon component.
Usage
Basic Example
color and size values unless overridden by individual icon props.
Nested Contexts
Overriding Context Values
Configuration Options
The context value accepts an object with the following properties:Default color for all icons in the context. Accepts any valid CSS color value (hex, rgb, named colors, etc.).
Default size for all icons in the context. Can be any valid CSS size unit (px, em, rem, etc.).
CSS class name(s) to apply to all icons in the context. These are merged with any className props on individual icons.
Inline styles to apply to all icons in the context. These are merged with any style props on individual icons, with icon-specific styles taking precedence.
Additional SVG attributes to apply to all icons in the context. Useful for setting viewBox, preserveAspectRatio, or other SVG-specific attributes.
Default Context
When noIconContext.Provider is used, icons use the default context values:
Advanced Patterns
Theme-Based Icon Styling
Responsive Icon Sizing
Consistent Animation
Implementation Details
- The context is a standard React Context created with
React.createContext() - Icons consume the context using
IconContext.Consumer - Context values are merged with icon props, with icon props taking precedence
- The context is optional; icons work without any provider
- Multiple providers can be nested, with inner providers overriding outer ones
Source Reference
For implementation details, see:iconContext.tsx:3-9- IconContext interface definitioniconContext.tsx:11-17- DefaultContext valuesiconContext.tsx:19-20- Context creationiconBase.tsx:42-72- Context consumption in IconBase