fix(frontend): follow bottom-nav taps made while a view transition runs (#5605)

This commit is contained in:
Sam Dornan
2026-11-15 17:26:10 -06:00
committed by GitHub
parent d505be3f02
commit 07b193c3ff
3 changed files with 135 additions and 9 deletions

View File

@@ -1,7 +1,12 @@
import { mount } from "@vue/test-utils";
import { describe, expect, it, vi } from "vitest";
import { afterEach, describe, expect, it, vi } from "vitest";
import { defineComponent, ref } from "vue";
import {
popEscapable,
pushEscapable,
} from "@/lib/overlays/RDialog/escapeStack";
import { dropSupersededSkip, isTransitionSkipped } from "@/plugins/transition";
import { stubActiveViewTransition } from "@/test-utils/viewTransition";
import BottomNav from "./BottomNav.vue";
type Target = { fullPath: string };
@@ -10,11 +15,13 @@ const hooks: {
after: ((to: Target) => void) | null;
onError: ((error: unknown, to: Target) => void) | null;
} = { before: null, after: null, onError: null };
const pushed: string[] = [];
vi.mock("vue-i18n");
vi.mock("vue-router", () => ({
useRoute: () => ({ path: "/collections" }),
useRouter: () => ({
push: (to: string) => pushed.push(to),
beforeEach: (fn: (to: Target) => void) => {
hooks.before = fn;
return () => {};
@@ -46,10 +53,28 @@ const RouterLink = defineComponent({
});
function mountNav() {
return mount(BottomNav, { global: { stubs: { RouterLink } } });
return mount(BottomNav, {
attachTo: document.body,
global: { stubs: { RouterLink } },
});
}
// Taps on <html> where a view transition sends them, at the centre of `el`.
function tapRootOver(el: Element | undefined) {
vi.spyOn(el!, "getBoundingClientRect").mockReturnValue(
new DOMRect(100, 700, 80, 52),
);
document.documentElement.dispatchEvent(
new MouseEvent("click", { bubbles: true, clientX: 140, clientY: 726 }),
);
}
describe("BottomNav", () => {
afterEach(() => {
delete (document as { activeViewTransition?: unknown })
.activeViewTransition;
});
it("uses the filled glyph on every tab", () => {
const wrapper = mountNav();
const icons = wrapper.findAll(".r-icon").map((i) => i.classes());
@@ -190,4 +215,56 @@ describe("BottomNav", () => {
expect(tabs[2]?.classes()).toContain("r-segmented-control__btn--active");
});
it("follows a tab tap that a running view transition sent to <html>", async () => {
const wrapper = mountNav();
const tabs = wrapper.findAll(".r-segmented-control__btn");
const skipTransition = stubActiveViewTransition();
pushed.length = 0;
tapRootOver(tabs[1]?.element);
await wrapper.vm.$nextTick();
expect(skipTransition).toHaveBeenCalledOnce();
expect(pushed).toEqual(["/platforms"]);
expect(isTransitionSkipped({ fullPath: "/platforms" })).toBe(true);
expect(tabs[1]?.classes()).toContain("r-segmented-control__btn--active");
dropSupersededSkip({ fullPath: "/elsewhere" });
wrapper.unmount();
});
it("ignores a tap on <html> outside every tab", () => {
const wrapper = mountNav();
const skipTransition = stubActiveViewTransition();
pushed.length = 0;
document.documentElement.dispatchEvent(
new MouseEvent("click", { bubbles: true, clientX: 5, clientY: 5 }),
);
expect(skipTransition).not.toHaveBeenCalled();
expect(pushed).toEqual([]);
wrapper.unmount();
});
it("leaves a tap over a tab to the overlay covering the bar", () => {
const wrapper = mountNav();
const tabs = wrapper.findAll(".r-segmented-control__btn");
const skipTransition = stubActiveViewTransition();
const panel = document.createElement("div");
const sheet = { close: vi.fn(), persistent: true, panel: () => panel };
pushEscapable(sheet);
pushed.length = 0;
try {
tapRootOver(tabs[1]?.element);
} finally {
popEscapable(sheet);
}
expect(pushed).toEqual([]);
expect(skipTransition).not.toHaveBeenCalled();
expect(sheet.close).not.toHaveBeenCalled();
wrapper.unmount();
});
});

View File

@@ -1,6 +1,7 @@
<script setup lang="ts">
// Primary-nav tab bar on sm-and-down, where AppNav drops its centre pill.
// Mount-gated (not `display: none`) so hidden links never take focus.
import { useEventListener } from "@vueuse/core";
import { computed, onBeforeUnmount, ref } from "vue";
import { useI18n } from "vue-i18n";
import { useRouter } from "vue-router";
@@ -9,8 +10,12 @@ import {
NAV_FILLED_ICONS,
useNavDestinations,
} from "@/composables/useNavDestinations";
import type { NavDestinationId } from "@/composables/useNavDestinations";
import type {
NavDestination,
NavDestinationId,
} from "@/composables/useNavDestinations";
import { RIcon, RSegmentedControl } from "@/lib";
import { isUnderOpenEscapable } from "@/lib/overlays/RDialog/escapeStack";
import { skipTransitionTo } from "@/plugins/transition";
const { t } = useI18n();
@@ -22,19 +27,49 @@ const router = useRouter();
const pendingId = ref<NavDestinationId | null>(null);
const shownId = computed(() => pendingId.value ?? activeId.value);
const navEl = ref<HTMLElement | null>(null);
// RouterLink leaves these clicks to the browser, so no navigation follows.
const isModified = (event: MouseEvent) =>
event.ctrlKey || event.metaKey || event.shiftKey || event.altKey;
function markTap(tapped: NavDestination) {
pendingId.value = tapped.id;
skipTransitionTo(tapped.to);
}
// Runs in the capture phase: a loaded view navigates inside RouterLink's own handler.
function onTap(event: MouseEvent) {
// RouterLink leaves these clicks to the browser, so no navigation follows.
if (event.ctrlKey || event.metaKey || event.shiftKey || event.altKey) return;
if (isModified(event)) return;
const href = (event.target as HTMLElement | null)
?.closest("a")
?.getAttribute("href");
const tapped = destinations.value.find((d) => d.to === href);
if (!tapped) return;
pendingId.value = tapped.id;
skipTransitionTo(tapped.to);
if (tapped) markTap(tapped);
}
// A running view transition hit-tests taps to <html>, so the tab is found by
// position; under an open overlay the tap may be the overlay's, so it is left.
function onRootTap(event: MouseEvent) {
if (event.target !== document.documentElement || isModified(event)) return;
if (isUnderOpenEscapable(navEl.value)) return;
const { clientX: x, clientY: y } = event;
const link = Array.from(navEl.value?.querySelectorAll("a") ?? []).find(
(a) => {
const r = a.getBoundingClientRect();
return x >= r.left && x <= r.right && y >= r.top && y <= r.bottom;
},
);
const tapped = destinations.value.find(
(d) => d.to === link?.getAttribute("href"),
);
if (!tapped) return;
markTap(tapped);
document.activeViewTransition?.skipTransition();
void router.push(tapped.to);
}
useEventListener(document, "click", onRootTap, { capture: true });
// Only the latest navigation may clear the highlight; a superseded one finishes on its own.
let latestPath: string | null = null;
const stopTracking = router.beforeEach((to) => {
@@ -57,7 +92,11 @@ onBeforeUnmount(() => {
<template>
<div v-if="smAndDown" class="r-bottom-nav-anchor">
<nav class="r-bottom-nav" :aria-label="t('common.primary-navigation')">
<nav
ref="navEl"
class="r-bottom-nav"
:aria-label="t('common.primary-navigation')"
>
<RSegmentedControl
:model-value="shownId"
:items="destinations"

View File

@@ -25,3 +25,13 @@ export function stubStartViewTransition(ready: Promise<void>) {
});
return finished;
}
/** Stubs a running transition as `document.activeViewTransition`; returns its `skipTransition` spy. */
export function stubActiveViewTransition() {
const skipTransition = vi.fn();
Object.defineProperty(document, "activeViewTransition", {
configurable: true,
value: { skipTransition },
});
return skipTransition;
}