# Grid

## Introduction

The Grid component is a powerful layout system built on CSS Grid. It uses a 12-column grid system, making it easy to create responsive layouts and apply different layouts at different breakpoints. It is well suited for complex dashboards, product catalogs, card layouts, and more.

```tsx
<Grid container spacing={args.spacing}>
    <GridItem xs={6}>
      <Typography>Item 1 (xs=6)</Typography>
    </GridItem>
    <GridItem xs={6}>
      <Typography>Item 2 (xs=6)</Typography>
    </GridItem>
    <GridItem xs={4}>
      <Typography>Item 3 (xs=4)</Typography>
    </GridItem>
    <GridItem xs={8}>
      <Typography>Item 4 (xs=8)</Typography>
    </GridItem>
  </Grid>
```

| Field     | Description       | Default |
| --------- | ----------------- | ------- |
| container | Grid 컨테이너 여부      | true    |
| spacing   | 그리드 간격            | 2       |
| xs        | xs 브레이크포인트에서의 열 수 | —       |
| sm        | sm 브레이크포인트에서의 열 수 | —       |
| md        | md 브레이크포인트에서의 열 수 | —       |
| lg        | lg 브레이크포인트에서의 열 수 | —       |
| xl        | xl 브레이크포인트에서의 열 수 | —       |

## Usage

```tsx
import { Grid } from '@ceed/cds';

function MyComponent() {
  return (
    <Grid container spacing={2}>
      <Grid item xs={12} sm={6} md={4}>
        첫 번째 아이템
      </Grid>
      <Grid item xs={12} sm={6} md={4}>
        두 번째 아이템
      </Grid>
      <Grid item xs={12} sm={6} md={4}>
        세 번째 아이템
      </Grid>
    </Grid>
  );
}
```

## Examples

### Basic Usage

The basic usage of the 12-column grid system.

```tsx
<div
    style={{
      display: "flex",
      flexDirection: "column",
      gap: "2rem"
    }}
  >
    <div>
      <Typography
        level="title-md"
        sx={{
          mb: 2
        }}
      >
        12열 그리드 시스템
      </Typography>
      <Grid container spacing={2}>
        <GridItem xs={12}>xs=12</GridItem>
        <GridItem xs={6}>xs=6</GridItem>
        <GridItem xs={6}>xs=6</GridItem>
        <GridItem xs={4}>xs=4</GridItem>
        <GridItem xs={4}>xs=4</GridItem>
        <GridItem xs={4}>xs=4</GridItem>
        <GridItem xs={3}>xs=3</GridItem>
        <GridItem xs={3}>xs=3</GridItem>
        <GridItem xs={3}>xs=3</GridItem>
        <GridItem xs={3}>xs=3</GridItem>
      </Grid>
    </div>
  </div>
```

### Responsive Grid

A responsive grid that applies different layouts across different screen sizes.

```tsx
<div>
    <Typography
      level="title-md"
      sx={{
        mb: 2
      }}
    >
      반응형 그리드 (브라우저 크기를 조절해보세요)
    </Typography>
    <Grid container spacing={2}>
      <GridItem xs={12} sm={6} md={4} lg={3}>
        <div>
          <Typography level="body-xs">xs=12</Typography>
          <Typography level="body-xs">sm=6</Typography>
          <Typography level="body-xs">md=4</Typography>
          <Typography level="body-xs">lg=3</Typography>
        </div>
      </GridItem>
      <GridItem xs={12} sm={6} md={4} lg={3}>
        <div>
          <Typography level="body-xs">xs=12</Typography>
          <Typography level="body-xs">sm=6</Typography>
          <Typography level="body-xs">md=4</Typography>
          <Typography level="body-xs">lg=3</Typography>
        </div>
      </GridItem>
      <GridItem xs={12} sm={6} md={4} lg={3}>
        <div>
          <Typography level="body-xs">xs=12</Typography>
          <Typography level="body-xs">sm=6</Typography>
          <Typography level="body-xs">md=4</Typography>
          <Typography level="body-xs">lg=3</Typography>
        </div>
      </GridItem>
      <GridItem xs={12} sm={6} md={4} lg={3}>
        <div>
          <Typography level="body-xs">xs=12</Typography>
          <Typography level="body-xs">sm=6</Typography>
          <Typography level="body-xs">md=4</Typography>
          <Typography level="body-xs">lg=3</Typography>
        </div>
      </GridItem>
    </Grid>
  </div>
```

### Different Spacing

Grid layouts with different spacing values.

