@cloudlet/toast 1.2.0 → 1.4.0-rc.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 (60) hide show
  1. package/CHANGELOG.md +139 -0
  2. package/README.md +7 -3
  3. package/dist/agent.d.ts +334 -0
  4. package/dist/agent.d.ts.map +1 -0
  5. package/dist/agent.js +494 -0
  6. package/dist/agent.js.map +1 -0
  7. package/dist/contracts/events.d.ts +14 -0
  8. package/dist/contracts/events.d.ts.map +1 -1
  9. package/dist/contracts/internal.d.ts +16 -2
  10. package/dist/contracts/internal.d.ts.map +1 -1
  11. package/dist/contracts/notifications.d.ts +16 -0
  12. package/dist/contracts/notifications.d.ts.map +1 -1
  13. package/dist/contracts/snapshots.d.ts +8 -0
  14. package/dist/contracts/snapshots.d.ts.map +1 -1
  15. package/dist/core/actions.d.ts +69 -0
  16. package/dist/core/actions.d.ts.map +1 -0
  17. package/dist/core/actions.js +160 -0
  18. package/dist/core/actions.js.map +1 -0
  19. package/dist/core/agent-port.d.ts +116 -0
  20. package/dist/core/agent-port.d.ts.map +1 -0
  21. package/dist/core/agent-port.js +138 -0
  22. package/dist/core/agent-port.js.map +1 -0
  23. package/dist/core/bridge.d.ts.map +1 -1
  24. package/dist/core/bridge.js +120 -231
  25. package/dist/core/bridge.js.map +1 -1
  26. package/dist/core/controller.d.ts +44 -9
  27. package/dist/core/controller.d.ts.map +1 -1
  28. package/dist/core/controller.js +367 -292
  29. package/dist/core/controller.js.map +1 -1
  30. package/dist/core/validation.d.ts.map +1 -1
  31. package/dist/core/validation.js +103 -94
  32. package/dist/core/validation.js.map +1 -1
  33. package/dist/react/card.d.ts.map +1 -1
  34. package/dist/react/card.js +3 -3
  35. package/dist/react/card.js.map +1 -1
  36. package/dist/react/gestures.d.ts +6 -0
  37. package/dist/react/gestures.d.ts.map +1 -1
  38. package/dist/react/gestures.js +28 -15
  39. package/dist/react/gestures.js.map +1 -1
  40. package/dist/react/host.d.ts.map +1 -1
  41. package/dist/react/host.js +290 -270
  42. package/dist/react/host.js.map +1 -1
  43. package/dist/react.d.ts +9 -1
  44. package/dist/react.d.ts.map +1 -1
  45. package/dist/styles.css +1 -1
  46. package/package.json +3 -2
  47. package/src/agent.ts +879 -0
  48. package/src/contracts/events.ts +15 -0
  49. package/src/contracts/internal.ts +14 -1
  50. package/src/contracts/notifications.ts +16 -0
  51. package/src/contracts/snapshots.ts +8 -0
  52. package/src/core/actions.ts +149 -0
  53. package/src/core/agent-port.ts +237 -0
  54. package/src/core/bridge.ts +19 -105
  55. package/src/core/controller.ts +183 -56
  56. package/src/core/validation.ts +6 -3
  57. package/src/react/card.tsx +4 -3
  58. package/src/react/gestures.ts +45 -8
  59. package/src/react/host.tsx +48 -12
  60. package/src/react.ts +9 -1
