Spacer
A utility component for adding consistent spacing between elements.
Spacer
The Spacer component provides a simple way to add consistent spacing between UI elements. Instead of using margin or padding on components directly, you can use Spacer to create predictable spacing in your layouts.
Features
- Orientation Options: Horizontal or vertical spacing
- Customizable Size: Control the exact amount of spacing
- Simple API: Easy to use with minimal props
- Styling Control: Accepts additional Tailwind classes and style objects
- Layout Flexibility: Can be used in any container component
Import
Props
Prop | Type | Default | Description |
---|---|---|---|
size | number | 4 | Size of the spacer in points |
orientation | 'horizontal' | 'vertical' | 'vertical' | Direction of the spacing |
className | string | '' | Additional Tailwind classes |
style | ViewStyle | — | Additional style object |
Usage Examples
Basic Vertical Spacing
Horizontal Spacing
Multiple Spacers for Consistent Layout
Custom Sized Spacers
Form Layout Example
Card Layout Example
Best Practices
Consistent Spacing System
Create a consistent spacing system throughout your app:
Compose with Layout Components
Use Spacer with other layout components for more complex designs:
Choose Appropriate Tools
Choose the right spacing tool for the right job:
Implementation Details
The Spacer component uses React Native's View
component with a specified width or height based on the orientation. For vertical spacers, it creates empty vertical space with the specified height. For horizontal spacers, it creates empty horizontal space with the specified width.
The component is intended to be placed between UI elements to create consistent spacing without adding margin or padding directly to the elements themselves.
Notes
- Spacer is primarily used for creating "gaps" between elements
- The component doesn't render any visible content, just empty space
- For consistent spacing between multiple items in a row or column, consider using the Stack component instead
- The Spacer size is measured in logical pixels rather than in a responsive unit
- Using Spacer can help create more readable layout code compared to specifying margins directly