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

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