@@ -51,6 +51,12 @@ export interface SwipeCandidate {
51
51
  interface SwipeOptions {
52
52
  /** Null ignores the input; the candidate is released exactly once. */
53
53
  readonly begin: (target: Element, input: 'pointer' | 'wheel') => SwipeCandidate | null;
54
+ /**
55
+ * The open Island under a wheel stream: until the stream's axis is decided, and for every
56
+ * mostly horizontal event after that, the browser gets no wheel event from it (no history swipe),
57
+ * even where no swipe can start (an end of the slider, reduced motion, outside the slides).
58
+ */
59
+ readonly claimsWheel?: (target: Element) => boolean;
54
60
  readonly now: () => number;
55
61
  }
56
62
 
@@ -72,7 +78,13 @@ interface PointerSwipe {
72
78
  }
73
79
 
74
80
  interface WheelSwipe {
75
- readonly candidate: SwipeCandidate | null;
81
+ /** Started over the open Island (`claimsWheel`). */
82
+ readonly claimed: boolean;
83
+ /** Decided after `MOTION.swipe.intent` px of summed travel, like a pointer. */
84
+ axis: SwipeAxis | null;
85
+ travelX: number;
86
+ travelY: number;
87
+ candidate: SwipeCandidate | null;
76
88
  session: SwipeSession | null;
77
89
  /** Committed early: the rest of the stream (macOS momentum) is swallowed until it goes idle. */
78
90
  spent: boolean;
@@ -235,18 +247,43 @@ export function createSwipe(root: HTMLElement, options: SwipeOptions) {
235
247
  }
236
248
  }
237
249
  if (!wheel) {
238
- const target = event.target;
239
- const candidate = Math.abs(dx) > Math.abs(dy) && target instanceof view.Element ? options.begin(target, 'wheel') : null;
240
- const session = candidate?.x ? candidate.start('x') : null;
241
- if (candidate && !session) candidate.release();
242
- // A stream that started vertical stays native until it goes idle.
243
- wheel = { candidate: session ? candidate : null, session, spent: false, spentAt: 0, lastDx: dx, offset: 0, velocity: 0, time, timer: 0 };
250
+ const claimed = event.target instanceof view.Element && (options.claimsWheel?.(event.target) ?? false);
251
+ wheel = {
252
+ claimed, axis: null, travelX: 0, travelY: 0, candidate: null, session: null,
253
+ spent: false, spentAt: 0, lastDx: dx, offset: 0, velocity: 0, time, timer: 0,
254
+ };
244
255
  }
245
256
  const stream = wheel;
246
257
  view.clearTimeout(stream.timer);
247
258
  stream.timer = view.setTimeout(() => endWheel(true), swipe.wheelIdle);
248
259
  stream.lastDx = dx;
249
- if (!stream.session) return;
260
+ if (!stream.axis) {
261
+ // The first event alone does not decide: a trackpad swipe often lands diagonally (1, 2 px).
262
+ stream.travelX += dx;
263
+ stream.travelY += dy;
264
+ const horizontal = Math.abs(stream.travelX);
265
+ const vertical = Math.abs(stream.travelY);
266
+ if (Math.max(horizontal, vertical) >= swipe.intent) {
267
+ stream.axis = horizontal > vertical ? 'x' : 'y';
268
+ const target = event.target;
269
+ const candidate = stream.axis === 'x' && target instanceof view.Element ? options.begin(target, 'wheel') : null;
270
+ const session = candidate?.x ? candidate.start('x') : null;
271
+ if (candidate && !session) candidate.release();
272
+ if (session) {
273
+ stream.candidate = candidate;
274
+ stream.session = session;
275
+ // The travel of the intent window counts, as a pointer's does.
276
+ stream.offset = dx - stream.travelX;
277
+ }
278
+ }
279
+ }
280
+ if (!stream.session) {
281
+ // A stream that started vertical stays native until it goes idle; over the Island its
282
+ // sideways events are still held back from the browser's back/forward swipe.
283
+ if (stream.claimed && (!stream.axis || Math.abs(dx) > Math.abs(dy))) event.preventDefault();
284
+ stream.time = time;
285
+ return;
286
+ }
250
287
  event.preventDefault();
251
288
  // Fingers moving left scroll right (positive deltaX): the content follows the fingers.
252
289
  const elapsed = time - stream.time;
@@ -141,6 +141,8 @@ function createHostRuntime(owner: Host) {
141
141
  motion?.setConstants(constants);
142
142
  };
143
143
  let reduced = false;
144
+ /** Re-resolves reduced motion (system, `reducedMotion`, an ancestor's attribute); set while mounted. */
145
+ let syncReduced: (() => void) | null = null;
144
146
  let hovering = false;
145
147
  let observer: ResizeObserver | null = null;
146
148
  /** Live layout box of the shell (the target shape); the SVG and clip follow it. */
@@ -252,6 +254,8 @@ function createHostRuntime(owner: Host) {
252
254
  }
253
255
  const bottomEdge = () => rootRef.current?.dataset.edge === 'bottom';
254
256
  const edgeSign = (): -1 | 1 => bottomEdge() ? 1 : -1;
257
+ /** Where the layers hang in the stack's layout box: 0 at its left, ½ in the centre, 1 at its right (CSS `--cloudlet-anchor`). */
258
+ const anchor = () => { const align = rootRef.current?.dataset.align; return align === 'left' ? 0 : align === 'right' ? 1 : 0.5; };
255
259
 
256
260
  /** The only DOM writer of animated values: scoped CSSOM rules and SVG attributes. */
257
261
  function write(frame: ShellFrame) {
@@ -263,6 +267,10 @@ function createHostRuntime(owner: Host) {
263
267
  // At the bottom edge the silhouette grows upwards: it is anchored to the layout bottom.
264
268
  const bottom = bottomEdge();
265
269
  const top = bottom ? layout.height - height : 0;
270
+ // A side position keeps the silhouette against its edge of the layout box: the morph grows away from it.
271
+ const along = anchor();
272
+ const hang = `translateX(${-along * 100}%)`;
273
+ const left = (layout.width - width) * along;
266
274
  if (r.shell) {
267
275
  r.shell.opacity = String(clamp01(frame.opacity));
268
276
  r.shell.transform = still ? 'none' : `translate(${px(frame.dragX)}, ${px(frame.y + frame.dragY)}) scale(${frame.scale})`;
@@ -279,13 +287,13 @@ function createHostRuntime(owner: Host) {
279
287
  // Layout keeps the box at the top and a transform carries it to the bottom edge: a growing
280
288
  // silhouette then moves no layout start point, so the morph is not counted as layout shift (CLS).
281
289
  r.bg.top = '0px'; r.bg.bottom = 'auto';
282
- r.bg.transform = `translateX(-50%) translateY(${px(top)})`;
290
+ r.bg.transform = `${hang} translateY(${px(top)})`;
283
291
  }
284
- if (r.clip) r.clip.clipPath = `path("${createRoundedRectPath(width, height, radius, (layout.width - width) / 2, top)}")`;
292
+ if (r.clip) r.clip.clipPath = `path("${createRoundedRectPath(width, height, radius, left, top)}")`;
285
293
  if (r.badge) {
286
294
  const scale = still ? 1 : constants.badge.scale + (1 - constants.badge.scale) * frame.badge;
287
- // Outer corner: top right, mirrored in RTL; the bottom corner at the bottom edge.
288
- const x = (width / 2 - 10) * (rtl ? -1 : 1);
295
+ // Outer corner: top right, mirrored in RTL; the bottom corner at the bottom edge. The badge hangs from the layout centre.
296
+ const x = (rtl ? left + 10 : left + width - 10) - layout.width / 2;
289
297
  r.badge.opacity = String(clamp01(frame.badge));
290
298
  r.badge.transform = `translate(calc(${px(x)} - 50%), ${bottom ? '6px' : '-6px'}) scale(${scale})`;
291
299
  }
@@ -298,7 +306,7 @@ function createHostRuntime(owner: Host) {
298
306
  rule.width = px(width);
299
307
  rule.height = px(Math.min(height, tokens.pillHeight));
300
308
  rule.opacity = String(clamp01(frame[`tail${index}Opacity`]));
301
- rule.transform = `translateX(-50%) translateY(${px(inward * slot * (tokens.tailOffset + constants.hoverLift * frame.lift))}) scale(${Math.pow(tokens.tailScale, slot)})`;
309
+ rule.transform = `${hang} translateY(${px(inward * slot * (tokens.tailOffset + constants.hoverLift * frame.lift))}) scale(${Math.pow(tokens.tailScale, slot)})`;
302
310
  }
303
311
  if (r.slideOut) {
304
312
  r.slideOut.transform = still ? 'none' : `translateX(${px(frame.slide - frame.slideDirection * frame.slideWidth)})`;
@@ -332,7 +340,7 @@ function createHostRuntime(owner: Host) {
332
340
  if (r.bodyOut) r.bodyOut.opacity = String(clamp01(frame.bodyOut));
333
341
  if (r.exit) {
334
342
  r.exit.opacity = String(clamp01(frame.exitOpacity));
335
- r.exit.transform = still ? 'translateX(-50%)' : `translateX(-50%) translate(${px(frame.exitX)}, ${px(frame.exitY)}) scale(${frame.exitScale})`;
343
+ r.exit.transform = still ? hang : `${hang} translate(${px(frame.exitX)}, ${px(frame.exitY)}) scale(${frame.exitScale})`;
336
344
  }
337
345
  if (progressActive && progressFill?.isConnected) progressFill.setAttribute('stroke-dasharray', `${Math.round(frame.progress * 10000) / 100} 100`);
338
346
  }
@@ -664,6 +672,11 @@ function createHostRuntime(owner: Host) {
664
672
  },
665
673
  };
666
674
  }
675
+ /** A wheel stream over the open Island never reaches the browser's back/forward swipe. */
676
+ function claimsWheel(target: Element): boolean {
677
+ const shell = shellRef.current;
678
+ return !!shell && shellView(owner.props.snapshot).open && shell.contains(target);
679
+ }
667
680
  function componentDidMount() {
668
681
  const root = rootRef.current;
669
682
  const shell = shellRef.current;
@@ -679,9 +692,14 @@ function createHostRuntime(owner: Host) {
679
692
  }
680
693
  for (const container of [exitRef.current, slideOutRef.current, chromeOutRef.current]) container?.setAttribute('inert', '');
681
694
  announcer.mount(root);
682
- swipe = createSwipe(root, { begin: beginSwipe, now: () => view.performance.now() });
695
+ swipe = createSwipe(root, { begin: beginSwipe, claimsWheel, now: () => view.performance.now() });
683
696
  const media = view.matchMedia?.('(prefers-reduced-motion: reduce)');
684
- reduced = media?.matches ?? false;
697
+ // The app's setting can only add calm: the system, the prop or `data-reduced-motion` on any
698
+ // ancestor (by presence, as the registry components read it). The search starts at the parent,
699
+ // so the host's own output attribute never feeds back.
700
+ let fromAncestor = !!root.parentElement?.closest('[data-reduced-motion]');
701
+ const resolveReduced = () => (media?.matches ?? false) || owner.props.options.reducedMotion === true || fromAncestor;
702
+ reduced = resolveReduced();
685
703
  root.dataset.reducedMotion = String(reduced);
686
704
  motion = createShellMotion({
687
705
  now: () => view.performance.now(), requestFrame: callback => view.requestAnimationFrame(callback),
@@ -689,11 +707,22 @@ function createHostRuntime(owner: Host) {
689
707
  timeScale: owner.props.options.motion?.timeScale, constants,
690
708
  });
691
709
  const preference = () => {
692
- reduced = media?.matches ?? false;
710
+ const next = resolveReduced();
711
+ if (next === reduced) return;
712
+ reduced = next;
693
713
  root.dataset.reducedMotion = String(reduced);
694
714
  if (reduced) swipe?.cancel();
695
715
  motion?.setReducedMotion(reduced);
696
716
  };
717
+ syncReduced = preference;
718
+ // Only the ancestor chain, only this attribute: the host's own attribute is not observed.
719
+ const ancestors = view.MutationObserver ? new view.MutationObserver(() => {
720
+ fromAncestor = !!root.parentElement?.closest('[data-reduced-motion]');
721
+ preference();
722
+ }) : null;
723
+ for (let node = root.parentElement; node && ancestors; node = node.parentElement) {
724
+ ancestors.observe(node, { attributes: true, attributeFilter: ['data-reduced-motion'] });
725
+ }
697
726
  // `auto` follows the same breakpoint as the stylesheet: re-render when it flips.
698
727
  const wide = view.matchMedia?.('(min-width: 48rem)');
699
728
  // `data-resolved-theme` follows the system scheme while the theme is `system`.
@@ -726,7 +755,11 @@ function createHostRuntime(owner: Host) {
726
755
  if (scheme) listeners.push([scheme, 'change', followScheme]);
727
756
  if (view.visualViewport) listeners.push([view.visualViewport, 'resize', relayout], [view.visualViewport, 'scroll', measureViewport]);
728
757
  for (const [target, type, listener, capture] of listeners) target.addEventListener(type, listener, capture);
729
- cleanup = () => { for (const [target, type, listener, capture] of listeners) target.removeEventListener(type, listener, capture); };
758
+ cleanup = () => {
759
+ for (const [target, type, listener, capture] of listeners) target.removeEventListener(type, listener, capture);
760
+ ancestors?.disconnect();
761
+ syncReduced = null;
762
+ };
730
763
  // Measure HTML only: the clip's layout box never depends on animated values.
731
764
  observer = view.ResizeObserver ? new view.ResizeObserver(() => { measure(); acknowledgePresentation(); }) : null;
732
765
  if (clipRef.current) observer?.observe(clipRef.current);
@@ -781,6 +814,7 @@ function createHostRuntime(owner: Host) {
781
814
  if (armed && !clearable(owner.props)) disarm(false);
782
815
  motion?.setTimeScale(owner.props.options.motion?.timeScale);
783
816
  syncConstants();
817
+ syncReduced?.();
784
818
  // A swipe never continues on another record or state (arrival, programmatic dismiss, Escape).
785
819
  const next = shellView(owner.props.snapshot);
786
820
  if (gestureTarget && (gestureTarget.handle !== next.presented?.handle || gestureTarget.open !== next.open)) swipe?.cancel();
@@ -872,7 +906,9 @@ function createHostRuntime(owner: Host) {
872
906
  if (current.badge) badgeCount = current.count;
873
907
  const preview = open && peek ? snapshot.notifications.find(item => item.handle === peek!.handle) : undefined;
874
908
  const position = options.position ?? 'auto';
875
- const bottom = position === 'bottom-center' || (position === 'auto' && view?.matchMedia?.('(min-width: 48rem)').matches === false);
909
+ const named = String(position);
910
+ const bottom = named.startsWith('bottom-') || (position === 'auto' && view?.matchMedia?.('(min-width: 48rem)').matches === false);
911
+ const align = named.endsWith('-left') ? 'left' : named.endsWith('-right') ? 'right' : 'center';
876
912
  const tails = Math.min(2, Math.max(current.depth, heldTails));
877
913
  const slot = presented ? slotRenderer(bridge, lease, presented.handle, rejectSlotFocus) : null;
878
914
  const page = format(labels.page, defaultLabels.page, index + 1, total);
@@ -883,7 +919,7 @@ function createHostRuntime(owner: Host) {
883
919
  <div ref={rootRef} className={part('host', 'cloudlet-host')} role="region" aria-label={labels.region} tabIndex={-1}
884
920
  data-cloudlet-focus-anchor="" data-cloudlet-instance={instance} data-theme={theme}
885
921
  data-resolved-theme={theme === 'system' ? view?.matchMedia?.('(prefers-color-scheme: dark)').matches ? 'dark' : 'light' : theme}
886
- data-position={position} data-edge={bottom ? 'bottom' : 'top'} data-state={open ? 'island' : 'stack'}
922
+ data-position={position} data-edge={bottom ? 'bottom' : 'top'} data-align={align} data-state={open ? 'island' : 'stack'}
887
923
  data-island-reason={snapshot.island.reason ?? undefined} data-overflow={snapshot.overflow.pending} dir={dir}
888
924
  onPointerDownCapture={event => { activation = (event.target as Element).closest('button'); }}
889
925
  onPointerCancelCapture={() => { activation = null; }}
package/src/react.ts CHANGED
@@ -167,7 +167,8 @@ export type CloudletPart = 'host' | 'stack' | 'pill' | 'tail' | 'badge' | 'islan
167
167
  | 'close' | 'body' | 'description' | 'actions' | 'action' | 'indicator' | 'clear' | 'overflow' | 'form' | 'field' | 'field-error';
168
168
 
169
169
  export interface ToasterProps {
170
- readonly position?: 'auto' | 'top-center' | 'bottom-center';
170
+ /** Physical: `-left` and `-right` stay on that side in RTL too. Default `auto`. */
171
+ readonly position?: 'auto' | 'top-left' | 'top-center' | 'top-right' | 'bottom-left' | 'bottom-center' | 'bottom-right';
171
172
  readonly theme?: 'system' | 'light' | 'dark';
172
173
  readonly dir?: 'ltr' | 'rtl';
173
174
  /** Read after mount; a connected HTML Element in the mounting root's document. Fixed until remount. */
@@ -184,6 +185,13 @@ export interface ToasterProps {
184
185
  readonly clearAll?: boolean;
185
186
  /** Read on every render. */
186
187
  readonly motion?: ToasterMotion;
188
+ /**
189
+ * The app's own reduced-motion setting. `true` removes motion whatever the system says; `false`
190
+ * and omitting it follow the system and `data-reduced-motion` on an ancestor (that attribute
191
+ * counts by presence, with any value). An app cannot turn motion on against the system's
192
+ * `prefers-reduced-motion: reduce`. Read on every render.
193
+ */
194
+ readonly reducedMotion?: boolean;
187
195
  /** Extra classes per part, added to the own `cloudlet-*` classes (never replacing them). Read on every render. */
188
196
  readonly classNames?: Partial<Record<CloudletPart, string>>;
189
197
  }