```tsx
<div
    style={{
      display: "flex",
      flexDirection: "column",
      gap: "3rem"
    }}
  >
    <div>
      <Typography
        level="title-sm"
        sx={{
          mb: 1
        }}
      >
        간격 없음 (spacing=0)
      </Typography>
      <Grid container spacing={0}>
        <GridItem xs={4}>Item 1</GridItem>
        <GridItem xs={4}>Item 2</GridItem>
        <GridItem xs={4}>Item 3</GridItem>
      </Grid>
    </div>

    <div>
      <Typography
        level="title-sm"
        sx={{
          mb: 1
        }}
      >
        작은 간격 (spacing=1)
      </Typography>
      <Grid container spacing={1}>
        <GridItem xs={4}>Item 1</GridItem>
        <GridItem xs={4}>Item 2</GridItem>
        <GridItem xs={4}>Item 3</GridItem>
      </Grid>
    </div>

    <div>
      <Typography
        level="title-sm"
        sx={{
          mb: 1
        }}
      >
        중간 간격 (spacing=2)
      </Typography>
      <Grid container spacing={2}>
        <GridItem xs={4}>Item 1</GridItem>
        <GridItem xs={4}>Item 2</GridItem>
        <GridItem xs={4}>Item 3</GridItem>
      </Grid>
    </div>

    <div>
      <Typography
        level="title-sm"
        sx={{
          mb: 1
        }}
      >
        큰 간격 (spacing=4)
      </Typography>
      <Grid container spacing={4}>
        <GridItem xs={4}>Item 1</GridItem>
        <GridItem xs={4}>Item 2</GridItem>
        <GridItem xs={4}>Item 3</GridItem>
      </Grid>
    </div>
  </div>
```

### Nested Grid

You can nest one grid inside another.

```tsx
<div>
    <Typography
      level="title-md"
      sx={{
        mb: 2
      }}
    >
      중첩된 그리드
    </Typography>
    <Grid container spacing={2}>
      <Grid xs={12} md={8}>
        <Box
          sx={{
            p: 2,
            bgcolor: "success.50",
            borderRadius: "md",
            minHeight: 200
          }}
        >
          <Typography
            level="title-sm"
            sx={{
              mb: 2
            }}
          >
            메인 콘텐츠 영역
          </Typography>
          <Grid container spacing={1}>
            <GridItem xs={6}>
              <Typography level="body-xs">중첩 아이템 1</Typography>
            </GridItem>
            <GridItem xs={6}>
              <Typography level="body-xs">중첩 아이템 2</Typography>
            </GridItem>
            <GridItem xs={12}>
              <Typography level="body-xs">중첩 아이템 3</Typography>
            </GridItem>
          </Grid>
        </Box>
      </Grid>

      <Grid xs={12} md={4}>
        <Box
          sx={{
            p: 2,
            bgcolor: "warning.50",
            borderRadius: "md",
            minHeight: 200
          }}
        >
          <Typography
            level="title-sm"
            sx={{
              mb: 2
            }}
          >
            사이드바
          </Typography>
          <Grid container spacing={1}>
            <GridItem xs={12}>
              <Typography level="body-xs">사이드바 아이템 1</Typography>
            </GridItem>
            <GridItem xs={12}>
              <Typography level="body-xs">사이드바 아이템 2</Typography>
            </GridItem>
          </Grid>
        </Box>
      </Grid>
    </Grid>
  </div>
```

### Auto Layout

A grid using auto layout.

```tsx
<div
    style={{
      display: "flex",
      flexDirection: "column",
      gap: "2rem"
    }}
  >
    <div>
      <Typography
        level="title-sm"
        sx={{
          mb: 1
        }}
      >
        자동 레이아웃 - 동일한 너비
      </Typography>
      <Grid container spacing={2}>
        <GridItem xs>자동 너비 1</GridItem>
        <GridItem xs>자동 너비 2</GridItem>
        <GridItem xs>자동 너비 3</GridItem>
      </Grid>
    </div>

    <div>
      <Typography
        level="title-sm"
        sx={{
          mb: 1
        }}
      >
        자동 레이아웃 - 내용에 따른 너비
      </Typography>
      <Grid container spacing={2}>
        <GridItem xs="auto">
          <Typography>짧은 내용</Typography>
        </GridItem>
        <GridItem xs>
          <Typography>남은 공간을 모두 차지하는 긴 내용</Typography>
        </GridItem>
        <GridItem xs="auto">
          <Typography>또 다른 짧은 내용</Typography>
        </GridItem>
      </Grid>
    </div>
  </div>
```

### Complex Layouts

Complex layout examples based on real use cases.

