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 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. import { router } from '@inertiajs/react';
  2. import { 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. <div
  101. className="space-y-5 rounded-xl border p-4"
  102. data-test="style-picker"
  103. >
  104. <div className="flex items-start gap-3">
  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. </div>
  114. <ul className="grid grid-cols-2 gap-2 sm:grid-cols-3">
  115. {presets.map((preset) => {
  116. const selected = active?.key === preset.key;
  117. return (
  118. <li key={preset.key}>
  119. <button
  120. type="button"
  121. disabled={disabled}
  122. aria-pressed={selected}
  123. onClick={() => send({ preset: preset.key })}
  124. className={cn(
  125. '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',
  126. selected &&
  127. 'border-foreground ring-1 ring-foreground',
  128. )}
  129. >
  130. <span
  131. aria-hidden="true"
  132. className="inline-flex w-fit px-3 py-1 text-xs font-semibold"
  133. style={sample(preset)}
  134. >
  135. Shop now
  136. </span>
  137. <span className="text-sm font-medium">
  138. {preset.label}
  139. </span>
  140. <span className="text-xs text-muted-foreground">
  141. {preset.hint}
  142. </span>
  143. {selected && (
  144. <Check className="absolute top-2 right-2 size-4" />
  145. )}
  146. </button>
  147. </li>
  148. );
  149. })}
  150. </ul>
  151. <details className="rounded-lg border px-3 py-2">
  152. <summary className="cursor-pointer text-sm font-medium">
  153. Fine-tune
  154. </summary>
  155. <div className="grid gap-3 py-3 sm:grid-cols-2">
  156. {fineTune.map((option) => (
  157. <div key={option.key} className="grid gap-1.5">
  158. <label
  159. htmlFor={`style-${option.key}`}
  160. className="text-sm"
  161. >
  162. {option.label}
  163. </label>
  164. <select
  165. id={`style-${option.key}`}
  166. value={current.options[option.key]}
  167. disabled={disabled}
  168. onChange={(event) =>
  169. send({
  170. style: {
  171. [option.key]: event.target.value,
  172. },
  173. })
  174. }
  175. className={fieldClass}
  176. >
  177. {Object.entries(option.choices).map(
  178. ([value, label]) => (
  179. <option key={value} value={value}>
  180. {label}
  181. </option>
  182. ),
  183. )}
  184. </select>
  185. </div>
  186. ))}
  187. </div>
  188. </details>
  189. </div>
  190. );
  191. }
  192. /** A tiny button in the preset's shape, in neutral colours. */
  193. function sample(preset: StylePreset): CSSProperties {
  194. const radius = radiusPx[preset.radius] ?? '10px';
  195. if (preset.style.buttons === 'outline') {
  196. return {
  197. borderRadius: radius,
  198. boxShadow: 'inset 0 0 0 1.5px currentColor',
  199. };
  200. }
  201. if (preset.style.buttons === 'shadow') {
  202. return {
  203. borderRadius: radius,
  204. background: '#facc15',
  205. color: '#111',
  206. border: '2px solid #111',
  207. boxShadow: '2px 2px 0 #111',
  208. };
  209. }
  210. return { borderRadius: radius, background: '#111', color: '#fff' };
  211. }