Usage
Box is a polymorphic component that can render as any HTML element via theas prop. It’s useful for creating flexible layout primitives while maintaining TypeScript type safety.
Examples
Default Div
As Section
As Button
As Link
As Article
Props
The HTML element or React component to render. Can be any valid HTML tag name (e.g.,
'div', 'section', 'button', 'a') or a React component.CSS classes to apply to the element.
The content to render inside the Box.
Ref forwarding is supported with proper TypeScript typing based on the
as prop.All other props are forwarded to the underlying element. The available props depend on the
as prop value and are fully typed.Type Safety
Box provides full TypeScript type safety. When you change theas prop, the available props automatically update to match the target element:
Use Cases
- Creating semantic HTML structure with consistent styling
- Building layout primitives that can adapt to different contexts
- Avoiding unnecessary wrapper divs
- Maintaining type safety in polymorphic components