|
| 1 | +--- |
| 2 | +title: Flex |
| 3 | +description: Flex Component |
| 4 | +version: 2.0+ |
| 5 | +--- |
| 6 | + |
| 7 | +# Flex |
| 8 | + |
| 9 | +Flex is `CBox` with `display: flex` and comes with helpful style shorthand. It renders a `div` element. |
| 10 | + |
| 11 | +## Import |
| 12 | + |
| 13 | +```js |
| 14 | +import { CFlex, CSpacer } from '@chakra-ui/vue-next'; |
| 15 | +``` |
| 16 | + |
| 17 | +- Flex: A `CBox` with `display: flex`. |
| 18 | +- Spacer: Creates an adjustable, empty space that can be used to tune the spacing between child elements within Flex. |
| 19 | + |
| 20 | +## Usage |
| 21 | + |
| 22 | +When using the Flex component, you can use some of the following helpful shorthand props: |
| 23 | + |
| 24 | +- `direction` for `flexDirection` |
| 25 | +- `wrap` for `flexWrap` |
| 26 | +- `align` for `alignItems` |
| 27 | +- `justify` for `justifyContent` |
| 28 | +<!-- - `basis` for `flexBasis` |
| 29 | +- `shrink` for `flexShrink` --> |
| 30 | +While you can pass the verbose props, using the shorthand can save you some time. |
| 31 | + |
| 32 | +::showcase |
| 33 | +::simple-flex{width=full} |
| 34 | +:: |
| 35 | +:: |
| 36 | + |
| 37 | +```html |
| 38 | +<CFlex color='white'> |
| 39 | + <CCenter w='100px' bg='green.500'> |
| 40 | + <CText>Box 1</CText> |
| 41 | + </CCenter> |
| 42 | + <CSquare bg='blue.500' size='150px'> |
| 43 | + <CText>Box 2</CText> |
| 44 | + </CSquare> |
| 45 | + <CBox flex='1' bg='tomato'> |
| 46 | + <CText>Box 3</CText> |
| 47 | + </CBox> |
| 48 | +</CFlex> |
| 49 | +``` |
| 50 | + |
| 51 | +### Using the Spacer |
| 52 | + |
| 53 | +As an alternative to `CStack`, you can combine `CFlex` and `CSpacer` to create stackable and responsive layouts. |
| 54 | + |
| 55 | +::showcase |
| 56 | +::spacer-flex{width=full} |
| 57 | +:: |
| 58 | +:: |
| 59 | + |
| 60 | +```html |
| 61 | +<CFlex> |
| 62 | + <CBox p='4' bg='red.400'> |
| 63 | + Box 1 |
| 64 | + </CBox> |
| 65 | + <CSpacer /> |
| 66 | + <CBox p='4' bg='green.400'> |
| 67 | + Box 2 |
| 68 | + </CBox> |
| 69 | +</CFlex> |
| 70 | +``` |
| 71 | + |
| 72 | +### Flex and Spacer vs Grid vs Stack |
| 73 | + |
| 74 | +The `CFlex` and `CSpacer` components, `CGrid` and `CHStack` treat children of different widths differently. |
| 75 | + |
| 76 | +- In `CHStack`, the children will have equal spacing between them but they won't span the entire width of the container. |
| 77 | +- In `CGrid`, the starting points of the children will be equally spaced but the gaps between them will not be equal. |
| 78 | +- With `CFlex` and `CSpacer`, the children will span the entire width of the container and also have equal spacing between them. |
| 79 | + |
| 80 | +::showcase |
| 81 | +::compare-flex{width=full} |
| 82 | +:: |
| 83 | +:: |
| 84 | + |
| 85 | +```html |
| 86 | +<CBox> |
| 87 | + <CText>Flex and Spacer: Full width, equal Spacing</CText> |
| 88 | + <CFlex> |
| 89 | + <CBox w='70px' h='10' bg='red.500' /> |
| 90 | + <CSpacer /> |
| 91 | + <CBox w='170px' h='10' bg='red.500' /> |
| 92 | + <CSpacer /> |
| 93 | + <CBox w='180px' h='10' bg='red.500' /> |
| 94 | + </CFlex> |
| 95 | + |
| 96 | + <CText> |
| 97 | + Grid: The children start at the beginning, the 1/3 mark and 2/3 mark |
| 98 | + </CText> |
| 99 | + <CGrid template-columns='repeat(3, 1fr)' gap={6}> |
| 100 | + <CBox w='70px' h='10' bg='blue.500' /> |
| 101 | + <CBox w='170px' h='10' bg='blue.500' /> |
| 102 | + <CBox w='180px' h='10' bg='blue.500' /> |
| 103 | + </CGrid> |
| 104 | + |
| 105 | + <CText> |
| 106 | + HStack: The children have equal spacing but don't span the whole container |
| 107 | + </CText> |
| 108 | + <CHStack spacing='24px'> |
| 109 | + <CBox w='70px' h='10' bg='teal.500' /> |
| 110 | + <CBox w='170px' h='10' bg='teal.500' /> |
| 111 | + <CBox w='180px' h='10' bg='teal.500' /> |
| 112 | + </CHStack> |
| 113 | + </CBox> |
| 114 | + ``` |
| 115 | + |
| 116 | + A good use case for `CSpacer` is to create a navbar with a signup/login button aligned to the right. |
| 117 | + |
| 118 | +Since `CSpacer` renders a `div`, any `gap` value provided to the parent is applied to both sides of this component, and therefore make the gap appear doubled when the spacer is completely collapsed. |
| 119 | + |
| 120 | +> The example below is not responsive on purpose as you might switch to a collapsed menu on mobile. |
| 121 | +
|
| 122 | +::showcase |
| 123 | +::sign-up-flex{width=full} |
| 124 | +:: |
| 125 | +:: |
| 126 | + |
| 127 | +```html |
| 128 | +<CFlex min-width='max-content' align-items='center' gap='2'> |
| 129 | + <CBox p='2'> |
| 130 | + <CHeading size='md'>Chakra App</CHeading> |
| 131 | + </CBox> |
| 132 | + <CSpacer /> |
| 133 | + <CButtonGroup p='2'> |
| 134 | + <CButton rounded="10px" color-scheme='teal'>Sign Up</CButton> |
| 135 | + <CButton rounded="10px" color-scheme='teal'>Log in</CButton> |
| 136 | + </CButtonGroup> |
| 137 | +</CFlex> |
| 138 | +``` |
0 commit comments