@oxyhq/bloom 0.54.0 → 0.55.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 (81) hide show
  1. package/lib/commonjs/tab-bar/TabBarBase.js +101 -16
  2. package/lib/commonjs/tab-bar/TabBarBase.js.map +1 -1
  3. package/lib/commonjs/tab-bar/shared.js +15 -1
  4. package/lib/commonjs/tab-bar/shared.js.map +1 -1
  5. package/lib/commonjs/toast/ToastRow.js +35 -10
  6. package/lib/commonjs/toast/ToastRow.js.map +1 -1
  7. package/lib/commonjs/toast/ToastSwipeHandler.js +15 -5
  8. package/lib/commonjs/toast/ToastSwipeHandler.js.map +1 -1
  9. package/lib/commonjs/toast/constants.js +18 -1
  10. package/lib/commonjs/toast/constants.js.map +1 -1
  11. package/lib/commonjs/toast/toast-store.js +39 -1
  12. package/lib/commonjs/toast/toast-store.js.map +1 -1
  13. package/lib/commonjs/toast/use-stack-hover.js +175 -0
  14. package/lib/commonjs/toast/use-stack-hover.js.map +1 -0
  15. package/lib/commonjs/toast/use-stack-hover.native.js +42 -0
  16. package/lib/commonjs/toast/use-stack-hover.native.js.map +1 -0
  17. package/lib/module/tab-bar/TabBarBase.js +103 -18
  18. package/lib/module/tab-bar/TabBarBase.js.map +1 -1
  19. package/lib/module/tab-bar/shared.js +14 -0
  20. package/lib/module/tab-bar/shared.js.map +1 -1
  21. package/lib/module/toast/ToastRow.js +35 -10
  22. package/lib/module/toast/ToastRow.js.map +1 -1
  23. package/lib/module/toast/ToastSwipeHandler.js +16 -4
  24. package/lib/module/toast/ToastSwipeHandler.js.map +1 -1
  25. package/lib/module/toast/constants.js +18 -1
  26. package/lib/module/toast/constants.js.map +1 -1
  27. package/lib/module/toast/toast-store.js +39 -1
  28. package/lib/module/toast/toast-store.js.map +1 -1
  29. package/lib/module/toast/use-stack-hover.js +168 -0
  30. package/lib/module/toast/use-stack-hover.js.map +1 -0
  31. package/lib/module/toast/use-stack-hover.native.js +35 -0
  32. package/lib/module/toast/use-stack-hover.native.js.map +1 -0
  33. package/lib/typescript/commonjs/tab-bar/TabBarBase.d.ts.map +1 -1
  34. package/lib/typescript/commonjs/tab-bar/shared.d.ts +13 -0
  35. package/lib/typescript/commonjs/tab-bar/shared.d.ts.map +1 -1
  36. package/lib/typescript/commonjs/tab-bar/types.d.ts +12 -0
  37. package/lib/typescript/commonjs/tab-bar/types.d.ts.map +1 -1
  38. package/lib/typescript/commonjs/toast/ToastRow.d.ts.map +1 -1
  39. package/lib/typescript/commonjs/toast/ToastSwipeHandler.d.ts +4 -0
  40. package/lib/typescript/commonjs/toast/ToastSwipeHandler.d.ts.map +1 -1
  41. package/lib/typescript/commonjs/toast/constants.d.ts.map +1 -1
  42. package/lib/typescript/commonjs/toast/toast-store.d.ts +15 -0
  43. package/lib/typescript/commonjs/toast/toast-store.d.ts.map +1 -1
  44. package/lib/typescript/commonjs/toast/use-stack-hover.d.ts +55 -0
  45. package/lib/typescript/commonjs/toast/use-stack-hover.d.ts.map +1 -0
  46. package/lib/typescript/commonjs/toast/use-stack-hover.native.d.ts +26 -0
  47. package/lib/typescript/commonjs/toast/use-stack-hover.native.d.ts.map +1 -0
  48. package/lib/typescript/module/tab-bar/TabBarBase.d.ts.map +1 -1
  49. package/lib/typescript/module/tab-bar/shared.d.ts +13 -0
  50. package/lib/typescript/module/tab-bar/shared.d.ts.map +1 -1
  51. package/lib/typescript/module/tab-bar/types.d.ts +12 -0
  52. package/lib/typescript/module/tab-bar/types.d.ts.map +1 -1
  53. package/lib/typescript/module/toast/ToastRow.d.ts.map +1 -1
  54. package/lib/typescript/module/toast/ToastSwipeHandler.d.ts +4 -0
  55. package/lib/typescript/module/toast/ToastSwipeHandler.d.ts.map +1 -1
  56. package/lib/typescript/module/toast/constants.d.ts.map +1 -1
  57. package/lib/typescript/module/toast/toast-store.d.ts +15 -0
  58. package/lib/typescript/module/toast/toast-store.d.ts.map +1 -1
  59. package/lib/typescript/module/toast/use-stack-hover.d.ts +55 -0
  60. package/lib/typescript/module/toast/use-stack-hover.d.ts.map +1 -0
  61. package/lib/typescript/module/toast/use-stack-hover.native.d.ts +26 -0
  62. package/lib/typescript/module/toast/use-stack-hover.native.d.ts.map +1 -0
  63. package/package.json +1 -1
  64. package/src/__tests__/TabBarNoSelection.test.tsx +458 -0
  65. package/src/__tests__/TabBarReanimatedDeps.test.ts +28 -4
  66. package/src/__tests__/Toaster.test.tsx +554 -0
  67. package/src/__tests__/animated-style-deps.test.ts +30 -6
  68. package/src/__tests__/toast-position-utils.test.ts +28 -0
  69. package/src/__tests__/toast-stack-hover.test.ts +92 -0
  70. package/src/__tests__/toast-store.test.ts +72 -0
  71. package/src/tab-bar/TabBar.stories.tsx +52 -1
  72. package/src/tab-bar/TabBarBase.tsx +113 -31
  73. package/src/tab-bar/shared.ts +12 -0
  74. package/src/tab-bar/types.ts +12 -0
  75. package/src/toast/Toast.stories.tsx +10 -0
  76. package/src/toast/ToastRow.tsx +33 -10
  77. package/src/toast/ToastSwipeHandler.tsx +14 -2
  78. package/src/toast/constants.ts +18 -1
  79. package/src/toast/toast-store.ts +39 -1
  80. package/src/toast/use-stack-hover.native.ts +34 -0
  81. package/src/toast/use-stack-hover.ts +182 -0