```tsx
<div
    style={{
      display: "flex",
      flexDirection: "column",
      gap: "3rem"
    }}
  >
    {/* Dashboard Layout */}
    <div>
      <Typography
        level="title-md"
        sx={{
          mb: 2
        }}
      >
        대시보드 레이아웃
      </Typography>
      <Grid container spacing={2}>
        {/* Header */}
        <Grid xs={12}>
          <Box
            sx={{
              p: 3,
              bgcolor: "primary.100",
              borderRadius: "md",
              textAlign: "center"
            }}
          >
            <Typography level="title-lg">대시보드 헤더</Typography>
          </Box>
        </Grid>

        {/* Stats Cards */}
        <GridItem xs={12} sm={6} lg={3}>
          <div>
            <Typography level="title-sm">총 사용자</Typography>
            <Typography level="h2">1,234</Typography>
          </div>
        </GridItem>
        <GridItem xs={12} sm={6} lg={3}>
          <div>
            <Typography level="title-sm">매출</Typography>
            <Typography level="h2">₩5.6M</Typography>
          </div>
        </GridItem>
        <GridItem xs={12} sm={6} lg={3}>
          <div>
            <Typography level="title-sm">주문</Typography>
            <Typography level="h2">789</Typography>
          </div>
        </GridItem>
        <GridItem xs={12} sm={6} lg={3}>
          <div>
            <Typography level="title-sm">전환율</Typography>
            <Typography level="h2">3.2%</Typography>
          </div>
        </GridItem>

        {/* Main Content */}
        <Grid xs={12} lg={8}>
          <Box
            sx={{
              p: 3,
              bgcolor: "neutral.50",
              borderRadius: "md",
              minHeight: 300
            }}
          >
            <Typography level="title-md">차트 영역</Typography>
            <Typography level="body-sm">여기에 차트가 들어갑니다</Typography>
          </Box>
        </Grid>

        {/* Sidebar */}
        <Grid xs={12} lg={4}>
          <Box
            sx={{
              p: 3,
              bgcolor: "success.50",
              borderRadius: "md",
              minHeight: 300
            }}
          >
            <Typography level="title-md">최근 활동</Typography>
            <Typography level="body-sm">활동 목록이 여기 표시됩니다</Typography>
          </Box>
        </Grid>
      </Grid>
    </div>

    {/* Product Grid */}
    <div>
      <Typography
        level="title-md"
        sx={{
          mb: 2
        }}
      >
        제품 그리드
      </Typography>
      <Grid container spacing={2}>
        {[1, 2, 3, 4, 5, 6].map((item) => (
          <Grid key={item} xs={12} sm={6} md={4} lg={2}>
            <Box
              sx={{
                p: 2,
                bgcolor: "background.body",
                border: "1px solid",
                borderColor: "divider",
                borderRadius: "md",
                textAlign: "center"
              }}
            >
              <Box
                sx={{
                  width: "100%",
                  height: 80,
                  bgcolor: "neutral.100",
                  borderRadius: "sm",
                  mb: 1,
                  display: "flex",
                  alignItems: "center",
                  justifyContent: "center"
                }}
              >
                <Typography level="body-xs">제품 {item}</Typography>
              </Box>
              <Typography level="body-sm">제품명</Typography>
              <Typography
                level="body-xs"
                sx={{
                  color: "text.secondary"
                }}
              >
                ₩10,000
              </Typography>
            </Box>
          </Grid>
        ))}
      </Grid>
    </div>
  </div>
```

### Breakpoint Demo

An example showing how Grid behaves at each breakpoint.

```tsx
<div>
    <Typography
      level="title-md"
      sx={{
        mb: 2
      }}
    >
      브레이크포인트 데모 - 각 화면 크기에서 다른 레이아웃
    </Typography>

    <Box
      sx={{
        mb: 2
      }}
    >
      <Typography
        level="body-sm"
        sx={{
          color: "text.secondary"
        }}
      >
        • xs (0px+): 모든 아이템이 한 줄로
        <br />• sm (600px+): 2개씩 배치
        <br />• md (900px+): 3개씩 배치
        <br />• lg (1200px+): 4개씩 배치
      </Typography>
    </Box>

    <Grid container spacing={2}>
      <GridItem xs={12} sm={6} md={4} lg={3}>
        <Typography level="body-sm">첫 번째 아이템</Typography>
      </GridItem>
      <GridItem xs={12} sm={6} md={4} lg={3}>
        <Typography level="body-sm">두 번째 아이템</Typography>
      </GridItem>
      <GridItem xs={12} sm={6} md={4} lg={3}>
        <Typography level="body-sm">세 번째 아이템</Typography>
      </GridItem>
      <GridItem xs={12} sm={6} md={4} lg={3}>
        <Typography level="body-sm">네 번째 아이템</Typography>
      </GridItem>
      <GridItem xs={12} sm={6} md={4} lg={3}>
        <Typography level="body-sm">다섯 번째 아이템</Typography>
      </GridItem>
      <GridItem xs={12} sm={6} md={4} lg={3}>
        <Typography level="body-sm">여섯 번째 아이템</Typography>
      </GridItem>
      <GridItem xs={12} sm={6} md={4} lg={3}>
        <Typography level="body-sm">일곱 번째 아이템</Typography>
      </GridItem>
      <GridItem xs={12} sm={6} md={4} lg={3}>
        <Typography level="body-sm">여덟 번째 아이템</Typography>
      </GridItem>
    </Grid>
  </div>
```

