create-skybridge 0.0.0-dev.e2befb1 → 0.0.0-dev.e2c53a4

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 (142) hide show
  1. package/dist/index.js +335 -210
  2. package/package.json +9 -8
  3. package/templates/blank/.dockerignore +4 -0
  4. package/templates/blank/Dockerfile +53 -0
  5. package/templates/blank/README.md +92 -0
  6. package/{template → templates/blank}/_gitignore +3 -1
  7. package/templates/blank/node_modules/.bin/alpic +21 -0
  8. package/templates/blank/node_modules/.bin/sb +21 -0
  9. package/templates/blank/node_modules/.bin/skybridge +21 -0
  10. package/{template → templates/blank}/node_modules/.bin/tsc +2 -2
  11. package/{template → templates/blank}/node_modules/.bin/tsserver +2 -2
  12. package/{template → templates/blank}/node_modules/.bin/tsx +2 -2
  13. package/templates/blank/node_modules/.bin/vite +21 -0
  14. package/templates/blank/package.json +29 -0
  15. package/templates/blank/src/helpers.ts +4 -0
  16. package/templates/blank/src/server.ts +16 -0
  17. package/templates/blank/tsconfig.json +5 -0
  18. package/templates/blank/vite.config.ts +6 -0
  19. package/templates/demo/.dockerignore +4 -0
  20. package/templates/demo/AGENTS.md +1 -0
  21. package/templates/demo/Dockerfile +53 -0
  22. package/templates/demo/README.md +95 -0
  23. package/templates/demo/_gitignore +7 -0
  24. package/templates/demo/alpic.json +3 -0
  25. package/templates/demo/node_modules/.bin/alpic +21 -0
  26. package/templates/demo/node_modules/.bin/sb +21 -0
  27. package/templates/demo/node_modules/.bin/skybridge +21 -0
  28. package/templates/demo/node_modules/.bin/tsc +21 -0
  29. package/templates/demo/node_modules/.bin/tsserver +21 -0
  30. package/templates/demo/node_modules/.bin/tsx +21 -0
  31. package/templates/demo/node_modules/.bin/vite +21 -0
  32. package/templates/demo/package.json +41 -0
  33. package/templates/demo/src/helpers.ts +4 -0
  34. package/templates/demo/src/index.css +59 -0
  35. package/templates/demo/src/server.ts +94 -0
  36. package/templates/demo/src/views/components/doc-link.tsx +22 -0
  37. package/templates/demo/src/views/components/doc.tsx +21 -0
  38. package/templates/demo/src/views/components/nav.tsx +31 -0
  39. package/templates/demo/src/views/components/progress.tsx +35 -0
  40. package/templates/demo/src/views/components/steps/outro.tsx +68 -0
  41. package/templates/demo/src/views/components/steps/state.tsx +47 -0
  42. package/templates/demo/src/views/components/steps/tool-call.tsx +53 -0
  43. package/templates/demo/src/views/components/steps/tool-output.tsx +40 -0
  44. package/templates/demo/src/views/images/mascot/beret.png +0 -0
  45. package/templates/demo/src/views/images/mascot/chapka.png +0 -0
  46. package/templates/demo/src/views/images/mascot/cowboy-hat.png +0 -0
  47. package/templates/demo/src/views/images/mascot/fez.png +0 -0
  48. package/templates/demo/src/views/images/mascot/jester-hat.png +0 -0
  49. package/templates/demo/src/views/images/mascot/mitre.png +0 -0
  50. package/templates/demo/src/views/images/mascot/non-la.png +0 -0
  51. package/templates/demo/src/views/images/mascot/original.png +0 -0
  52. package/templates/demo/src/views/images/mascot/propeller-beanie.png +0 -0
  53. package/templates/demo/src/views/images/mascot/ski-mask.png +0 -0
  54. package/templates/demo/src/views/images/mascot/sombrero.png +0 -0
  55. package/templates/demo/src/views/images/mascot/top-hat.png +0 -0
  56. package/templates/demo/src/views/images/mascot/viking-helmet.png +0 -0
  57. package/templates/demo/src/views/onboarding.tsx +63 -0
  58. package/templates/demo/src/views/use-mascot.ts +60 -0
  59. package/templates/demo/tsconfig.json +11 -0
  60. package/templates/demo/vite.config.ts +14 -0
  61. package/templates/ecom/.dockerignore +4 -0
  62. package/templates/ecom/.env.template +2 -0
  63. package/templates/ecom/.ladle/components.tsx +26 -0
  64. package/templates/ecom/.ladle/config.mjs +11 -0
  65. package/templates/ecom/.ladle/vite.config.ts +11 -0
  66. package/templates/ecom/AGENTS.md +2 -0
  67. package/templates/ecom/Dockerfile +53 -0
  68. package/templates/ecom/README.md +92 -0
  69. package/templates/ecom/_gitignore +9 -0
  70. package/templates/ecom/alpic.json +3 -0
  71. package/templates/ecom/node_modules/.bin/alpic +21 -0
  72. package/templates/ecom/node_modules/.bin/ladle +21 -0
  73. package/templates/ecom/node_modules/.bin/sb +21 -0
  74. package/templates/ecom/node_modules/.bin/skybridge +21 -0
  75. package/templates/ecom/node_modules/.bin/tsc +21 -0
  76. package/templates/ecom/node_modules/.bin/tsserver +21 -0
  77. package/templates/ecom/node_modules/.bin/tsx +21 -0
  78. package/templates/ecom/node_modules/.bin/vite +21 -0
  79. package/templates/ecom/package.json +41 -0
  80. package/templates/ecom/src/components/chip.css.ts +56 -0
  81. package/templates/ecom/src/components/chip.stories.tsx +26 -0
  82. package/templates/ecom/src/components/chip.tsx +40 -0
  83. package/templates/ecom/src/components/empty-state.stories.tsx +3 -0
  84. package/templates/ecom/src/components/empty-state.tsx +12 -0
  85. package/templates/ecom/src/components/expandable-text.css.ts +49 -0
  86. package/templates/ecom/src/components/expandable-text.stories.tsx +20 -0
  87. package/templates/ecom/src/components/expandable-text.tsx +52 -0
  88. package/templates/ecom/src/components/image-gallery.css.ts +169 -0
  89. package/templates/ecom/src/components/image-gallery.stories.tsx +30 -0
  90. package/templates/ecom/src/components/image-gallery.tsx +172 -0
  91. package/templates/ecom/src/components/product-card.css.ts +155 -0
  92. package/templates/ecom/src/components/product-card.stories.tsx +58 -0
  93. package/templates/ecom/src/components/product-card.tsx +101 -0
  94. package/templates/ecom/src/components/product-carousel.css.ts +134 -0
  95. package/templates/ecom/src/components/product-carousel.stories.tsx +64 -0
  96. package/templates/ecom/src/components/product-carousel.tsx +202 -0
  97. package/templates/ecom/src/components/variant-picker.css.ts +27 -0
  98. package/templates/ecom/src/components/variant-picker.stories.tsx +64 -0
  99. package/templates/ecom/src/components/variant-picker.tsx +82 -0
  100. package/templates/ecom/src/components/view-frame.css.ts +22 -0
  101. package/templates/ecom/src/components/view-frame.tsx +27 -0
  102. package/templates/ecom/src/config.ts +12 -0
  103. package/templates/ecom/src/design/contract.css.ts +39 -0
  104. package/templates/ecom/src/design/fonts.css +15 -0
  105. package/templates/ecom/src/design/primitives.css.ts +101 -0
  106. package/templates/ecom/src/design/recipes/typography.css.ts +75 -0
  107. package/templates/ecom/src/design/sprinkles.css.ts +128 -0
  108. package/templates/ecom/src/design/themes/dark.css.ts +36 -0
  109. package/templates/ecom/src/design/themes/light.css.ts +36 -0
  110. package/templates/ecom/src/design/tokens.ts +8 -0
  111. package/templates/ecom/src/helpers.ts +4 -0
  112. package/templates/ecom/src/i18n.ts +34 -0
  113. package/templates/ecom/src/index.css +9 -0
  114. package/templates/ecom/src/lib/cx.ts +9 -0
  115. package/templates/ecom/src/lib/format.ts +9 -0
  116. package/templates/ecom/src/lib/variants.ts +79 -0
  117. package/templates/ecom/src/server.ts +43 -0
  118. package/templates/ecom/src/tools/render-carousel.ts +242 -0
  119. package/templates/ecom/src/tools/search-products.ts +182 -0
  120. package/templates/ecom/src/types.ts +13 -0
  121. package/templates/ecom/src/views/carousel/detail/detail.css.ts +100 -0
  122. package/templates/ecom/src/views/carousel/detail/detail.stories.tsx +93 -0
  123. package/templates/ecom/src/views/carousel/detail/index.tsx +209 -0
  124. package/templates/ecom/src/views/carousel/index.tsx +234 -0
  125. package/templates/ecom/tsconfig.json +11 -0
  126. package/templates/ecom/vite.config.ts +8 -0
  127. package/dist/index.test.d.ts +0 -1
  128. package/dist/index.test.js +0 -33
  129. package/template/README.md +0 -97
  130. package/template/node_modules/.bin/alpic +0 -21
  131. package/template/node_modules/.bin/sb +0 -21
  132. package/template/node_modules/.bin/skybridge +0 -21
  133. package/template/node_modules/.bin/vite +0 -21
  134. package/template/package.json +0 -33
  135. package/template/server/src/index.ts +0 -62
  136. package/template/tsconfig.json +0 -13
  137. package/template/web/src/helpers.ts +0 -4
  138. package/template/web/src/index.css +0 -152
  139. package/template/web/src/widgets/magic-8-ball.tsx +0 -27
  140. package/template/web/vite.config.ts +0 -15
  141. /package/{template → templates/blank}/AGENTS.md +0 -0
  142. /package/{template → templates/blank}/alpic.json +0 -0