@@ -14,6 +14,10 @@
14
14
  * covers `toast.custom` JSX and `unstyled` rows too, which never reach that
15
15
  * card. Every distance the gesture measures is a fraction of the capped
16
16
  * `rowWidth`, not of the window — see the constant.
17
+ *
18
+ * W13 — being THE row box also makes it the hover target: `useStackHover`'s
19
+ * handlers go here so a pointer over ANY row counts as a pointer over the stack.
20
+ * The handlers are empty on native.
17
21
  */
18
22
  import * as React from 'react';
19
23
  import {
@@ -37,6 +41,7 @@ import { easeInOutCircFn } from './animations';
37
41
  import { TOAST_MAX_ROW_WIDTH } from './constants';
38
42
  import { useToastContext } from './context';
39
43
  import type { ToastPosition, ToastProps } from './types';
44
+ import { useStackHover } from './use-stack-hover';
40
45
 
41
46
  /** Fraction of the row a horizontal swipe must cross to dismiss. */
42
47
  const HORIZONTAL_DISMISS_FRACTION = 0.25;
@@ -76,10 +81,16 @@ export const ToastSwipeHandler: React.FC<
76
81
  // The row is `width: '100%'` up to the cap, so this is its real width.
77
82
  const rowWidth = Math.min(windowWidth, TOAST_MAX_ROW_WIDTH);
78
83
  const translate = useSharedValue(0);
79
- const { swipeToDismissDirection: direction, position: positionCtx } =
80
- useToastContext();
84
+ const {
85
+ swipeToDismissDirection: direction,
86
+ position: positionCtx,
87
+ enableStacking,
88
+ } = useToastContext();
81
89
  const position = positionProps ?? positionCtx;
82
90
  const isAndroid = Platform.OS === 'android';
91
+ // W13 — hover lives on THE ROW BOX, so hovering any row counts as hovering the
92
+ // stack. Empty on native; see `use-stack-hover.native.ts`.
93
+ const hoverProps = useStackHover({ enableStacking });
83
94
 
84
95
  const pan = Gesture.Pan()
85
96
  .onBegin(() => {
@@ -199,6 +210,7 @@ export const ToastSwipeHandler: React.FC<
199
210
  styles.rowBox,
200
211
  style,
201
212
  ]}
213
+ {...hoverProps}
202
214
  // W9 — on web `LinearTransition`'s easing degrades to CSS `ease` because
203
215
  // a `bezierFn` result carries no easing-name symbol for the CSS mapper.
204
216
  // Accepted: the transition still runs, only its curve differs.
@@ -98,7 +98,24 @@ export const toastDefaults: {
98
98
  theme: 'system',
99
99
  autoWiggleOnUpdate: 'never',
100
100
  richColors: false,
101
- enableStacking: false,
101
+ /**
102
+ * ON, unlike upstream — the same class of correction as `TOAST_MAX_ROW_WIDTH`.
103
+ *
104
+ * sonner-NATIVE defaults this off; sonner (web) has no switch at all, because a
105
+ * collapsed stack — front row full size, the ones behind scaled and offset,
106
+ * expanding on interaction — IS its default presentation. The port inherited the
107
+ * mobile default onto every desktop consumer, where an unstacked run of toasts
108
+ * is a column of full cards rather than a stack.
109
+ *
110
+ * Bloom already deviates deliberately elsewhere (`position: 'bottom-center'` vs
111
+ * upstream's `top-center`, `duration: 3000` vs 4000, `gap: 8` vs 14), so this is
112
+ * in keeping rather than a break with the port.
113
+ *
114
+ * Turning it OFF (`<ToastOutlet enableStacking={false} />`) restores the flat
115
+ * column, which is still the right choice for a surface that must show several
116
+ * toasts at once without an interaction.
117
+ */
118
+ enableStacking: true,
102
119
  stackGap: 8,
103
120
  allowFontScaling: true,
104
121
  };
@@ -42,6 +42,7 @@ class ToastStore {
42
42
  private pendingPromises = new Set<string | number>();
43
43
  private collapseCooldown = false;
44
44
  private collapseCooldownTimeout: ReturnType<typeof setTimeout> | null = null;
45
+ private expansionHold: (() => boolean) | null = null;
45
46
 
46
47
  subscribe = (callback: Subscriber) => {
47
48
  this.subscribers.add(callback);
@@ -59,6 +60,24 @@ class ToastStore {
59
60
  this.config = config;
60
61
  };
61
62
 
63
+ /**
64
+ * Something outside the store can HOLD an expanded stack open — on web, a pointer
65
+ * resting on it. Registered by the platform trigger (`use-stack-hover`) so this
66
+ * module stays pure JS with no DOM and no platform branch; native registers
67
+ * nothing and the predicate stays `null`.
68
+ *
69
+ * It is asked at DECISION time rather than tracked as state on purpose: a row can
70
+ * stop being under the pointer with no event at all — rows unmount under the
71
+ * cursor and move under a stationary one, and neither fires `pointerleave` —
72
+ * so any cached boolean goes stale. Nothing renders from this, so it is
73
+ * deliberately not part of the snapshot.
74
+ */
75
+ setExpansionHold = (hold: (() => boolean) | null) => {
76
+ this.expansionHold = hold;
77
+ };
78
+
79
+ private isHeldOpen = (): boolean => this.expansionHold?.() === true;
80
+
62
81
  private notify = () => {
63
82
  this.subscribers.forEach((callback) => callback());
64
83
  };
@@ -305,8 +324,27 @@ class ToastStore {
305
324
  const updatedRefs = { ...this.state.toastRefs };
306
325
  delete updatedRefs[id];
307
326
 
327
+ /**
328
+ * An expanded stack collapses itself once a single row is left, because
329
+ * "expanded" then means nothing visually — a lone row resolves to the SAME
330
+ * offset and scale either way (`toast-position-utils.test.ts` pins that) — while
331
+ * `isExpanded` keeps every timer paused, so staying expanded would leave that
332
+ * last toast on screen forever with no visible reason. Inherited from
333
+ * sonner-native and kept deliberately: on native it is invisible and it is the
334
+ * only thing that stops the last row hanging.
335
+ *
336
+ * UNLESS something is holding the stack open. On web that is a pointer resting
337
+ * on it, and collapsing under the cursor would resume the timers hover had just
338
+ * paused — the toast would then expire while the user is reading it, which is
339
+ * exactly what hover exists to prevent.
340
+ *
341
+ * An EMPTY stack always resets, hold or no hold: it no longer exists to be held,
342
+ * and once the rows unmount no `pointerleave` will ever arrive to release it.
343
+ */
344
+ const isEmpty = filteredToasts.length === 0;
345
+ const heldOpen = !isEmpty && this.isHeldOpen();
308
346
  const shouldAutoCollapse =
309
- filteredToasts.length <= 1 && this.state.isExpanded;
347
+ filteredToasts.length <= 1 && this.state.isExpanded && !heldOpen;
310
348
 
311
349
  const updatedIndex = this.cloneIndex();
312
350
  updatedIndex.delete(id);
@@ -0,0 +1,34 @@
1
+ /**
2
+ * Bloom-original — NATIVE. Metro resolves this over `use-stack-hover.ts` on iOS and
3
+ * Android, the same filename split `ToastHost.native.tsx` uses.
4
+ *
5
+ * Native keeps PRESS as the only way to expand a stack, so this hands back no
6
+ * handlers at all. Two reasons it is a fork rather than a runtime `Platform` check:
7
+ * the web file's module-level hover state and its `setTimeout` have no business
8
+ * existing in a native bundle, and `onPointerEnter`/`onPointerLeave` DO fire on
9
+ * native for touch (RN's pointer events unify touch and mouse), which would make
10
+ * every tap expand-then-collapse while the press toggle fired as well.
11
+ *
12
+ * Keep the shape identical to the web file — `ToastSwipeHandler` spreads whatever
13
+ * this returns onto the row box, and `ToastRow` calls `isStackHovered()`
14
+ * unconditionally.
15
+ */
16
+ export type StackHoverProps = Record<string, never>;
17
+
18
+ /** Parity with the web file's export; nothing here waits for anything. */
19
+ export const HOVER_LEAVE_GRACE = 0;
20
+
21
+ /** Nothing hovers on a touch screen. */
22
+ export function isStackHovered(): boolean {
23
+ return false;
24
+ }
25
+
26
+ /** One frozen object, so spreading it never changes the row box's props. */
27
+ const NO_HOVER_PROPS: StackHoverProps = Object.freeze({});
28
+
29
+ export function useStackHover(_options: { enableStacking: boolean }): StackHoverProps {
30
+ return NO_HOVER_PROPS;
31
+ }
32
+
33
+ /** Parity with the web file so the shared suite can call it on either platform. */
34
+ export function resetStackHoverForTests(): void {}
@@ -0,0 +1,182 @@
1
+ /**
2
+ * Bloom-original — WEB/default. `use-stack-hover.native.ts` is the native no-op,
3
+ * filename-resolved by Metro exactly as `ToastHost.native.tsx` is, so `toast/`
4
+ * stays one universal engine with no `index.web` fork.
5
+ *
6
+ * sonner expands its stack on HOVER; sonner-native only has press, because a phone
7
+ * has no pointer. Bloom needs both: press is the touch and native affordance, hover
8
+ * is what a desktop user expects, and a touch-capable web device must not get a
9
+ * phantom hover from its own taps.
10
+ *
11
+ * THIS IS A TRIGGER, NOT A SECOND MECHANISM. It drives the store's existing
12
+ * `expand`/`collapse`, which already pause and resume the auto-close timers
13
+ * (`ToastStore.expand` calls `timers.pauseAll()`, `collapse` calls `resumeAll()`),
14
+ * so hovering to read a toast cannot let it expire under the cursor — no new
15
+ * pausing path, no new store state. With stacking OFF there is nothing to expand,
16
+ * so the pointer pauses the timers directly instead: the reason sonner pauses is
17
+ * that the pointer is over the toast, which has nothing to do with stacking.
18
+ *
19
+ * TWO THINGS ARE LOAD-BEARING:
20
+ *
21
+ * - `pointerType === 'mouse'`. Touch fires `pointerenter` before `pointerdown`
22
+ * and `pointerleave` after `pointerup` (the pointer ceases to exist), so
23
+ * without this guard every TAP on a touch-capable web device would expand then
24
+ * collapse the stack while the press toggle fired too — three handlers fighting
25
+ * over one tap. Hover is additive: pens and touch fall through to press.
26
+ * - THE LEAVE IS DEFERRED BY A FRAME-ISH GRACE PERIOD, and any enter cancels it.
27
+ * Rows are separate absolutely-positioned boxes with `gap` between them once
28
+ * expanded, so moving the pointer from one row to the next leaves the first
29
+ * before entering the second. Collapsing on that would snap the stack shut and
30
+ * reopen it on whatever row ends up under the pointer — a jitter loop, plus a
31
+ * resume/pause pair per crossing that re-banks each timer's remaining time.
32
+ *
33
+ * The state is module-level on purpose: there is ONE stack per document, the same
34
+ * reason `toastStore` is a module singleton. It is written and read only inside
35
+ * event handlers — never in render — so the React Compiler has nothing to freeze.
36
+ */
37
+ import { useMemo } from 'react';
38
+
39
+ import { toastStore } from './toast-store';
40
+
41
+ /**
42
+ * How long a leave waits for a neighbouring row to claim the pointer. One frame is
43
+ * enough for the same-tick leave/enter pair a row crossing produces; this is a
44
+ * little more so a slow frame cannot flash the stack shut. Exported so the suite
45
+ * asserts against the real value rather than a copy of it.
46
+ */
47
+ export const HOVER_LEAVE_GRACE = 80;
48
+
49
+ /** The portal root every toast row lives under — see `portal/index.web.tsx`. */
50
+ const PORTAL_ROOT_ID = 'bloom-portal-root';
51
+
52
+ type HoverEvent = { nativeEvent: { pointerType?: string } };
53
+
54
+ let hovered = false;
55
+ let leaveTimeout: ReturnType<typeof setTimeout> | null = null;
56
+
57
+ const isMouse = (event: HoverEvent) => event.nativeEvent.pointerType === 'mouse';
58
+
59
+ const cancelPendingLeave = () => {
60
+ if (leaveTimeout !== null) {
61
+ clearTimeout(leaveTimeout);
62
+ leaveTimeout = null;
63
+ }
64
+ };
65
+
66
+ /**
67
+ * Whether the pointer is over the stack RIGHT NOW.
68
+ *
69
+ * Two sources, and both are load-bearing:
70
+ *
71
+ * - the `hovered` latch answers "a mouse entered and has not left", which is the
72
+ * only thing available where there is no DOM (jest runs on `testEnvironment:
73
+ * 'node'`), and is a cheap early-out.
74
+ * - the live `:hover` query answers "a row is under the pointer", which the latch
75
+ * CANNOT: a row can stop being hovered with no event whatsoever. Measured — a row
76
+ * culled by `visibleToasts` under a stationary cursor fires no `pointerleave`,
77
+ * `pointerout` or `mouseleave` at all, and the restack then slides the remaining
78
+ * rows out from under the pointer. A latch alone therefore sticks TRUE, which
79
+ * would disable the press toggle for the rest of the session and suppress every
80
+ * later auto-collapse.
81
+ *
82
+ * Requiring both means a stale latch is corrected by the DOM and a stray DOM match
83
+ * (a pen, a touch) is rejected by the latch.
84
+ *
85
+ * COVERAGE, stated plainly: jest cannot see the DOM half at all
86
+ * (`testEnvironment: 'node'`), and a browser cannot isolate it either — its two
87
+ * consequences are a press toggle standing down (unreachable with a mouse, which
88
+ * always hovers what it presses) and a suppressed auto-collapse (which the deferred
89
+ * leave below independently corrects the moment any boundary event fires). Both
90
+ * halves are kept because each is measurably more accurate than the other alone, not
91
+ * because a test proves the composite.
92
+ *
93
+ * `ToastRow` asks so a press does not TOGGLE what hover already opened, and
94
+ * `toastStore` asks before auto-collapsing a stack down to its last row. The press
95
+ * still runs the toast's own `onPress`; only the expansion toggle steps aside.
96
+ */
97
+ export function isStackHovered(): boolean {
98
+ return hovered && rowUnderPointer() !== false;
99
+ }
100
+
101
+ /**
102
+ * The live DOM answer, or `undefined` where there is no DOM to ask (jest runs on
103
+ * `testEnvironment: 'node'`). Deliberately tri-state: "unknowable" and "no" must not
104
+ * collapse into the same value, because the two callers want opposite defaults —
105
+ * `isStackHovered` trusts the latch when it cannot check, while the deferred leave
106
+ * only stands down on a positive "yes, still hovered".
107
+ */
108
+ function rowUnderPointer(): boolean | undefined {
109
+ if (typeof document === 'undefined') {
110
+ return undefined;
111
+ }
112
+ return document.querySelector(`#${PORTAL_ROOT_ID} [aria-live]:hover`) !== null;
113
+ }
114
+
115
+ /**
116
+ * A pointer resting on the stack HOLDS it open, so the store must not auto-collapse
117
+ * it out from under the cursor. Registered here rather than passed in because this
118
+ * file is the one that knows what hovering means; the store only knows that
119
+ * something says "not yet". Native's fork registers nothing.
120
+ */
121
+ toastStore.setExpansionHold(isStackHovered);
122
+
123
+ export type StackHoverProps = {
124
+ onPointerEnter: (event: HoverEvent) => void;
125
+ onPointerLeave: (event: HoverEvent) => void;
126
+ };
127
+
128
+ export function useStackHover({
129
+ enableStacking,
130
+ }: {
131
+ enableStacking: boolean;
132
+ }): StackHoverProps {
133
+ // Stable per outlet configuration: these land on the row box as props, and every
134
+ // row re-renders on each stack change.
135
+ return useMemo(
136
+ () => ({
137
+ onPointerEnter: (event: HoverEvent) => {
138
+ if (!isMouse(event)) {
139
+ return;
140
+ }
141
+ cancelPendingLeave();
142
+ hovered = true;
143
+ if (enableStacking) {
144
+ // Pauses the timers as part of expanding.
145
+ toastStore.expand();
146
+ } else {
147
+ toastStore.pauseAllTimers();
148
+ }
149
+ },
150
+ onPointerLeave: (event: HoverEvent) => {
151
+ if (!isMouse(event) || !hovered) {
152
+ return;
153
+ }
154
+ cancelPendingLeave();
155
+ leaveTimeout = setTimeout(() => {
156
+ leaveTimeout = null;
157
+ // The rows may have moved rather than the pointer: an expand, a collapse
158
+ // or a restack slides them under a stationary cursor without firing a
159
+ // single pointer event, so re-ask the DOM before acting on a leave that
160
+ // fired 80ms ago. Only a positive "still hovered" stands the collapse down.
161
+ if (rowUnderPointer() === true) {
162
+ return;
163
+ }
164
+ hovered = false;
165
+ if (enableStacking) {
166
+ // Resumes the timers as part of collapsing.
167
+ toastStore.collapse();
168
+ } else {
169
+ toastStore.resumeAllTimers();
170
+ }
171
+ }, HOVER_LEAVE_GRACE);
172
+ },
173
+ }),
174
+ [enableStacking],
175
+ );
176
+ }
177
+
178
+ /** Test-only reset: the module state outlives a render tree, so a suite must clear it. */
179
+ export function resetStackHoverForTests(): void {
180
+ cancelPendingLeave();
181
+ hovered = false;
182
+ }