|
"use client" |
|
|
|
import { forwardRef } from "react" |
|
import { |
|
type ConditionalValue, |
|
type SystemContext, |
|
useChakraContext, |
|
} from "../../styled-system" |
|
import { mapObject } from "../../utils" |
|
import { Grid, type GridProps } from "./grid" |
|
|
|
interface SimpleGridOptions { |
|
|
|
|
|
|
|
minChildWidth?: GridProps["minWidth"] | undefined |
|
|
|
|
|
|
|
columns?: ConditionalValue<number> | undefined |
|
} |
|
|
|
export interface SimpleGridProps |
|
extends Omit<GridProps, "columns">, |
|
SimpleGridOptions {} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const SimpleGrid = forwardRef<HTMLDivElement, SimpleGridProps>( |
|
function SimpleGrid(props, ref) { |
|
const { columns, minChildWidth, ...rest } = props |
|
|
|
const sys = useChakraContext() |
|
const templateColumns = minChildWidth |
|
? widthToColumns(minChildWidth, sys) |
|
: countToColumns(columns) |
|
|
|
return <Grid ref={ref} templateColumns={templateColumns} {...rest} /> |
|
}, |
|
) |
|
|
|
function toPx(n: string | number) { |
|
return typeof n === "number" ? `${n}px` : n |
|
} |
|
|
|
function widthToColumns(width: any, sys: SystemContext) { |
|
return mapObject(width, (value) => { |
|
const _value = sys.tokens.getVar(`sizes.${value}`, toPx(value)) |
|
return value === null ? null : `repeat(auto-fit, minmax(${_value}, 1fr))` |
|
}) |
|
} |
|
|
|
function countToColumns(count: any) { |
|
return mapObject(count, (value) => |
|
value === null ? null : `repeat(${value}, minmax(0, 1fr))`, |
|
) |
|
} |
|
|