|
1 | 1 | import type { ComponentConfig } from "@puckeditor/core"; |
2 | | -import { cva } from "class-variance-authority"; |
3 | | -import { cn } from "@/lib/utils"; |
4 | | -import { fields, gapVariants, type Spacing } from "@/lib/puck/tokens"; |
| 2 | +import { defineProps, responsive, field } from "@/components/puck/define-props"; |
| 3 | +import { columnCount, spacing, type ColumnCount, type Spacing } from "@/lib/puck/tokens"; |
| 4 | +import type { ResponsiveValue } from "@/lib/puck/responsive"; |
| 5 | +import { getGridClassName, getMaxCols } from "@/components/puck/layout"; |
5 | 6 |
|
6 | | -const columnValues = ["2", "3", "4", "5", "6"] as const; |
7 | | -type ColumnCount = (typeof columnValues)[number]; |
8 | | - |
9 | | -const columnsVariants = cva("grid", { |
10 | | - variants: { |
11 | | - columns: { |
12 | | - "2": "grid-cols-1 md:grid-cols-2", |
13 | | - "3": "grid-cols-1 md:grid-cols-3", |
14 | | - "4": "grid-cols-1 md:grid-cols-2 lg:grid-cols-4", |
15 | | - "5": "grid-cols-1 md:grid-cols-3 lg:grid-cols-5", |
16 | | - "6": "grid-cols-1 md:grid-cols-3 lg:grid-cols-6", |
17 | | - }, |
18 | | - gap: gapVariants, |
19 | | - }, |
20 | | - defaultVariants: { |
21 | | - columns: "2", |
22 | | - gap: "md", |
23 | | - }, |
24 | | -}); |
| 7 | +type SlotKey = `column${ColumnCount}`; |
25 | 8 |
|
26 | 9 | type ColumnsProps = { |
27 | | - column1: any; |
28 | | - column2: any; |
29 | | - column3: any; |
30 | | - column4: any; |
31 | | - column5: any; |
32 | | - column6: any; |
33 | | - columns: ColumnCount; |
34 | | - gap: Spacing; |
| 10 | + [K in SlotKey]: any; |
| 11 | +} & { |
| 12 | + columns: ResponsiveValue<ColumnCount>; |
| 13 | + gap: ResponsiveValue<Spacing>; |
35 | 14 | }; |
36 | 15 |
|
37 | | -const slotField = { type: "slot" as const }; |
| 16 | +const slotField = { type: "slot" } as const; |
| 17 | +const columnSlotKeys = columnCount.options.map( |
| 18 | + ({ value }) => `column${value}` as SlotKey, |
| 19 | +); |
| 20 | + |
| 21 | +const columnSlotFields = Object.fromEntries( |
| 22 | + columnSlotKeys.map((key) => [key, slotField]), |
| 23 | +) as Record<SlotKey, typeof slotField>; |
| 24 | + |
| 25 | +const props = defineProps({ |
| 26 | + columns: responsive.token(columnCount, { label: "Columns", default: { base: "1", md: "2" } }), |
| 27 | + gap: responsive.token(spacing, { label: "Gap", default: "md" }), |
| 28 | +}); |
38 | 29 |
|
39 | 30 | export const Columns: ComponentConfig<ColumnsProps> = { |
40 | 31 | label: "Columns", |
41 | | - fields: { |
42 | | - column1: slotField, |
43 | | - column2: slotField, |
44 | | - column3: slotField, |
45 | | - column4: slotField, |
46 | | - column5: slotField, |
47 | | - column6: slotField, |
48 | | - columns: { |
49 | | - type: "select", |
50 | | - label: "Columns", |
51 | | - options: columnValues.map((v) => ({ label: v, value: v })), |
52 | | - }, |
53 | | - gap: fields.gap(), |
54 | | - }, |
55 | | - defaultProps: { |
56 | | - columns: "2", |
57 | | - gap: "md", |
58 | | - } as ColumnsProps, |
| 32 | + fields: { ...columnSlotFields, ...props.fields }, |
| 33 | + defaultProps: props.defaultProps as ColumnsProps, |
| 34 | + // Hide column slot fields that exceed the selected column count, |
| 35 | + // so the editor sidebar only shows slots that are actually rendered. |
59 | 36 | resolveFields: (data) => { |
60 | 37 | const f = { ...Columns.fields! }; |
61 | | - const colCount = parseInt(data.props.columns ?? "2", 10); |
| 38 | + const columns = data.props.columns ?? { base: "1", md: "2" }; |
| 39 | + const maxCols = getMaxCols(columns); |
62 | 40 |
|
63 | | - f.column1 = { ...f.column1, visible: colCount >= 1 }; |
64 | | - f.column2 = { ...f.column2, visible: colCount >= 2 }; |
65 | | - f.column3 = { ...f.column3, visible: colCount >= 3 }; |
66 | | - f.column4 = { ...f.column4, visible: colCount >= 4 }; |
67 | | - f.column5 = { ...f.column5, visible: colCount >= 5 }; |
68 | | - f.column6 = { ...f.column6, visible: colCount >= 6 }; |
| 41 | + for (const [index, key] of columnSlotKeys.entries()) { |
| 42 | + f[key] = { ...f[key], visible: index < maxCols }; |
| 43 | + } |
69 | 44 |
|
70 | 45 | return f; |
71 | 46 | }, |
72 | | - render: ({ |
73 | | - column1: Column1, |
74 | | - column2: Column2, |
75 | | - column3: Column3, |
76 | | - column4: Column4, |
77 | | - column5: Column5, |
78 | | - column6: Column6, |
79 | | - columns, |
80 | | - gap, |
81 | | - }) => { |
82 | | - const colCount = parseInt(columns ?? "2", 10); |
83 | | - const columnSlots = [Column1, Column2, Column3, Column4, Column5, Column6]; |
| 47 | + render: ({ columns, gap, ...slots }) => { |
| 48 | + const maxCols = getMaxCols(columns); |
| 49 | + const slotMap = slots as Record<SlotKey, any>; |
84 | 50 |
|
85 | 51 | return ( |
86 | | - <div className={cn(columnsVariants({ columns, gap }))}> |
87 | | - {columnSlots |
88 | | - .slice(0, colCount) |
89 | | - .map((Col, i) => Col && <Col key={i} />)} |
| 52 | + <div className={getGridClassName({ columns, rows: { base: "auto" }, gap })}> |
| 53 | + {columnSlotKeys.slice(0, maxCols).map((key) => { |
| 54 | + const Col = slotMap[key]; |
| 55 | + return Col && <Col key={key} />; |
| 56 | + })} |
90 | 57 | </div> |
91 | 58 | ); |
92 | 59 | }, |
|
0 commit comments