create-skybridge 0.0.0-dev.08be486 → 0.0.0-dev.09043e8
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.
- package/dist/index.js +52 -26
- package/package.json +1 -1
- package/templates/blank/package.json +2 -1
- package/templates/blank/vite.config.ts +1 -1
- package/templates/demo/package.json +2 -1
- package/templates/demo/vite.config.ts +1 -1
- package/templates/ecom/.dockerignore +4 -0
- package/templates/ecom/.env.template +7 -0
- package/templates/ecom/.ladle/components.tsx +26 -0
- package/templates/ecom/.ladle/config.mjs +11 -0
- package/templates/ecom/.ladle/vite.config.ts +11 -0
- package/templates/ecom/AGENTS.md +2 -0
- package/templates/ecom/Dockerfile +53 -0
- package/templates/ecom/README.md +92 -0
- package/templates/ecom/_gitignore +9 -0
- package/templates/ecom/alpic.json +3 -0
- package/templates/ecom/node_modules/.bin/alpic +21 -0
- package/templates/ecom/node_modules/.bin/ladle +21 -0
- package/templates/ecom/node_modules/.bin/sb +21 -0
- package/templates/ecom/node_modules/.bin/skybridge +21 -0
- package/templates/ecom/node_modules/.bin/tsc +21 -0
- package/templates/ecom/node_modules/.bin/tsserver +21 -0
- package/templates/ecom/node_modules/.bin/tsx +21 -0
- package/templates/ecom/node_modules/.bin/vite +21 -0
- package/templates/ecom/package.json +42 -0
- package/templates/ecom/src/catalog/index.ts +19 -0
- package/templates/ecom/src/catalog/mock.ts +134 -0
- package/templates/ecom/src/catalog/shopify.ts +264 -0
- package/templates/ecom/src/components/chip.css.ts +63 -0
- package/templates/ecom/src/components/chip.stories.tsx +27 -0
- package/templates/ecom/src/components/chip.tsx +49 -0
- package/templates/ecom/src/components/empty-state.stories.tsx +3 -0
- package/templates/ecom/src/components/empty-state.tsx +12 -0
- package/templates/ecom/src/components/expandable-text.css.ts +49 -0
- package/templates/ecom/src/components/expandable-text.stories.tsx +20 -0
- package/templates/ecom/src/components/expandable-text.tsx +53 -0
- package/templates/ecom/src/components/image-gallery.css.ts +172 -0
- package/templates/ecom/src/components/image-gallery.stories.tsx +30 -0
- package/templates/ecom/src/components/image-gallery.tsx +163 -0
- package/templates/ecom/src/components/product-card.css.ts +159 -0
- package/templates/ecom/src/components/product-card.stories.tsx +58 -0
- package/templates/ecom/src/components/product-card.tsx +102 -0
- package/templates/ecom/src/components/product-carousel.css.ts +134 -0
- package/templates/ecom/src/components/product-carousel.stories.tsx +64 -0
- package/templates/ecom/src/components/product-carousel.tsx +202 -0
- package/templates/ecom/src/components/variant-picker.css.ts +27 -0
- package/templates/ecom/src/components/variant-picker.stories.tsx +67 -0
- package/templates/ecom/src/components/variant-picker.tsx +70 -0
- package/templates/ecom/src/components/view-frame.css.ts +22 -0
- package/templates/ecom/src/components/view-frame.tsx +27 -0
- package/templates/ecom/src/config.ts +12 -0
- package/templates/ecom/src/design/contract.css.ts +39 -0
- package/templates/ecom/src/design/fonts.css +15 -0
- package/templates/ecom/src/design/primitives.css.ts +101 -0
- package/templates/ecom/src/design/recipes/typography.css.ts +75 -0
- package/templates/ecom/src/design/sprinkles.css.ts +128 -0
- package/templates/ecom/src/design/themes/dark.css.ts +36 -0
- package/templates/ecom/src/design/themes/light.css.ts +36 -0
- package/templates/ecom/src/design/tokens.ts +8 -0
- package/templates/ecom/src/helpers.ts +4 -0
- package/templates/ecom/src/i18n.ts +35 -0
- package/templates/ecom/src/index.css +9 -0
- package/templates/ecom/src/lib/cx.ts +9 -0
- package/templates/ecom/src/lib/format.ts +9 -0
- package/templates/ecom/src/lib/variants.ts +150 -0
- package/templates/ecom/src/server.ts +43 -0
- package/templates/ecom/src/tools/render-carousel.ts +161 -0
- package/templates/ecom/src/tools/search-products.ts +196 -0
- package/templates/ecom/src/types.ts +87 -0
- package/templates/ecom/src/views/carousel/detail/detail.css.ts +115 -0
- package/templates/ecom/src/views/carousel/detail/detail.stories.tsx +133 -0
- package/templates/ecom/src/views/carousel/detail/index.tsx +254 -0
- package/templates/ecom/src/views/carousel/index.tsx +233 -0
- package/templates/ecom/tsconfig.json +11 -0
- package/templates/ecom/vite.config.ts +8 -0
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import type { ReactNode } from "react";
|
|
2
|
+
import { useUser } from "skybridge/web";
|
|
3
|
+
import { text } from "../design/tokens";
|
|
4
|
+
import { useLabels } from "../i18n";
|
|
5
|
+
import { cx } from "../lib/cx";
|
|
6
|
+
import { formatPrice } from "../lib/format";
|
|
7
|
+
import type { Price } from "../types.js";
|
|
8
|
+
import * as styles from "./product-card.css";
|
|
9
|
+
|
|
10
|
+
type ProductCardProps = {
|
|
11
|
+
title: string;
|
|
12
|
+
price?: Price;
|
|
13
|
+
media?: string[]; // all images; the card decides which to show
|
|
14
|
+
outOfStock?: boolean;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
// @todo: for boxed cards (border + background), set this to true and leave the
|
|
18
|
+
// carousel unframed. Pick one, not both.
|
|
19
|
+
const FRAMED = false;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* A single product card: image, title, price, and an out-of-stock treatment.
|
|
23
|
+
* Maps a product's display fields to markup; the view passes them in.
|
|
24
|
+
*/
|
|
25
|
+
export function ProductCard({
|
|
26
|
+
title,
|
|
27
|
+
price,
|
|
28
|
+
media,
|
|
29
|
+
outOfStock,
|
|
30
|
+
}: ProductCardProps) {
|
|
31
|
+
const { locale } = useUser();
|
|
32
|
+
const labels = useLabels();
|
|
33
|
+
// Show the first image. @todo: the rest of `media` is available here, e.g. to
|
|
34
|
+
// swap in media[1] on hover (do not cross-fade, breaks with transparency)
|
|
35
|
+
const cover = media?.[0];
|
|
36
|
+
return (
|
|
37
|
+
<article className={styles.card({ framed: FRAMED })}>
|
|
38
|
+
<div className={styles.imageBox}>
|
|
39
|
+
{cover ? (
|
|
40
|
+
<img
|
|
41
|
+
className={cx(styles.image, outOfStock && styles.imageDimmed)}
|
|
42
|
+
src={cover}
|
|
43
|
+
alt={title}
|
|
44
|
+
loading="lazy"
|
|
45
|
+
draggable={false}
|
|
46
|
+
/>
|
|
47
|
+
) : (
|
|
48
|
+
<div className={styles.placeholder} />
|
|
49
|
+
)}
|
|
50
|
+
{outOfStock ? (
|
|
51
|
+
<span className={styles.oosBadge}>{labels.outOfStock}</span>
|
|
52
|
+
) : null}
|
|
53
|
+
</div>
|
|
54
|
+
|
|
55
|
+
<div className={styles.body}>
|
|
56
|
+
<p
|
|
57
|
+
className={cx(
|
|
58
|
+
text({ style: "bodyS", weight: "medium" }),
|
|
59
|
+
styles.title,
|
|
60
|
+
)}
|
|
61
|
+
>
|
|
62
|
+
{title}
|
|
63
|
+
</p>
|
|
64
|
+
{price ? (
|
|
65
|
+
<p className={cx(text({ style: "bodyS" }), styles.price)}>
|
|
66
|
+
{formatPrice(price, locale)}
|
|
67
|
+
</p>
|
|
68
|
+
) : null}
|
|
69
|
+
{/* @todo: to show a custom Meta field (rating → stars, discountPct →
|
|
70
|
+
badge, badges → chips), add it to ProductCardProps, pass
|
|
71
|
+
card.<field> from the carousel view, and render it here. */}
|
|
72
|
+
</div>
|
|
73
|
+
</article>
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** Placeholder card shown while the tool resolves. Same footprint as the card
|
|
78
|
+
* so the layout does not jump when data arrives. */
|
|
79
|
+
export function ProductCardSkeleton() {
|
|
80
|
+
const titleLines: ReactNode[] = [];
|
|
81
|
+
for (let i = 0; i < styles.TITLE_LINES; i++) {
|
|
82
|
+
const isLast = i === styles.TITLE_LINES - 1;
|
|
83
|
+
titleLines.push(
|
|
84
|
+
<div
|
|
85
|
+
key={i}
|
|
86
|
+
className={cx(
|
|
87
|
+
styles.skeletonBox,
|
|
88
|
+
isLast ? styles.skeletonLineShort : styles.skeletonLine,
|
|
89
|
+
)}
|
|
90
|
+
/>,
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
return (
|
|
94
|
+
<div className={styles.card({ framed: FRAMED })} aria-hidden="true">
|
|
95
|
+
<div className={cx(styles.skeletonBox, styles.skeletonImage)} />
|
|
96
|
+
<div className={styles.skeletonBody}>
|
|
97
|
+
{titleLines}
|
|
98
|
+
<div className={cx(styles.skeletonBox, styles.skeletonPrice)} />
|
|
99
|
+
</div>
|
|
100
|
+
</div>
|
|
101
|
+
);
|
|
102
|
+
}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { globalStyle, style } from "@vanilla-extract/css";
|
|
2
|
+
import { recipe } from "@vanilla-extract/recipes";
|
|
3
|
+
import { colors, primitives } from "../design/tokens";
|
|
4
|
+
|
|
5
|
+
// Carousel tuning knobs. @todo: tune to your card design.
|
|
6
|
+
const gap = primitives.space["2xs"]; // gap between cards
|
|
7
|
+
// Cards visible at each width; the fractional part is the peek that signals the
|
|
8
|
+
// row scrolls. If you change a count, adjust the gap multiplier in its
|
|
9
|
+
// `--card-width` calc (full gaps visible = the whole-number part).
|
|
10
|
+
const CARDS_VISIBLE = 2.5;
|
|
11
|
+
const CARDS_VISIBLE_COMPACT = 1.5;
|
|
12
|
+
// Below this container width, switch to the compact count.
|
|
13
|
+
const COMPACT_MAX_WIDTH = "560px";
|
|
14
|
+
|
|
15
|
+
// Structural wrapper (kept as a plain class so the nav-reveal globalStyle below
|
|
16
|
+
// can target it). The recipe composes it as its base.
|
|
17
|
+
const carouselBase = style({
|
|
18
|
+
position: "relative",
|
|
19
|
+
// Size cards against the carousel width, not the iframe viewport.
|
|
20
|
+
containerType: "inline-size",
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
// `framed: true` frames the whole carousel; the default is flush (pair it with
|
|
24
|
+
// framed cards instead). Pick one of the two.
|
|
25
|
+
export const carousel = recipe({
|
|
26
|
+
base: carouselBase,
|
|
27
|
+
variants: {
|
|
28
|
+
framed: {
|
|
29
|
+
// @todo: tune the outer frame to your brand. Soften the shadow in dark
|
|
30
|
+
// mode if the strip reads as a hard box against the host.
|
|
31
|
+
true: {
|
|
32
|
+
// Pad top/bottom only; the leading/trailing inset lives on the track's
|
|
33
|
+
// own `framed` variant so it scrolls away. Cards bleed to the left/right
|
|
34
|
+
// border, and `overflow: hidden` masks them to the rounded corners, so
|
|
35
|
+
// a scrolled card disappears exactly at the frame edge.
|
|
36
|
+
paddingBlock: primitives.space.xs,
|
|
37
|
+
borderRadius: primitives.radius.l,
|
|
38
|
+
border: `${primitives.stroke.thin} solid ${colors.border.thin}`,
|
|
39
|
+
backgroundColor: colors.surface.extraLight,
|
|
40
|
+
boxShadow: "0 10px 20px -20px rgba(0, 0, 0, 0.18)",
|
|
41
|
+
overflow: "hidden",
|
|
42
|
+
},
|
|
43
|
+
false: {},
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
defaultVariants: { framed: false },
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
// Kept as a plain class so the scrollbar-hiding globalStyle below can target it.
|
|
50
|
+
// The recipe composes it as its base.
|
|
51
|
+
const trackBase = style({
|
|
52
|
+
display: "flex",
|
|
53
|
+
gap,
|
|
54
|
+
overflowX: "auto",
|
|
55
|
+
scrollSnapType: "x mandatory",
|
|
56
|
+
scrollBehavior: "smooth",
|
|
57
|
+
scrollbarWidth: "none",
|
|
58
|
+
vars: { "--card-width": `calc((100% - 2 * ${gap}) / ${CARDS_VISIBLE})` },
|
|
59
|
+
"@container": {
|
|
60
|
+
[`(max-width: ${COMPACT_MAX_WIDTH})`]: {
|
|
61
|
+
vars: {
|
|
62
|
+
"--card-width": `calc((100% - ${gap}) / ${CARDS_VISIBLE_COMPACT})`,
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
},
|
|
66
|
+
"@media": {
|
|
67
|
+
"(prefers-reduced-motion: reduce)": { scrollBehavior: "auto" },
|
|
68
|
+
},
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
globalStyle(`${trackBase}::-webkit-scrollbar`, { display: "none" });
|
|
72
|
+
|
|
73
|
+
// `framed: true` adds an inset on both ends. Padding on the scroll track (not on
|
|
74
|
+
// the frame) so it shows before the first card and after the last at rest, but
|
|
75
|
+
// scrolls away in between: a card scrolled past clips at the border, not at a
|
|
76
|
+
// fixed gutter. scroll-padding keeps mandatory snap from collapsing the inset.
|
|
77
|
+
export const track = recipe({
|
|
78
|
+
base: trackBase,
|
|
79
|
+
variants: {
|
|
80
|
+
framed: {
|
|
81
|
+
true: {
|
|
82
|
+
paddingInline: primitives.space.xs,
|
|
83
|
+
scrollPaddingInline: primitives.space.xs,
|
|
84
|
+
},
|
|
85
|
+
false: {},
|
|
86
|
+
},
|
|
87
|
+
// Skeleton state: lock the scroll, there is nothing to scroll to yet.
|
|
88
|
+
loading: {
|
|
89
|
+
true: { overflowX: "hidden" },
|
|
90
|
+
false: {},
|
|
91
|
+
},
|
|
92
|
+
},
|
|
93
|
+
defaultVariants: { framed: false, loading: false },
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
export const cell = style({
|
|
97
|
+
flex: "0 0 var(--card-width)",
|
|
98
|
+
scrollSnapAlign: "start",
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
// Prev/next buttons: desktop only. Hidden on touch, revealed on hover/focus,
|
|
102
|
+
// disabled at the scroll ends.
|
|
103
|
+
export const nav = style({
|
|
104
|
+
position: "absolute",
|
|
105
|
+
top: "50%",
|
|
106
|
+
transform: "translateY(-50%)",
|
|
107
|
+
display: "grid",
|
|
108
|
+
placeItems: "center",
|
|
109
|
+
width: "36px",
|
|
110
|
+
height: "36px",
|
|
111
|
+
padding: 0,
|
|
112
|
+
borderRadius: primitives.radius.full,
|
|
113
|
+
border: `${primitives.stroke.thin} solid ${colors.border.subtle}`,
|
|
114
|
+
backgroundColor: colors.surface.extraLight,
|
|
115
|
+
color: colors.content.intense,
|
|
116
|
+
cursor: "pointer",
|
|
117
|
+
opacity: 0,
|
|
118
|
+
transition: "opacity 150ms ease",
|
|
119
|
+
selectors: {
|
|
120
|
+
"&:disabled": { opacity: 0, pointerEvents: "none" },
|
|
121
|
+
},
|
|
122
|
+
"@media": {
|
|
123
|
+
"(hover: none)": { display: "none" },
|
|
124
|
+
},
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
export const navPrev = style({ left: primitives.space["3xs"] });
|
|
128
|
+
export const navNext = style({ right: primitives.space["3xs"] });
|
|
129
|
+
|
|
130
|
+
// Reveal the (enabled) buttons on hover or keyboard focus within the carousel.
|
|
131
|
+
globalStyle(
|
|
132
|
+
`${carouselBase}:hover ${nav}:not(:disabled), ${carouselBase}:focus-within ${nav}:not(:disabled)`,
|
|
133
|
+
{ opacity: 1 },
|
|
134
|
+
);
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import type { ReactNode } from "react";
|
|
2
|
+
import type { Price } from "../types.js";
|
|
3
|
+
import { ProductCard, ProductCardSkeleton } from "./product-card";
|
|
4
|
+
import { ProductCarousel } from "./product-carousel";
|
|
5
|
+
|
|
6
|
+
const IMAGE =
|
|
7
|
+
"data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='240' height='240'%3E%3Crect width='240' height='240' fill='%23e5e5e5'/%3E%3Ccircle cx='120' cy='120' r='70' fill='%23bcbcbc'/%3E%3C/svg%3E";
|
|
8
|
+
|
|
9
|
+
type Sample = { title: string; price: Price; outOfStock?: boolean };
|
|
10
|
+
|
|
11
|
+
const SAMPLE: Sample[] = [
|
|
12
|
+
{ title: "Merino wool sweater", price: { amount: 129.9, currency: "EUR" } },
|
|
13
|
+
{ title: "Oxford cotton shirt", price: { amount: 79, currency: "EUR" } },
|
|
14
|
+
{ title: "Slim chino trousers", price: { amount: 89.5, currency: "EUR" } },
|
|
15
|
+
{
|
|
16
|
+
title: "Leather derby shoes",
|
|
17
|
+
price: { amount: 210, currency: "EUR" },
|
|
18
|
+
outOfStock: true,
|
|
19
|
+
},
|
|
20
|
+
{ title: "Cashmere scarf", price: { amount: 145, currency: "EUR" } },
|
|
21
|
+
{ title: "Quilted field jacket", price: { amount: 320, currency: "EUR" } },
|
|
22
|
+
];
|
|
23
|
+
|
|
24
|
+
function sampleCards(): ReactNode[] {
|
|
25
|
+
const cards: ReactNode[] = [];
|
|
26
|
+
for (let i = 0; i < SAMPLE.length; i++) {
|
|
27
|
+
const item = SAMPLE[i];
|
|
28
|
+
cards.push(
|
|
29
|
+
<ProductCard
|
|
30
|
+
key={i}
|
|
31
|
+
title={item.title}
|
|
32
|
+
price={item.price}
|
|
33
|
+
media={[IMAGE]}
|
|
34
|
+
outOfStock={item.outOfStock}
|
|
35
|
+
/>,
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
return cards;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export const Default = () => <ProductCarousel>{sampleCards()}</ProductCarousel>;
|
|
42
|
+
|
|
43
|
+
export const Loading = () => {
|
|
44
|
+
const skeletons: ReactNode[] = [];
|
|
45
|
+
for (let i = 0; i < 4; i++) {
|
|
46
|
+
skeletons.push(<ProductCardSkeleton key={i} />);
|
|
47
|
+
}
|
|
48
|
+
return <ProductCarousel loading>{skeletons}</ProductCarousel>;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
export const FewItems = () => (
|
|
52
|
+
<ProductCarousel>
|
|
53
|
+
<ProductCard
|
|
54
|
+
title="Merino wool sweater"
|
|
55
|
+
price={{ amount: 129.9, currency: "EUR" }}
|
|
56
|
+
media={[IMAGE]}
|
|
57
|
+
/>
|
|
58
|
+
<ProductCard
|
|
59
|
+
title="Oxford cotton shirt"
|
|
60
|
+
price={{ amount: 79, currency: "EUR" }}
|
|
61
|
+
media={[IMAGE]}
|
|
62
|
+
/>
|
|
63
|
+
</ProductCarousel>
|
|
64
|
+
);
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Children,
|
|
3
|
+
type ReactNode,
|
|
4
|
+
type RefObject,
|
|
5
|
+
useCallback,
|
|
6
|
+
useEffect,
|
|
7
|
+
useRef,
|
|
8
|
+
useState,
|
|
9
|
+
} from "react";
|
|
10
|
+
import { useLabels } from "../i18n";
|
|
11
|
+
import { cx } from "../lib/cx";
|
|
12
|
+
import * as styles from "./product-carousel.css";
|
|
13
|
+
|
|
14
|
+
// How long to wait after the last intersection change before reporting, so a
|
|
15
|
+
// fast scroll does not spam updates.
|
|
16
|
+
const VISIBILITY_DEBOUNCE_MS = 200;
|
|
17
|
+
|
|
18
|
+
// @todo: for boxed cards (border + background), set this to true and leave the
|
|
19
|
+
// carousel unframed. Pick one, not both.
|
|
20
|
+
const FRAMED = false;
|
|
21
|
+
|
|
22
|
+
function Chevron({ direction }: { direction: "left" | "right" }) {
|
|
23
|
+
const d = direction === "left" ? "M15 18l-6-6 6-6" : "M9 18l6-6-6-6";
|
|
24
|
+
return (
|
|
25
|
+
<svg
|
|
26
|
+
width="20"
|
|
27
|
+
height="20"
|
|
28
|
+
viewBox="0 0 24 24"
|
|
29
|
+
fill="none"
|
|
30
|
+
stroke="currentColor"
|
|
31
|
+
strokeWidth="2"
|
|
32
|
+
strokeLinecap="round"
|
|
33
|
+
strokeLinejoin="round"
|
|
34
|
+
aria-hidden="true"
|
|
35
|
+
>
|
|
36
|
+
<path d={d} />
|
|
37
|
+
</svg>
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
type ProductCarouselProps = {
|
|
42
|
+
children: ReactNode;
|
|
43
|
+
// Reports which child indices are at least half in view, in ascending order,
|
|
44
|
+
// debounced. Use it to narrate only what the user actually sees.
|
|
45
|
+
onVisibleChange?: (indices: number[]) => void;
|
|
46
|
+
// Skeleton state: locks the scroll and hides the nav buttons.
|
|
47
|
+
loading?: boolean;
|
|
48
|
+
// Optional external ref to the scroll track. The orchestrator uses it to save
|
|
49
|
+
// and restore scroll position across the carousel ⇄ detail transition (the
|
|
50
|
+
// carousel is hidden, not unmounted, but display:none resets scrollLeft).
|
|
51
|
+
trackRef?: RefObject<HTMLElement | null>;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Horizontal, scroll-snapping track of cards. Generic: it renders whatever
|
|
56
|
+
* children it is given, one per snap cell. Native scroll handles touch; the
|
|
57
|
+
* prev/next buttons are desktop-only affordances (see product-carousel.css.ts).
|
|
58
|
+
*/
|
|
59
|
+
export function ProductCarousel({
|
|
60
|
+
children,
|
|
61
|
+
onVisibleChange,
|
|
62
|
+
loading = false,
|
|
63
|
+
trackRef: externalTrackRef,
|
|
64
|
+
}: ProductCarouselProps) {
|
|
65
|
+
const labels = useLabels();
|
|
66
|
+
const trackRef = useRef<HTMLElement>(null);
|
|
67
|
+
// Merge the internal ref with the optional external one via a callback ref, so
|
|
68
|
+
// the orchestrator can read/restore scroll while `trackRef` stays a plain
|
|
69
|
+
// useRef (its `.current` reads need no effect dependency).
|
|
70
|
+
const setTrackRef = useCallback(
|
|
71
|
+
(el: HTMLElement | null) => {
|
|
72
|
+
trackRef.current = el;
|
|
73
|
+
if (externalTrackRef) {
|
|
74
|
+
externalTrackRef.current = el;
|
|
75
|
+
}
|
|
76
|
+
},
|
|
77
|
+
[externalTrackRef],
|
|
78
|
+
);
|
|
79
|
+
const [canScrollLeft, setCanScrollLeft] = useState(false);
|
|
80
|
+
const [canScrollRight, setCanScrollRight] = useState(false);
|
|
81
|
+
|
|
82
|
+
useEffect(() => {
|
|
83
|
+
const el = trackRef.current;
|
|
84
|
+
if (!el) {
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
const updateEdges = () => {
|
|
88
|
+
setCanScrollLeft(el.scrollLeft > 0);
|
|
89
|
+
setCanScrollRight(el.scrollLeft + el.clientWidth < el.scrollWidth - 1);
|
|
90
|
+
};
|
|
91
|
+
updateEdges();
|
|
92
|
+
el.addEventListener("scroll", updateEdges, { passive: true });
|
|
93
|
+
const observer = new ResizeObserver(updateEdges);
|
|
94
|
+
observer.observe(el);
|
|
95
|
+
return () => {
|
|
96
|
+
el.removeEventListener("scroll", updateEdges);
|
|
97
|
+
observer.disconnect();
|
|
98
|
+
};
|
|
99
|
+
}, []);
|
|
100
|
+
|
|
101
|
+
// Track which cells are in view and report them (only when a consumer asks).
|
|
102
|
+
// Pass a stable `onVisibleChange` (e.g. a useState setter): a new identity
|
|
103
|
+
// each render re-subscribes the observer.
|
|
104
|
+
const childCount = Children.count(children);
|
|
105
|
+
// biome-ignore lint/correctness/useExhaustiveDependencies: childCount re-subscribes when the number of cells changes; it is not read in the body.
|
|
106
|
+
useEffect(() => {
|
|
107
|
+
const el = trackRef.current;
|
|
108
|
+
if (!el || !onVisibleChange) {
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
const cells = Array.from(el.children);
|
|
112
|
+
const visible = new Set<number>();
|
|
113
|
+
let timer: number | undefined;
|
|
114
|
+
const emit = () => {
|
|
115
|
+
const indices = [...visible].sort((a, b) => a - b);
|
|
116
|
+
onVisibleChange?.(indices);
|
|
117
|
+
};
|
|
118
|
+
const observer = new IntersectionObserver(
|
|
119
|
+
(entries) => {
|
|
120
|
+
for (const entry of entries) {
|
|
121
|
+
const index = cells.indexOf(entry.target);
|
|
122
|
+
if (index === -1) {
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
if (entry.isIntersecting) {
|
|
126
|
+
visible.add(index);
|
|
127
|
+
} else {
|
|
128
|
+
visible.delete(index);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
window.clearTimeout(timer);
|
|
132
|
+
timer = window.setTimeout(emit, VISIBILITY_DEBOUNCE_MS);
|
|
133
|
+
},
|
|
134
|
+
{ root: el, threshold: 0.5 },
|
|
135
|
+
);
|
|
136
|
+
for (const cell of cells) {
|
|
137
|
+
observer.observe(cell);
|
|
138
|
+
}
|
|
139
|
+
return () => {
|
|
140
|
+
window.clearTimeout(timer);
|
|
141
|
+
observer.disconnect();
|
|
142
|
+
};
|
|
143
|
+
}, [childCount, onVisibleChange]);
|
|
144
|
+
|
|
145
|
+
const step = (direction: 1 | -1) => {
|
|
146
|
+
const el = trackRef.current;
|
|
147
|
+
if (!el) {
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
const cell = el.firstElementChild as HTMLElement | null;
|
|
151
|
+
const gap = Number.parseFloat(getComputedStyle(el).columnGap) || 0;
|
|
152
|
+
const distance = cell ? cell.offsetWidth + gap : el.clientWidth;
|
|
153
|
+
// No `behavior`: inherits the track's CSS scroll-behavior (respects
|
|
154
|
+
// prefers-reduced-motion).
|
|
155
|
+
el.scrollBy({ left: direction * distance });
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
const cells: ReactNode[] = [];
|
|
159
|
+
const items = Children.toArray(children);
|
|
160
|
+
for (let i = 0; i < items.length; i++) {
|
|
161
|
+
cells.push(
|
|
162
|
+
<div key={i} className={styles.cell}>
|
|
163
|
+
{items[i]}
|
|
164
|
+
</div>,
|
|
165
|
+
);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
return (
|
|
169
|
+
<div className={styles.carousel({ framed: FRAMED })}>
|
|
170
|
+
<section
|
|
171
|
+
ref={setTrackRef}
|
|
172
|
+
className={styles.track({ framed: FRAMED, loading })}
|
|
173
|
+
aria-roledescription={labels.carousel}
|
|
174
|
+
aria-label={labels.products}
|
|
175
|
+
>
|
|
176
|
+
{cells}
|
|
177
|
+
</section>
|
|
178
|
+
{loading ? null : (
|
|
179
|
+
<>
|
|
180
|
+
<button
|
|
181
|
+
type="button"
|
|
182
|
+
aria-label={labels.previous}
|
|
183
|
+
className={cx(styles.nav, styles.navPrev)}
|
|
184
|
+
disabled={!canScrollLeft}
|
|
185
|
+
onClick={() => step(-1)}
|
|
186
|
+
>
|
|
187
|
+
<Chevron direction="left" />
|
|
188
|
+
</button>
|
|
189
|
+
<button
|
|
190
|
+
type="button"
|
|
191
|
+
aria-label={labels.next}
|
|
192
|
+
className={cx(styles.nav, styles.navNext)}
|
|
193
|
+
disabled={!canScrollRight}
|
|
194
|
+
onClick={() => step(1)}
|
|
195
|
+
>
|
|
196
|
+
<Chevron direction="right" />
|
|
197
|
+
</button>
|
|
198
|
+
</>
|
|
199
|
+
)}
|
|
200
|
+
</div>
|
|
201
|
+
);
|
|
202
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { style } from "@vanilla-extract/css";
|
|
2
|
+
import { colors, primitives } from "../design/tokens";
|
|
3
|
+
|
|
4
|
+
export const picker = style({
|
|
5
|
+
display: "flex",
|
|
6
|
+
flexDirection: "column",
|
|
7
|
+
gap: primitives.space.xs,
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
export const section = style({
|
|
11
|
+
display: "flex",
|
|
12
|
+
flexDirection: "column",
|
|
13
|
+
gap: primitives.space["3xs"],
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
export const label = style({
|
|
17
|
+
color: colors.content.subtle,
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
// Chip row: wraps to the next line when it overflows. @todo: swap for a
|
|
21
|
+
// horizontal scroll row with an edge fade (as the carousel track) if you expect
|
|
22
|
+
// many values on one axis.
|
|
23
|
+
export const values = style({
|
|
24
|
+
display: "flex",
|
|
25
|
+
flexWrap: "wrap",
|
|
26
|
+
gap: primitives.space["3xs"],
|
|
27
|
+
});
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { useState } from "react";
|
|
2
|
+
import { initialSelection, type Selection } from "../lib/variants.js";
|
|
3
|
+
import type { Product } from "../types.js";
|
|
4
|
+
import { VariantPicker } from "./variant-picker";
|
|
5
|
+
|
|
6
|
+
// A sparse catalog: {white,42} does not exist, so "42" is hard-disabled under
|
|
7
|
+
// "White"; {black,42} is sold out, so it renders struck (yet clickable) under
|
|
8
|
+
// "Black".
|
|
9
|
+
function variant(
|
|
10
|
+
id: string,
|
|
11
|
+
color: string,
|
|
12
|
+
size: string,
|
|
13
|
+
outOfStock = false,
|
|
14
|
+
): Product["variants"][number] {
|
|
15
|
+
return {
|
|
16
|
+
id,
|
|
17
|
+
selection: { color, size },
|
|
18
|
+
title: `Sneaker ${color} ${size}`,
|
|
19
|
+
price: { amount: 120, currency: "EUR" },
|
|
20
|
+
media: [],
|
|
21
|
+
specs: [],
|
|
22
|
+
outOfStock,
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const PRODUCT: Product = {
|
|
27
|
+
id: "sneaker",
|
|
28
|
+
options: [
|
|
29
|
+
{
|
|
30
|
+
id: "color",
|
|
31
|
+
label: "Color",
|
|
32
|
+
values: [
|
|
33
|
+
{ id: "black", label: "Black" },
|
|
34
|
+
{ id: "white", label: "White" },
|
|
35
|
+
],
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
id: "size",
|
|
39
|
+
label: "Size",
|
|
40
|
+
values: [
|
|
41
|
+
{ id: "40", label: "40" },
|
|
42
|
+
{ id: "42", label: "42" },
|
|
43
|
+
],
|
|
44
|
+
},
|
|
45
|
+
],
|
|
46
|
+
variants: [
|
|
47
|
+
variant("s-b-40", "black", "40"),
|
|
48
|
+
variant("s-b-42", "black", "42", true),
|
|
49
|
+
variant("s-w-40", "white", "40"),
|
|
50
|
+
],
|
|
51
|
+
card: { title: "Sneaker", media: [], specs: [] },
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
export const Contingency = () => {
|
|
55
|
+
const [selection, setSelection] = useState<Selection>(() =>
|
|
56
|
+
initialSelection(PRODUCT),
|
|
57
|
+
);
|
|
58
|
+
return (
|
|
59
|
+
<div style={{ maxWidth: 320 }}>
|
|
60
|
+
<VariantPicker
|
|
61
|
+
product={PRODUCT}
|
|
62
|
+
selection={selection}
|
|
63
|
+
onChange={setSelection}
|
|
64
|
+
/>
|
|
65
|
+
</div>
|
|
66
|
+
);
|
|
67
|
+
};
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { text } from "../design/tokens";
|
|
2
|
+
import { cx } from "../lib/cx";
|
|
3
|
+
import { applyChoice, axisStates, type Selection } from "../lib/variants.js";
|
|
4
|
+
import type { Product } from "../types.js";
|
|
5
|
+
import { Chip } from "./chip";
|
|
6
|
+
import * as styles from "./variant-picker.css";
|
|
7
|
+
|
|
8
|
+
type VariantPickerProps = {
|
|
9
|
+
product: Product;
|
|
10
|
+
selection: Selection;
|
|
11
|
+
onChange: (selection: Selection) => void;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Renders one section per option axis. Chip states come from axisStates
|
|
16
|
+
* (lib/variants.ts, top-down): nonexistent values are hard-disabled, sold-out
|
|
17
|
+
* ones struck but clickable (the CTA names the cause), and an axis that does
|
|
18
|
+
* not apply hides its row. A pick goes through applyChoice, which keeps
|
|
19
|
+
* still-existing later choices and snaps the rest onto a real variant.
|
|
20
|
+
*
|
|
21
|
+
* This is the in-place model: every axis is local state, no remount. @todo: if
|
|
22
|
+
* a catalog models an axis (e.g. color) as separate products, promote it to a
|
|
23
|
+
* cross-product switch that changes the opened product id instead.
|
|
24
|
+
*/
|
|
25
|
+
export function VariantPicker({
|
|
26
|
+
product,
|
|
27
|
+
selection,
|
|
28
|
+
onChange,
|
|
29
|
+
}: VariantPickerProps) {
|
|
30
|
+
return (
|
|
31
|
+
<div className={styles.picker}>
|
|
32
|
+
{product.options.map((option) => {
|
|
33
|
+
// Empty map: the axis does not apply to the current configuration.
|
|
34
|
+
const states = axisStates(product, option.id, selection);
|
|
35
|
+
if (states.size === 0) {
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
return (
|
|
39
|
+
<div
|
|
40
|
+
key={option.id}
|
|
41
|
+
className={styles.section}
|
|
42
|
+
role="radiogroup"
|
|
43
|
+
aria-label={option.label}
|
|
44
|
+
>
|
|
45
|
+
<span className={cx(text({ style: "labelS" }), styles.label)}>
|
|
46
|
+
{option.label}
|
|
47
|
+
</span>
|
|
48
|
+
<div className={styles.values}>
|
|
49
|
+
{option.values.map((value) => (
|
|
50
|
+
<Chip
|
|
51
|
+
key={value.id}
|
|
52
|
+
label={value.label}
|
|
53
|
+
media={value.media}
|
|
54
|
+
selected={selection[option.id] === value.id}
|
|
55
|
+
nonExistent={!states.has(value.id)}
|
|
56
|
+
outOfStock={states.get(value.id) === "soldOut"}
|
|
57
|
+
onSelect={() =>
|
|
58
|
+
onChange(
|
|
59
|
+
applyChoice(product, selection, option.id, value.id),
|
|
60
|
+
)
|
|
61
|
+
}
|
|
62
|
+
/>
|
|
63
|
+
))}
|
|
64
|
+
</div>
|
|
65
|
+
</div>
|
|
66
|
+
);
|
|
67
|
+
})}
|
|
68
|
+
</div>
|
|
69
|
+
);
|
|
70
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { globalStyle, style } from "@vanilla-extract/css";
|
|
2
|
+
import { colors, primitives } from "../design/tokens";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Base frame for every view. Transparent so the widget blends into the host
|
|
6
|
+
* surface; it pins the design system's font + content color so descendants
|
|
7
|
+
* inherit from the DS, not from the host page (whose text color could be white
|
|
8
|
+
* on a dark host and make card text vanish).
|
|
9
|
+
*/
|
|
10
|
+
export const viewFrame = style({
|
|
11
|
+
color: colors.content.intense,
|
|
12
|
+
fontFamily: primitives.font.family.primary,
|
|
13
|
+
fontSize: primitives.font.size.m,
|
|
14
|
+
lineHeight: primitives.font.lineHeight.normal,
|
|
15
|
+
WebkitFontSmoothing: "antialiased",
|
|
16
|
+
MozOsxFontSmoothing: "grayscale",
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
// Box-sizing reset scoped to the frame subtree.
|
|
20
|
+
globalStyle(`${viewFrame} *, ${viewFrame} *::before, ${viewFrame} *::after`, {
|
|
21
|
+
boxSizing: "border-box",
|
|
22
|
+
});
|