Repository navigation
Expand file tree
/
Copy pathcomponents.ts
More file actions
287 lines (253 loc) · 10.5 KB
/
Copy pathcomponents.ts
File metadata and controls
287 lines (253 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
'use client';
import {
createContext,
createElement,
Fragment,
useContext,
useEffect,
useLayoutEffect,
useMemo,
useRef,
useState,
type CSSProperties,
type ReactElement,
type ReactNode,
} from 'react';
import { loadCrosslyElements, type CrosslyTag } from './loader.js';
import type {
CatalogLoadedDetail,
CheckoutDetail,
CrosslyRange,
ErrorDetail,
IndexLoadedDetail,
MarkLoadedDetail,
} from './types.js';
/* ── Provider ──────────────────────────────────────────────────────── */
export interface CrosslyConfig {
/** Publishable key (`crossly_pk_live_…` / `crossly_pk_test_…`). Public by design. */
pk?: string;
/** Override the API base (the embed's `api` attribute). Defaults to `https://crossly.net/api`. For testing. */
api?: string;
}
const CrosslyContext = createContext<CrosslyConfig>({});
export interface CrosslyProviderProps extends CrosslyConfig {
children?: ReactNode;
}
/** Supplies a default `pk` (and `api`) to every Crossly component below it. A component's own prop wins. */
export function CrosslyProvider({ pk, api, children }: CrosslyProviderProps): ReactElement {
const value = useMemo<CrosslyConfig>(() => ({ pk, api }), [pk, api]);
return createElement(CrosslyContext.Provider, { value }, children);
}
/** The `pk`/`api` from the nearest `<CrosslyProvider>`. */
export function useCrosslyConfig(): CrosslyConfig {
return useContext(CrosslyContext);
}
/* ── Shared plumbing ───────────────────────────────────────────────── */
/** A typed event callback: the event's `detail` first, the raw `CustomEvent` second. */
export type CrosslyHandler<D> = (detail: D, event: CustomEvent<D>) => void;
export interface CrosslyBaseProps extends CrosslyConfig {
id?: string;
/** Applied to the custom element as `class`. Target it to set `--crossly-*` variables. */
className?: string;
/** Inline style on the custom element. CSS custom properties work: `{ '--crossly-accent': '#ff5a5f' }`. */
style?: CSSProperties & Record<`--${string}`, string | number | undefined>;
/**
* Rendered on the server and until the elements are registered in the browser
* (one dynamic import, shared by every component). Use it for a skeleton.
*/
fallback?: ReactNode;
/** `crossly:error`: the request failed (bad key, origin not allowed, network…). Also called if the embed script itself fails to load. */
onError?: CrosslyHandler<ErrorDetail>;
}
// useLayoutEffect warns during SSR in React 18. Nothing below runs on a server
// anyway (the element only renders once `ready`), so plain useEffect there.
const useIsoLayoutEffect = typeof window === 'undefined' ? useEffect : useLayoutEffect;
type Attrs = Record<string, string | number | null | undefined>;
type AnyHandler = CrosslyHandler<never> | undefined;
function stringAttrs(attrs: Attrs): Record<string, string> {
const out: Record<string, string> = {};
for (const [name, value] of Object.entries(attrs)) {
if (value === undefined || value === null || value === '') continue;
out[name] = String(value);
}
return out;
}
function syncAttr(el: Element, name: string, value: string | undefined): void {
if (value === undefined || value === '') {
if (el.hasAttribute(name)) el.removeAttribute(name);
} else if (el.getAttribute(name) !== value) {
el.setAttribute(name, value);
}
}
interface ElementSpec {
tag: CrosslyTag;
base: CrosslyBaseProps;
/** Rendered as attributes by React, so they are present before the element connects. */
attrs: Attrs;
/**
* Attributes React cannot render. `key` is reserved by React, so
* `<crossly-mark key>` gets it from a layout effect: the element's first,
* synchronous pass sees it missing and makes no request, then the attribute
* lands in the same commit (before paint) and triggers the one real load.
*/
imperative?: Record<string, string | undefined>;
/** DOM event name → callback. The key set must be stable per component. */
events: Record<string, AnyHandler>;
}
function useCrosslyElement(spec: ElementSpec): ReactElement | null {
const { tag, base, attrs, imperative, events } = spec;
const config = useContext(CrosslyContext);
const [ready, setReady] = useState(false);
const ref = useRef<HTMLElement | null>(null);
const handlers = useRef(events);
const onError = useRef(base.onError);
useIsoLayoutEffect(() => {
handlers.current = events;
onError.current = base.onError;
});
useEffect(() => {
let live = true;
loadCrosslyElements().then(
() => {
if (live) setReady(true);
},
(err: unknown) => {
if (!live) return;
const detail: ErrorDetail = {
message: err instanceof Error ? err.message : 'Could not load the Crossly embed.',
};
onError.current?.(detail, new CustomEvent('crossly:error', { detail }));
},
);
return () => {
live = false;
};
}, []);
const imperativeKey = imperative ? JSON.stringify(imperative) : '';
useIsoLayoutEffect(() => {
const el = ref.current;
if (!el || !imperative) return;
for (const [name, value] of Object.entries(imperative)) syncAttr(el, name, value);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [ready, imperativeKey]);
useIsoLayoutEffect(() => {
const el = ref.current;
if (!el) return;
const names = [...Object.keys(handlers.current), 'crossly:error'];
const listeners = names.map((name) => {
const listener = (event: Event) => {
const custom = event as CustomEvent<never>;
// Ignore events bubbling up from a nested element we don't own.
if (event.target !== el) return;
const handler = name === 'crossly:error' ? onError.current : handlers.current[name];
handler?.(custom.detail, custom);
};
el.addEventListener(name, listener);
return [name, listener] as const;
});
return () => {
for (const [name, listener] of listeners) el.removeEventListener(name, listener);
};
}, [ready]);
if (!ready) return base.fallback === undefined ? null : createElement(Fragment, null, base.fallback);
return createElement(tag, {
ref,
id: base.id,
// Custom elements take `class`, not `className`, in React 18.
class: base.className,
style: base.style,
...stringAttrs({
pk: base.pk ?? config.pk,
api: base.api ?? config.api,
...attrs,
}),
});
}
/* ── <Catalog> ─────────────────────────────────────────────────────── */
export interface CatalogProps extends CrosslyBaseProps {
/** How many listings to show, 1–48. The API defaults to 12. */
limit?: number;
/** Free-text filter over title and brand. See the README: fresh searches need a proof-of-work the 0.1.0 embed does not send. */
q?: string;
/** `crossly:loaded`: the listings that were rendered. */
onLoaded?: CrosslyHandler<CatalogLoadedDetail>;
}
/** `<crossly-catalog>`: the seller's active listings as a responsive grid. */
export function Catalog({ limit, q, onLoaded, ...base }: CatalogProps): ReactElement | null {
return useCrosslyElement({
tag: 'crossly-catalog',
base,
attrs: { limit, q },
events: { 'crossly:loaded': onLoaded as AnyHandler },
});
}
/* ── <BuyButton> ───────────────────────────────────────────────────── */
export interface BuyButtonProps extends CrosslyBaseProps {
/** The listing id. Required. */
listing: string;
/** Units to buy. Default 1 (the API accepts 1–10). */
quantity?: number;
/** Button text. Default "Buy now". */
label?: string;
/** Where the hosted checkout opens. `_self` (default) navigates this tab; anything else is passed to `window.open`. */
target?: '_self' | '_blank' | (string & {});
/**
* `crossly:checkout`: a Crossly-hosted checkout URL was issued. Fired just
* before the embed navigates. The event is `cancelable`, but @crossly/embed
* 0.1.0 navigates regardless of `preventDefault()`.
*/
onCheckout?: CrosslyHandler<CheckoutDetail>;
}
/** `<crossly-buy-button>`: starts a Crossly-hosted checkout. It never takes payment details on your page. */
export function BuyButton({
listing,
quantity,
label,
target,
onCheckout,
...base
}: BuyButtonProps): ReactElement | null {
return useCrosslyElement({
tag: 'crossly-buy-button',
base,
attrs: { listing, quantity, label, target },
events: { 'crossly:checkout': onCheckout as AnyHandler },
});
}
/* ── <PriceIndex> ──────────────────────────────────────────────────── */
export interface PriceIndexProps extends CrosslyBaseProps {
/** The Vault Markets index slug, e.g. `crossly-50`. Required. */
slug: string;
/** Sparkline history. Default `1y`. */
range?: CrosslyRange;
/** `crossly:loaded`: the index payload that was rendered. */
onLoaded?: CrosslyHandler<IndexLoadedDetail>;
}
/** `<crossly-index>`: a Crossly Vault Markets index level with its quality, n and as-of date. */
export function PriceIndex({ slug, range, onLoaded, ...base }: PriceIndexProps): ReactElement | null {
return useCrosslyElement({
tag: 'crossly-index',
base,
attrs: { slug, range },
events: { 'crossly:loaded': onLoaded as AnyHandler },
});
}
/* ── <PriceMark> ───────────────────────────────────────────────────── */
export interface PriceMarkProps extends CrosslyBaseProps {
/** The market's book key, `<variantId>:<condition>:<grade>`. Rendered as the element's `key` attribute. Required. */
bookKey: string;
/** Sparkline history. Default `1y`. */
range?: CrosslyRange;
/** `crossly:loaded`: the mark payload that was rendered. */
onLoaded?: CrosslyHandler<MarkLoadedDetail>;
}
/** `<crossly-mark>`: one Vault Markets market's mark, with its quality, trades in the last 30 days and as-of date. */
export function PriceMark({ bookKey, range, onLoaded, ...base }: PriceMarkProps): ReactElement | null {
return useCrosslyElement({
tag: 'crossly-mark',
base,
attrs: { range },
imperative: { key: bookKey || undefined },
events: { 'crossly:loaded': onLoaded as AnyHandler },
});
}