## Grid System

### 12-Column System

Grid is based on a 12-column system. Each item can span 1 to 12 columns.

```tsx
<Grid container spacing={2}>
  <Grid item xs={12}>
    전체 너비 (12/12)
  </Grid>
  <Grid item xs={6}>
    절반 너비 (6/12)
  </Grid>
  <Grid item xs={6}>
    절반 너비 (6/12)
  </Grid>
  <Grid item xs={4}>
    3분의 1 너비 (4/12)
  </Grid>
  <Grid item xs={4}>
    3분의 1 너비 (4/12)
  </Grid>
  <Grid item xs={4}>
    3분의 1 너비 (4/12)
  </Grid>
</Grid>
```

### Container & Item

- `container`: Used to create a Grid container
- `item`: Used to create a Grid item
- A single component can be both a `container` and an `item` at the same time (nested grid)

## Breakpoints

Grid supports the following breakpoints:

| Breakpoint | Size    | Description                       |
| ---------- | ------- | --------------------------------- |
| `xs`       | 0px+    | Mobile (portrait)                 |
| `sm`       | 600px+  | Mobile (landscape) / small tablet |
| `md`       | 900px+  | Tablet                            |
| `lg`       | 1200px+ | Desktop                           |
| `xl`       | 1536px+ | Large desktop                     |

### Responsive Usage

```tsx
<Grid container spacing={2}>
  <Grid item xs={12} sm={6} md={4} lg={3}>
    {/*
      xs: 100% (12/12) - full width on mobile
      sm: 50% (6/12)   - half width on small screens
      md: 33% (4/12)   - one-third width on tablets
      lg: 25% (3/12)   - one-quarter width on desktop
    */}
    반응형 아이템
  </Grid>
</Grid>
```

## Spacing

You can control the spacing between grid items using the `spacing` prop.

| Value | Size | Description             |
| ----- | ---- | ----------------------- |
| `0`   | 0px  | No spacing              |
| `1`   | 8px  | Very small spacing      |
| `2`   | 16px | Small spacing (default) |
| `3`   | 24px | Medium spacing          |
| `4`   | 32px | Large spacing           |
| `5`   | 40px | Very large spacing      |
| `6`   | 48px | Maximum spacing         |

## Auto Layout

You can let items size and position automatically without explicitly defining the column count:

```tsx
<Grid container spacing={2}>
  <Grid item xs>자동 너비 1</Grid>
  <Grid item xs>자동 너비 2</Grid>
  <Grid item xs>자동 너비 3</Grid>
</Grid>

<Grid container spacing={2}>
  <Grid item xs="auto">내용에 맞춰 자동 크기</Grid>
  <Grid item xs>남은 공간 모두 차지</Grid>
  <Grid item xs="auto">내용에 맞춰 자동 크기</Grid>
</Grid>
```

## Common Use Cases

### Card Layout

```tsx
<Grid container spacing={3}>
  {products.map((product) => (
    <Grid key={product.id} item xs={12} sm={6} md={4} lg={3}>
      <Card>
        <CardContent>
          <Typography level="title-md">{product.name}</Typography>
          <Typography level="body-sm">{product.price}</Typography>
        </CardContent>
      </Card>
    </Grid>
  ))}
</Grid>
```

### Dashboard Layout

