| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- import { router } from '@inertiajs/react';
- import { ChevronDown, LayoutTemplate } from 'lucide-react';
- import { useState } from 'react';
- import SectionLayoutController from '@/actions/App/Http/Controllers/SectionLayoutController';
- import { SectionName } from '@/components/stores/preview-highlight';
- import { typeNames } from '@/components/stores/section-names';
- import type { LayoutSection, Store } from '@/types';
-
- const fieldClass =
- 'h-9 w-full min-w-0 rounded-md border border-input bg-transparent px-3 py-1 text-base shadow-xs outline-none focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50 disabled:opacity-50 md:text-sm';
-
- const layoutNames: Record<string, Record<string, string>> = {
- header: { simple: 'Logo on the left', centered: 'Logo in the middle' },
- trust_bar: { row: 'Icon row', boxed: 'Small cards' },
- shop_by: { circles: 'Round photos', tiles: 'Photo tiles' },
- bento: { mosaic: 'One big tile and smaller ones', even: 'Equal tiles' },
- video: { wide: 'Wide video', split: 'Words beside the video' },
- product_grid: {
- grid: 'Grid',
- minimal: 'Centred grid',
- carousel: 'Sliding row',
- large: 'Large cards',
- },
- featured_product: {
- 'image-left': 'Photo on the left',
- 'image-right': 'Photo on the right',
- stacked: 'Wide photo on top',
- },
- about: {
- simple: 'Centred text',
- 'with-stats': 'Text with numbers',
- statement: 'One big statement',
- },
- testimonials: {
- cards: 'Cards',
- single: 'One big quote',
- wall: 'Wall of quotes',
- },
- faq: {
- list: 'Fold-open list',
- 'two-column': 'Two columns',
- cards: 'All answers in cards',
- },
- newsletter: {
- banner: 'Colour banner',
- card: 'Colour card',
- split: 'Heading beside the form',
- },
- contact: { simple: 'Simple', card: 'Card' },
- footer: { simple: 'One line', centered: 'Centred', columns: 'Columns' },
- };
-
- /**
- * The Design tab's Layouts box: the layout of every section, header and footer included.
- * Heroes have their own box.
- */
- export default function LayoutPicker({
- store,
- sections,
- options,
- busy,
- }: {
- store: Store;
- sections: LayoutSection[];
- options: Record<string, string[]>;
- busy: boolean;
- }) {
- const [saving, setSaving] = useState<string | null>(null);
-
- if (sections.length === 0) {
- return null;
- }
-
- function choose(section: LayoutSection, variant: string) {
- router.put(
- SectionLayoutController.update.url({
- slug: store.slug,
- sectionId: section.section_id,
- }),
- { variant },
- {
- preserveScroll: true,
- onStart: () => setSaving(section.section_id),
- onFinish: () => setSaving(null),
- },
- );
- }
-
- return (
- <details
- className="group rounded-xl border p-4"
- data-test="layout-picker"
- >
- <summary className="flex cursor-pointer list-none items-start gap-3 [&::-webkit-details-marker]:hidden">
- <LayoutTemplate className="mt-0.5 size-5 shrink-0 text-muted-foreground" />
- <div>
- <h3 className="font-medium">Layouts</h3>
- <p className="mt-0.5 text-sm text-muted-foreground">
- How each part of your store is arranged. Open to change
- one.
- </p>
- </div>
- <ChevronDown
- className="mt-1 ml-auto size-4 shrink-0 text-muted-foreground transition-transform group-open:rotate-180"
- aria-hidden="true"
- />
- </summary>
-
- <ul className="mt-4 divide-y">
- {sections.map((section) => {
- const id = `layout-${section.section_id}`;
- const names = layoutNames[section.type] ?? {};
-
- return (
- <li
- key={section.section_id}
- className="grid gap-2 py-3 sm:grid-cols-[1fr_220px] sm:items-center"
- >
- <SectionName
- target={{
- sectionId: section.section_id,
- pageId: section.page_id,
- label:
- typeNames[section.type] ?? section.type,
- }}
- className="text-sm"
- >
- <span className="font-medium">
- {typeNames[section.type] ?? section.type}
- </span>
- {section.label &&
- !['header', 'footer'].includes(
- section.type,
- ) && (
- <span className="text-muted-foreground">
- {' '}
- · “{section.label}”
- </span>
- )}
- {section.page_id &&
- section.page_id !== 'home' && (
- <span className="text-muted-foreground">
- {' '}
- on {section.page_title}
- </span>
- )}
- </SectionName>
- <select
- id={id}
- aria-label={`Layout of ${typeNames[section.type] ?? section.type}`}
- value={section.variant}
- disabled={busy || saving === section.section_id}
- onChange={(event) =>
- choose(section, event.target.value)
- }
- className={fieldClass}
- >
- {(options[section.type] ?? []).map(
- (variant) => (
- <option key={variant} value={variant}>
- {names[variant] ?? variant}
- </option>
- ),
- )}
- </select>
- </li>
- );
- })}
- </ul>
- </details>
- );
- }
|