@cloudlet/toast 1.0.0-rc.3 → 1.0.0

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/CHANGELOG.md +42 -7
  2. package/README.md +5 -4
  3. package/dist/contracts/events.d.ts +1 -1
  4. package/dist/contracts/events.d.ts.map +1 -1
  5. package/dist/core/bridge.d.ts +5 -0
  6. package/dist/core/bridge.d.ts.map +1 -0
  7. package/dist/core/bridge.js +277 -0
  8. package/dist/core/bridge.js.map +1 -0
  9. package/dist/core/controller.d.ts +113 -3
  10. package/dist/core/controller.d.ts.map +1 -1
  11. package/dist/core/controller.js +416 -583
  12. package/dist/core/controller.js.map +1 -1
  13. package/dist/react/announcer.d.ts.map +1 -1
  14. package/dist/react/announcer.js +29 -23
  15. package/dist/react/announcer.js.map +1 -1
  16. package/dist/react/bindings.d.ts +9 -0
  17. package/dist/react/bindings.d.ts.map +1 -1
  18. package/dist/react/bindings.js +32 -69
  19. package/dist/react/bindings.js.map +1 -1
  20. package/dist/react/card.d.ts +1 -1
  21. package/dist/react/card.js.map +1 -1
  22. package/dist/react/generators.d.ts +2 -2
  23. package/dist/react/generators.js.map +1 -1
  24. package/dist/react/gestures.js.map +1 -1
  25. package/dist/react/host.d.ts.map +1 -1
  26. package/dist/react/host.js +1 -1
  27. package/dist/react/host.js.map +1 -1
  28. package/dist/react/motion.d.ts +3 -3
  29. package/dist/react/motion.js.map +1 -1
  30. package/dist/react/plan.d.ts +1 -1
  31. package/dist/react/plan.d.ts.map +1 -1
  32. package/dist/react/plan.js.map +1 -1
  33. package/dist/react/renderer.d.ts +7 -0
  34. package/dist/react/renderer.d.ts.map +1 -0
  35. package/dist/react/renderer.js +62 -0
  36. package/dist/react/renderer.js.map +1 -0
  37. package/dist/react.d.ts +4 -4
  38. package/dist/react.d.ts.map +1 -1
  39. package/package.json +4 -2
  40. package/src/contracts/events.ts +2 -1
  41. package/src/core/bridge.ts +280 -0
  42. package/src/core/controller.ts +83 -268
  43. package/src/react/announcer.ts +9 -0
  44. package/src/react/bindings.tsx +28 -72
  45. package/src/react/card.tsx +3 -3
  46. package/src/react/generators.ts +2 -2
  47. package/src/react/gestures.ts +1 -1
  48. package/src/react/host.tsx +10 -4
  49. package/src/react/motion.ts +4 -4
  50. package/src/react/plan.ts +2 -2
  51. package/src/react/renderer.tsx +77 -0
  52. package/src/react.ts +4 -4
@@ -269,7 +269,13 @@ function createHostRuntime(owner: Host) {
269
269
  pathRef.current?.setAttribute('d', createRoundedRectPath(width, height, radius));
270
270
  }
271
271
  // The shadow token is a box-shadow: the SVG box follows the path's corners.
272
- if (r.bg) r.bg.borderRadius = px(radius);
272
+ if (r.bg) {
273
+ r.bg.borderRadius = px(radius);
274
+ // Layout keeps the box at the top and a transform carries it to the bottom edge: a growing
275
+ // silhouette then moves no layout start point, so the morph is not counted as layout shift (CLS).
276
+ r.bg.top = '0px'; r.bg.bottom = 'auto';
277
+ r.bg.transform = `translateX(-50%) translateY(${px(top)})`;
278
+ }
273
279
  if (r.clip) r.clip.clipPath = `path("${createRoundedRectPath(width, height, radius, (layout.width - width) / 2, top)}")`;
