Skip to content

Commit adb28e7

Browse files
committed
Merge the phone app's web branch (a menu closes before will-navigate)
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012xYtaUjbkB1hwLtK4TEzhq
2 parents 5960aef + 6bfc937 commit adb28e7

3 files changed

Lines changed: 102 additions & 0 deletions

File tree

‎app/src/lib/native-shell/use-tab-navigation.ts‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,13 +178,77 @@ export const NAVIGATE_OK = "openbot:navigate-ok";
178178
/** How long the page waits for that answer before it goes ahead anyway. */
179179
export const NAVIGATE_OK_MS = 120;
180180

181+
/**
182+
* What is drawn over the page for a moment: a menu, a select's list, a popover, a tooltip, a sheet
183+
* or a dialog, as the primitives portal them (Base UI). The toasts' region, always there, is not.
184+
*/
185+
const OVERLAY =
186+
'[data-base-ui-portal] :is([role="menu"], [role="listbox"], [role="dialog"], [role="alertdialog"], [role="tooltip"], [data-slot$="-content"], [data-slot$="-popup"])';
187+
188+
function overlayOnScreen(): Element | null {
189+
for (const element of document.querySelectorAll(OVERLAY))
190+
if (!element.closest('[data-slot="toast-viewport"]')) return element;
191+
return null;
192+
}
193+
194+
/** How long a navigation may be held for an overlay to go, at most, and in frames. */
195+
const OVERLAY_MS = 100;
196+
const OVERLAY_FRAMES = 2;
197+
198+
/**
199+
* Runs `then` once no overlay is on screen and that has been drawn, so the app's picture of the page
200+
* at `will-navigate` has no menu open in it. With none, at once. An item's own press usually closes
201+
* its menu by the next frame; one still open then is sent Escape, as a person would close it.
202+
* Never more than `OVERLAY_FRAMES` frames and the frame that draws its going, and never more than
203+
* `OVERLAY_MS`. Its exit animation is off in the shell (styles.css), so it goes at once.
204+
*/
205+
function withoutOverlays(then: () => void): void {
206+
if (!overlayOnScreen()) {
207+
then();
208+
return;
209+
}
210+
let done = false;
211+
const finish = () => {
212+
if (done) return;
213+
done = true;
214+
clearTimeout(cap);
215+
then();
216+
};
217+
const cap = setTimeout(finish, OVERLAY_MS);
218+
let frames = 0;
219+
const tick = () => {
220+
if (done) return;
221+
const open = overlayOnScreen();
222+
// Gone: one more frame, so its going is drawn before the app takes its picture.
223+
if (!open) return void requestAnimationFrame(finish);
224+
frames += 1;
225+
if (frames === 1)
226+
open.dispatchEvent(
227+
new KeyboardEvent("keydown", {
228+
key: "Escape",
229+
code: "Escape",
230+
bubbles: true,
231+
cancelable: true,
232+
}),
233+
);
234+
if (frames > OVERLAY_FRAMES) return finish();
235+
requestAnimationFrame(tick);
236+
};
237+
requestAnimationFrame(tick);
238+
}
239+
181240
/**
182241
* Tells the app a navigation the page started is coming (`will-navigate`), then runs `go`: at once,
183242
* or on iOS, where the app takes a snapshot of the page for its swipe back, once the app answers
184243
* `openbot:navigate-ok` or after `NAVIGATE_OK_MS`, whichever is first. Never waits where nothing
185244
* received the message, nor on Android, which does not answer.
186245
*/
187246
function announce(direction: Direction, next: TabStacks, go: () => void): void {
247+
withoutOverlays(() => say(direction, next, go));
248+
}
249+
250+
/** `announce`, once nothing is drawn over the page. */
251+
function say(direction: Direction, next: TabStacks, go: () => void): void {
188252
const message = {
189253
type: "will-navigate",
190254
tab: next.current,

‎app/src/styles.css‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,3 +688,13 @@ html[data-native-shell] .text-xs {
688688
font-size: 0.875rem;
689689
}
690690
}
691+
692+
/*
693+
* In the phone app, what opens over the page (a menu, a popover, a sheet, a dialog) leaves at once
694+
* when it closes, without its fade: the app takes a picture of the page as a navigation starts, for
695+
* its swipe back, and a menu half faded would be in it (lib/native-shell/use-tab-navigation.ts).
696+
*/
697+
html[data-native-shell] [data-base-ui-portal] [data-closed] {
698+
animation: none;
699+
transition: none;
700+
}

‎app/tests/native-shell-app.test.tsx‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,8 @@ describe("the app's swipe back", () => {
640640
path?: string;
641641
at: number;
642642
showing: string;
643+
/** Whether a menu was drawn over the page when the app heard it. */
644+
menu: boolean;
643645
};
644646
/**
645647
* What the app hears, with when and what the page was showing at that moment. On iOS through its
@@ -656,6 +658,7 @@ describe("the app's swipe back", () => {
656658
...(message as { type: string }),
657659
at: performance.now(),
658660
showing: document.body.textContent ?? "",
661+
menu: document.querySelector('[role="menu"]') !== null,
659662
});
660663
if (answer && message.type === "will-navigate")
661664
window.dispatchEvent(new CustomEvent("openbot:navigate-ok"));
@@ -803,6 +806,31 @@ describe("the app's swipe back", () => {
803806
expect(heard).toEqual([]);
804807
});
805808

809+
test("a page opened from the Account menu is announced only once the menu is gone", async () => {
810+
inShell(true);
811+
const heard = hear("ios", true);
812+
serve([]);
813+
const { view, router } = renderApp("/");
814+
await view.findByRole("heading", { name: "Home" });
815+
await userEvent
816+
.setup({ document: view.container.ownerDocument })
817+
.click(view.getByRole("button", { name: "Account" }));
818+
const settings = await within(document.body).findByRole("menuitem", {
819+
name: "Settings",
820+
});
821+
expect(document.querySelector('[role="menu"]')).not.toBeNull();
822+
await userEvent
823+
.setup({ document: view.container.ownerDocument })
824+
.click(settings);
825+
await waitFor(() =>
826+
expect(router.state.location.pathname).toBe("/settings"),
827+
);
828+
const announced = of(heard, "will-navigate");
829+
expect(announced).toHaveLength(1);
830+
// The app's picture of the page has no menu open in it.
831+
expect(announced[0]?.menu).toBe(false);
832+
});
833+
806834
test("Back the app asks for is not announced back to it", async () => {
807835
inShell(true);
808836
const heard = hear("ios", true);

0 commit comments

Comments
 (0)