Box Component
The Box component is a wrapper component for most CSS utility needs. It serves as a generic container with access to all style functions from the system.Import
Implementation
FromBox/Box.js:7-10:
Type Definition
FromBox/Box.d.ts:53-60:
Basic Usage
Component Prop
Render as any HTML element or React component:Box.test.js:49-58:
System Props
Box accepts all system style props directly:Available System Props
FromBox.d.ts:26-40, Box supports all these style function groups:
The sx Prop
Use thesx prop for more complex styles:
Responsive Values
Array Syntax
Object Syntax
Combining Props and sx
System props and thesx prop can be combined. From Box.test.js:264-272:
sx prop takes precedence:
Layout Examples
Flexbox Container
Grid Layout
Centered Content
Card Layout
Border Styling
FromBox.test.js:60-98, borders can be styled with proper precedence:
Prop Forwarding
Box does not forward style props to the DOM. FromBox.test.js:32-47:
CSS Class
Box adds a utility class for styling. FromBox.test.js:274-278:
TypeScript
Basic Usage
With Custom Component
With System Props
Common Patterns
Section Container
Absolute Positioning
Full Height Sidebar
Responsive Padding
Performance
Box is optimized for performance:- Props are not rendered as DOM attributes
- Styles are computed once and memoized
- CSS is generated efficiently with the styled engine
Best Practices
- Use for layout - Box is ideal for layout and spacing
- Combine props - Mix system props and sx for flexibility
- Semantic HTML - Use the
componentprop for semantic elements - Avoid overuse - For repeated components, use
styled() - Type your components - Use TypeScript for better DX