@seed-design/stackflow 1.1.24 → 1.1.25

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.
@@ -185,11 +185,12 @@ function useGlobalInteraction() {
185
185
  stopRunningAnims,
186
186
  cancelPendingPushRAF
187
187
  ]);
188
+ const topActivityId = stack?.activities.find((activity) => activity.isTop)?.id;
188
189
  (0, react.useLayoutEffect)(() => {
189
190
  if (stack?.globalTransitionState !== "idle") return;
190
191
  const stackEl = stackRef.current;
191
192
  if (stackEl) require_dom.clearTopActivityStyles(stackEl);
192
- }, [stack?.globalTransitionState]);
193
+ }, [stack?.globalTransitionState, topActivityId]);
193
194
  (0, react.useEffect)(() => {
194
195
  return () => {
195
196
  cancelPendingPushRAF();
@@ -184,11 +184,12 @@ function useGlobalInteraction() {
184
184
  stopRunningAnims,
185
185
  cancelPendingPushRAF
186
186
  ]);
187
+ const topActivityId = stack?.activities.find((activity) => activity.isTop)?.id;
187
188
  useLayoutEffect(() => {
188
189
  if (stack?.globalTransitionState !== "idle") return;
189
190
  const stackEl = stackRef.current;
190
191
  if (stackEl) clearTopActivityStyles(stackEl);
191
- }, [stack?.globalTransitionState]);
192
+ }, [stack?.globalTransitionState, topActivityId]);
192
193
  useEffect(() => {
193
194
  return () => {
194
195
  cancelPendingPushRAF();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@seed-design/stackflow",
3
- "version": "1.1.24",
3
+ "version": "1.1.25",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/daangn/seed-design.git",
@@ -36,7 +36,7 @@
36
36
  "clsx": "^2.1.1"
37
37
  },
38
38
  "devDependencies": {
39
- "@seed-design/css": "1.2.16",
39
+ "@seed-design/css": "1.2.15",
40
40
  "@stackflow/core": "^1.2.0",
41
41
  "@stackflow/react": "^1.5.1",
42
42
  "@types/react": "^19.2.2",
@@ -0,0 +1,159 @@
1
+ import { aggregate, makeEvent, type DomainEvent, type Stack } from "@stackflow/core";
2
+ import { render } from "@testing-library/react";
3
+ import { beforeEach, describe, expect, it, mock } from "bun:test";
4
+ import { appBarAnatomy } from "../AppBar/anatomy";
5
+ import { appScreenAnatomy } from "../AppScreen/anatomy";
6
+
7
+ // `useGlobalInteraction` (and the `useTopActivity` it calls) only consume
8
+ // `useStack` from @stackflow/react. Mocking just that lets the test feed real
9
+ // Stack snapshots — built by @stackflow/core's own reducer — without pulling in
10
+ // a renderer plugin. Registered before the dynamic import below so the hook
11
+ // picks it up.
12
+ let currentStack: Stack;
13
+ mock.module("@stackflow/react", () => ({ useStack: () => currentStack }));
14
+
15
+ const { useGlobalInteraction } = await import("./useGlobalInteraction");
16
+ const { findTransitionTargets, setIdlePositions } = await import("./dom");
17
+
18
+ // happy-dom ships no WAAPI. A `finished` that never settles models an
19
+ // animation that is still in flight, which is all these tests need.
20
+ Element.prototype.animate = (() => ({
21
+ finished: new Promise<void>(() => {}),
22
+ cancel() {},
23
+ })) as Element["animate"];
24
+
25
+ // ─── Real stack snapshots ───────────────────────────────────────────────────
26
+
27
+ const T0 = 1_000_000;
28
+ const TRANSITION_DURATION = 350;
29
+
30
+ /** Zero-padded so the reducer's lexicographic sort by id matches insertion order. */
31
+ function evt<T extends DomainEvent["name"]>(
32
+ seq: number,
33
+ name: T,
34
+ params: Omit<Extract<DomainEvent, { name: T }>, "id" | "name" | "eventDate">,
35
+ ) {
36
+ return makeEvent(name, {
37
+ ...params,
38
+ id: String(seq).padStart(4, "0"),
39
+ eventDate: T0 + seq,
40
+ });
41
+ }
42
+
43
+ const BASE_EVENTS: DomainEvent[] = [
44
+ evt(1, "Initialized", { transitionDuration: TRANSITION_DURATION }),
45
+ evt(2, "ActivityRegistered", { activityName: "Screen" }),
46
+ evt(3, "Pushed", { activityId: "a1", activityName: "Screen", activityParams: {} }),
47
+ evt(4, "Pushed", { activityId: "a2", activityName: "Screen", activityParams: {} }),
48
+ ];
49
+
50
+ /** Far enough past every eventDate that both pushes have settled. */
51
+ const SETTLED_AT = T0 + TRANSITION_DURATION * 10;
52
+
53
+ const POP_SEQ = 5;
54
+
55
+ function stackAfter(...extraEvents: DomainEvent[]): Stack {
56
+ return aggregate([...BASE_EVENTS, ...extraEvents], SETTLED_AT);
57
+ }
58
+
59
+ /** `pop({ animate: false })` — stackflow maps it to `skipExitActiveState`. */
60
+ const POP_WITHOUT_ANIMATION = evt(POP_SEQ, "Popped", { skipExitActiveState: true });
61
+
62
+ /** A plain animated `pop()`, still mid-flight at `SETTLED_AT`. */
63
+ const POP_WITH_ANIMATION = makeEvent("Popped", {
64
+ id: String(POP_SEQ).padStart(4, "0"),
65
+ eventDate: SETTLED_AT,
66
+ });
67
+
68
+ // ─── Harness ────────────────────────────────────────────────────────────────
69
+
70
+ function ActivityMarkup({ id, isTop }: { id: string; isTop: boolean }) {
71
+ return (
72
+ <section
73
+ data-part={appScreenAnatomy.activity}
74
+ data-activity-id={id}
75
+ data-transition-style="slideFromRightIOS"
76
+ {...(isTop ? { "data-activity-is-top": "" } : {})}
77
+ >
78
+ <div data-part={appScreenAnatomy.layer} data-testid={`${id}-layer`} />
79
+ <div data-part={appScreenAnatomy.dim} />
80
+ <div data-part={appBarAnatomy.root}>
81
+ <div data-part={appBarAnatomy.background} />
82
+ <div data-part={appBarAnatomy.main} data-testid={`${id}-title`} />
83
+ <div data-part={appBarAnatomy.icon} data-testid={`${id}-icon`} />
84
+ </div>
85
+ </section>
86
+ );
87
+ }
88
+
89
+ function Harness() {
90
+ const { stackRef } = useGlobalInteraction();
91
+
92
+ // Mirrors the core's `visibleActivities` filter — exit-done is unmounted.
93
+ const visible = currentStack.activities.filter((a) => a.transitionState !== "exit-done");
94
+
95
+ return (
96
+ <div ref={stackRef} data-testid="stack">
97
+ {visible.map((activity) => (
98
+ <ActivityMarkup key={activity.id} id={activity.id} isTop={activity.isTop} />
99
+ ))}
100
+ </div>
101
+ );
102
+ }
103
+
104
+ // ─── Tests ──────────────────────────────────────────────────────────────────
105
+
106
+ describe("useGlobalInteraction — settle safety-net", () => {
107
+ beforeEach(() => {
108
+ currentStack = stackAfter();
109
+ });
110
+
111
+ it("leaves the behind layer pinned at its idle offset while a2 is on top", () => {
112
+ // Premise check: a settled push is idle, with a2 on top.
113
+ expect(currentStack.globalTransitionState).toBe("idle");
114
+ expect(currentStack.activities.find((a) => a.isTop)?.id).toBe("a2");
115
+
116
+ const { getByTestId, rerender } = render(<Harness />);
117
+
118
+ // Exactly what the push's finished handler leaves behind.
119
+ setIdlePositions(findTransitionTargets(getByTestId("stack")), "slideFromRightIOS");
120
+ rerender(<Harness />);
121
+
122
+ expect(getByTestId("a1-layer").style.transform).toBe("translate3d(-30%, 0, 0)");
123
+ });
124
+
125
+ it("clears the landing activity when pop({ animate: false }) skips exit-active", () => {
126
+ const { getByTestId, rerender } = render(<Harness />);
127
+ setIdlePositions(findTransitionTargets(getByTestId("stack")), "slideFromRightIOS");
128
+
129
+ currentStack = stackAfter(POP_WITHOUT_ANIMATION);
130
+
131
+ // Premise: the transition state machine never moves — a1 just becomes top.
132
+ expect(currentStack.globalTransitionState).toBe("idle");
133
+ expect(currentStack.activities.find((a) => a.isTop)?.id).toBe("a1");
134
+
135
+ rerender(<Harness />);
136
+
137
+ expect(getByTestId("a1-layer").style.transform).toBe("");
138
+ expect(getByTestId("a1-title").style.transform).toBe("");
139
+ expect(getByTestId("a1-title").style.opacity).toBe("");
140
+ expect(getByTestId("a1-icon").style.opacity).toBe("");
141
+ });
142
+
143
+ it("does not touch inline styles while a transition is still in flight", () => {
144
+ const { getByTestId, rerender } = render(<Harness />);
145
+ setIdlePositions(findTransitionTargets(getByTestId("stack")), "slideFromRightIOS");
146
+
147
+ currentStack = stackAfter(POP_WITH_ANIMATION);
148
+
149
+ // Premise: an animated pop parks a2 in exit-active, so the stack is loading.
150
+ expect(currentStack.globalTransitionState).toBe("loading");
151
+ expect(currentStack.activities.find((a) => a.id === "a2")?.transitionState).toBe("exit-active");
152
+
153
+ rerender(<Harness />);
154
+
155
+ // The WAAPI pop animation owns the unwind from here — the safety-net must
156
+ // stay out of the way until everything settles.
157
+ expect(getByTestId("a1-layer").style.transform).toBe("translate3d(-30%, 0, 0)");
158
+ });
159
+ });
@@ -341,13 +341,22 @@ export function useGlobalInteraction() {
341
341
  // top must always be in a clean default state. Wipe all leftover inline
342
342
  // styles from the top activity — the behind is left untouched (stays at
343
343
  // -30%), this only runs at idle, and it's a no-op if already clean.
344
+ //
345
+ // Keyed on the top activity too, not just globalTransitionState: an
346
+ // `animate: false` navigation makes the core skip the enter-active /
347
+ // exit-active phase entirely, so globalTransitionState never leaves "idle"
348
+ // and a state-only dependency would never re-run. That is how a behind
349
+ // layer became the top while still pinned at -30%.
350
+ const topActivityId = stack?.activities.find((activity) => activity.isTop)?.id;
351
+
352
+ // biome-ignore lint/correctness/useExhaustiveDependencies: topActivityId is a trigger, not a value read here — clearTopActivityStyles re-queries the DOM
344
353
  useLayoutEffect(() => {
345
354
  if (stack?.globalTransitionState !== "idle") return;
346
355
  const stackEl = stackRef.current;
347
356
  if (stackEl) {
348
357
  clearTopActivityStyles(stackEl);
349
358
  }
350
- }, [stack?.globalTransitionState]);
359
+ }, [stack?.globalTransitionState, topActivityId]);
351
360
 
352
361
  // Cancel any pending push rAF and running animations on unmount so
353
362
  // late-firing finished handlers can't run against a torn-down stack.