|
| 1 | +--- |
| 2 | +title: Grid |
| 3 | +description: A flexible layout component that provides CSS grid functionality with responsive props and spacing controls. |
| 4 | +source: 'sentry/components/core/layout/grid' |
| 5 | +resources: |
| 6 | + js: https://github.com/getsentry/sentry/blob/master/static/app/components/core/layout/grid.tsx |
| 7 | +--- |
| 8 | + |
| 9 | +import {Container, Grid} from 'sentry/components/core/layout'; |
| 10 | +import * as Storybook from 'sentry/stories'; |
| 11 | + |
| 12 | +import APIReference from '!!type-loader!sentry/components/core/layout/grid'; |
| 13 | + |
| 14 | +export const types = {Grid: APIReference.Grid}; |
| 15 | + |
| 16 | +The `Grid` component is a layout component that extends the `Container` component with CSS grid properties. |
| 17 | + |
| 18 | +## Basic Usage |
| 19 | + |
| 20 | +To create a basic grid container, wrap elements in `<Grid>` and define columns using `columns`. |
| 21 | + |
| 22 | +```jsx |
| 23 | +<Grid columns="repeat(3, 1fr)" gap="md"> |
| 24 | + <div>Item 1</div> |
| 25 | + <div>Item 2</div> |
| 26 | + <div>Item 3</div> |
| 27 | +</Grid> |
| 28 | +``` |
| 29 | + |
| 30 | +### Grid Areas |
| 31 | + |
| 32 | +<Storybook.Demo> |
| 33 | + <Grid |
| 34 | + areas={` |
| 35 | + "header header" |
| 36 | + "sidebar main" |
| 37 | + "footer footer"`} |
| 38 | + columns="100px 1fr" |
| 39 | + rows="60px 1fr 60px" |
| 40 | + gap="md" |
| 41 | + padding="md" |
| 42 | + height="300px" |
| 43 | + > |
| 44 | + <Container |
| 45 | + area="header" |
| 46 | + border="primary" |
| 47 | + padding="md" |
| 48 | + background="primary" |
| 49 | + radius="md" |
| 50 | + > |
| 51 | + Header |
| 52 | + </Container> |
| 53 | + <Container |
| 54 | + area="sidebar" |
| 55 | + border="primary" |
| 56 | + padding="md" |
| 57 | + background="primary" |
| 58 | + radius="md" |
| 59 | + > |
| 60 | + Sidebar |
| 61 | + </Container> |
| 62 | + <Container |
| 63 | + area="main" |
| 64 | + border="primary" |
| 65 | + padding="md" |
| 66 | + minWidth="300px" |
| 67 | + background="primary" |
| 68 | + radius="md" |
| 69 | + > |
| 70 | + Main Content |
| 71 | + </Container> |
| 72 | + <Container |
| 73 | + area="footer" |
| 74 | + border="primary" |
| 75 | + padding="md" |
| 76 | + background="primary" |
| 77 | + radius="md" |
| 78 | + > |
| 79 | + Footer |
| 80 | + </Container> |
| 81 | + </Grid> |
| 82 | +</Storybook.Demo> |
| 83 | +```jsx |
| 84 | +<Grid |
| 85 | + areas=" |
| 86 | + header header |
| 87 | + sidebar main |
| 88 | + footer footer" |
| 89 | + columns="200px 1fr" |
| 90 | + rows="60px 1fr 60px" |
| 91 | + gap="md" |
| 92 | +> |
| 93 | + <Container area="header">Header</Container> |
| 94 | + <Container area="sidebar">Sidebar</Container> |
| 95 | + <Container area="main">Main Content</Container> |
| 96 | + <Container area="footer">Footer</Container> |
| 97 | +</Grid> |
| 98 | +``` |
| 99 | + |
| 100 | +### Alignment |
| 101 | + |
| 102 | +Grid provides fine-grained control over alignment using `justify`, `align`, `justifyItems`, and `alignContent`. |
| 103 | + |
| 104 | +<Storybook.Demo> |
| 105 | + <Grid columns="repeat(1, 1fr)" gap="md" padding="md" width="100%"> |
| 106 | + {[ |
| 107 | + {justify: 'start', align: 'start', label: 'start'}, |
| 108 | + {justify: 'center', align: 'center', label: 'center'}, |
| 109 | + {justify: 'end', align: 'end', label: 'end'}, |
| 110 | + {justify: 'between', align: 'between', label: 'between'}, |
| 111 | + {justify: 'around', align: 'around', label: 'around'}, |
| 112 | + {justify: 'evenly', align: 'evenly', label: 'evenly'}, |
| 113 | + ].map(({justify, align, label}) => ( |
| 114 | + <Grid key={label} columns="repeat(2, 25%)" gap="sm" justify={justify} align={align}> |
| 115 | + <Container padding="md" background="primary" radius="md" border="primary"> |
| 116 | + {label} |
| 117 | + </Container> |
| 118 | + <Container padding="md" background="primary" radius="md" border="primary"> |
| 119 | + alignment |
| 120 | + </Container> |
| 121 | + </Grid> |
| 122 | + ))} |
| 123 | + </Grid> |
| 124 | +</Storybook.Demo> |
| 125 | +```jsx |
| 126 | +<Grid columns="repeat(2, 1fr)" justify="center" align="center"> |
| 127 | + <div>Centered Item</div> |
| 128 | + <div>Another Item</div> |
| 129 | +</Grid> |
| 130 | +``` |
| 131 | + |
| 132 | +### Responsive Props |
| 133 | + |
| 134 | +All props support responsive values using breakpoint objects. Breakpoints are: `xs`, `sm`, `md`, `lg`, `xl`, `2xl`. |
| 135 | + |
| 136 | +Example of a responsive grid container that changes from single column on small screens to multiple columns on larger screens. |
| 137 | + |
| 138 | +<Storybook.Demo> |
| 139 | + <Grid columns={{xs: '1fr', sm: 'repeat(2, 1fr)', md: 'repeat(3, 1fr)'}} gap="md" p="md"> |
| 140 | + <Container padding="md" border="primary" radius="md" background="primary"> |
| 141 | + Responsive |
| 142 | + </Container> |
| 143 | + <Container padding="md" border="primary" radius="md" background="primary"> |
| 144 | + Grid |
| 145 | + </Container> |
| 146 | + <Container padding="md" border="primary" radius="md" background="primary"> |
| 147 | + Layout |
| 148 | + </Container> |
| 149 | + <Container padding="md" border="primary" radius="md" background="primary"> |
| 150 | + Items |
| 151 | + </Container> |
| 152 | + <Container padding="md" border="primary" radius="md" background="primary"> |
| 153 | + 🔥 |
| 154 | + </Container> |
| 155 | + <Container padding="md" border="primary" radius="md" background="primary"> |
| 156 | + Awesome |
| 157 | + </Container> |
| 158 | + </Grid> |
| 159 | +</Storybook.Demo> |
| 160 | +```jsx |
| 161 | +<Grid |
| 162 | + // Single column on xs, 2 columns on sm, 3 columns on md+ |
| 163 | + columns={{xs: '1fr', sm: 'repeat(2, 1fr)', md: 'repeat(3, 1fr)'}} |
| 164 | + gap="md" |
| 165 | +> |
| 166 | + <Container>Responsive</Container> |
| 167 | + <Container>Grid</Container> |
| 168 | + <Container>Layout</Container> |
| 169 | + <Container>Items</Container> |
| 170 | + <Container>🔥</Container> |
| 171 | + <Container>Awesome</Container> |
| 172 | +</Grid> |
| 173 | +``` |
| 174 | + |
| 175 | +If a prop is not specified for a breakpoint, the value will **not** be inherited from the previous breakpoint. |
0 commit comments