Skip to main content

Overview

The IconContext 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

All icons within the provider will inherit the 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:
string
Default color for all icons in the context. Accepts any valid CSS color value (hex, rgb, named colors, etc.).
string
Default size for all icons in the context. Can be any valid CSS size unit (px, em, rem, etc.).
string
CSS class name(s) to apply to all icons in the context. These are merged with any className props on individual icons.
React.CSSProperties
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.
React.SVGAttributes<SVGElement>
Additional SVG attributes to apply to all icons in the context. Useful for setting viewBox, preserveAspectRatio, or other SVG-specific attributes.

Default Context

When no IconContext.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 definition
  • iconContext.tsx:11-17 - DefaultContext values
  • iconContext.tsx:19-20 - Context creation
  • iconBase.tsx:42-72 - Context consumption in IconBase