import Area from '@components/common/Area.js'; import { isPageBuilderActive, useWidgetUid } from '@components/common/page-builder/index.js'; import React, { useEffect, useState } from 'react'; export type ColumnAnchor = | 'tl' | 'tc' | 'tr' | 'ml' | 'mc' | 'mr' | 'bl' | 'bc' | 'br'; interface ColumnsProps { columnsWidget: { columnCount: number; gap: number; ratio?: string | null; background?: string | null; padding?: string | null; /** 9-anchor content alignment applied uniformly to every column. The * inner Area is wrapped in a flex container; the anchor maps to * `justify-*` (main axis) + `items-*` (cross axis) classes. */ contentPosition?: ColumnAnchor | null; }; } // Anchor → flex classes. Only `justify-*` (vertical / main axis) varies; // `text-*` handles horizontal alignment of inner content. Cross-axis is // left as the default `stretch` so child widgets keep their full column // width — earlier `items-center` etc. collapsed widgets to content width, // hiding empty / inline children behind their drop zones. const ANCHOR_CLASS: Record = { tl: 'justify-start text-left', tc: 'justify-start text-center', tr: 'justify-start text-right', ml: 'justify-center text-left', mc: 'justify-center text-center', mr: 'justify-center text-right', bl: 'justify-end text-left', bc: 'justify-end text-center', br: 'justify-end text-right' }; // Responsive padding presets — fixed string literals so Tailwind's JIT // picks them up. Vertical scales faster than horizontal across breakpoints // because hero-like rows usually want a tall band on desktop without // crushing mobile viewports. const PADDING_CLASS: Record = { none: '', sm: 'py-3 px-3 md:py-4 md:px-4', md: 'py-5 px-4 md:py-7 md:px-6', lg: 'py-7 px-4 md:py-12 md:px-8', xl: 'py-10 px-4 md:py-16 md:px-12' }; function parseRatio( ratio: string | null | undefined, fallbackCount: number ): { parts: number[]; gridCols: string } { const raw = typeof ratio === 'string' && ratio.length > 0 ? ratio : Array.from({ length: Math.max(1, fallbackCount) }, () => '1').join('-'); const parts = raw .split('-') .map((p) => Math.max(1, Math.min(6, Number(p) || 1))); return { parts, gridCols: parts.map((p) => `${p}fr`).join(' ') }; } /** * Container widget. Renders one Area per column with synthetic id * `columnsContainer__col_`. `loadWidgetInstances` emits this * widget's children with matching `areaId` values, so child widgets * render inside their column via the standard Area mechanism. * * Layout knobs (all optional, sane fallbacks): * - `ratio` — e.g. "1-1", "1-2-1". Drives column count + proportions. * Falls back to evenly-split `columnCount` for back-compat with widgets * saved before the ratio field existed. * - `gap` — pixel gap between columns. * - `background` — hex color applied to the row container. * - `padding` — preset (`none|sm|md|lg|xl`) mapped to responsive * Tailwind classes so mobile breakpoints don't crush the layout. * * Responsive behavior: below `md` (768px) all columns stack full-width * (single-column grid) regardless of `ratio`/`columnCount` — a narrow * viewport can't fit multiple ratio-based columns without crushing their * content. The ratio grid only applies from `md` up, via a CSS custom * property (`--evershop-columns-grid`) read by an injected media-query * rule, matching the pattern used by FooterMenu / TieredCategories. * * Outside the page builder, `useWidgetUid` returns the widget's uuid via * the `WidgetContextProvider` mounted by `WidgetChrome` for every widget * (also outside the iframe — chrome's outer branch always wraps in * provider, no DOM overhead). */ export default function Columns({ columnsWidget: { columnCount, gap, ratio, background, padding, contentPosition } }: ColumnsProps) { const uid = useWidgetUid(); // SSR-stable mode detection: first render passes through identically. const [inPb, setInPb] = useState(false); useEffect(() => { setInPb(isPageBuilderActive()); }, []); const safeGap = typeof gap === 'number' ? Math.max(0, Math.min(80, gap)) : 16; const { parts, gridCols } = parseRatio(ratio, columnCount || 2); const paddingClass = PADDING_CLASS[padding || 'none'] ?? ''; const wrapperBg = background || undefined; const anchor = (contentPosition || 'mc') as ColumnAnchor; const anchorClass = ANCHOR_CLASS[anchor] ?? ANCHOR_CLASS.mc; if (!uid) return null; return ( <> {/* Mobile stacks columns full-width; the ratio-based grid only * kicks in from `md` (768px) up, matching the breakpoint used by * FooterMenu / TieredCategories for the same reason — narrow * viewports can't fit multiple ratio columns without crushing * their content. * Note: `grid-template-columns` must live entirely in this * stylesheet (not inline on the element) — an inline style always * wins over any stylesheet rule, media query or not, so setting it * inline would make the desktop override never apply. */}