Box
Box is an extremely powerful component. It is a low-level primitive component that accepts props for standard spacing, background and text colors, display properties and more. It will negate the need to create many custom styled components in your applications.
Installation
npm install @nib/layout
Usage
import {Box} from '@nib/layout';
Do not style layout components
Interactive demo
Props
Prop | Type | Default | Description |
---|---|---|---|
mode | string or object | The mode to use. Must be one of [default , alt , feature ], or an object with brands as keys and modes as values. | |
padding , paddingVertical , paddingHorizontal , paddingTop , paddingRight , paddingBottom , paddingLeft | number or object | A size from our spacing scale. Can be made responsive by passing an object of breakpoints. Value(s) must be one of 1...10 . | |
margin , marginVertical , marginHorizontal , marginTop , marginRight , marginBottom , marginLeft | number or object or string | A size from our spacing scale and auto . Can be made responsive by passing an object of breakpoints. Value(s) must be one of 1...10 or auto . | |
paddingBlockStart , paddingInlineEnd , paddingBlockEnd , paddingInlineStart , paddingBlock , paddingInline | number or object | A size from our spacing scale. Can be made responsive by passing an object of breakpoints. Value(s) must be one of 1...10 . | |
marginBlockStart , marginInlineEnd , marginBlockEnd , marginInlineStart , marginBlock , marginInline | number or object | A size from our spacing scale. Can be made responsive by passing an object of breakpoints. Value(s) must be one of 1...10 . | |
display | string or object | How the box should display. Can be made responsive by passing an object of breakpoints. Value(s) must be one of none , inline , block , inline-block , flex , inline-flex . | |
width | string or object | The width of the box. Can be made responsive by passing an object of breakpoints. Value(s) must be a valid css value with units. | |
minWidth | string or object | The minimum width of the box, growing if content is less than the minWidth. Can be made responsive by passing an object of breakpoints. Value(s) must be a valid css value with units. | |
maxWidth | string or object | The maximum width of the box, shrinking if content is greater than the maxWidth. Can be made responsive by passing an object of breakpoints. Value(s) must be a valid css value with units. | |
height | string or object | The height of the box. Can be made responsive by passing an object of breakpoints. Value(s) must be a valid css value with units. | |
minHeight | string or object | The minimum height of the box, growing if content is less than the minHeight. Can be made responsive by passing an object of breakpoints. Value(s) must be a valid css value with units. | |
maxHeight | string or object | The maximum height of the box, shrinking if content is greater than the maxHeight. Can be made responsive by passing an object of breakpoints. Value(s) must be a valid css value with units. | |
justifyContent | string or object | When display is set to flex/inline-flex this aligns the Box children along the cross axis (horizontal by default). Can be made responsive by passing an object of breakpoints. Value(s) must be one of center , flex-start , flex-end , space-between , or space-around . | |
alignItems | string or object | When display is set to flex/inline-flex this aligns the Box children along the main axis (vertical by default). Can be made responsive by passing an object of breakpoints. Value(s) must be one of flex-start , center , flex-end or stretch . | |
alignSelf | string or object | When parent component's display is set to flex/inline-flex this aligns the Box itself along the main axis, overriding the align-items value of the parent. Can be made responsive by passing an object of breakpoints. Value(s) must be one of flex-start , center , flex-end or stretch . | |
flexDirection | string or object | When display is set to flex/inline-flex this sets the main axis and direction of the Box. Can be made responsive by passing an object of breakpoints. Value(s) must be one of row , column , row-reverse , or column-reverse . | |
flexWrap | string or object | When display is set to flex/inline-flex this sets whether flex items are forced onto one line or can wrap onto multiple lines. Can be made responsive by passing an object of breakpoints. Value(s) must be one of wrap , wrap-reverse , or nowrap . | |
flexGrow | number or object | When Box is a flex-item this sets whether the Box should grow to fill remaining space. Can be made responsive by passing an object of breakpoints. Value(s) must be a number. | |
flexShrink | number or object | When Box is a flex-item this sets whether the Box should shrink to fit within available space. Can be made responsive by passing an object of breakpoints. Value(s) must be a number. | |
textAlign | string or object | The text alignment of the Box. Can be made responsive by passing an object of breakpoints. Value(s) must be one of start , end , left , center , or right . | |
background | string | The background color of the Box. Can be made responsive by passing an object of breakpoints. Can be any of the validBgColorValues . | |
color | string | The text color of the Box. Can be made responsive by passing an object of breakpoints. Can be any of the validFgColorValues . | |
borderRadius | string | The border radius of the Box. Must be one of none , small , standard , or full . | |
boxShadow | string | The box shadow of the Box. Must be one of none or standard . | |
gap | number or object | Sets the gaps between children elements . A size from our spacing scale. Can be made responsive by passing an object of breakpoints. Value(s) must be one of 1...10 values. | |
lineHeight | string or object | Sets the line-height for the Box component. Must be valid line-height property values. Note: Copy component defines has it's own line-height | |
overflow | string or object | Sets the overflow property . Must be valid overflow property values. | |
borderWidth | string or object | Sets the border width for the Box component . Value(s) must be a valid css value with units or valid borderWidth token values none , fine , thin , medium or thick | |
borderStyle | string or object | Sets the border style for the Box component . Values must be one of none , hidden , dotted , dashed , solid , double , groove , inset , ridge , outset . | |
borderColor | string or object | Sets the border color for the Box component. Can be any of the validBorderColorValues | |
position | string or object | Sets the position property . Must be one of static , relative , absolute , fixed or sticky . | |
top | string or object | Specifies the vertical position of a positioned element. It has no effect on non-positioned elements. | |
bottom | string or object | Specifies the vertical position of a positioned element. It has no effect on non-positioned elements. | |
left | string or object | Specifies the horizontal position of a positioned element. It has no effect on non-positioned elements. | |
right | string or object | Specifies the horizontal position of a positioned element. It has no effect on non-positioned elements. | |
inset | string or object | Shorthand that corresponds to the top , right , bottom , and/or left properties | |
isolate | boolean | Determines whether an element must create a new stacking context | |
zIndex | number or object | Sets the z-index property on Box component. | |
children | node | The contents of the Box. |
Considerations
The Box
component can be used in place of a div
whenever padding, background color, text color, border radius, text alignment or basic flex styling needs to be applied to a component.
Prior to this Box
component, you would have had to have done something like this to add common, standard styling to a component:
import styled from 'styled-components';import {colorDarkest, colorLightest} from '@nib-components/theme';import {m, p} from '@nib/layout';const MyBox = styled.div`${m(5)};${p({xs: 4, md: 6})};background-color: ${colorDarkest};border-radius: 4px;color: ${colorLightest};text-align: center;`;
Now with Box
you do not need to step out of the component you are working on to import a bunch of boilerplate and define a styled component that meets your specific needs. Just use the props provided by Box
:
<Box margin={5} padding={{xs: 4, md: 6}} background="darkest" color="lightest" borderRadius="standard" textAlign="center">Hello world</Box>
Set mode directly on Box
Since v3.7.0
mode
can be set directly on the Box
component instead of wrapping in a ModeProvider
.
See ModeProvider props for details on the mode
prop.
DOM element
The Box
component is a styled div element. To change this you can use the styled components as
prop:
<Box padding={4} as="aside"><Stack space={4}><Heading size={4}>Newsletter</Heading><Copy>Lorem ipsum dolor sit amet.</Copy><Link href="/subsribe">Sign up now</Link></Stack></Box>
Logical Properties
Logical properties and values in CSS provide a powerful way to control layout and style based on the flow of content. Unlike traditional physical properties (such as margin-top or padding-left), logical properties adapt to different writing modes (horizontal or vertical) and text directions (left-to-right or right-to-left). This makes CSS more flexible and easier to internationalize.
Along with the above props, we do have below props to support the logical properties:
Prop | Type | Default | Description |
---|---|---|---|
paddingBlockStart , paddingInlineEnd , paddingBlockEnd , paddingInlineStart , paddingBlock , paddingInline | number or object | A size from our spacing scale. Can be made responsive by passing an object of breakpoints. Value(s) must be one of 1...10 . | |
marginBlockStart , marginInlineEnd , marginBlockEnd , marginInlineStart , marginBlock , marginInline | number or object | A size from our spacing scale. Can be made responsive by passing an object of breakpoints. Value(s) must be one of 1...10 . |
Note: Note: you can only pass a single value for both end and start for the
paddingBlock
,paddingInline
,marginBlock
andmarginInline
props.