# Grid URL: /react/components/layout/grid Source: https://github.com/daangn/seed-design/blob/dev/docs/content/react/components/(foundation)/layout/grid.mdx The Grid component uses CSS Grid and helps you use design tokens in JSX. ## Preview ```tsx import { Flex, Grid } from "@seed-design/react"; export default function GridPreview() { return ( {Array.from({ length: 6 }).map((_, index) => ( {index + 1} ))} ); } ``` ## Props ### `Grid` ### `GridItem` `GridItem` is a component that controls item placement within a Grid container. Not all items inside Grid need to be `GridItem`. ## Columns and Rows You can set `grid-template-columns` or `grid-template-rows` to `repeat(${columns|rows}, minmax(0, 1fr))` by specifying a number to the `columns` or `rows` prop of ``. ```tsx import { Divider, Flex, Grid, HStack } from "@seed-design/react"; export default function GridNumber() { return ( {[1, 2, 3, 4, 5].map((n) => ( {n} ))} {[1, 2, 3, 4, 5].map((n) => ( {n} ))} ); } ``` You can also directly specify `grid-template-columns` or `grid-template-rows` values to the `columns` or `rows` of ``. ```tsx import { Divider, Flex, Grid, HStack } from "@seed-design/react"; export default function GridString() { return ( {[1, 2, 3, 4, 5].map((n) => ( {n} ))} {[1, 2, 3, 4, 5].map((n) => ( {n} ))} ); } ``` ## Spanning Items You can use `` to make grid items span multiple columns or rows. - Specify column span using the `colSpan` or `colStart`, `colEnd` props. Passing `colSpan="full"` will span the entire row (both left and right ends). - Specify row span using the `rowSpan` or `rowStart`, `rowEnd` props. Passing `rowSpan="full"` will span the entire column (both top and bottom ends). ```tsx import { Divider, Grid, GridItem, HStack } from "@seed-design/react"; export default function Spanning() { return ( 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 ); } ``` ## Auto Flow You can specify the direction in which items are placed using the `autoFlow` prop of ``. ```tsx import { useState } from "react"; import { Grid, type GridProps, GridItem, VStack } from "@seed-design/react"; import { SegmentedControl, SegmentedControlItem } from "seed-design/ui/segmented-control"; type AutoFlow = NonNullable; export default function AutoFlow() { const [autoFlow, setAutoFlow] = useState("row"); const isColumn = autoFlow.startsWith("column"); const color = isColumn ? "green" : "purple"; const gridProps = isColumn ? { rows: 3 } : { columns: 3 }; const spanProps = isColumn ? { rowSpan: 2 } : { colSpan: 2 }; return ( {[1, 2].map((n) => ( {n} ))} {[3, 4, 5].map((n) => ( {n} ))} setAutoFlow(value as AutoFlow)} aria-label="Auto Flow" > row row dense column column dense ); } ``` ## Auto Rows / Columns You can specify the size of implicitly created rows or columns using the `autoRows` or `autoColumns` prop of ``. This is useful for grids where items are dynamically added. ```tsx import { Divider, Flex, Grid, HStack } from "@seed-design/react"; export default function AutoRowsColumns() { return ( {[1, 2, 3, 4, 5].map((n) => ( {n === 2 ? "Ea anim non aute minim ea deserunt enim Elit deserunt laborum et quis sit." : n} ))} {[1, 2, 3, 4, 5].map((n) => ( {n === 2 ? "Ea anim non aute minim ea deserunt enim Elit deserunt laborum et quis sit." : n} ))} ); } ``` ## Using `asChild` Prop `GridItem` can apply grid properties directly to child elements using the `asChild` prop. Learn more about the `asChild` prop. ```tsx Link spanning 2 columns ``` ## Using Box for Grid Item Placement `GridItem` internally converts `colSpan`, `colStart`, `colEnd`, `rowSpan`, `rowStart`, `rowEnd` props to `gridColumn` and `gridRow` styles and applies them to `Box`. Therefore, you can also directly use `gridColumn`, `gridRow` props in the `Box` component. ```tsx colSpan=2 colSpan=full colStart=2 colEnd=4 ``` ## Responsive Design You can adjust the layout according to viewport size by passing responsive values to Grid's `columns`, `rows`, `gap`, etc.