create-brainerce-store 1.62.0 → 1.63.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (196) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +71 -71
  3. package/dist/index.js +5 -2
  4. package/messages/en.json +502 -502
  5. package/messages/he.json +502 -502
  6. package/package.json +1 -1
  7. package/templates/nextjs/base/.claude/commands/design.md +60 -60
  8. package/templates/nextjs/base/.env.local.ejs +24 -24
  9. package/templates/nextjs/base/.eslintrc.json +57 -57
  10. package/templates/nextjs/base/.mcp.json +8 -8
  11. package/templates/nextjs/base/AI-GUIDE.md +174 -174
  12. package/templates/nextjs/base/TRANSLATIONS.md +200 -200
  13. package/templates/nextjs/base/package.json.ejs +52 -52
  14. package/templates/nextjs/base/src/app/blog/[slug]/page.tsx.ejs +262 -262
  15. package/templates/nextjs/base/src/app/blog/page.tsx.ejs +276 -276
  16. package/templates/nextjs/base/src/app/cart/page.tsx +7 -7
  17. package/templates/nextjs/base/src/app/category/[slug]/page.tsx +106 -106
  18. package/templates/nextjs/base/src/app/checkout/page.tsx +12 -0
  19. package/templates/nextjs/base/src/app/globals.css +36 -36
  20. package/templates/nextjs/base/src/app/indexnow-key.txt/route.ts +26 -26
  21. package/templates/nextjs/base/src/app/layout.tsx.ejs +10 -0
  22. package/templates/nextjs/base/src/app/llms.txt/route.ts +44 -44
  23. package/templates/nextjs/base/src/app/order-confirmation/page.tsx +295 -278
  24. package/templates/nextjs/base/src/app/order-status/page.tsx +121 -121
  25. package/templates/nextjs/base/src/app/page.tsx +64 -64
  26. package/templates/nextjs/base/src/app/products/[slug]/page.tsx +175 -175
  27. package/templates/nextjs/base/src/app/products/page.tsx +19 -19
  28. package/templates/nextjs/base/src/components/account/order-history.tsx +385 -385
  29. package/templates/nextjs/base/src/components/brainerce-bot.tsx +70 -70
  30. package/templates/nextjs/base/src/components/checkout/checkout-form.tsx +612 -612
  31. package/templates/nextjs/base/src/components/checkout/custom-fields-step.tsx +292 -292
  32. package/templates/nextjs/base/src/components/checkout/date-picker.tsx +309 -309
  33. package/templates/nextjs/base/src/components/checkout/datetime-picker.tsx +144 -144
  34. package/templates/nextjs/base/src/components/checkout/order-bump-card.tsx +242 -242
  35. package/templates/nextjs/base/src/components/checkout/pickup-step.tsx +198 -198
  36. package/templates/nextjs/base/src/components/checkout/shipping-step.tsx +125 -125
  37. package/templates/nextjs/base/src/components/checkout/tax-display.tsx +74 -74
  38. package/templates/nextjs/base/src/components/seo/article-json-ld.tsx +59 -59
  39. package/templates/nextjs/base/src/components/seo/breadcrumbs.tsx +37 -37
  40. package/templates/nextjs/base/src/components/seo/category-json-ld.tsx +61 -61
  41. package/templates/nextjs/base/src/components/seo/organization-json-ld.tsx +97 -97
  42. package/templates/nextjs/base/src/components/seo/product-json-ld.tsx +179 -179
  43. package/templates/nextjs/base/src/components/tracking-bootstrap.tsx +62 -0
  44. package/templates/nextjs/base/src/components/ui/accordion.tsx +53 -53
  45. package/templates/nextjs/base/src/components/ui/badge.tsx +32 -32
  46. package/templates/nextjs/base/src/components/ui/button.tsx +49 -49
  47. package/templates/nextjs/base/src/components/ui/card.tsx +55 -55
  48. package/templates/nextjs/base/src/components/ui/checkbox.tsx +28 -28
  49. package/templates/nextjs/base/src/components/ui/dialog.tsx +107 -107
  50. package/templates/nextjs/base/src/components/ui/input.tsx +22 -22
  51. package/templates/nextjs/base/src/components/ui/label.tsx +21 -21
  52. package/templates/nextjs/base/src/components/ui/radio-group.tsx +38 -38
  53. package/templates/nextjs/base/src/components/ui/select.tsx +153 -153
  54. package/templates/nextjs/base/src/components/ui/separator.tsx +26 -26
  55. package/templates/nextjs/base/src/components/ui/sheet.tsx +125 -125
  56. package/templates/nextjs/base/src/components/ui/skeleton.tsx +7 -7
  57. package/templates/nextjs/base/src/components/ui/tabs.tsx +55 -55
  58. package/templates/nextjs/base/src/components/ui/textarea.tsx +21 -21
  59. package/templates/nextjs/base/src/components/ui/tooltip.tsx +32 -32
  60. package/templates/nextjs/base/src/core/hooks/use-cart-page.ts +58 -58
  61. package/templates/nextjs/base/src/core/hooks/use-home-data.ts +51 -51
  62. package/templates/nextjs/base/src/core/hooks/use-product-listing.ts +295 -295
  63. package/templates/nextjs/base/src/core/hooks/use-product-page.ts +20 -2
  64. package/templates/nextjs/base/src/core/lib/image-hosts.ts +7 -7
  65. package/templates/nextjs/base/src/core/lib/product-options.ts +121 -121
  66. package/templates/nextjs/base/src/core/lib/resolve-currency.ts +20 -20
  67. package/templates/nextjs/base/src/core/lib/safe-redirect.ts +38 -38
  68. package/templates/nextjs/base/src/core/lib/seo.ts +46 -46
  69. package/templates/nextjs/base/src/core/lib/store-info.ts +9 -0
  70. package/templates/nextjs/base/src/core/lib/tracking.ts +171 -0
  71. package/templates/nextjs/base/src/core/lib/use-currency.ts +19 -19
  72. package/templates/nextjs/base/src/core/lib/utils.ts +21 -21
  73. package/templates/nextjs/base/src/middleware.ts.ejs +54 -2
  74. package/templates/nextjs/base/src/ui/cart/cart-bundle-offer.tsx +117 -117
  75. package/templates/nextjs/base/src/ui/cart/cart-item.tsx +146 -146
  76. package/templates/nextjs/base/src/ui/cart/cart-nudges.tsx +28 -28
  77. package/templates/nextjs/base/src/ui/cart/cart-summary.tsx +110 -110
  78. package/templates/nextjs/base/src/ui/cart/cart-upgrade-banner.tsx +126 -126
  79. package/templates/nextjs/base/src/ui/cart/cart-view.tsx +140 -140
  80. package/templates/nextjs/base/src/ui/cart/coupon-input.tsx +134 -134
  81. package/templates/nextjs/base/src/ui/cart/free-shipping-bar.tsx +61 -61
  82. package/templates/nextjs/base/src/ui/cart/reservation-countdown.tsx +95 -95
  83. package/templates/nextjs/base/src/ui/home/discount-banner-strip.tsx +20 -20
  84. package/templates/nextjs/base/src/ui/home/featured-products-section.tsx +24 -24
  85. package/templates/nextjs/base/src/ui/home/hero-section.tsx +27 -27
  86. package/templates/nextjs/base/src/ui/home/home-client.tsx +29 -29
  87. package/templates/nextjs/base/src/ui/layout/announcement-bar.tsx.ejs +116 -116
  88. package/templates/nextjs/base/src/ui/layout/faq-section.tsx.ejs +90 -90
  89. package/templates/nextjs/base/src/ui/layout/header-account.tsx +40 -40
  90. package/templates/nextjs/base/src/ui/layout/language-switcher.tsx.ejs +80 -80
  91. package/templates/nextjs/base/src/ui/layout/site-footer.tsx.ejs +144 -144
  92. package/templates/nextjs/base/src/ui/layout/site-header.tsx.ejs +152 -152
  93. package/templates/nextjs/base/src/ui/product/customization-fields.tsx +457 -457
  94. package/templates/nextjs/base/src/ui/product/discount-badge.tsx +18 -18
  95. package/templates/nextjs/base/src/ui/product/frequently-bought-together.tsx +191 -191
  96. package/templates/nextjs/base/src/ui/product/modifier-group-selector.tsx +206 -206
  97. package/templates/nextjs/base/src/ui/product/price-display.tsx +62 -62
  98. package/templates/nextjs/base/src/ui/product/product-card.tsx +7 -2
  99. package/templates/nextjs/base/src/ui/product/product-client-section.tsx +455 -455
  100. package/templates/nextjs/base/src/ui/product/product-listing.tsx +446 -446
  101. package/templates/nextjs/base/src/ui/product/recommendation-section.tsx +102 -102
  102. package/templates/nextjs/base/src/ui/product/review-form.tsx +294 -294
  103. package/templates/nextjs/base/src/ui/product/reviews-section.tsx.ejs +108 -108
  104. package/templates/nextjs/base/src/ui/product/stock-badge.tsx +61 -61
  105. package/templates/nextjs/base/src/ui/product/variant-selector.tsx +291 -291
  106. package/templates/nextjs/base/src/ui/shared/cdn-image.tsx +26 -26
  107. package/templates/nextjs/base/tailwind.config.ts +69 -69
  108. package/templates/nextjs/designs/atelier/app-overlay/layout.tsx.ejs +10 -0
  109. package/templates/nextjs/designs/atelier/globals.css +402 -402
  110. package/templates/nextjs/designs/atelier/messages-patch/en.json +100 -100
  111. package/templates/nextjs/designs/atelier/messages-patch/he.json +100 -100
  112. package/templates/nextjs/designs/atelier/ui/cart/cart-bundle-offer.tsx +110 -110
  113. package/templates/nextjs/designs/atelier/ui/cart/cart-drawer.tsx +163 -163
  114. package/templates/nextjs/designs/atelier/ui/cart/cart-item.tsx +140 -140
  115. package/templates/nextjs/designs/atelier/ui/cart/cart-nudges.tsx +34 -34
  116. package/templates/nextjs/designs/atelier/ui/cart/cart-summary.tsx +107 -107
  117. package/templates/nextjs/designs/atelier/ui/cart/cart-upgrade-banner.tsx +123 -123
  118. package/templates/nextjs/designs/atelier/ui/cart/cart-view.tsx +147 -147
  119. package/templates/nextjs/designs/atelier/ui/cart/coupon-input.tsx +142 -142
  120. package/templates/nextjs/designs/atelier/ui/cart/free-shipping-bar.tsx +77 -77
  121. package/templates/nextjs/designs/atelier/ui/cart/reservation-countdown.tsx +89 -89
  122. package/templates/nextjs/designs/atelier/ui/home/benefits-band.tsx +34 -34
  123. package/templates/nextjs/designs/atelier/ui/home/category-tiles.tsx +151 -151
  124. package/templates/nextjs/designs/atelier/ui/home/discount-banner-strip.tsx +27 -27
  125. package/templates/nextjs/designs/atelier/ui/home/editorial-band.tsx +75 -75
  126. package/templates/nextjs/designs/atelier/ui/home/featured-products-section.tsx +36 -36
  127. package/templates/nextjs/designs/atelier/ui/home/hero-section.tsx +174 -174
  128. package/templates/nextjs/designs/atelier/ui/home/home-client.tsx +54 -54
  129. package/templates/nextjs/designs/atelier/ui/home/newsletter-section.tsx +78 -78
  130. package/templates/nextjs/designs/atelier/ui/home/testimonials-band.tsx +46 -46
  131. package/templates/nextjs/designs/atelier/ui/layout/announcement-bar.tsx +118 -118
  132. package/templates/nextjs/designs/atelier/ui/layout/faq-section.tsx +96 -96
  133. package/templates/nextjs/designs/atelier/ui/layout/header-account.tsx +36 -36
  134. package/templates/nextjs/designs/atelier/ui/layout/header-cart.tsx +36 -36
  135. package/templates/nextjs/designs/atelier/ui/layout/language-switcher.tsx.ejs +137 -137
  136. package/templates/nextjs/designs/atelier/ui/layout/nav-url.ts +14 -14
  137. package/templates/nextjs/designs/atelier/ui/layout/rich-text-block.tsx +34 -34
  138. package/templates/nextjs/designs/atelier/ui/layout/site-footer.tsx.ejs +141 -141
  139. package/templates/nextjs/designs/atelier/ui/layout/site-header.tsx.ejs +138 -138
  140. package/templates/nextjs/designs/atelier/ui/product/customization-fields.tsx +466 -466
  141. package/templates/nextjs/designs/atelier/ui/product/discount-badge.tsx +17 -17
  142. package/templates/nextjs/designs/atelier/ui/product/frequently-bought-together.tsx +196 -196
  143. package/templates/nextjs/designs/atelier/ui/product/modifier-group-selector.tsx +193 -193
  144. package/templates/nextjs/designs/atelier/ui/product/price-display.tsx +69 -69
  145. package/templates/nextjs/designs/atelier/ui/product/product-card.tsx +13 -11
  146. package/templates/nextjs/designs/atelier/ui/product/product-client-section.tsx +477 -477
  147. package/templates/nextjs/designs/atelier/ui/product/product-grid.tsx +42 -42
  148. package/templates/nextjs/designs/atelier/ui/product/product-listing.tsx +260 -260
  149. package/templates/nextjs/designs/atelier/ui/product/recommendation-section.tsx +95 -95
  150. package/templates/nextjs/designs/atelier/ui/product/review-form.tsx +267 -267
  151. package/templates/nextjs/designs/atelier/ui/product/reviews-section.tsx.ejs +148 -148
  152. package/templates/nextjs/designs/atelier/ui/product/stock-badge.tsx +66 -66
  153. package/templates/nextjs/designs/atelier/ui/product/variant-selector.tsx +206 -206
  154. package/templates/nextjs/designs/atelier/ui/shared/cdn-image.tsx +29 -29
  155. package/templates/nextjs/designs/atelier/ui/shared/fly-to-cart.ts +86 -86
  156. package/templates/nextjs/designs/atelier/ui/shared/icons.tsx +276 -276
  157. package/templates/nextjs/designs/atelier/ui/shared/loading-spinner.tsx +63 -63
  158. package/templates/nextjs/designs/atelier/ui/shared/reveal.tsx +71 -71
  159. package/templates/nextjs/designs/atelier/ui/shared/select-menu.tsx +125 -125
  160. package/templates/nextjs/ui-canvas/cart/cart-bundle-offer.tsx +89 -89
  161. package/templates/nextjs/ui-canvas/cart/cart-item.tsx +123 -123
  162. package/templates/nextjs/ui-canvas/cart/cart-nudges.tsx +27 -27
  163. package/templates/nextjs/ui-canvas/cart/cart-summary.tsx +99 -99
  164. package/templates/nextjs/ui-canvas/cart/cart-upgrade-banner.tsx +105 -105
  165. package/templates/nextjs/ui-canvas/cart/cart-view.tsx +106 -106
  166. package/templates/nextjs/ui-canvas/cart/coupon-input.tsx +119 -119
  167. package/templates/nextjs/ui-canvas/cart/free-shipping-bar.tsx +54 -54
  168. package/templates/nextjs/ui-canvas/cart/reservation-countdown.tsx +81 -81
  169. package/templates/nextjs/ui-canvas/home/discount-banner-strip.tsx +19 -19
  170. package/templates/nextjs/ui-canvas/home/featured-products-section.tsx +23 -23
  171. package/templates/nextjs/ui-canvas/home/hero-section.tsx +21 -21
  172. package/templates/nextjs/ui-canvas/home/home-client.tsx +26 -26
  173. package/templates/nextjs/ui-canvas/layout/announcement-bar.tsx.ejs +101 -101
  174. package/templates/nextjs/ui-canvas/layout/faq-section.tsx.ejs +71 -71
  175. package/templates/nextjs/ui-canvas/layout/header-account.tsx +31 -31
  176. package/templates/nextjs/ui-canvas/layout/language-switcher.tsx.ejs +76 -76
  177. package/templates/nextjs/ui-canvas/layout/rich-text-block.tsx.ejs +36 -36
  178. package/templates/nextjs/ui-canvas/layout/site-footer.tsx.ejs +82 -82
  179. package/templates/nextjs/ui-canvas/layout/site-header.tsx.ejs +119 -119
  180. package/templates/nextjs/ui-canvas/product/customization-fields.tsx +445 -445
  181. package/templates/nextjs/ui-canvas/product/discount-badge.tsx +19 -19
  182. package/templates/nextjs/ui-canvas/product/frequently-bought-together.tsx +168 -168
  183. package/templates/nextjs/ui-canvas/product/modifier-group-selector.tsx +165 -165
  184. package/templates/nextjs/ui-canvas/product/price-display.tsx +52 -52
  185. package/templates/nextjs/ui-canvas/product/product-card.tsx +7 -2
  186. package/templates/nextjs/ui-canvas/product/product-client-section.tsx +352 -352
  187. package/templates/nextjs/ui-canvas/product/product-grid.tsx +27 -27
  188. package/templates/nextjs/ui-canvas/product/product-listing.tsx +224 -224
  189. package/templates/nextjs/ui-canvas/product/recommendation-section.tsx +99 -99
  190. package/templates/nextjs/ui-canvas/product/review-form.tsx +266 -266
  191. package/templates/nextjs/ui-canvas/product/reviews-section.tsx.ejs +96 -96
  192. package/templates/nextjs/ui-canvas/product/stock-badge.tsx +45 -45
  193. package/templates/nextjs/ui-canvas/product/variant-selector.tsx +212 -212
  194. package/templates/nextjs/ui-canvas/shared/cdn-image.tsx +31 -31
  195. package/templates/nextjs/ui-canvas/shared/loading-spinner.tsx +27 -27
  196. package/templates/nextjs/base/tsconfig.tsbuildinfo +0 -1
