@spatial-elements/sveltekit 0.1.0-beta.1

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 (52) hide show
  1. package/LICENSE +373 -0
  2. package/README.md +17 -0
  3. package/dist/catalog/ScrollNavigationBridge.svelte +394 -0
  4. package/dist/catalog/ScrollNavigationBridge.svelte.d.ts +13 -0
  5. package/dist/catalog/catalogEndpointAction.d.ts +6 -0
  6. package/dist/catalog/catalogEndpointAction.js +18 -0
  7. package/dist/catalog/catalogNavigation.d.ts +7 -0
  8. package/dist/catalog/catalogNavigation.js +19 -0
  9. package/dist/catalog/catalogTransition.css +59 -0
  10. package/dist/index.d.ts +6 -0
  11. package/dist/index.js +6 -0
  12. package/dist/spatial-element/BrandStageShell.svelte +106 -0
  13. package/dist/spatial-element/BrandStageShell.svelte.d.ts +12 -0
  14. package/dist/spatial-element/CarouselSection.svelte +350 -0
  15. package/dist/spatial-element/CarouselSection.svelte.d.ts +15 -0
  16. package/dist/spatial-element/CarouselSpatialElement.svelte +388 -0
  17. package/dist/spatial-element/CarouselSpatialElement.svelte.d.ts +17 -0
  18. package/dist/spatial-element/ContentPage.svelte +68 -0
  19. package/dist/spatial-element/ContentPage.svelte.d.ts +9 -0
  20. package/dist/spatial-element/ListSection.svelte +30 -0
  21. package/dist/spatial-element/ListSection.svelte.d.ts +8 -0
  22. package/dist/spatial-element/Section.svelte +191 -0
  23. package/dist/spatial-element/Section.svelte.d.ts +14 -0
  24. package/dist/spatial-element/SpatialElementBreadcrumbs.svelte +69 -0
  25. package/dist/spatial-element/SpatialElementBreadcrumbs.svelte.d.ts +8 -0
  26. package/dist/spatial-element/SpatialElementCard.svelte +256 -0
  27. package/dist/spatial-element/SpatialElementCard.svelte.d.ts +9 -0
  28. package/dist/spatial-element/SpatialElementMediaRail.svelte +144 -0
  29. package/dist/spatial-element/SpatialElementMediaRail.svelte.d.ts +7 -0
  30. package/dist/spatial-element/SpatialElementPage.svelte +249 -0
  31. package/dist/spatial-element/SpatialElementPage.svelte.d.ts +9 -0
  32. package/dist/spatial-element/SpatialElementStickyHeader.svelte +351 -0
  33. package/dist/spatial-element/SpatialElementStickyHeader.svelte.d.ts +9 -0
  34. package/dist/spatial-element/SpatialElementSummaryPanel.svelte +423 -0
  35. package/dist/spatial-element/SpatialElementSummaryPanel.svelte.d.ts +7 -0
  36. package/dist/spatial-element/SpatialElementTabList.svelte +266 -0
  37. package/dist/spatial-element/SpatialElementTabList.svelte.d.ts +9 -0
  38. package/dist/spatial-element/SpatialElementTabs.svelte +123 -0
  39. package/dist/spatial-element/SpatialElementTabs.svelte.d.ts +8 -0
  40. package/dist/spatial-element/SpatialListPage.svelte +23 -0
  41. package/dist/spatial-element/SpatialListPage.svelte.d.ts +11 -0
  42. package/dist/spatial-element/brandContext.d.ts +5 -0
  43. package/dist/spatial-element/brandContext.js +14 -0
  44. package/dist/spatial-element/index.d.ts +11 -0
  45. package/dist/spatial-element/index.js +8 -0
  46. package/dist/stage/Panel.svelte +355 -0
  47. package/dist/stage/Panel.svelte.d.ts +24 -0
  48. package/dist/stage/Stage.svelte +747 -0
  49. package/dist/stage/Stage.svelte.d.ts +22 -0
  50. package/dist/stage/StageViewport.svelte +73 -0
  51. package/dist/stage/StageViewport.svelte.d.ts +13 -0
  52. package/package.json +55 -0
