| 1 | import { SlateElement, type SlateElementProps } from "platejs/static"; |
| 2 | |
| 3 | import { cn } from "@/lib/utils"; |
| 4 | import { getDefaultColumnSize, type PresentationColumnSize } from "../../utils"; |
| 5 | |
| 6 | function getStaticComparisonGridColumns( |
| 7 | columnSize: PresentationColumnSize, |
| 8 | itemCount: number, |
| 9 | ) { |
| 10 | const columnSizeToColumns = { |
| 11 | sm: 4, |
| 12 | md: 3, |
| 13 | lg: 2, |
| 14 | xl: 1, |
| 15 | } satisfies Record<PresentationColumnSize, number>; |
| 16 | const columns = Math.max( |
| 17 | 1, |
| 18 | Math.min(itemCount, columnSizeToColumns[columnSize]), |
| 19 | ); |
| 20 | |
| 21 | if (columns === 4) return "grid-cols-4"; |
| 22 | if (columns === 3) return "grid-cols-3"; |
| 23 | if (columns === 2) return "grid-cols-2"; |
| 24 | return "grid-cols-1"; |
| 25 | } |
| 26 | |
| 27 | export default function CompareGroupStatic(props: SlateElementProps) { |
| 28 | const { alignment = "center" } = props.element as { |
| 29 | alignment?: "left" | "center" | "right"; |
| 30 | columnSize?: PresentationColumnSize; |
| 31 | }; |
| 32 | const childCount = props.element.children?.length ?? 0; |
| 33 | const columnSize = |
| 34 | (props.element.columnSize as PresentationColumnSize | undefined) ?? |
| 35 | getDefaultColumnSize(childCount); |
| 36 | |
| 37 | return ( |
| 38 | <SlateElement {...props} className="mb-4"> |
| 39 | <div |
| 40 | className={cn( |
| 41 | "relative grid max-w-full auto-rows-fr items-stretch gap-6 *:min-w-0", |
| 42 | getStaticComparisonGridColumns(columnSize, childCount), |
| 43 | alignment === "left" && "justify-start", |
| 44 | alignment === "right" && "justify-end", |
| 45 | alignment === "center" && "justify-center", |
| 46 | )} |
| 47 | > |
| 48 | {props.children} |
| 49 | </div> |
| 50 | </SlateElement> |
| 51 | ); |
| 52 | } |
| 53 |