@@ -0,0 +1,40 @@
1
+ import * as styles from "./chip.css";
2
+
3
+ type ChipProps = {
4
+ label: string;
5
+ selected?: boolean;
6
+ disabled?: boolean;
7
+ onSelect?: () => void;
8
+ // Optional swatch image (e.g. one per color); omit for a plain text chip.
9
+ media?: string;
10
+ };
11
+
12
+ /**
13
+ * One option value as a selectable pill. Rendered as a `radio` inside the
14
+ * variant picker's `radiogroup`; the picker owns selection state and passes
15
+ * `selected`/`disabled` computed from the sparse variant list.
16
+ */
17
+ export function Chip({
18
+ label,
19
+ selected,
20
+ disabled,
21
+ onSelect,
22
+ media,
23
+ }: ChipProps) {
24
+ return (
25
+ <button
26
+ type="button"
27
+ role="radio"
28
+ aria-checked={selected ?? false}
29
+ aria-disabled={disabled ?? false}
30
+ disabled={disabled}
31
+ className={styles.chip({ selected, disabled })}
32
+ onClick={disabled ? undefined : onSelect}
33
+ >
34
+ {media ? (
35
+ <img className={styles.swatch} src={media} alt="" draggable={false} />
36
+ ) : null}
37
+ {label}
38
+ </button>
39
+ );
40
+ }
@@ -0,0 +1,3 @@
1
+ import { EmptyState } from "./empty-state";
2
+
3
+ export const NoResults = () => <EmptyState message="No products to show." />;
@@ -0,0 +1,12 @@
1
+ import { sprinkles, text } from "../design/tokens";
2
+
3
+ /** Centered message shown when there is nothing to render (e.g. no results). */
4
+ export function EmptyState({ message }: { message: string }) {
5
+ return (
6
+ <div
7
+ className={sprinkles({ p: "l", textAlign: "center", color: "subtle" })}
8
+ >
9
+ <p className={text({ style: "bodyM" })}>{message}</p>
10
+ </div>
11
+ );
12
+ }
@@ -0,0 +1,49 @@
1
+ import { style } from "@vanilla-extract/css";
2
+ import { recipe } from "@vanilla-extract/recipes";
3
+ import { colors, primitives } from "../design/tokens";
4
+
5
+ // @todo: collapsed height before "read more" appears. Tune to your type scale.
6
+ const COLLAPSED_MAX_HEIGHT = "6em";
7
+
8
+ export const container = style({
9
+ display: "flex",
10
+ flexDirection: "column",
11
+ alignItems: "flex-start",
12
+ gap: primitives.space["4xs"],
13
+ });
14
+
15
+ // `clamp` caps the height (applied whenever collapsed, so overflow can be
16
+ // measured); `fade` masks the last line to a soft cutoff instead of an ellipsis
17
+ // and is applied only when the text actually overflows (never on short copy).
18
+ export const body = recipe({
19
+ base: {
20
+ color: colors.content.intense,
21
+ whiteSpace: "pre-line", // preserve paragraph breaks from the source
22
+ },
23
+ variants: {
24
+ clamp: {
25
+ true: { maxHeight: COLLAPSED_MAX_HEIGHT, overflow: "hidden" },
26
+ false: {},
27
+ },
28
+ fade: {
29
+ true: {
30
+ maskImage:
31
+ "linear-gradient(to bottom, black 0%, black 55%, transparent 100%)",
32
+ WebkitMaskImage:
33
+ "linear-gradient(to bottom, black 0%, black 55%, transparent 100%)",
34
+ },
35
+ false: {},
36
+ },
37
+ },
38
+ defaultVariants: { clamp: false, fade: false },
39
+ });
40
+
41
+ export const toggle = style({
42
+ padding: 0,
43
+ border: "none",
44
+ background: "none",
45
+ color: colors.common.accent,
46
+ fontFamily: primitives.font.family.primary,
47
+ fontSize: primitives.font.size.s,
48
+ cursor: "pointer",
49
+ });
@@ -0,0 +1,20 @@
1
+ import { ExpandableText } from "./expandable-text";
2
+
3
+ const frame = { maxWidth: 360 };
4
+
5
+ const LONG =
6
+ "A relaxed-fit jacket in water-repellent cotton.\n\nDropped shoulders, a two-way zip, and ribbed cuffs. Fully lined, with two zip pockets at the front and one inside. Designed to layer over a knit through the cooler months, and cut long enough to sit past the hip.";
7
+
8
+ export const Long = () => (
9
+ <div style={frame}>
10
+ <ExpandableText>{LONG}</ExpandableText>
11
+ </div>
12
+ );
13
+
14
+ export const Short = () => (
15
+ <div style={frame}>
16
+ <ExpandableText>
17
+ A short description that never needs a toggle.
18
+ </ExpandableText>
19
+ </div>
20
+ );
@@ -0,0 +1,52 @@
1
+ import { useLayoutEffect, useRef, useState } from "react";
2
+ import { text } from "../design/tokens";
3
+ import { useLabels } from "../i18n";
4
+ import { cx } from "../lib/cx";
5
+ import * as styles from "./expandable-text.css";
6
+
7
+ /**
8
+ * Clamps long copy to a few lines with a "read more" toggle. Truncation is
9
+ * measured, not assumed: the toggle only appears when the text actually
10
+ * overflows the collapsed height, so short copy shows no button.
11
+ */
12
+ export function ExpandableText({ children }: { children: string }) {
13
+ const labels = useLabels();
14
+ const bodyRef = useRef<HTMLParagraphElement>(null);
15
+ const [expanded, setExpanded] = useState(false);
16
+ const [overflows, setOverflows] = useState(false);
17
+
18
+ const collapsed = !expanded;
19
+
20
+ // Measure against the collapsed height (the clamp is applied whenever
21
+ // collapsed, so scrollHeight vs clientHeight is meaningful). Re-run when the
22
+ // text changes (e.g. the client switches variant) so the toggle tracks it.
23
+ useLayoutEffect(() => {
24
+ const el = bodyRef.current;
25
+ if (el && collapsed) {
26
+ setOverflows(el.scrollHeight > el.clientHeight + 1);
27
+ }
28
+ }, [children, collapsed]);
29
+
30
+ return (
31
+ <div className={styles.container}>
32
+ <p
33
+ ref={bodyRef}
34
+ className={cx(
35
+ text({ style: "bodyS" }),
36
+ styles.body({ clamp: collapsed, fade: collapsed && overflows }),
37
+ )}
38
+ >
39
+ {children}
40
+ </p>
41
+ {overflows ? (
42
+ <button
43
+ type="button"
44
+ className={styles.toggle}
45
+ onClick={() => setExpanded((v) => !v)}
46
+ >
47
+ {expanded ? labels.readLess : labels.readMore}
48
+ </button>
49
+ ) : null}
50
+ </div>
51
+ );
52
+ }
@@ -0,0 +1,169 @@
1
+ import { globalStyle, style } from "@vanilla-extract/css";
2
+ import { colors, primitives } from "../design/tokens";
3
+
4
+ export const gallery = style({
5
+ position: "relative",
6
+ });
7
+
8
+ // Rail mode only (THUMBNAIL_RAIL): lay the rail beside the image on desktop. The
9
+ // breakpoint matches the PDP's two-column grid and resolves against the same
10
+ // `.detail` container, so the rail appears exactly when the page goes two-column.
11
+ export const galleryRail = style({
12
+ "@container": {
13
+ "(min-width: 560px)": {
14
+ display: "flex",
15
+ flexDirection: "row",
16
+ gap: primitives.space["2xs"],
17
+ alignItems: "stretch",
18
+ },
19
+ },
20
+ });
21
+
22
+ // Image column: the positioning context for the overlaid chevrons, and the flex
23
+ // child that takes the width left of the rail on desktop.
24
+ export const mainCol = style({
25
+ position: "relative",
26
+ minWidth: 0,
27
+ "@container": {
28
+ "(min-width: 560px)": { flex: 1 },
29
+ },
30
+ });
31
+
32
+ // Scroll-snap track: one image per view. Native swipe on touch; the chevrons
33
+ // are the pointer affordance. Scrollbar hidden.
34
+ export const track = style({
35
+ display: "flex",
36
+ overflowX: "auto",
37
+ scrollSnapType: "x mandatory",
38
+ scrollBehavior: "smooth",
39
+ scrollbarWidth: "none",
40
+ borderRadius: primitives.radius.m,
41
+ "@media": {
42
+ "(prefers-reduced-motion: reduce)": { scrollBehavior: "auto" },
43
+ },
44
+ });
45
+
46
+ globalStyle(`${track}::-webkit-scrollbar`, { display: "none" });
47
+
48
+ export const slide = style({
49
+ flex: "0 0 100%",
50
+ scrollSnapAlign: "start",
51
+ // Reserve a square box so images load without shifting the page. @todo: match
52
+ // the carousel card's aspect ratio / fit if you changed them there.
53
+ aspectRatio: "1",
54
+ backgroundColor: colors.surface.subtle,
55
+ });
56
+
57
+ export const image = style({
58
+ width: "100%",
59
+ height: "100%",
60
+ objectFit: "contain",
61
+ display: "block",
62
+ pointerEvents: "none",
63
+ userSelect: "none",
64
+ });
65
+
66
+ // Prev/next chevrons, vertically centered, disabled at the ends.
67
+ export const nav = style({
68
+ position: "absolute",
69
+ top: "50%",
70
+ transform: "translateY(-50%)",
71
+ display: "grid",
72
+ placeItems: "center",
73
+ width: "36px",
74
+ height: "36px",
75
+ padding: 0,
76
+ borderRadius: primitives.radius.full,
77
+ border: `${primitives.stroke.thin} solid ${colors.border.subtle}`,
78
+ backgroundColor: colors.surface.extraLight,
79
+ color: colors.content.intense,
80
+ cursor: "pointer",
81
+ transition: "opacity 150ms ease",
82
+ selectors: {
83
+ "&:disabled": { opacity: 0, pointerEvents: "none" },
84
+ },
85
+ });
86
+
87
+ export const navPrev = style({ left: primitives.space["3xs"] });
88
+ export const navNext = style({ right: primitives.space["3xs"] });
89
+
90
+ // Position indicator: a thin track with a fill sized to (index + 1) / count.
91
+ // @todo: style to your brand — height, track/fill colors, radius, width, and placement.
92
+ export const progress = style({
93
+ height: "3px",
94
+ marginTop: primitives.space["3xs"],
95
+ borderRadius: primitives.radius.full,
96
+ backgroundColor: colors.border.thin,
97
+ overflow: "hidden",
98
+ });
99
+
100
+ export const progressFill = style({
101
+ height: "100%",
102
+ borderRadius: primitives.radius.full,
103
+ backgroundColor: colors.content.intense,
104
+ transition: "width 200ms ease",
105
+ "@media": {
106
+ "(prefers-reduced-motion: reduce)": { transition: "none" },
107
+ },
108
+ });
109
+
110
+ // In rail mode the rail conveys position on desktop, so the progress bar is
111
+ // mobile-only there.
112
+ export const progressMobileOnly = style({
113
+ "@container": {
114
+ "(min-width: 560px)": { display: "none" },
115
+ },
116
+ });
117
+
118
+ // Desktop thumbnail rail (THUMBNAIL_RAIL). Hidden on mobile, where the swipe
119
+ // track + progress bar own navigation. @todo: tune the rail width / thumb size.
120
+ export const rail = style({
121
+ display: "none",
122
+ "@container": {
123
+ "(min-width: 560px)": {
124
+ display: "flex",
125
+ flexDirection: "column",
126
+ flexShrink: 0,
127
+ gap: primitives.space["2xs"],
128
+ width: "64px",
129
+ minHeight: 0,
130
+ overflowY: "auto",
131
+ scrollbarWidth: "none",
132
+ },
133
+ },
134
+ });
135
+
136
+ globalStyle(`${rail}::-webkit-scrollbar`, { display: "none" });
137
+
138
+ export const thumb = style({
139
+ boxSizing: "border-box",
140
+ flexShrink: 0,
141
+ width: "100%",
142
+ aspectRatio: "1",
143
+ minHeight: 0, // let aspect-ratio win over the flex-item default min-height
144
+ padding: primitives.space["4xs"],
145
+ borderRadius: primitives.radius.m,
146
+ border: `${primitives.stroke.thin} solid ${colors.border.subtle}`,
147
+ backgroundColor: colors.surface.extraLight,
148
+ cursor: "pointer",
149
+ transition: "border-color 150ms ease",
150
+ "@media": {
151
+ "(prefers-reduced-motion: reduce)": { transition: "none" },
152
+ },
153
+ });
154
+
155
+ // Selected thumb: thicker, accented border. border-box keeps the width swap from
156
+ // shifting the rail layout.
157
+ export const thumbActive = style({
158
+ borderColor: colors.common.accent,
159
+ borderWidth: primitives.stroke.medium,
160
+ });
161
+
162
+ export const thumbImage = style({
163
+ width: "100%",
164
+ height: "100%",
165
+ objectFit: "contain",
166
+ display: "block",
167
+ pointerEvents: "none",
168
+ userSelect: "none",
169
+ });
@@ -0,0 +1,30 @@
1
+ import { ImageGallery } from "./image-gallery";
2
+
3
+ function shot(fill: string) {
4
+ return `data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='400' height='400'%3E%3Crect width='400' height='400' fill='%23${fill}'/%3E%3Ccircle cx='200' cy='200' r='110' fill='%23ffffff' fill-opacity='0.5'/%3E%3C/svg%3E`;
5
+ }
6
+
7
+ // The gallery's desktop layout (and the optional THUMBNAIL_RAIL) query a
8
+ // `container-type` host, which the PDP's `.detail` provides in the app. The
9
+ // story supplies its own so it previews at a realistic width and renders
10
+ // correctly whichever way THUMBNAIL_RAIL is set — swipe when off, rail when on.
11
+ const frame = {
12
+ containerType: "inline-size" as const,
13
+ width: 600,
14
+ maxWidth: "100%",
15
+ };
16
+
17
+ export const Multiple = () => (
18
+ <div style={frame}>
19
+ <ImageGallery
20
+ media={[shot("e1e1e1"), shot("c9d4f5"), shot("f5d4c9")]}
21
+ alt="Product"
22
+ />
23
+ </div>
24
+ );
25
+
26
+ export const Single = () => (
27
+ <div style={frame}>
28
+ <ImageGallery media={[shot("e1e1e1")]} alt="Product" />
29
+ </div>
30
+ );
@@ -0,0 +1,172 @@
1
+ import { useEffect, useRef, useState } from "react";
2
+ import { useLabels } from "../i18n";
3
+ import { cx } from "../lib/cx";
4
+ import * as styles from "./image-gallery.css";
5
+
6
+ // @todo: show a thumbnail rail beside the image on desktop (at the two-column
7
+ // breakpoint); swipe-only when false. Off by default. When true, tune the rail
8
+ // width / thumb size in image-gallery.css.ts. Deferred polish (add if the rail
9
+ // earns it): an edge-fade mask and auto-centering the active thumb, both of
10
+ // which need a vertical scroll-edges hook.
11
+ const THUMBNAIL_RAIL: boolean = false;
12
+
13
+ function Chevron({ direction }: { direction: "left" | "right" }) {
14
+ const d = direction === "left" ? "M15 18l-6-6 6-6" : "M9 18l6-6-6-6";
15
+ return (
16
+ <svg
17
+ width="20"
18
+ height="20"
19
+ viewBox="0 0 24 24"
20
+ fill="none"
21
+ stroke="currentColor"
22
+ strokeWidth="2"
23
+ strokeLinecap="round"
24
+ strokeLinejoin="round"
25
+ aria-hidden="true"
26
+ >
27
+ <path d={d} />
28
+ </svg>
29
+ );
30
+ }
31
+
32
+ /**
33
+ * Product image gallery: a native scroll-snap track with prev/next chevrons and
34
+ * a progress bar; the current index is read from scroll position (no library).
35
+ * With the rail on, a desktop thumbnail rail is added as a second controller of
36
+ * the SAME track (shared index, click = scroll). Mobile is always the swipe
37
+ * track.
38
+ */
39
+ export function ImageGallery({ media, alt }: { media: string[]; alt: string }) {
40
+ const labels = useLabels();
41
+ const trackRef = useRef<HTMLDivElement>(null);
42
+ const railRef = useRef<HTMLDivElement>(null);
43
+ const [index, setIndex] = useState(0);
44
+ const [imageHeight, setImageHeight] = useState<number>();
45
+
46
+ function onScroll() {
47
+ const el = trackRef.current;
48
+ if (el) {
49
+ setIndex(Math.round(el.scrollLeft / el.clientWidth));
50
+ }
51
+ }
52
+
53
+ function scrollToIndex(i: number) {
54
+ const el = trackRef.current;
55
+ if (el) {
56
+ el.scrollTo({ left: i * el.clientWidth });
57
+ }
58
+ }
59
+
60
+ // The gallery stays mounted across a variant switch, so reset to the first
61
+ // image when the media set changes; otherwise a stale index can point past a
62
+ // shorter set (bad progress width, wrong nav state).
63
+ useEffect(() => {
64
+ setIndex(0);
65
+ trackRef.current?.scrollTo({ left: 0 });
66
+ }, [media]);
67
+
68
+ // Rail only: cap the rail to the main image's height so it scrolls instead of
69
+ // stretching the layout. A vertical scroll container needs a definite height;
70
+ // the square image provides it, measured here.
71
+ useEffect(() => {
72
+ if (!THUMBNAIL_RAIL) {
73
+ return;
74
+ }
75
+ const el = trackRef.current;
76
+ if (!el) {
77
+ return;
78
+ }
79
+ const measure = () => setImageHeight(el.clientHeight);
80
+ measure();
81
+ const observer = new ResizeObserver(measure);
82
+ observer.observe(el);
83
+ return () => observer.disconnect();
84
+ }, []);
85
+
86
+ const multiple = media.length > 1;
87
+
88
+ return (
89
+ <div
90
+ className={cx(styles.gallery, THUMBNAIL_RAIL && styles.galleryRail)}
91
+ aria-roledescription={labels.carousel}
92
+ aria-label={alt}
93
+ >
94
+ {THUMBNAIL_RAIL && multiple ? (
95
+ <div
96
+ ref={railRef}
97
+ className={styles.rail}
98
+ style={{ maxHeight: imageHeight ? `${imageHeight}px` : undefined }}
99
+ >
100
+ {media.map((src, i) => (
101
+ <button
102
+ key={src}
103
+ type="button"
104
+ className={cx(styles.thumb, i === index && styles.thumbActive)}
105
+ aria-label={`${alt} (${i + 1})`}
106
+ aria-current={i === index}
107
+ onClick={() => scrollToIndex(i)}
108
+ >
109
+ <img
110
+ className={styles.thumbImage}
111
+ src={src}
112
+ alt=""
113
+ draggable={false}
114
+ />
115
+ </button>
116
+ ))}
117
+ </div>
118
+ ) : null}
119
+
120
+ <div className={styles.mainCol}>
121
+ <div ref={trackRef} className={styles.track} onScroll={onScroll}>
122
+ {media.map((src, i) => (
123
+ <div key={src} className={styles.slide}>
124
+ <img
125
+ className={styles.image}
126
+ src={src}
127
+ alt={i === 0 ? alt : ""}
128
+ loading={i === 0 ? "eager" : "lazy"}
129
+ draggable={false}
130
+ />
131
+ </div>
132
+ ))}
133
+ </div>
134
+
135
+ {multiple ? (
136
+ <>
137
+ <button
138
+ type="button"
139
+ aria-label={labels.previous}
140
+ className={cx(styles.nav, styles.navPrev)}
141
+ disabled={index === 0}
142
+ onClick={() => scrollToIndex(index - 1)}
143
+ >
144
+ <Chevron direction="left" />
145
+ </button>
146
+ <button
147
+ type="button"
148
+ aria-label={labels.next}
149
+ className={cx(styles.nav, styles.navNext)}
150
+ disabled={index === media.length - 1}
151
+ onClick={() => scrollToIndex(index + 1)}
152
+ >
153
+ <Chevron direction="right" />
154
+ </button>
155
+ <div
156
+ className={cx(
157
+ styles.progress,
158
+ THUMBNAIL_RAIL && styles.progressMobileOnly,
159
+ )}
160
+ aria-hidden="true"
161
+ >
162
+ <div
163
+ className={styles.progressFill}
164
+ style={{ width: `${((index + 1) / media.length) * 100}%` }}
165
+ />
166
+ </div>
167
+ </>
168
+ ) : null}
169
+ </div>
170
+ </div>
171
+ );
172
+ }