@@ -0,0 +1,350 @@
1
+ <script lang="ts">
2
+ import { getContext, onMount, tick, untrack } from 'svelte';
3
+ import Section from './Section.svelte';
4
+ import CarouselSpatialElement from './CarouselSpatialElement.svelte';
5
+ import { CATALOG_POSE_CHANGED } from '@spatial-elements/core/catalog/catalogPose';
6
+ import type { CarouselPresentation } from '@spatial-elements/core/catalog/catalogPose';
7
+ import type { SpatialListItem, SpatialElementSectionDefinition } from '@spatial-elements/core/spatial-element/types';
8
+ import { CATALOG_SECTIONS, type CatalogSections } from '@spatial-elements/core/catalog/CatalogSections';
9
+ import {
10
+ carouselKey,
11
+ resolveCarouselSelection,
12
+ stepCarouselSelection
13
+ } from '@spatial-elements/core/catalog/carouselSelection';
14
+
15
+ let {
16
+ section,
17
+ presentation,
18
+ list,
19
+ initialItemKey,
20
+ onselectionchange
21
+ }: {
22
+ section: SpatialElementSectionDefinition;
23
+ list: SpatialListItem[];
24
+ presentation?: CarouselPresentation;
25
+ initialItemKey?: string;
26
+ onselectionchange?: (selection: { itemKey: string; spatialElementId: string }) => void;
27
+ } = $props();
28
+ const sections = getContext<CatalogSections | undefined>(CATALOG_SECTIONS);
29
+
30
+ let requested = $state<string>();
31
+ let interactive = $state(false);
32
+ let phase = $state(0);
33
+ let windowPhase = $state(0);
34
+ $effect(() => {
35
+ if (Math.abs(phase - windowPhase) > 0.75) windowPhase = Math.round(phase);
36
+ });
37
+ let ring: HTMLDivElement;
38
+ let frame = 0;
39
+ let visible = true;
40
+ let suppressClick = false;
41
+ let clickTimer: ReturnType<typeof setTimeout>;
42
+ let announced = $state<string>();
43
+ let selectionDirty = false;
44
+ let previousKeys: string | undefined;
45
+ function rememberSelection() {
46
+ if (!selectionDirty) return;
47
+ selectionDirty = false;
48
+ sections?.remember();
49
+ }
50
+ let drag: { id: number; x: number; y: number; phase: number; active: boolean } | undefined;
51
+ function invalidate() {
52
+ window.dispatchEvent(new Event(CATALOG_POSE_CHANGED));
53
+ }
54
+ function releaseDrag() {
55
+ const current = drag;
56
+ drag = undefined;
57
+ if (current && ring?.hasPointerCapture(current.id)) ring.releasePointerCapture(current.id);
58
+ }
59
+ function settle() {
60
+ cancelAnimationFrame(frame);
61
+ frame = 0;
62
+ releaseDrag();
63
+ phase = Math.max(0, keys.indexOf(selected ?? ''));
64
+ announced = selected;
65
+ void tick().then(invalidate);
66
+ rememberSelection();
67
+ }
68
+ function animate(target: number) {
69
+ cancelAnimationFrame(frame);
70
+ const start = phase;
71
+ const started = performance.now();
72
+ if (
73
+ Math.abs(target - start) > 1.5 ||
74
+ !visible ||
75
+ document.hidden ||
76
+ matchMedia('(prefers-reduced-motion: reduce)').matches
77
+ ) {
78
+ phase = target;
79
+ announced = selected;
80
+ invalidate();
81
+ rememberSelection();
82
+ return;
83
+ }
84
+ const advance = (now: number) => {
85
+ if (ring?.closest('[data-catalog-transition]')) {
86
+ frame = 0;
87
+ return;
88
+ }
89
+ const t = Math.min(1, (now - started) / 320);
90
+ phase = start + (target - start) * (1 - (1 - t) ** 2);
91
+ void tick().then(invalidate);
92
+ if (t < 1) frame = requestAnimationFrame(advance);
93
+ else {
94
+ frame = 0;
95
+ announced = selected;
96
+ rememberSelection();
97
+ }
98
+ };
99
+ frame = requestAnimationFrame(advance);
100
+ }
101
+ function pointerdown(event: PointerEvent) {
102
+ if (
103
+ list.length < 2 ||
104
+ event.button !== 0 ||
105
+ !(event.target as Element).closest('.spatial-element-visual')
106
+ )
107
+ return;
108
+ drag = { id: event.pointerId, x: event.clientX, y: event.clientY, phase, active: false };
109
+ }
110
+ function pointermove(event: PointerEvent) {
111
+ if (!drag || drag.id !== event.pointerId) return;
112
+ const x = event.clientX - drag.x,
113
+ y = event.clientY - drag.y;
114
+ if (!drag.active) {
115
+ if (Math.abs(y) > 10 && Math.abs(y) > Math.abs(x)) {
116
+ drag = undefined;
117
+ return;
118
+ }
119
+ if (Math.abs(x) < 10 || Math.abs(x) < Math.abs(y) * 1.2) return;
120
+ drag.active = true;
121
+ suppressClick = true;
122
+ cancelAnimationFrame(frame);
123
+ ring.setPointerCapture(event.pointerId);
124
+ }
125
+ phase = Math.max(0, Math.min(keys.length - 1, drag.phase - x / 220));
126
+ invalidate();
127
+ }
128
+ function pointerup(event: PointerEvent) {
129
+ if (!drag || drag.id !== event.pointerId) return;
130
+ const active = drag.active;
131
+ releaseDrag();
132
+ if (active) select(keys[Math.round(phase)]);
133
+ clearTimeout(clickTimer);
134
+ clickTimer = setTimeout(() => (suppressClick = false), 0);
135
+ }
136
+ function pointercancel() {
137
+ releaseDrag();
138
+ suppressClick = false;
139
+ animate(Math.max(0, keys.indexOf(selected ?? '')));
140
+ }
141
+ const keys = $derived(list.map(carouselKey));
142
+ const selected = $derived(resolveCarouselSelection(keys, requested ?? initialItemKey));
143
+ $effect(() => {
144
+ // Data reordering/removal changes indices, never the stable selection identity.
145
+ const signature = JSON.stringify(keys);
146
+ if (interactive)
147
+ untrack(() => {
148
+ if (previousKeys !== undefined && previousKeys !== signature) selectionDirty = true;
149
+ previousKeys = signature;
150
+ settle();
151
+ });
152
+ });
153
+ function select(key: string | undefined) {
154
+ selectionDirty = true;
155
+ requested = key;
156
+ // Persist in the current entry before a possible popstate changes the history index.
157
+ rememberSelection();
158
+ const target = Math.max(0, keys.indexOf(key ?? ''));
159
+ animate(target);
160
+ const spatialElement = list.find((item) => carouselKey(item) === selected);
161
+ if (spatialElement && selected) onselectionchange?.({ itemKey: selected, spatialElementId: spatialElement.id });
162
+ }
163
+ function keydown(event: KeyboardEvent) {
164
+ const step = event.key === 'ArrowRight' ? 1 : event.key === 'ArrowLeft' ? -1 : 0;
165
+ if (step) select(stepCarouselSelection(keys, selected, step));
166
+ else if (event.key === 'Home') select(keys[0]);
167
+ else if (event.key === 'End') select(keys.at(-1));
168
+ else return;
169
+ event.preventDefault();
170
+ }
171
+ onMount(() => {
172
+ interactive = true;
173
+ const release = sections?.register(section.id, {
174
+ read: () => selected,
175
+ restore: (key) => {
176
+ selectionDirty = false;
177
+ requested = resolveCarouselSelection(keys, key ?? initialItemKey);
178
+ cancelAnimationFrame(frame);
179
+ phase = Math.max(0, keys.indexOf(requested ?? ''));
180
+ announced = requested;
181
+ }
182
+ });
183
+ const observer = new IntersectionObserver(([entry]) => {
184
+ visible = entry.isIntersecting;
185
+ if (!visible) {
186
+ settle();
187
+ }
188
+ });
189
+ observer.observe(ring);
190
+ let sizingFrame = 0;
191
+ const measure = () => {
192
+ sizingFrame = 0;
193
+ const heights = [...ring.querySelectorAll<HTMLElement>('.summary')].map(
194
+ (panel) => panel.offsetHeight
195
+ );
196
+ const contentHeight = Math.max(0, ...heights);
197
+ const height =
198
+ ring.clientWidth < 700 ? 408 + contentHeight : Math.max(560, contentHeight + 64);
199
+ const value = height + 'px';
200
+ if (ring.style.getPropertyValue('--carousel-height') !== value)
201
+ ring.style.setProperty('--carousel-height', value);
202
+ };
203
+ const scheduleSize = () => {
204
+ if (!sizingFrame) sizingFrame = requestAnimationFrame(measure);
205
+ };
206
+ const sizing = new ResizeObserver(scheduleSize);
207
+ const contentChanges = new MutationObserver(() => {
208
+ sizing.disconnect();
209
+ sizing.observe(ring);
210
+ for (const panel of ring.querySelectorAll('.summary')) sizing.observe(panel);
211
+ scheduleSize();
212
+ });
213
+ contentChanges.observe(ring, { childList: true, subtree: true });
214
+ sizing.observe(ring);
215
+ for (const panel of ring.querySelectorAll('.summary')) sizing.observe(panel);
216
+ const stop = () => pointercancel();
217
+ window.addEventListener('resize', stop);
218
+ const hidden = () => {
219
+ if (document.hidden) settle();
220
+ };
221
+ document.addEventListener('visibilitychange', hidden);
222
+ const escape = (event: KeyboardEvent) => {
223
+ if (event.key === 'Escape') stop();
224
+ };
225
+ window.addEventListener('keydown', escape);
226
+ return () => {
227
+ cancelAnimationFrame(frame);
228
+ clearTimeout(clickTimer);
229
+ releaseDrag();
230
+ observer.disconnect();
231
+ sizing.disconnect();
232
+ contentChanges.disconnect();
233
+ cancelAnimationFrame(sizingFrame);
234
+ release?.();
235
+ window.removeEventListener('resize', stop);
236
+ window.removeEventListener('keydown', escape);
237
+ document.removeEventListener('visibilitychange', hidden);
238
+ };
239
+ });
240
+ </script>
241
+
242
+ <Section {section} presentation="list">
243
+ <div
244
+ class="carousel"
245
+ class:interactive
246
+ class:populated={list.length > 0}
247
+ data-carousel-section={section.id}
248
+ >
249
+ <div
250
+ class="ring"
251
+ bind:this={ring}
252
+ onpointerdown={pointerdown}
253
+ onpointermove={pointermove}
254
+ onpointerup={pointerup}
255
+ onpointercancel={pointercancel}
256
+ onlostpointercapture={() => {
257
+ if (drag) pointercancel();
258
+ }}
259
+ role="group"
260
+ aria-label={section.title}
261
+ >
262
+ {#each list as spatialElement, index (carouselKey(spatialElement))}
263
+ <CarouselSpatialElement
264
+ {spatialElement}
265
+ sectionId={section.id}
266
+ {presentation}
267
+ {index}
268
+ count={list.length}
269
+ {phase}
270
+ {windowPhase}
271
+ selected={selected === carouselKey(spatialElement)}
272
+ {interactive}
273
+ onselect={() => {
274
+ if (!suppressClick) select(carouselKey(spatialElement));
275
+ }}
276
+ />
277
+ {/each}
278
+ </div>
279
+ {#if interactive && list.length > 1}
280
+ <div class="controls" role="group" aria-label={`${section.title}: element selection`}>
281
+ {#each list as spatialElement (carouselKey(spatialElement))}
282
+ <button
283
+ onkeydown={keydown}
284
+ aria-label={spatialElement.eyebrow}
285
+ aria-pressed={selected === carouselKey(spatialElement)}
286
+ onclick={() => select(carouselKey(spatialElement))}><span aria-hidden="true"></span></button
287
+ >
288
+ {/each}
289
+ </div>
290
+ {/if}
291
+ {#if interactive}<p class="sr-only" role="status">
292
+ {list.find((item) => carouselKey(item) === announced)?.title ?? ''}
293
+ </p>{/if}
294
+ </div>
295
+ </Section>
296
+
297
+ <style>
298
+ .carousel {
299
+ pointer-events: auto;
300
+ margin-top: 24px;
301
+ }
302
+ .ring {
303
+ container-type: inline-size;
304
+ position: relative;
305
+ touch-action: pan-y;
306
+ }
307
+ .interactive.populated .ring {
308
+ min-height: var(--carousel-height, 560px);
309
+ }
310
+ .controls {
311
+ display: flex;
312
+ justify-content: center;
313
+ flex-wrap: wrap;
314
+ margin-top: 16px;
315
+ }
316
+ .controls button {
317
+ display: grid;
318
+ place-items: center;
319
+ width: 44px;
320
+ height: 44px;
321
+ padding: 0;
322
+ border: 0;
323
+ background: transparent;
324
+ border-radius: 50%;
325
+ cursor: pointer;
326
+ }
327
+ .controls span {
328
+ width: 10px;
329
+ height: 10px;
330
+ border: 1px solid var(--spatial-element-accent);
331
+ border-radius: 50%;
332
+ }
333
+ .controls button[aria-pressed='true'] span {
334
+ background: var(--spatial-element-accent);
335
+ }
336
+ .controls button:focus-visible {
337
+ outline: 2px solid var(--spatial-element-accent);
338
+ outline-offset: -4px;
339
+ }
340
+ .sr-only {
341
+ position: absolute;
342
+ width: 1px;
343
+ height: 1px;
344
+ padding: 0;
345
+ margin: -1px;
346
+ overflow: hidden;
347
+ clip-path: inset(50%);
348
+ white-space: nowrap;
349
+ }
350
+ </style>
@@ -0,0 +1,15 @@
1
+ import type { CarouselPresentation } from '@spatial-elements/core/catalog/catalogPose';
2
+ import type { SpatialListItem, SpatialElementSectionDefinition } from '@spatial-elements/core/spatial-element/types';
3
+ type $$ComponentProps = {
4
+ section: SpatialElementSectionDefinition;
5
+ list: SpatialListItem[];
6
+ presentation?: CarouselPresentation;
7
+ initialItemKey?: string;
8
+ onselectionchange?: (selection: {
9
+ itemKey: string;
10
+ spatialElementId: string;
11
+ }) => void;
12
+ };
13
+ declare const CarouselSection: import("svelte").Component<$$ComponentProps, {}, "">;
14
+ type CarouselSection = ReturnType<typeof CarouselSection>;
15
+ export default CarouselSection;