AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

style-picker.tsx 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. import { router } from '@inertiajs/react';
  2. import { ChevronDown, Check, Palette } from 'lucide-react';
  3. import type { CSSProperties } from 'react';
  4. import { useState } from 'react';
  5. import StoreStyleController from '@/actions/App/Http/Controllers/StoreStyleController';
  6. import { cn } from '@/lib/utils';
  7. import type { Store, StoreStyleInfo, StylePreset } from '@/types';
  8. const fieldClass =
  9. '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';
  10. /** The fine-tune choices, in plain words. Keys and values match PlanSchema::STYLE_OPTIONS. */
  11. const fineTune: {
  12. key: string;
  13. label: string;
  14. choices: Record<string, string>;
  15. }[] = [
  16. {
  17. key: 'buttons',
  18. label: 'Buttons',
  19. choices: { solid: 'Solid', outline: 'Outline', shadow: 'Bold shadow' },
  20. },
  21. {
  22. key: 'cards',
  23. label: 'Cards and photos',
  24. choices: {
  25. border: 'Thin line',
  26. shadow: 'Soft shadow',
  27. plain: 'No edge',
  28. hard: 'Bold line and shadow',
  29. },
  30. },
  31. {
  32. key: 'spacing',
  33. label: 'Spacing',
  34. choices: { compact: 'Compact', normal: 'Normal', airy: 'Airy' },
  35. },
  36. {
  37. key: 'headings',
  38. label: 'Headings',
  39. choices: {
  40. normal: 'Normal',
  41. uppercase: 'Spaced capitals',
  42. heavy: 'Extra bold',
  43. },
  44. },
  45. {
  46. key: 'backgrounds',
  47. label: 'Backgrounds',
  48. choices: { plain: 'Plain', alternate: 'Alternating bands' },
  49. },
  50. {
  51. key: 'motion',
  52. label: 'Motion',
  53. choices: { none: 'None', gentle: 'Gentle fade-in' },
  54. },
  55. ];
  56. const radiusPx: Record<string, string> = {
  57. none: '0px',
  58. small: '4px',
  59. medium: '10px',
  60. large: '16px',
  61. full: '9999px',
  62. };
  63. /**
  64. * The Design tab's Style box: pick a ready-made look, or fine-tune one option at a time.
  65. * Colours are kept as they are.
  66. */
  67. export default function StylePicker({
  68. store,
  69. current,
  70. presets,
  71. busy,
  72. }: {
  73. store: Store;
  74. current: StoreStyleInfo | null;
  75. presets: StylePreset[];
  76. busy: boolean;
  77. }) {
  78. const [saving, setSaving] = useState(false);
  79. if (!current) {
  80. return null;
  81. }
  82. function send(data: { preset?: string; style?: Record<string, string> }) {
  83. router.put(StoreStyleController.update.url(store.slug), data, {
  84. preserveScroll: true,
  85. onStart: () => setSaving(true),
  86. onFinish: () => setSaving(false),
  87. });
  88. }
  89. const active = presets.find(
  90. (preset) =>
  91. preset.radius === current.radius &&
  92. preset.fonts.heading === current.fonts?.heading &&
  93. preset.fonts.body === current.fonts?.body &&
  94. Object.entries(preset.style).every(
  95. ([key, value]) => current.options[key] === value,
  96. ),
  97. );
  98. const disabled = busy || saving;
  99. return (
  100. <details
  101. className="group rounded-xl border p-4"
  102. data-test="style-picker"
  103. >
  104. <summary className="flex cursor-pointer list-none items-start gap-3 [&::-webkit-details-marker]:hidden">
  105. <Palette className="mt-0.5 size-5 shrink-0 text-muted-foreground" />
  106. <div>
  107. <h3 className="font-medium">Style</h3>
  108. <p className="mt-0.5 text-sm text-muted-foreground">
  109. The feel of your whole store: buttons, cards, fonts,
  110. spacing and motion. Your colours stay the same.
  111. </p>
  112. </div>
  113. <ChevronDown
  114. className="mt-1 ml-auto size-4 shrink-0 text-muted-foreground transition-transform group-open:rotate-180"
  115. aria-hidden="true"
  116. />
  117. </summary>
  118. <div className="mt-5 space-y-5">
  119. <ul className="grid grid-cols-2 gap-2 sm:grid-cols-3">
  120. {presets.map((preset) => {
  121. const selected = active?.key === preset.key;
  122. return (
  123. <li key={preset.key}>
  124. <button
  125. type="button"
  126. disabled={disabled}
  127. aria-pressed={selected}
  128. onClick={() => send({ preset: preset.key })}
  129. className={cn(
  130. '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',
  131. selected &&
  132. 'border-foreground ring-1 ring-foreground',
  133. )}
  134. >
  135. <span
  136. aria-hidden="true"
  137. className="inline-flex w-fit px-3 py-1 text-xs font-semibold"
  138. style={sample(preset)}
  139. >
  140. Shop now
  141. </span>
  142. <span className="text-sm font-medium">
  143. {preset.label}
  144. </span>
  145. <span className="text-xs text-muted-foreground">
  146. {preset.hint}
  147. </span>
  148. {selected && (
  149. <Check className="absolute top-2 right-2 size-4" />
  150. )}
  151. </button>
  152. </li>
  153. );
  154. })}
  155. </ul>
  156. <details className="rounded-lg border px-3 py-2">
  157. <summary className="cursor-pointer text-sm font-medium">
  158. Fine-tune
  159. </summary>
  160. <div className="grid gap-3 py-3 sm:grid-cols-2">
  161. {fineTune.map((option) => (
  162. <div key={option.key} className="grid gap-1.5">
  163. <label
  164. htmlFor={`style-${option.key}`}
  165. className="text-sm"
  166. >
  167. {option.label}
  168. </label>
  169. <select
  170. id={`style-${option.key}`}
  171. value={current.options[option.key]}
  172. disabled={disabled}
  173. onChange={(event) =>
  174. send({
  175. style: {
  176. [option.key]:
  177. event.target.value,
  178. },
  179. })
  180. }
  181. className={fieldClass}
  182. >
  183. {Object.entries(option.choices).map(
  184. ([value, label]) => (
  185. <option key={value} value={value}>
  186. {label}
  187. </option>
  188. ),
  189. )}
  190. </select>
  191. </div>
  192. ))}
  193. </div>
  194. </details>
  195. </div>
  196. </details>
  197. );
  198. }
  199. /** A tiny button in the preset's shape, in neutral colours. */
  200. function sample(preset: StylePreset): CSSProperties {
  201. const radius = radiusPx[preset.radius] ?? '10px';
  202. if (preset.style.buttons === 'outline') {
  203. return {
  204. borderRadius: radius,
  205. boxShadow: 'inset 0 0 0 1.5px currentColor',
  206. };
  207. }
  208. if (preset.style.buttons === 'shadow') {
  209. return {
  210. borderRadius: radius,
  211. background: '#facc15',
  212. color: '#111',
  213. border: '2px solid #111',
  214. boxShadow: '2px 2px 0 #111',
  215. };
  216. }
  217. return { borderRadius: radius, background: '#111', color: '#fff' };
  218. }