View
View is a general purpose container with no specific semantics that can be used for custom styling purposes. It supports Spectrum style props to ensure consistency with other Spectrum components.
install | yarn add @react-spectrum/view |
---|---|
version | 3.1.8 |
usage | import {View} from '@react-spectrum/view' |
Introduction#
The View
component can be used as a general purpose container element, similar to a
div
. It supports Spectrum style props, which enable you to use Spectrum variables for
styling to ensure consistency with other Spectrum components. In addition, Spectrum
variables automatically adapt to color scheme changes (e.g. dark mode), and platform
scale (e.g. touch vs. mouse input). See Styling to learn more.
Example#
The following example shows how you might build a container element using Spectrum defined variables for the border and padding. Use the color scheme and scale pickers to see how the view changes automatically based on these variables.
<View
borderWidth="thin"
borderColor="dark"
borderRadius="medium"
padding="size-250">
<TextField label="Name" />
</View>
<View
borderWidth="thin"
borderColor="dark"
borderRadius="medium"
padding="size-250">
<TextField label="Name" />
</View>
<View
borderWidth="thin"
borderColor="dark"
borderRadius="medium"
padding="size-250"
>
<TextField label="Name" />
</View>
Props#
Name | Type | Default | Description |
elementType | string | JSXElementConstructor<any> | — | The element to render as the node. |
children | ReactNode | — | Children to be displayed in the View. |
Layout
Name | Type | Default | Description |
flex | Responsive<string
| number
| boolean> | — | When used in a flex layout, specifies how the element will grow or shrink to fit the space available. See MDN. |
flexGrow | Responsive<number> | — | When used in a flex layout, specifies how the element will grow to fit the space available. See MDN. |
flexShrink | Responsive<number> | — | When used in a flex layout, specifies how the element will shrink to fit the space available. See MDN. |
flexBasis | Responsive<number | string> | — | When used in a flex layout, specifies the initial main size of the element. See MDN. |
alignSelf | Responsive<'auto'
| 'normal'
| 'start'
| 'end'
| 'center'
| 'flex-start'
| 'flex-end'
| 'self-start'
| 'self-end'
| 'stretch'> | — | Overrides the alignItems property of a flex or grid container. See MDN. |
justifySelf | Responsive<'auto'
| 'normal'
| 'start'
| 'end'
| 'flex-start'
| 'flex-end'
| 'self-start'
| 'self-end'
| 'center'
| 'left'
| 'right'
| 'stretch'> | — | Specifies how the element is justified inside a flex or grid container. See MDN. |
order | Responsive<number> | — | The layout order for the element within a flex or grid container. See MDN. |
gridArea | Responsive<string> | — | When used in a grid layout, specifies the named grid area that the element should be placed in within the grid. See MDN. |
gridColumn | Responsive<string> | — | When used in a grid layout, specifies the column the element should be placed in within the grid. See MDN. |
gridRow | Responsive<string> | — | When used in a grid layout, specifies the row the element should be placed in within the grid. See MDN. |
gridColumnStart | Responsive<string> | — | When used in a grid layout, specifies the starting column to span within the grid. See MDN. |
gridColumnEnd | Responsive<string> | — | When used in a grid layout, specifies the ending column to span within the grid. See MDN. |
gridRowStart | Responsive<string> | — | When used in a grid layout, specifies the starting row to span within the grid. See MDN. |
gridRowEnd | Responsive<string> | — | When used in a grid layout, specifies the ending row to span within the grid. See MDN. |
overflow | Responsive<string> | — | Species what to do when the element's content is too long to fit its size. See MDN. |
Spacing
Name | Type | Default | Description |
margin | Responsive<DimensionValue> | — | The margin for all four sides of the element. See MDN. |
marginTop | Responsive<DimensionValue> | — | The margin for the top side of the element. See MDN. |
marginBottom | Responsive<DimensionValue> | — | The margin for the bottom side of the element. See MDN. |
marginStart | Responsive<DimensionValue> | — | The margin for the logical start side of the element, depending on layout direction. See MDN. |
marginEnd | Responsive<DimensionValue> | — | The margin for the logical end side of an element, depending on layout direction. See MDN. |
marginX | Responsive<DimensionValue> | — | The margin for both the left and right sides of the element. See MDN. |
marginY | Responsive<DimensionValue> | — | The margin for both the top and bottom sides of the element. See MDN. |
padding | Responsive<DimensionValue> | — | The padding for all four sides of the element. See MDN. |
paddingTop | Responsive<DimensionValue> | — | The padding for the top side of the element. See MDN. |
paddingBottom | Responsive<DimensionValue> | — | The padding for the bottom side of the element. See MDN. |
paddingStart | Responsive<DimensionValue> | — | The padding for the logical start side of the element, depending on layout direction. See MDN. |
paddingEnd | Responsive<DimensionValue> | — | The padding for the logical end side of an element, depending on layout direction. See MDN. |
paddingX | Responsive<DimensionValue> | — | The padding for both the left and right sides of the element. See MDN. |
paddingY | Responsive<DimensionValue> | — | The padding for both the top and bottom sides of the element. See MDN. |
Sizing
Name | Type | Default | Description |
width | Responsive<DimensionValue> | — | The width of the element. See MDN. |
minWidth | Responsive<DimensionValue> | — | The minimum width of the element. See MDN. |
maxWidth | Responsive<DimensionValue> | — | The maximum width of the element. See MDN. |
height | Responsive<DimensionValue> | — | The height of the element. See MDN. |
minHeight | Responsive<DimensionValue> | — | The minimum height of the element. See MDN. |
maxHeight | Responsive<DimensionValue> | — | The maximum height of the element. See MDN. |
Background
Name | Type | Default | Description |
backgroundColor | Responsive<BackgroundColorValue> | — | The background color for the element. |
Borders
Name | Type | Default | Description |
borderWidth | Responsive<BorderSizeValue> | — | The width of the element's border on all four sides. See MDN. |
borderStartWidth | Responsive<BorderSizeValue> | — | The width of the border on the logical start side, depending on the layout direction. See MDN. |
borderEndWidth | Responsive<BorderSizeValue> | — | The width of the border on the logical end side, depending on the layout direction. See MDN. |
borderTopWidth | Responsive<BorderSizeValue> | — | The width of the top border. See MDN. |
borderBottomWidth | Responsive<BorderSizeValue> | — | The width of the bottom border. See MDN. |
borderXWidth | Responsive<BorderSizeValue> | — | The width of the left and right borders. See MDN. |
borderYWidth | Responsive<BorderSizeValue> | — | The width of the top and bottom borders. See MDN. |
borderColor | Responsive<BorderColorValue> | — | The color of the element's border on all four sides. See MDN. |
borderStartColor | Responsive<BorderColorValue> | — | The color of the border on the logical start side, depending on the layout direction. See MDN. |
borderEndColor | Responsive<BorderColorValue> | — | The color of the border on the logical end side, depending on the layout direction. See MDN. |
borderTopColor | Responsive<BorderColorValue> | — | The color of the top border. See MDN. |
borderBottomColor | Responsive<BorderColorValue> | — | The color of the bottom border. See MDN. |
borderXColor | Responsive<BorderColorValue> | — | The color of the left and right borders. See MDN. |
borderYColor | Responsive<BorderColorValue> | — | The color of the top and bottom borders. See MDN. |
borderRadius | Responsive<BorderRadiusValue> | — | The border radius on all four sides of the element. See MDN. |
borderTopStartRadius | Responsive<BorderRadiusValue> | — | The border radius for the top start corner of the element, depending on the layout direction. See MDN. |
borderTopEndRadius | Responsive<BorderRadiusValue> | — | The border radius for the top end corner of the element, depending on the layout direction. See MDN. |
borderBottomStartRadius | Responsive<BorderRadiusValue> | — | The border radius for the bottom start corner of the element, depending on the layout direction. See MDN. |
borderBottomEndRadius | Responsive<BorderRadiusValue> | — | The border radius for the bottom end corner of the element, depending on the layout direction. See MDN. |
Positioning
Name | Type | Default | Description |
position | Responsive<'static'
| 'relative'
| 'absolute'
| 'fixed'
| 'sticky'> | — | Specifies how the element is positioned. See MDN. |
top | Responsive<DimensionValue> | — | The top position for the element. See MDN. |
bottom | Responsive<DimensionValue> | — | The bottom position for the element. See MDN. |
left | Responsive<DimensionValue> | — | The left position for the element. See MDN. Consider using start instead for RTL support. |
right | Responsive<DimensionValue> | — | The right position for the element. See MDN. Consider using start instead for RTL support. |
start | Responsive<DimensionValue> | — | The logical start position for the element, depending on layout direction. See MDN. |
end | Responsive<DimensionValue> | — | The logical end position for the element, depending on layout direction. See MDN. |
zIndex | Responsive<number> | — | The stacking order for the element. See MDN. |
isHidden | Responsive<boolean> | — | Hides the element. |
Advanced
Name | Type | Default | Description |
UNSAFE_className | string | — | Sets the CSS className for the element. Only use as a last resort. Use style props instead. |
UNSAFE_style | CSSProperties | — | Sets inline style for the element. Only use as a last resort. Use style props instead. |