274
280
  if (r.badge) {
275
281
  const scale = still ? 1 : constants.badge.scale + (1 - constants.badge.scale) * frame.badge;
@@ -441,7 +447,7 @@ function createHostRuntime(owner: Host) {
441
447
  if (plan.header && head) clones.header = inertClone(head);
442
448
  const body = bodyElement();
443
449
  // Rapid updates (progress text every 150 ms) must not restart the crossfade: within
444
- // `bodyQuiet` of the previous update the new text lands in place (task 17, owner).
450
+ // `bodyQuiet` of the previous update the new text lands in place.
445
451
  const now = motion?.now() ?? 0;
446
452
  const rapid = plan.body && now - lastBodyUpdate < (motion?.constants.bodyQuiet ?? 0);
447
453
  if (plan.body) lastBodyUpdate = now;
@@ -493,7 +499,7 @@ function createHostRuntime(owner: Host) {
493
499
  else if (flung) motion.release('dragX', 0);
494
500
  if (plan.presence === 'exit') motion.hide();
495
501
  }
496
- // An arrival keeps the shell (D47): the title blur-swaps and the width springs to the new text.
502
+ // An arrival keeps the shell: the title blur-swaps and the width springs to the new text.
497
503
  if (plan.presence === 'enter' || plan.presence === 'replace') {
498
504
  motion.enter(edge, reveal('presence'));
499
505
  if (plan.presence === 'enter') {
@@ -857,7 +863,7 @@ function createHostRuntime(owner: Host) {
857
863
  onClickCapture={event => {
858
864
  const target = (event.target as Element).closest('button');
859
865
  // A double click must not fire a one-shot action twice; close, the pill and the dots are
860
- // repeatable on purpose (each click dismisses or shows another record; task 17, owner).
866
+ // repeatable on purpose (each click dismisses or shows another record).
861
867
  const oneShot = !!target?.matches('[data-cloudlet-control="action"], [data-cloudlet-control="retry"], [data-cloudlet-control="cancel"]');
862
868
  if (target && (repeating || (oneShot && event.detail > 1) || (activation && activation !== target))) { event.preventDefault(); event.stopPropagation(); }
863
869
  activation = null;
@@ -3,7 +3,7 @@ import { spring, tween as tweenGenerator } from './generators.js';
3
3
  import type { Generator } from './generators.js';
4
4
 
5
5
  /**
6
- * Values of DESIGN §7 accepted by the owner on the laboratory (task 17, D47), the one place
6
+ * Values tuned on the laboratory, the one place
7
7
  * to tune them. Durations are milliseconds at `timeScale: 1`; offsets are CSS px.
8
8
  */
9
9
  export const MOTION = {
@@ -33,7 +33,7 @@ export const MOTION = {
33
33
  /** Reduced motion: every transition becomes an opacity change of this length; swipes are off. */
34
34
  reduced: 80,
35
35
  /**
36
- * Swipes (DESIGN §2–§3): the axis is decided after `intent` px; a release commits past
36
+ * Swipes: the axis is decided after `intent` px; a release commits past
37
37
  * `distance` of the swiped dimension, or after `minDistance` px with `velocity` px/ms
38
38
  * towards the commit (a flick back cancels). Edges resist with the rubber-band
39
39
  * coefficient; a trackpad swipe ends after `wheelIdle` ms without wheel events and commits
@@ -50,7 +50,7 @@ const tunable: CloudletMotionConstants = MOTION;
50
50
  const positive = new Set(['stiffness', 'mass']);
51
51
 
52
52
  /**
53
- * Resolves `ToasterMotion.overrides` (a debugging channel, DESIGN §7) onto the starting
53
+ * Resolves `ToasterMotion.overrides` (a debugging channel) onto the starting
54
54
  * values. A field that is not a finite number in range (springs: stiffness and mass > 0,
55
55
  * everything else ≥ 0) keeps its default; unknown keys are ignored.
56
56
  */
@@ -309,7 +309,7 @@ export function createShellMotion(options: ShellMotionOptions) {
309
309
  enter(edge: -1 | 1, done?: () => void) {
310
310
  request(time => {
311
311
  jump('opacity', 0); jump('scale', m.enter.scale); jump('y', edge * m.enter.offset);
312
- // The shell becomes opaque within `fade`; only scale and y follow the spring (D47).
312
+ // The shell becomes opaque within `fade`; only scale and y follow the spring.
313
313
  to('opacity', 1, tween(m.enter.fade), time);
314
314
  for (const key of ['scale', 'y'] as const) to(key, key === 'y' ? 0 : 1, enterSpring(), time);
315
315
  after('presence', ['opacity', 'scale', 'y'], done);
package/src/react/plan.ts CHANGED
@@ -14,7 +14,7 @@ export interface ShellView {
14
14
  readonly total: number;
15
15
  /** Tails behind the collapsed pill (0–2); none in the Island. */
16
16
  readonly depth: number;
17
- /** Missed notifications (overflow), part of the badge and the pill name (DESIGN §10). */
17
+ /** Missed notifications (overflow), part of the badge and the pill name. */
18
18
  readonly missed: number;
19
19
  /** Badge number: records in the stack plus missed ones; shown on the pill at ≥ 2 or with missed ones. */
20
20
  readonly count: number;
@@ -79,7 +79,7 @@ export function planShell(previous: ShellView, next: ShellView): ShellPlan {
79
79
  if (previous.open !== next.open) plan.morph = next.open ? 'open' : 'close';
80
80
  if (before.handle !== after.handle) {
81
81
  if (previous.open && next.open) {
82
- // Only the body slides (task 17, owner): the title and icon blur-swap in place.
82
+ // Only the body slides: the title and icon blur-swap in place.
83
83
  plan.slide = next.index >= previous.index ? 1 : -1;
84
84
  plan.header = headerKey(before) !== headerKey(after) || before.content.icon !== after.content.icon;
85
85
  }
@@ -0,0 +1,77 @@
1
+ import { useEffect, useRef, useState, useSyncExternalStore } from 'react';
2
+ import type { ReactElement } from 'react';
3
+ import { createPortal } from 'react-dom';
4
+ import { getRendererBridge } from '../core/bridge.js';
5
+ import type { HostLease } from '../contracts/internal.js';
6
+ import type { ReactCloudletController, ToasterProps } from '../react.js';
7
+ import { Host } from './host.js';
8
+
9
+ // The renderer chunk: Toaster in bindings.tsx loads this module on the client once, after mount.
10
+ const subscribeMount = () => () => {};
11
+ const clientMounted = () => true;
12
+ const serverMounted = () => false;
13
+
14
+ interface Attachment {
15
+ readonly controller: ReactCloudletController;
16
+ readonly lease: HostLease;
17
+ readonly container: Element;
18
+ readonly document: Document;
19
+ }
20
+
21
+ export function AttachedToaster({ controller, options }: { controller: ReactCloudletController; options: ToasterProps }): ReactElement | null {
22
+ // React supplies the server value during hydration, then the client value.
23
+ // The inert marker can therefore identify the owner document without a store
24
+ // or effect whose only purpose is an extra mounting render.
25
+ const mounted = useSyncExternalStore(subscribeMount, clientMounted, serverMounted);
26
+ const bridge = getRendererBridge(controller);
27
+ const snapshot = useSyncExternalStore(bridge.subscribe, bridge.getSnapshot, bridge.getServerSnapshot);
28
+ const marker = useRef<HTMLTemplateElement>(null);
29
+ const [attachment, setAttachment] = useState<Attachment | null>(null);
30
+ useEffect(() => {
31
+ if (!mounted || !marker.current) return;
32
+ const ownerDocument = marker.current.ownerDocument;
33
+ let container: Element;
34
+ try {
35
+ const requested = typeof options.container === 'function' ? options.container() : options.container;
36
+ const candidate = requested ?? ownerDocument.body;
37
+ // nodeType/ownerDocument also work when the mounting React root is in another realm.
38
+ if (!candidate || candidate.nodeType !== 1 || candidate.ownerDocument !== ownerDocument
39
+ || candidate.namespaceURI !== 'http://www.w3.org/1999/xhtml' || !candidate.isConnected
40
+ || candidate.getRootNode().nodeType === 11) {
41
+ bridge.reportInvalidContainer('Toaster container must be a connected HTML Element in the mounting document, outside Shadow DOM');
42
+ return;
43
+ }
44
+ container = candidate;
45
+ } catch (error) {
46
+ bridge.reportInvalidContainer('Toaster container resolver failed', error);
47
+ return;
48
+ }
49
+ const delay = options.attention?.delay;
50
+ let attached = bridge.attach(delay === undefined ? undefined : { attentionDelay: delay });
51
+ // An invalid delay must not suppress the host: fall back to the controller default and report it.
52
+ const invalidDelay = attached.status === 'rejected' && attached.reason === 'invalid-input';
53
+ if (invalidDelay) attached = bridge.attach();
54
+ if (attached.status !== 'attached') return;
55
+ if (invalidDelay) bridge.reportDiagnostic(attached.lease, { code: 'invalid-input', source: 'Toaster.attention',
56
+ message: 'attention.delay must be a finite nonnegative number of milliseconds' });
57
+ const current = { controller, lease: attached.lease, container, document: ownerDocument };
58
+ applyAutoDismiss(current.lease, false);
59
+ setAttachment(current);
60
+ return () => { bridge.detach(current.lease); };
61
+ // Container and attention delay are fixed for this attachment; changing them requires remount.
62
+ }, [controller, mounted]);
63
+ // Applied before the first presentation, then whenever the prop changes (which reports an invalid value once).
64
+ function applyAutoDismiss(lease: HostLease, report: boolean) {
65
+ const value = options.autoDismiss;
66
+ const valid = value === undefined || typeof value === 'boolean';
67
+ bridge.setAutoDismiss(lease, valid ? value : undefined);
68
+ if (!valid && report) bridge.reportDiagnostic(lease, { code: 'invalid-input', source: 'Toaster.autoDismiss',
69
+ message: 'autoDismiss must be a boolean' });
70
+ }
71
+ useEffect(() => { if (attachment) applyAutoDismiss(attachment.lease, true); }, [attachment, options.autoDismiss]);
72
+
73
+ if (!mounted) return null;
74
+ return <><template ref={marker} />{attachment?.controller === controller && !snapshot.disposed
75
+ ? createPortal(<Host key={attachment.lease} bridge={bridge} lease={attachment.lease} snapshot={snapshot}
76
+ options={options} document={attachment.document} />, attachment.container) : null}</>;
77
+ }
package/src/react.ts CHANGED
@@ -77,7 +77,7 @@ export interface ToasterAttention {
77
77
  readonly delay?: number;
78
78
  }
79
79
 
80
- /** A physical spring of DESIGN §7. */
80
+ /** A physical spring of the motion system. */
81
81
  export interface CloudletSpring {
82
82
  readonly stiffness: number;
83
83
  readonly damping: number;
@@ -91,7 +91,7 @@ export interface CloudletTimedSpring {
91
91
  }
92
92
 
93
93
  /**
94
- * The motion constants of DESIGN §7: durations in ms at `timeScale: 1`, offsets and blur in CSS px.
94
+ * The motion constants: durations in ms at `timeScale: 1`, offsets and blur in CSS px.
95
95
  * Reported by the laboratory's tuning panel; see `ToasterMotion.overrides`.
96
96
  */
97
97
  export interface CloudletMotionConstants {
@@ -148,7 +148,7 @@ export interface ToasterMotion {
148
148
  */
149
149
  readonly timeScale?: number;
150
150
  /**
151
- * Debugging and laboratory channel (task 16): replaces DESIGN §7 constants live, for
151
+ * Debugging and laboratory channel: replaces the motion constants live, for
152
152
  * transitions started after the change. A field that is not a finite number in range
153
153
  * (stiffness and mass > 0, the rest ≥ 0) keeps its default. Not a styling API: the
154
154
  * values may change between releases; do not ship overrides to production.
@@ -156,7 +156,7 @@ export interface ToasterMotion {
156
156
  readonly overrides?: CloudletMotionOverrides;
157
157
  }
158
158
 
159
- /** Styling hooks of the renderer (DESIGN §8); see `ToasterProps.classNames`. */
159
+ /** Styling hooks of the renderer; see `ToasterProps.classNames`. */
160
160
  export type CloudletPart = 'host' | 'stack' | 'pill' | 'tail' | 'badge' | 'island' | 'header' | 'icon' | 'title'
161
161
  | 'close' | 'body' | 'description' | 'actions' | 'action' | 'indicator' | 'overflow';
162
162