```tsx
<Grid container spacing={2}>
  {/* Header */}
  <Grid item xs={12}>
    <Paper sx={{ p: 2 }}>Dashboard Header</Paper>
  </Grid>

  {/* Stats */}
  <Grid item xs={12} sm={6} md={3}>
    <StatCard title="Users" value="1,234" />
  </Grid>
  <Grid item xs={12} sm={6} md={3}>
    <StatCard title="Revenue" value="$5.6M" />
  </Grid>
  <Grid item xs={12} sm={6} md={3}>
    <StatCard title="Orders" value="789" />
  </Grid>
  <Grid item xs={12} sm={6} md={3}>
    <StatCard title="Growth" value="+12%" />
  </Grid>

  {/* Main Content */}
  <Grid item xs={12} lg={8}>
    <Paper sx={{ p: 2, minHeight: 400 }}>Chart Area</Paper>
  </Grid>

  {/* Sidebar */}
  <Grid item xs={12} lg={4}>
    <Paper sx={{ p: 2, minHeight: 400 }}>Recent Activity</Paper>
  </Grid>
</Grid>
```

### Form Layout

```tsx
<Grid container spacing={2}>
  <Grid item xs={12} md={6}>
    <FormControl fullWidth>
      <FormLabel>First Name</FormLabel>
      <Input placeholder="Enter first name" />
    </FormControl>
  </Grid>

  <Grid item xs={12} md={6}>
    <FormControl fullWidth>
      <FormLabel>Last Name</FormLabel>
      <Input placeholder="Enter last name" />
    </FormControl>
  </Grid>

  <Grid item xs={12}>
    <FormControl fullWidth>
      <FormLabel>Email</FormLabel>
      <Input type="email" placeholder="Enter email" />
    </FormControl>
  </Grid>

  <Grid item xs={12}>
    <Button type="submit" fullWidth>
      Submit
    </Button>
  </Grid>
</Grid>
```

### Image Gallery

```tsx
<Grid container spacing={1}>
  {images.map((image, index) => (
    <Grid key={index} item xs={6} sm={4} md={3} lg={2}>
      <Box
        sx={{
          position: 'relative',
          paddingTop: '100%', // maintain a 1:1 ratio
          overflow: 'hidden',
          borderRadius: 'sm',
        }}
      >
        <img
          src={image.src}
          alt={image.alt}
          style={{
            position: 'absolute',
            top: 0,
            left: 0,
            width: '100%',
            height: '100%',
            objectFit: 'cover',
          }}
        />
      </Box>
    </Grid>
  ))}
</Grid>
```

## Best Practices

1. **Use meaningful structure**: Choose appropriate elements while considering semantic HTML structure when using Grid.

2. **Design responsively first**: Start with mobile layouts and scale up to larger screens.

3. **Keep spacing consistent**: Use consistent spacing values across the project.

4. **Optimize performance**:
   - Avoid unnecessarily deep nesting
   - Consider virtualization when rendering many items

5. **Consider accessibility**:
   - Use appropriate semantic tags
   - Arrange content so keyboard navigation remains logical

6. **Prioritize content**: Let the grid adapt to the content instead of forcing content into the grid.

7. **Debug effectively**: Apply background colors to Grid items during development to inspect the layout more easily.

## Props and Customization

### Key Props

| Prop        | Type                                                     | Default | Description                          |
| ----------- | -------------------------------------------------------- | ------- | ------------------------------------ |
| `children`  | `ReactNode`                                              | -       | Grid children (items)                |
| `container` | `boolean`                                                | `false` | Sets the element as a grid container |
| `xs`        | `number \| 'auto' \| boolean`                            | -       | Column span at xs breakpoint         |
| `sm`        | `number \| 'auto' \| boolean`                            | -       | Column span at sm breakpoint         |
| `md`        | `number \| 'auto' \| boolean`                            | -       | Column span at md breakpoint         |
| `lg`        | `number \| 'auto' \| boolean`                            | -       | Column span at lg breakpoint         |
| `xl`        | `number \| 'auto' \| boolean`                            | -       | Column span at xl breakpoint         |
| `spacing`   | `number \| string`                                       | `0`     | Gap between grid items               |
| `direction` | `'row' \| 'row-reverse' \| 'column' \| 'column-reverse'` | `'row'` | Flex direction                       |
| `columns`   | `number`                                                 | `12`    | Total number of columns              |
| `sx`        | `SxProps`                                                | -       | Custom styles                        |

> **Note**: Grid accepts all Joy UI Grid (MUI) props.

## Troubleshooting

### Common Issues

1. **Items are positioned differently than expected**
   - Check that the total column count does not exceed 12
   - Make sure the `container` and `item` props are set correctly

2. **Spacing is not applied**
   - Verify that the `spacing` prop is set on the container
   - Verify that child elements are wrapped in Grid items

3. **Responsive behavior is not working**
   - Check that the breakpoint values are correct
   - Make sure the intended total at each breakpoint is 12 or less

Grid is a powerful tool for building complex layouts. Responsive design and consistent spacing help improve the overall user experience.
