Box
Group sections of content and easily adjust position, spacing and size
import { Box } from "@vitality-ds/components";
The Box component is useful for adding ad-hoc padding, margin, width and height properties (non exhaustive). The box is not for changing colours and visual styles, just layouts. This means you don't need to created a new styled()
component every time you need to tweak the layout.
() => { const DecorativeBox = () => <DocsFlex>Content</DocsFlex>; return ( <DocsBox css={{ backgroundColor: "$neutral2", border: "1px solid $neutral6"}}> <Box padding="lg" margin="sm" width={300} height={100}> <FauxBadge>One</FauxBadge> <FauxBadge>Two</FauxBadge> </Box> <Box padding="lg" margin="lg" width={200} height={100}> <FauxBadge>Three</FauxBadge> <FauxBadge>Four</FauxBadge> </DocsBox> </div> ); };
Padding can be set using padding
, px
, py
, pt
, pb
, pl
and pr
. You can pass any valid value in the Vitality space scale or none
for a zero value.
() => { return ( <div style={{ backgroundColor: "var(--vitality-colors-neutral12)", boxSizing: "border-box", }} > <Box padding="lg" pl="sm"> <FauxBadge>Item</FauxBadge> </Box> </div> ); };
Margin can be set using margin
, mx
, my
, mt
, mb
, ml
and mr
. You can pass any valid value in the Vitality space scale or none
for a zero value.
() => { return ( <div style={{ backgroundColor: "var(--vitality-colors-neutral12)", boxSizing: "border-box", }} > <Box margin="lg" ml="sm"> <FauxBadge>Item</FauxBadge> </Box> </div> ); };
In most cases, you should not need to set an explicit width on an element. Usually the parent component should specify the width based on flex
or grid
properties, or the Box
element's children would dictate the width. In cases where you do need to enforce a width, simply pass any valid CSS value to the width
property.
() => { return ( <div style={{ backgroundColor: "var(--vitality-colors-neutral12)", boxSizing: "border-box", width: "100%", }} > <Box width="50%"> <FauxBadge>Item</FauxBadge> </Box> </div> ); };
() => { return ( <div style={{ backgroundColor: "var(--vitality-colors-neutral12)", boxSizing: "border-box", width: "100%", }} > <Box maxWidth={200}> <FauxBadge>Item</FauxBadge> </Box> </div> ); };
() => { return ( <div style={{ backgroundColor: "var(--vitality-colors-neutral12)", boxSizing: "border-box", width: "20%", padding: "32px", }} > <Box minWidth={200}> <FauxBadge>Item</FauxBadge> </Box> </div> ); };
() => { return ( <div style={{ backgroundColor: "var(--vitality-colors-neutral12)", boxSizing: "border-box", }} > <Box height={100}> <FauxBadge>Item</FauxBadge> </Box> </div> ); };
The below example shows how the invisible container around the items is not exceeding 10 pixels height.
() => { return ( <div style={{ backgroundColor: "var(--vitality-colors-neutral12)", boxSizing: "border-box", width: "100%", height: "100%", padding: 32, }} > <Box maxHeight={10}> <FauxBadge>Item</FauxBadge> <FauxBadge>Item</FauxBadge> </Box> </div> ); };
The below example shows how the invisible container around the items is no less than 100 pixels height.
() => { return ( <div style={{ backgroundColor: "var(--vitality-colors-neutral12)", boxSizing: "border-box", height: 80, padding: "32px", }} > <Box minHeight={100}> <FauxBadge>Item</FauxBadge> <FauxBadge>Item</FauxBadge> <FauxBadge>Item</FauxBadge> </Box> </div> ); };
When the Box
component is used within a display="flex"
container (or using the Flex
component), you can control whether it should grow/shrink.
shouldShrink
istrue
by default, meaning most elements will try to shrink in order to fit othersshouldGrow
isfalse
by default, meaning most elements will not attempt to grow beyond what their content requires.
These defaults are based on the default CSS values for the flex-shrink
and flex-grow
properties.
If you require fine-granular control over an element's
flex
properties, consider creating your ownstyled()
components.
<DocsFlex style={{ direction: "row", width: "100%", spacing: "xl", wrap: "noWrap", }} > <Box shouldShrink={false}> <FauxBadge> This element will avoid shrinking despite the other element attempting to grow. </FauxBadge> </Box> <Box> <Typography> This element will attempt to grow because of the size of its content. </Typography> </Box> </DocsFlex>
This example demonstrates how you can "enforce" a Box
to grow despite its contents not intrinsicly requiring its growth.
<DocsFlex style={{ direction: "row", width: "100%", spacing: "lg", justify: "between", align: "center", }} > <Box shouldGrow> <Button shouldFitContainer>Click here</Button> </Box> <Box width={200}> <FauxBadge> This element will try to grow but cannot because the other element is forced to grow. </FauxBadge> </Box> </DocsFlex>
You can set the CSS position
property on any Box component as well as pass any valid CSS value to the top
, bottom
, left
, right
and inset
properties.
<DocsFlex style={{ backgroundColor: "var(--vitality-colors-neutral12)", width: "100%", height: 200, }} > <Box position="relative" width="100%" height="100%"> <Box position="absolute" bottom={6} right="20%" width={200}> <FauxBadge>Element</FauxBadge> </Box> </Box> </DocsFlex>
Type
Bottom
Description
The content of the box
Type
ReactNode
Description
When used in a CSS grid, control the gridColumn` and `gridRow property. Takes any valid value passed to these CSS properties.
Type
{ cols?: GridColumn; rows?: GridRow; }
Type
Height
Type
string
Type
Inset
Type
Left
Type
SpaceKeys
Type
MaxHeight
Type
MaxWidth
Type
SpaceKeys
Type
MinHeight
Type
MinWidth
Type
SpaceKeys
Type
SpaceKeys
Type
SpaceKeys
Type
SpaceKeys
Type
SpaceKeys
Type
SpaceKeys
Type
SpaceKeys
Type
SpaceKeys
Type
"fixed" | "absolute" | "relative" | "static" | "sticky"
Type
SpaceKeys
Type
SpaceKeys
Type
SpaceKeys
Type
SpaceKeys
Type
Right
Type
boolean
Type
boolean
Default Value
Type
Top
Type
Width