Stack
Stack is a layout component used to group elements together and apply a space between them.
Import
import { Stack, HStack, VStack } from '@hoverinc/design-system-react-web';- VStack: used to stack elements in the vertical direction
- HStack: used to stack elements in the horizontal direction
- Stack: used to stack elements in the vertical or horizontal direction
Usage
Stack uses a modified version of the CSS lobotomized owl selector to add spacing between its children.
To stack elements in horizontal or vertical direction only, use the HStack or
VStack components. You can also use the Stack component as well and pass the
direction prop.
<HStack spacing="400">
<Center w="350" h="350" bg="primary.100">
1
</Center>
<Center w="350" h="350" bg="primary.500">
2
</Center>
<Center w="350" h="350" bg="neutral.200">
3
</Center>
</HStack>Responsive direction
You can pass responsive values to the Stack component to change stack
direction and/or spacing between elements.
<Stack direction={['column', 'row']} spacing="400">
<Center w="350" h="350" bg="primary.100">
1
</Center>
<Center w="350" h="350" bg="primary.500">
2
</Center>
<Center w="350" h="350" bg="neutral.200">
3
</Center>
</Stack>Stack Dividers
In some scenarios, you may want to add dividers between stacked elements. Pass
the divider prop and set its value to the StackDivider or any custom React
element.
<VStack
divider={<StackDivider borderColor="neutral.200" />}
spacing="400"
align="stretch"
>
<Center h="350" bg="primary.100">
1
</Center>
<Center h="350" bg="primary.500">
2
</Center>
<Center h="350" bg="neutral.200">
3
</Center>
</VStack>Stack items horizontally
Pass the direction and set it to row. Optionally, you can use align and
justify to adjust the alignment and distribution of the items.
Feature Cards with Stack Component
() => {
function Feature({ title, desc, ...rest }) {
return (
<Box
borderWidth="1px"
flexDir="column"
padding="400"
shadow="distance300"
{...rest}
>
<Heading size="400">{title}</Heading>
<Body marginTop="100">{desc}</Body>
</Box>
);
}
return (
<Stack spacing="400" direction="row">
<Feature
title="Plan Money"
desc="The future can be even brighter but a goal without a plan is just a wish"
/>
<Feature
title="Save Money"
desc="You deserve good things. With a whooping 10-15% interest rate per annum, grow your savings on your own terms with our completely automated process"
/>
</Stack>
);
};Feature Cards with HStack Component
In the example following, we can notice that for the HStack component, we
don't need the direction props, since this component is specifically for
horizontally stacking items.
() => {
function Feature({ title, desc, ...rest }) {
return (
<Box
borderWidth="1px"
flexDir="column"
padding="400"
shadow="distance300"
{...rest}
>
<Heading size="400">{title}</Heading>
<Body marginTop="100">{desc}</Body>
</Box>
);
}
return (
<HStack spacing="400">
<Feature
title="Plan Money"
desc="The future can be even brighter but a goal without a plan is just a wish"
/>
<Feature
title="Save Money"
desc="You deserve good things. With a whooping 10-15% interest rate per annum, grow your savings on your own terms with our completely automated process"
/>
</HStack>
);
};Notes on Stack vs Flex
The Stack component and the Flex component have their children spaced out
evenly but the key difference is that the Stack won't span the entire
width of the container whereas the Flex will. Another thing to note is that
the items in both Stack and Flex are aligned in the center by default.