@@ -1,309 +1,309 @@
1
- 'use client';
2
-
3
- import { useEffect, useRef, useState } from 'react';
4
- import { isCalendarDateAllowed, type DateAvailabilityConstraints } from 'brainerce';
5
- import { cn } from '@/core/lib/utils';
6
-
7
- /**
8
- * Custom calendar dropdown for checkout DATE custom fields — replaces the
9
- * native `<input type="date">`, whose OS/browser-rendered popup can't be
10
- * restyled with CSS in any browser (only the closed field can be).
11
- *
12
- * Lives next to `custom-fields-step.tsx` (not under `src/ui/`) and stays
13
- * dependency-free from any design pack for the same reason that file does:
14
- * canvas scaffolds wipe `src/ui` and ship no pack, but never touch
15
- * `src/components/`, so this must work without either.
16
- *
17
- * Day availability is delegated wholesale to the SDK's `isCalendarDateAllowed`
18
- * — min/max date, blocked weekdays AND blocked specific dates in one call, the
19
- * exact predicate the server re-runs on submit. Reimplementing any part of it
20
- * here is how a picker ends up offering a day the API then rejects with 400.
21
- */
22
- interface DatePickerProps {
23
- id?: string;
24
- value: string; // 'YYYY-MM-DD' or ''
25
- onChange: (value: string) => void;
26
- required?: boolean;
27
- /** The field's `dateAvailability`, passed straight through from the API. */
28
- availability?: DateAvailabilityConstraints | null;
29
- /**
30
- * Extra per-day gate ANDed with `isCalendarDateAllowed`. DATETIME fields use
31
- * it to grey out days that pass the calendar rules but have no business-hours
32
- * window — those are closed all day, and offering them is what produces a
33
- * "no business hours are configured for this day" 400 after the shopper has
34
- * already committed to a date.
35
- */
36
- isDateSelectable?: (dateYYYYMMDD: string) => boolean;
37
- placeholder: string;
38
- todayLabel: string;
39
- clearLabel: string;
40
- prevMonthLabel: string;
41
- nextMonthLabel: string;
42
- className?: string;
43
- }
44
-
45
- function pad(n: number): string {
46
- return String(n).padStart(2, '0');
47
- }
48
-
49
- function toYMD(y: number, m: number, d: number): string {
50
- return `${y}-${pad(m + 1)}-${pad(d)}`;
51
- }
52
-
53
- function parseYMD(value: string): { y: number; m: number; d: number } | null {
54
- const match = /^(\d{4})-(\d{2})-(\d{2})$/.exec(value);
55
- if (!match) return null;
56
- return { y: Number(match[1]), m: Number(match[2]) - 1, d: Number(match[3]) };
57
- }
58
-
59
- export function DatePicker({
60
- id,
61
- value,
62
- onChange,
63
- required,
64
- availability,
65
- isDateSelectable,
66
- placeholder,
67
- todayLabel,
68
- clearLabel,
69
- prevMonthLabel,
70
- nextMonthLabel,
71
- className,
72
- }: DatePickerProps) {
73
- const [open, setOpen] = useState(false);
74
- const [locale, setLocale] = useState<string | undefined>(undefined);
75
- const [rtl, setRtl] = useState(false);
76
- const rootRef = useRef<HTMLDivElement>(null);
77
- const triggerRef = useRef<HTMLButtonElement>(null);
78
-
79
- useEffect(() => {
80
- setLocale(document.documentElement.lang || undefined);
81
- setRtl(document.documentElement.dir === 'rtl');
82
- }, []);
83
-
84
- const selected = parseYMD(value);
85
- const today = new Date();
86
- const [view, setView] = useState(() => {
87
- const initial = selected ?? { y: today.getFullYear(), m: today.getMonth() };
88
- return { y: initial.y, m: initial.m };
89
- });
90
-
91
- useEffect(() => {
92
- if (!open) return;
93
- function onDocClick(e: MouseEvent) {
94
- if (rootRef.current && !rootRef.current.contains(e.target as Node)) setOpen(false);
95
- }
96
- function onKey(e: KeyboardEvent) {
97
- if (e.key === 'Escape') {
98
- setOpen(false);
99
- triggerRef.current?.focus();
100
- }
101
- }
102
- document.addEventListener('mousedown', onDocClick);
103
- document.addEventListener('keydown', onKey);
104
- return () => {
105
- document.removeEventListener('mousedown', onDocClick);
106
- document.removeEventListener('keydown', onKey);
107
- };
108
- }, [open]);
109
-
110
- function isDisabled(y: number, m: number, d: number): boolean {
111
- const ymd = toYMD(y, m, d);
112
- if (!isCalendarDateAllowed(ymd, availability)) return true;
113
- return isDateSelectable ? !isDateSelectable(ymd) : false;
114
- }
115
-
116
- function selectDay(y: number, m: number, d: number) {
117
- if (isDisabled(y, m, d)) return;
118
- onChange(toYMD(y, m, d));
119
- setView({ y, m });
120
- setOpen(false);
121
- triggerRef.current?.focus();
122
- }
123
-
124
- function shiftMonth(delta: number) {
125
- setView((v) => {
126
- let m = v.m + delta;
127
- let y = v.y;
128
- if (m < 0) {
129
- m = 11;
130
- y -= 1;
131
- } else if (m > 11) {
132
- m = 0;
133
- y += 1;
134
- }
135
- return { y, m };
136
- });
137
- }
138
-
139
- const monthLabel = new Date(view.y, view.m, 1).toLocaleDateString(locale, {
140
- month: 'long',
141
- year: 'numeric',
142
- });
143
- const weekdayLabels = Array.from({ length: 7 }, (_, i) => {
144
- // A Sunday-anchored week (2023-01-01 was a Sunday) formatted with the
145
- // page's own locale — matches whatever week-start convention that
146
- // locale's date formatting implies.
147
- const d = new Date(2023, 0, 1 + i);
148
- return d.toLocaleDateString(locale, { weekday: 'narrow' });
149
- });
150
-
151
- const firstOfMonth = new Date(view.y, view.m, 1);
152
- const startWeekday = firstOfMonth.getDay();
153
- const daysInMonth = new Date(view.y, view.m + 1, 0).getDate();
154
- const daysInPrevMonth = new Date(view.y, view.m, 0).getDate();
155
-
156
- const cells: Array<{ day: number; muted: boolean; y: number; m: number }> = [];
157
- for (let i = startWeekday - 1; i >= 0; i--) {
158
- const m = view.m === 0 ? 11 : view.m - 1;
159
- const y = view.m === 0 ? view.y - 1 : view.y;
160
- cells.push({ day: daysInPrevMonth - i, muted: true, y, m });
161
- }
162
- for (let d = 1; d <= daysInMonth; d++) {
163
- cells.push({ day: d, muted: false, y: view.y, m: view.m });
164
- }
165
- while (cells.length % 7 !== 0) {
166
- const idx = cells.length - startWeekday - daysInMonth + 1;
167
- const m = view.m === 11 ? 0 : view.m + 1;
168
- const y = view.m === 11 ? view.y + 1 : view.y;
169
- cells.push({ day: idx, muted: true, y, m });
170
- }
171
-
172
- const displayValue = selected
173
- ? new Date(selected.y, selected.m, selected.d).toLocaleDateString(locale)
174
- : '';
175
-
176
- return (
177
- <div ref={rootRef} className={cn('relative', className)}>
178
- <button
179
- ref={triggerRef}
180
- type="button"
181
- id={id}
182
- onClick={() => setOpen((o) => !o)}
183
- aria-haspopup="dialog"
184
- aria-expanded={open}
185
- aria-required={required}
186
- className={cn(
187
- 'bg-background focus-visible:ring-primary flex h-11 w-full items-center rounded-lg border px-3.5 pe-11 text-start text-sm focus-visible:outline-none focus-visible:ring-2',
188
- displayValue ? 'text-foreground' : 'text-muted-foreground'
189
- )}
190
- >
191
- {displayValue || placeholder}
192
- <svg
193
- aria-hidden="true"
194
- width={18}
195
- height={18}
196
- viewBox="0 0 24 24"
197
- fill="none"
198
- stroke="currentColor"
199
- strokeWidth={1.75}
200
- strokeLinecap="round"
201
- strokeLinejoin="round"
202
- className="text-muted-foreground pointer-events-none absolute end-3.5 top-1/2 -translate-y-1/2"
203
- >
204
- <rect x="3.5" y="5" width="17" height="15.5" rx="2.5" />
205
- <path d="M3.5 9.5h17M8 3v3.5M16 3v3.5" />
206
- </svg>
207
- </button>
208
-
209
- {open && (
210
- <div
211
- role="dialog"
212
- aria-label={placeholder}
213
- className="border-border bg-background absolute start-0 z-20 mt-2 w-[19rem] rounded-2xl border p-4 shadow-xl"
214
- >
215
- <div className="mb-3 flex items-center justify-between">
216
- <div className="flex gap-1">
217
- <button
218
- type="button"
219
- onClick={() => shiftMonth(rtl ? 1 : -1)}
220
- aria-label={prevMonthLabel}
221
- className="border-border hover:bg-secondary flex h-8 w-8 items-center justify-center rounded-full border text-sm"
222
- >
223
- {rtl ? '›' : '‹'}
224
- </button>
225
- <button
226
- type="button"
227
- onClick={() => shiftMonth(rtl ? -1 : 1)}
228
- aria-label={nextMonthLabel}
229
- className="border-border hover:bg-secondary flex h-8 w-8 items-center justify-center rounded-full border text-sm"
230
- >
231
- {rtl ? '‹' : '›'}
232
- </button>
233
- </div>
234
- <div className="font-display text-sm font-medium capitalize">{monthLabel}</div>
235
- </div>
236
-
237
- <div className="mb-1 grid grid-cols-7">
238
- {weekdayLabels.map((w, i) => (
239
- <span
240
- key={i}
241
- className="text-muted-foreground py-1 text-center text-[0.7rem] font-semibold"
242
- >
243
- {w}
244
- </span>
245
- ))}
246
- </div>
247
-
248
- <div className="grid grid-cols-7 gap-0.5">
249
- {cells.map((c, i) => {
250
- const disabled = c.muted || isDisabled(c.y, c.m, c.day);
251
- const isToday =
252
- !c.muted &&
253
- c.y === today.getFullYear() &&
254
- c.m === today.getMonth() &&
255
- c.day === today.getDate();
256
- const isSelected =
257
- selected &&
258
- !c.muted &&
259
- c.y === selected.y &&
260
- c.m === selected.m &&
261
- c.day === selected.d;
262
- return (
263
- <button
264
- key={i}
265
- type="button"
266
- disabled={disabled}
267
- onClick={() => selectDay(c.y, c.m, c.day)}
268
- className={cn(
269
- 'flex aspect-square items-center justify-center rounded-full text-sm tabular-nums',
270
- c.muted && 'text-muted-foreground/40',
271
- disabled && !c.muted && 'text-muted-foreground/40 cursor-not-allowed',
272
- !disabled && !isSelected && 'hover:bg-secondary',
273
- isToday && !isSelected && 'ring-primary font-semibold ring-1',
274
- isSelected && 'bg-primary text-primary-foreground font-bold'
275
- )}
276
- >
277
- {c.day}
278
- </button>
279
- );
280
- })}
281
- </div>
282
-
283
- <div className="border-border mt-3 flex justify-between border-t pt-3">
284
- <button
285
- type="button"
286
- onClick={() => {
287
- const t = new Date();
288
- selectDay(t.getFullYear(), t.getMonth(), t.getDate());
289
- }}
290
- className="text-primary text-sm font-semibold hover:underline"
291
- >
292
- {todayLabel}
293
- </button>
294
- <button
295
- type="button"
296
- onClick={() => {
297
- onChange('');
298
- setOpen(false);
299
- }}
300
- className="text-primary text-sm font-semibold hover:underline"
301
- >
302
- {clearLabel}
303
- </button>
304
- </div>
305
- </div>
306
- )}
307
- </div>
308
- );
309
- }
1
+ 'use client';
2
+
3
+ import { useEffect, useRef, useState } from 'react';
4
+ import { isCalendarDateAllowed, type DateAvailabilityConstraints } from 'brainerce';
5
+ import { cn } from '@/core/lib/utils';
6
+
7
+ /**
8
+ * Custom calendar dropdown for checkout DATE custom fields — replaces the
9
+ * native `<input type="date">`, whose OS/browser-rendered popup can't be
10
+ * restyled with CSS in any browser (only the closed field can be).
11
+ *
12
+ * Lives next to `custom-fields-step.tsx` (not under `src/ui/`) and stays
13
+ * dependency-free from any design pack for the same reason that file does:
14
+ * canvas scaffolds wipe `src/ui` and ship no pack, but never touch
15
+ * `src/components/`, so this must work without either.
16
+ *
17
+ * Day availability is delegated wholesale to the SDK's `isCalendarDateAllowed`
18
+ * — min/max date, blocked weekdays AND blocked specific dates in one call, the
19
+ * exact predicate the server re-runs on submit. Reimplementing any part of it
20
+ * here is how a picker ends up offering a day the API then rejects with 400.
21
+ */
22
+ interface DatePickerProps {
23
+ id?: string;
24
+ value: string; // 'YYYY-MM-DD' or ''
25
+ onChange: (value: string) => void;
26
+ required?: boolean;
27
+ /** The field's `dateAvailability`, passed straight through from the API. */
28
+ availability?: DateAvailabilityConstraints | null;
29
+ /**
30
+ * Extra per-day gate ANDed with `isCalendarDateAllowed`. DATETIME fields use
31
+ * it to grey out days that pass the calendar rules but have no business-hours
32
+ * window — those are closed all day, and offering them is what produces a
33
+ * "no business hours are configured for this day" 400 after the shopper has
34
+ * already committed to a date.
35
+ */
36
+ isDateSelectable?: (dateYYYYMMDD: string) => boolean;
37
+ placeholder: string;
38
+ todayLabel: string;
39
+ clearLabel: string;
40
+ prevMonthLabel: string;
41
+ nextMonthLabel: string;
42
+ className?: string;
43
+ }
44
+
45
+ function pad(n: number): string {
46
+ return String(n).padStart(2, '0');
47
+ }
48
+
49
+ function toYMD(y: number, m: number, d: number): string {
50
+ return `${y}-${pad(m + 1)}-${pad(d)}`;
51
+ }
52
+
53
+ function parseYMD(value: string): { y: number; m: number; d: number } | null {
54
+ const match = /^(\d{4})-(\d{2})-(\d{2})$/.exec(value);
55
+ if (!match) return null;
56
+ return { y: Number(match[1]), m: Number(match[2]) - 1, d: Number(match[3]) };
57
+ }
58
+
59
+ export function DatePicker({
60
+ id,
61
+ value,
62
+ onChange,
63
+ required,
64
+ availability,
65
+ isDateSelectable,
66
+ placeholder,
67
+ todayLabel,
68
+ clearLabel,
69
+ prevMonthLabel,
70
+ nextMonthLabel,
71
+ className,
72
+ }: DatePickerProps) {
73
+ const [open, setOpen] = useState(false);
74
+ const [locale, setLocale] = useState<string | undefined>(undefined);
75
+ const [rtl, setRtl] = useState(false);
76
+ const rootRef = useRef<HTMLDivElement>(null);
77
+ const triggerRef = useRef<HTMLButtonElement>(null);
78
+
79
+ useEffect(() => {
80
+ setLocale(document.documentElement.lang || undefined);
81
+ setRtl(document.documentElement.dir === 'rtl');
82
+ }, []);
83
+
84
+ const selected = parseYMD(value);
85
+ const today = new Date();
86
+ const [view, setView] = useState(() => {
87
+ const initial = selected ?? { y: today.getFullYear(), m: today.getMonth() };
88
+ return { y: initial.y, m: initial.m };
89
+ });
90
+
91
+ useEffect(() => {
92
+ if (!open) return;
93
+ function onDocClick(e: MouseEvent) {
94
+ if (rootRef.current && !rootRef.current.contains(e.target as Node)) setOpen(false);
95
+ }
96
+ function onKey(e: KeyboardEvent) {
97
+ if (e.key === 'Escape') {
98
+ setOpen(false);
99
+ triggerRef.current?.focus();
100
+ }
101
+ }
102
+ document.addEventListener('mousedown', onDocClick);
103
+ document.addEventListener('keydown', onKey);
104
+ return () => {
105
+ document.removeEventListener('mousedown', onDocClick);
106
+ document.removeEventListener('keydown', onKey);
107
+ };
108
+ }, [open]);
109
+
110
+ function isDisabled(y: number, m: number, d: number): boolean {
111
+ const ymd = toYMD(y, m, d);
112
+ if (!isCalendarDateAllowed(ymd, availability)) return true;
113
+ return isDateSelectable ? !isDateSelectable(ymd) : false;
114
+ }
115
+
116
+ function selectDay(y: number, m: number, d: number) {
117
+ if (isDisabled(y, m, d)) return;
118
+ onChange(toYMD(y, m, d));
119
+ setView({ y, m });
120
+ setOpen(false);
121
+ triggerRef.current?.focus();
122
+ }
123
+
124
+ function shiftMonth(delta: number) {
125
+ setView((v) => {
126
+ let m = v.m + delta;
127
+ let y = v.y;
128
+ if (m < 0) {
129
+ m = 11;
130
+ y -= 1;
131
+ } else if (m > 11) {
132
+ m = 0;
133
+ y += 1;
134
+ }
135
+ return { y, m };
136
+ });
137
+ }
138
+
139
+ const monthLabel = new Date(view.y, view.m, 1).toLocaleDateString(locale, {
140
+ month: 'long',
141
+ year: 'numeric',
142
+ });
143
+ const weekdayLabels = Array.from({ length: 7 }, (_, i) => {
144
+ // A Sunday-anchored week (2023-01-01 was a Sunday) formatted with the
145
+ // page's own locale — matches whatever week-start convention that
146
+ // locale's date formatting implies.
147
+ const d = new Date(2023, 0, 1 + i);
148
+ return d.toLocaleDateString(locale, { weekday: 'narrow' });
149
+ });
150
+
151
+ const firstOfMonth = new Date(view.y, view.m, 1);
152
+ const startWeekday = firstOfMonth.getDay();
153
+ const daysInMonth = new Date(view.y, view.m + 1, 0).getDate();
154
+ const daysInPrevMonth = new Date(view.y, view.m, 0).getDate();
155
+
156
+ const cells: Array<{ day: number; muted: boolean; y: number; m: number }> = [];
157
+ for (let i = startWeekday - 1; i >= 0; i--) {
158
+ const m = view.m === 0 ? 11 : view.m - 1;
159
+ const y = view.m === 0 ? view.y - 1 : view.y;
160
+ cells.push({ day: daysInPrevMonth - i, muted: true, y, m });
161
+ }
162
+ for (let d = 1; d <= daysInMonth; d++) {
163
+ cells.push({ day: d, muted: false, y: view.y, m: view.m });
164
+ }
165
+ while (cells.length % 7 !== 0) {
166
+ const idx = cells.length - startWeekday - daysInMonth + 1;
167
+ const m = view.m === 11 ? 0 : view.m + 1;
168
+ const y = view.m === 11 ? view.y + 1 : view.y;
169
+ cells.push({ day: idx, muted: true, y, m });
170
+ }
171
+
172
+ const displayValue = selected
173
+ ? new Date(selected.y, selected.m, selected.d).toLocaleDateString(locale)
174
+ : '';
175
+
176
+ return (
177
+ <div ref={rootRef} className={cn('relative', className)}>
178
+ <button
179
+ ref={triggerRef}
180
+ type="button"
181
+ id={id}
182
+ onClick={() => setOpen((o) => !o)}
183
+ aria-haspopup="dialog"
184
+ aria-expanded={open}
185
+ aria-required={required}
186
+ className={cn(
187
+ 'bg-background focus-visible:ring-primary flex h-11 w-full items-center rounded-lg border px-3.5 pe-11 text-start text-sm focus-visible:outline-none focus-visible:ring-2',
188
+ displayValue ? 'text-foreground' : 'text-muted-foreground'
189
+ )}
190
+ >
191
+ {displayValue || placeholder}
192
+ <svg
193
+ aria-hidden="true"
194
+ width={18}
195
+ height={18}
196
+ viewBox="0 0 24 24"
197
+ fill="none"
198
+ stroke="currentColor"
199
+ strokeWidth={1.75}
200
+ strokeLinecap="round"
201
+ strokeLinejoin="round"
202
+ className="text-muted-foreground pointer-events-none absolute end-3.5 top-1/2 -translate-y-1/2"
203
+ >
204
+ <rect x="3.5" y="5" width="17" height="15.5" rx="2.5" />
205
+ <path d="M3.5 9.5h17M8 3v3.5M16 3v3.5" />
206
+ </svg>
207
+ </button>
208
+
209
+ {open && (
210
+ <div
211
+ role="dialog"
212
+ aria-label={placeholder}
213
+ className="border-border bg-background absolute start-0 z-20 mt-2 w-[19rem] rounded-2xl border p-4 shadow-xl"
214
+ >
215
+ <div className="mb-3 flex items-center justify-between">
216
+ <div className="flex gap-1">
217
+ <button
218
+ type="button"
219
+ onClick={() => shiftMonth(rtl ? 1 : -1)}
220
+ aria-label={prevMonthLabel}
221
+ className="border-border hover:bg-secondary flex h-8 w-8 items-center justify-center rounded-full border text-sm"
222
+ >
223
+ {rtl ? '›' : '‹'}
224
+ </button>
225
+ <button
226
+ type="button"
227
+ onClick={() => shiftMonth(rtl ? -1 : 1)}
228
+ aria-label={nextMonthLabel}
229
+ className="border-border hover:bg-secondary flex h-8 w-8 items-center justify-center rounded-full border text-sm"
230
+ >
231
+ {rtl ? '‹' : '›'}
232
+ </button>
233
+ </div>
234
+ <div className="font-display text-sm font-medium capitalize">{monthLabel}</div>
235
+ </div>
236
+
237
+ <div className="mb-1 grid grid-cols-7">
238
+ {weekdayLabels.map((w, i) => (
239
+ <span
240
+ key={i}
241
+ className="text-muted-foreground py-1 text-center text-[0.7rem] font-semibold"
242
+ >
243
+ {w}
244
+ </span>
245
+ ))}
246
+ </div>
247
+
248
+ <div className="grid grid-cols-7 gap-0.5">
249
+ {cells.map((c, i) => {
250
+ const disabled = c.muted || isDisabled(c.y, c.m, c.day);
251
+ const isToday =
252
+ !c.muted &&
253
+ c.y === today.getFullYear() &&
254
+ c.m === today.getMonth() &&
255
+ c.day === today.getDate();
256
+ const isSelected =
257
+ selected &&
258
+ !c.muted &&
259
+ c.y === selected.y &&
260
+ c.m === selected.m &&
261
+ c.day === selected.d;
262
+ return (
263
+ <button
264
+ key={i}
265
+ type="button"
266
+ disabled={disabled}
267
+ onClick={() => selectDay(c.y, c.m, c.day)}
268
+ className={cn(
269
+ 'flex aspect-square items-center justify-center rounded-full text-sm tabular-nums',
270
+ c.muted && 'text-muted-foreground/40',
271
+ disabled && !c.muted && 'text-muted-foreground/40 cursor-not-allowed',
272
+ !disabled && !isSelected && 'hover:bg-secondary',
273
+ isToday && !isSelected && 'ring-primary font-semibold ring-1',
274
+ isSelected && 'bg-primary text-primary-foreground font-bold'
275
+ )}
276
+ >
277
+ {c.day}
278
+ </button>
279
+ );
280
+ })}
281
+ </div>
282
+
283
+ <div className="border-border mt-3 flex justify-between border-t pt-3">
284
+ <button
285
+ type="button"
286
+ onClick={() => {
287
+ const t = new Date();
288
+ selectDay(t.getFullYear(), t.getMonth(), t.getDate());
289
+ }}
290
+ className="text-primary text-sm font-semibold hover:underline"
291
+ >
292
+ {todayLabel}
293
+ </button>
294
+ <button
295
+ type="button"
296
+ onClick={() => {
297
+ onChange('');
298
+ setOpen(false);
299
+ }}
300
+ className="text-primary text-sm font-semibold hover:underline"
301
+ >
302
+ {clearLabel}
303
+ </button>
304
+ </div>
305
+ </div>
306
+ )}
307
+ </div>
308
+ );
309
+ }