The Box component serves as a wrapper component for most of the CSS utility needs. It allows you to control styling using style props to build a variety of components.
Dark Mode
Box
Tonic UI
Import
import{Box}from'@trendmicro/react-styled-ui';
Usage
EDITABLE EXAMPLE
<Box
backgroundColor="blue:50"
color="black:primary"
display="inline-block"
px="4x"
py="3x"
fontSize="xl"
lineHeight="xl"
>
My component
</Box>
The as prop and custom component
By default, the Box component renders a div element. There might be cases where you want to render a different element, you can use the as prop to do that.
EDITABLE EXAMPLE
<Box
as="button"
backgroundColor="blue:70"
color="white:primary"
border="none"
borderRadius="sm"
cursor="pointer"
px="2x"
py="1x"
>
Button
</Box>
Using pseudo props
Box also provides useful props to style common CSS pseudo-class and pseudo-element selectors.
A pseudo-class selector targets elements depending on their state rather than on information from the document tree. For example, the selector :hover matches when the user interacts with an element with a pointing device, but does not necessary activate it.
A pseudo-element selector applies styles to parts of your document content in scenarios where there isn't a specific HTML element to select. For example, ::before creates a pseudo-element that is the first child of the selected element. It is often used to add cosmetic content to an element with the content property.
Name
Selector
_active
&:active
_checked
&:checked
_disabled
&:disabled
_empty
&:empty
_enabled
&:enabled
_firstChild
&:first-child
_firstOfType
&:first-of-type
_fullscreen
&:fullscreen
_focus
&:focus
_focusWithin
&:focus-within
_hover
&:hover
_indeterminate
&:indeterminate
_invalid
&:invalid
_lastChild
&:last-child
_lastOfType
&:last-of-type
_nthOfType
&:nth-of-type()
_readOnly
&:read-only
_visited
&:visited
__after
&::after
__backdrop
&::backdrop
__before
&::before
__cue
&::cue
__firstLetter
&::first-letter
__firstLine
&::first-line
__placeholder
&::placeholder
__selection
&::selection
:hover
Add the _hover prop to apply style props on hover.
EDITABLE EXAMPLE
<Box
as="button"
bg="blue:40"
color="white:primary"
py="2x"
px="3x"
border={0}
borderRadius="sm"
cursor="pointer"
_hover={{
bg:'blue:50',
color:'white:primary',
}}
>
Hover Me
</Box>
:active
Add the _active prop to apply style props on active.
EDITABLE EXAMPLE
<Box
as="button"
bg="blue:40"
color="white:primary"
py="2x"
px="3x"
border={0}
borderRadius="sm"
cursor="pointer"
_hover={{
bg:'blue:50',
color:'white:primary',
}}
_active={{
bg:'blue:60',
color:'white:primary',
}}
>
Click Me
</Box>
:focus
Add the _focus prop to apply style props on focus.
EDITABLE EXAMPLE
<Box
as="input"
placeholder="Click Me"
py="2x"
px="3x"
bg="gray:20"
border={1}
borderColor="transparent"
borderRadius="sm"
outline="none"
_focus={{
bg:'white:primary',
boxShadow:'0 0 0 .2rem rgba(111, 155, 244, .5)',
}}
/>
:disabled
Add the _disabled prop to style an element which is disabled.
EDITABLE EXAMPLE
<Stackdirection="row"spacing=".5rem">
<Box
as="button"
bg="blue:40"
color="white:primary"
py="2x"
px="3x"
border={0}
borderRadius="sm"
cursor="pointer"
_hover={{
bg:'blue:50',
color:'white:primary',
}}
_active={{
bg:'blue:60',
color:'white:primary',
}}
>
Click Me
</Box>
<Box
as="button"
bg="blue:40"
color="white:primary"
py="2x"
px="3x"
border={0}
borderRadius="sm"
cursor="pointer"
disabled
_disabled={{
cursor:'not-allowed',
bg:'blue:40',
color:'white:primary',
opacity:.6,
}}
_hover={{
bg:'blue:50',
color:'white:primary',
}}
_active={{
bg:'blue:60',
color:'white:primary',
}}
>
Click Me
</Box>
</Stack>
:visited
Add the _visited prop to style a visited link.
EDITABLE EXAMPLE
<Stackdirection="row"spacing=".5rem">
<Box
as="a"
href=""
color="blue:40"
fontWeight="semibold"
textDecoration="none"
_hover={{
textDecoration:'underline',
}}
>
Normal link
</Box>
<Box
as="a"
href=""
color="blue:40"
fontWeight="semibold"
textDecoration="none"
_hover={{
textDecoration:'underline',
}}
_visited={{
color:'purple:60',
}}
>
Visited link
</Box>
</Stack>
:first-child
Add the _firstChild prop to style an element that is the first element among its siblings.
Add the _nthOfType prop to match elements of a given type, based on their position among a group of siblings. The value can be represented as an array or an object in the following form: