Grid
Gridは、グリッドレイアウトを管理するためのコンポーネントです。また、便利なスタイルのショートハンドが用意されています。
インポート
import { Grid, GridItem } from "@yamada-ui/react"
Gridに用意されているショートハンドは下記の通りです。
templateColumns:gridTemplateColumnsを短縮したpropsです。templateRows:gridTemplateRowsを短縮したpropsです。templateAreas:gridTemplateAreasを短縮したpropsです。column:gridColumnを短縮したpropsです。row:gridRowを短縮したpropsです。area:gridAreaを短縮したpropsです。autoFlow:gridAutoFlowを短縮したpropsです。autoColumns:gridAutoColumnsを短縮したpropsです。autoRows:gridAutoRowsを短縮したpropsです。
GridItemに用意されているショートハンドは下記の通りです。
area:gridAreaを短縮したpropsです。colStart:gridColumnStartを短縮したpropsです。colEnd:gridColumnEndを短縮したpropsです。rowStart:gridRowStartを短縮したpropsです。rowEnd:gridRowEndを短縮したpropsです。
ショートハンド以外にも、冗長なpropsを渡すこともできます。
使い方
編集可能な例
<Grid templateColumns="repeat(4, 1fr)" gap="md"> <For each={["primary", "secondary", "warning", "danger"]}> {(bg, index) => ( <GridItem key={index} w="full" h="4xs" rounded="md" bg={bg} /> )} </For> </Grid>
templateAreasを使う
編集可能な例
<Grid templateAreas={` "one one two three" "four five two six" "four seven seven eight" `} gap="md" > <For each={[ { area: "one", bg: "primary" }, { area: "two", bg: "secondary" }, { area: "three", bg: "warning" }, { area: "four", bg: "danger" }, { area: "five", bg: "primary" }, { area: "six", bg: "secondary" }, { area: "seven", bg: "warning" }, { area: "eight", bg: "danger" }, ]} > {({ area, bg }, index) => ( <GridItem key={index} area={area} w="full" minH="4xs" rounded="md" bg={bg} /> )} </For> </Grid>
複数の列と行をまたがる
編集可能な例
<Grid w="full" templateColumns="repeat(4, 1fr)" templateRows="repeat(3, 1fr)" gap="md" > <GridItem colSpan={2} w="full" minH="4xs" rounded="md" bg="primary" /> <GridItem colSpan={2} rowSpan={3} w="full" minH="4xs" rounded="md" bg="secondary" /> <GridItem rowStart={2} rowEnd={4} w="full" minH="4xs" rounded="md" bg="warning" /> <GridItem colStart={2} colEnd={3} rowStart={2} rowEnd={4} w="full" minH="4xs" rounded="md" bg="danger" /> </Grid>
GitHubでこのページを編集する

