import { useScopedFormContext } from '@components/common/page-builder/WidgetSettingsScope.js';
import { _ } from '@evershop/evershop/lib/locale/translate/_';
import { ChevronDown } from 'lucide-react';
import React, { useEffect, useState } from 'react';
// ---------------------------------------------------------------------------
// Drawer-style helpers — same vocabulary as the other widget setting drawers
// (compact 11px label Field, collapsible Section card). Local for now.
// ---------------------------------------------------------------------------
function Field({
label,
hint,
children
}: {
label?: string;
hint?: string;
children: React.ReactNode;
}) {
return (
{label && (
{label}
)}
{children}
{hint &&
{hint}
}
);
}
function Section({
title,
children,
rightSlot
}: {
title: string;
children: React.ReactNode;
rightSlot?: React.ReactNode;
}) {
const [open, setOpen] = useState(true);
return (
{rightSlot}
{open && (
{children}
)}
);
}
// Slider with local display state — controlled rAF/round-trip latency would
// make dragging feel like a click otherwise (same bug we hit on slideshow).
function Slider({
value,
min,
max,
step = 1,
onCommit,
unit
}: {
value: number;
min: number;
max: number;
step?: number;
onCommit: (v: number) => void;
unit?: string;
}) {
const [display, setDisplay] = useState(value);
useEffect(() => {
setDisplay(value);
}, [value]);
return (
);
}
// Segmented control (used for padding).
function Segmented({
value,
options,
onChange
}: {
value: T;
options: ReadonlyArray<{ value: T; label: string }>;
onChange: (v: T) => void;
}) {
return (
{options.map((opt) => {
const active = value === opt.value;
return (
);
})}
);
}
// ---------------------------------------------------------------------------
// Ratio presets. Sorted by column count so the picker reads as a progression.
// SVG-ish previews are pure flex divs — cheaper than an SVG and inherit
// theming.
// ---------------------------------------------------------------------------
interface RatioPreset {
value: string;
label: string;
parts: number[];
}
const RATIO_PRESETS: ReadonlyArray = [
{ value: '1', label: '1', parts: [1] },
{ value: '1-1', label: '1 : 1', parts: [1, 1] },
{ value: '1-2', label: '1 : 2', parts: [1, 2] },
{ value: '2-1', label: '2 : 1', parts: [2, 1] },
{ value: '1-1-1', label: '1 : 1 : 1', parts: [1, 1, 1] },
{ value: '2-1-1', label: '2 : 1 : 1', parts: [2, 1, 1] },
{ value: '1-2-1', label: '1 : 2 : 1', parts: [1, 2, 1] },
{ value: '1-1-2', label: '1 : 1 : 2', parts: [1, 1, 2] },
{ value: '1-1-1-1', label: '1 : 1 : 1 : 1', parts: [1, 1, 1, 1] }
];
function RatioTile({
preset,
active,
onClick
}: {
preset: RatioPreset;
active: boolean;
onClick: () => void;
}) {
return (
);
}
// ---------------------------------------------------------------------------
// Background swatches. "None" is the first option (empty string) so picking
// it round-trips the field to null on the server.
// ---------------------------------------------------------------------------
const BG_SWATCHES: ReadonlyArray<{ value: string; label: string }> = [
{ value: '', label: _('None') },
{ value: '#ffffff', label: _('White') },
{ value: '#f7f7f7', label: _('Light gray') },
{ value: '#111827', label: _('Charcoal') },
{ value: '#fef3c7', label: _('Cream') },
{ value: '#dbeafe', label: _('Sky') },
{ value: '#dcfce7', label: _('Mint') },
{ value: '#fee2e2', label: _('Blush') }
];
const PADDING_OPTIONS: ReadonlyArray<{ value: PaddingPreset; label: string }> =
[
{ value: 'none', label: _('None') },
{ value: 'sm', label: _('S') },
{ value: 'md', label: _('M') },
{ value: 'lg', label: _('L') },
{ value: 'xl', label: _('XL') }
];
type PaddingPreset = 'none' | 'sm' | 'md' | 'lg' | 'xl';
type ColumnAnchor =
| 'tl'
| 'tc'
| 'tr'
| 'ml'
| 'mc'
| 'mr'
| 'bl'
| 'bc'
| 'br';
const ANCHOR_CELLS: ReadonlyArray = [
'tl',
'tc',
'tr',
'ml',
'mc',
'mr',
'bl',
'bc',
'br'
];
// 3×3 dot-grid anchor picker. Same shape and interaction as the Slideshow
// drawer's anchor picker; the widget applies the chosen anchor uniformly
// to all columns.
function AnchorPicker({
value,
onChange
}: {
value: ColumnAnchor;
onChange: (v: ColumnAnchor) => void;
}) {
return (
{ANCHOR_CELLS.map((c) => {
const active = value === c;
return (
);
})}
);
}
// ---------------------------------------------------------------------------
// Component.
// ---------------------------------------------------------------------------
interface ColumnsSettingProps {
// Optional: page-builder drawer mounts this without GraphQL props.
columnsWidget?: {
columnCount?: number;
gap?: number;
ratio?: string | null;
background?: string | null;
padding?: string | null;
contentPosition?: ColumnAnchor | null;
};
}
export default function ColumnsSetting({
columnsWidget
}: ColumnsSettingProps) {
const {
columnCount = 2,
gap = 16,
ratio = null,
background = null,
padding = 'none',
contentPosition = 'mc'
} = columnsWidget ?? {};
const { register, watch, setValue } = useScopedFormContext();
// The ratio is the authoritative layout descriptor; columnCount is derived
// and kept only for back-compat with widgets stored before ratio existed.
// Default falls back to "1-1" so two-column widgets keep their look without
// a migration.
const watchedRatio = (watch('settings.ratio') ?? null) as string | null;
const watchedGap = watch('settings.gap');
const watchedBg = (watch('settings.background') ?? '') as string;
const watchedPadding = (watch('settings.padding') ?? padding ?? 'none') as
| PaddingPreset
| string;
const watchedAnchor = (watch('settings.contentPosition') ??
contentPosition ??
'mc') as ColumnAnchor;
const effectiveRatio: string =
typeof watchedRatio === 'string' && watchedRatio.length > 0
? watchedRatio
: ratio ||
Array.from({ length: Math.max(1, columnCount || 2) }, () => '1').join(
'-'
);
const effectiveGap =
typeof watchedGap === 'number'
? watchedGap
: typeof watchedGap === 'string'
? Number(watchedGap) || gap
: gap;
const effectivePadding: PaddingPreset = (
typeof watchedPadding === 'string' && watchedPadding.length > 0
? watchedPadding
: padding ?? 'none'
) as PaddingPreset;
const handlePickRatio = (next: string) => {
setValue('settings.ratio', next, { shouldDirty: true });
// Keep `columnCount` in sync — older storefront consumers and the legacy
// edit form may still read it before the ratio rolls out everywhere.
const parts = next.split('-').filter(Boolean);
setValue('settings.columnCount', parts.length, { shouldDirty: true });
};
const handlePickBg = (next: string) => {
// Empty string clears the field; resolver maps that to null at the
// GraphQL boundary.
setValue('settings.background', next, { shouldDirty: true });
};
// Custom hex input — kept separate from the swatch grid so picking a swatch
// doesn't have to update an additional piece of UI state.
const isCustomBg =
watchedBg.length > 0 && !BG_SWATCHES.some((s) => s.value === watchedBg);
return (
{/* Layout (ratio) */}
{RATIO_PRESETS.map((p) => (
handlePickRatio(p.value)}
/>
))}
setValue('settings.gap', v, { shouldDirty: true })
}
/>
setValue('settings.contentPosition', v, { shouldDirty: true })
}
/>
{/* Row appearance */}
{/* Hidden mirrors so the standalone widgetEdit `
);
}
export const query = `
query Query(
$columnCount: Int
$gap: Float
$ratio: String
$background: String
$padding: String
$contentPosition: String
) {
columnsWidget(
columnCount: $columnCount
gap: $gap
ratio: $ratio
background: $background
padding: $padding
contentPosition: $contentPosition
) {
columnCount
gap
ratio
background
padding
contentPosition
}
}
`;
export const variables = `{
columnCount: getWidgetSetting("columnCount", 2),
gap: getWidgetSetting("gap", 16),
ratio: getWidgetSetting("ratio"),
background: getWidgetSetting("background"),
padding: getWidgetSetting("padding"),
contentPosition: getWidgetSetting("contentPosition")
}`;