| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 |
- import { router } from '@inertiajs/react';
- import { Check, Palette } from 'lucide-react';
- import type { CSSProperties } from 'react';
- import { useState } from 'react';
- import StoreStyleController from '@/actions/App/Http/Controllers/StoreStyleController';
- import { cn } from '@/lib/utils';
- import type { Store, StoreStyleInfo, StylePreset } 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';
-
- /** The fine-tune choices, in plain words. Keys and values match PlanSchema::STYLE_OPTIONS. */
- const fineTune: {
- key: string;
- label: string;
- choices: Record<string, string>;
- }[] = [
- {
- key: 'buttons',
- label: 'Buttons',
- choices: { solid: 'Solid', outline: 'Outline', shadow: 'Bold shadow' },
- },
- {
- key: 'cards',
- label: 'Cards and photos',
- choices: {
- border: 'Thin line',
- shadow: 'Soft shadow',
- plain: 'No edge',
- hard: 'Bold line and shadow',
- },
- },
- {
- key: 'spacing',
- label: 'Spacing',
- choices: { compact: 'Compact', normal: 'Normal', airy: 'Airy' },
- },
- {
- key: 'headings',
- label: 'Headings',
- choices: {
- normal: 'Normal',
- uppercase: 'Spaced capitals',
- heavy: 'Extra bold',
- },
- },
- {
- key: 'backgrounds',
- label: 'Backgrounds',
- choices: { plain: 'Plain', alternate: 'Alternating bands' },
- },
- {
- key: 'motion',
- label: 'Motion',
- choices: { none: 'None', gentle: 'Gentle fade-in' },
- },
- ];
-
- const radiusPx: Record<string, string> = {
- none: '0px',
- small: '4px',
- medium: '10px',
- large: '16px',
- full: '9999px',
- };
-
- /**
- * The Design tab's Style box: pick a ready-made look, or fine-tune one option at a time.
- * Colours are kept as they are.
- */
- export default function StylePicker({
- store,
- current,
- presets,
- busy,
- }: {
- store: Store;
- current: StoreStyleInfo | null;
- presets: StylePreset[];
- busy: boolean;
- }) {
- const [saving, setSaving] = useState(false);
-
- if (!current) {
- return null;
- }
-
- function send(data: { preset?: string; style?: Record<string, string> }) {
- router.put(StoreStyleController.update.url(store.slug), data, {
- preserveScroll: true,
- onStart: () => setSaving(true),
- onFinish: () => setSaving(false),
- });
- }
-
- const active = presets.find(
- (preset) =>
- preset.radius === current.radius &&
- preset.fonts.heading === current.fonts?.heading &&
- preset.fonts.body === current.fonts?.body &&
- Object.entries(preset.style).every(
- ([key, value]) => current.options[key] === value,
- ),
- );
- const disabled = busy || saving;
-
- return (
- <div
- className="space-y-5 rounded-xl border p-4"
- data-test="style-picker"
- >
- <div className="flex items-start gap-3">
- <Palette className="mt-0.5 size-5 shrink-0 text-muted-foreground" />
- <div>
- <h3 className="font-medium">Style</h3>
- <p className="mt-0.5 text-sm text-muted-foreground">
- The feel of your whole store: buttons, cards, fonts,
- spacing and motion. Your colours stay the same.
- </p>
- </div>
- </div>
-
- <ul className="grid grid-cols-2 gap-2 sm:grid-cols-3">
- {presets.map((preset) => {
- const selected = active?.key === preset.key;
-
- return (
- <li key={preset.key}>
- <button
- type="button"
- disabled={disabled}
- aria-pressed={selected}
- onClick={() => send({ preset: preset.key })}
- className={cn(
- 'relative flex h-full w-full flex-col gap-2 rounded-lg border p-3 text-left transition-colors hover:bg-muted/50 disabled:opacity-50',
- selected &&
- 'border-foreground ring-1 ring-foreground',
- )}
- >
- <span
- aria-hidden="true"
- className="inline-flex w-fit px-3 py-1 text-xs font-semibold"
- style={sample(preset)}
- >
- Shop now
- </span>
- <span className="text-sm font-medium">
- {preset.label}
- </span>
- <span className="text-xs text-muted-foreground">
- {preset.hint}
- </span>
- {selected && (
- <Check className="absolute top-2 right-2 size-4" />
- )}
- </button>
- </li>
- );
- })}
- </ul>
-
- <details className="rounded-lg border px-3 py-2">
- <summary className="cursor-pointer text-sm font-medium">
- Fine-tune
- </summary>
- <div className="grid gap-3 py-3 sm:grid-cols-2">
- {fineTune.map((option) => (
- <div key={option.key} className="grid gap-1.5">
- <label
- htmlFor={`style-${option.key}`}
- className="text-sm"
- >
- {option.label}
- </label>
- <select
- id={`style-${option.key}`}
- value={current.options[option.key]}
- disabled={disabled}
- onChange={(event) =>
- send({
- style: {
- [option.key]: event.target.value,
- },
- })
- }
- className={fieldClass}
- >
- {Object.entries(option.choices).map(
- ([value, label]) => (
- <option key={value} value={value}>
- {label}
- </option>
- ),
- )}
- </select>
- </div>
- ))}
- </div>
- </details>
- </div>
- );
- }
-
- /** A tiny button in the preset's shape, in neutral colours. */
- function sample(preset: StylePreset): CSSProperties {
- const radius = radiusPx[preset.radius] ?? '10px';
-
- if (preset.style.buttons === 'outline') {
- return {
- borderRadius: radius,
- boxShadow: 'inset 0 0 0 1.5px currentColor',
- };
- }
-
- if (preset.style.buttons === 'shadow') {
- return {
- borderRadius: radius,
- background: '#facc15',
- color: '#111',
- border: '2px solid #111',
- boxShadow: '2px 2px 0 #111',
- };
- }
-
- return { borderRadius: radius, background: '#111', color: '#fff' };
- }
|