mirror of
https://github.com/rommapp/romm.git
synced 2026-12-03 10:33:22 +01:00
Track AppNav's window scroll with useWindowScroll (#5025)
This commit is contained in:
80
frontend/src/v2/components/AppShell/AppNav.test.ts
Normal file
80
frontend/src/v2/components/AppShell/AppNav.test.ts
Normal file
@@ -0,0 +1,80 @@
|
||||
import { enableAutoUnmount, mount } from "@vue/test-utils";
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import { computed, nextTick, ref } from "vue";
|
||||
import AppNav from "./AppNav.vue";
|
||||
|
||||
vi.mock("vue-i18n", () => ({
|
||||
useI18n: () => ({ t: (key: string) => key }),
|
||||
}));
|
||||
|
||||
vi.mock("@/v2/composables/useNavDestinations", () => ({
|
||||
useNavDestinations: () => ({
|
||||
destinations: computed(() => []),
|
||||
activeId: computed(() => "home"),
|
||||
}),
|
||||
}));
|
||||
|
||||
vi.mock("@/v2/composables/useBreakpoint", () => ({
|
||||
useBreakpoint: () => ({ smAndDown: ref(false) }),
|
||||
}));
|
||||
|
||||
vi.mock("@/v2/components/AppShell/ScanningIndicator.vue", () => ({
|
||||
default: { template: "<div />" },
|
||||
}));
|
||||
vi.mock("@/v2/components/AppShell/UserMenu.vue", () => ({
|
||||
default: { template: "<div />" },
|
||||
}));
|
||||
vi.mock("@/v2/components/Soundtrack/NowPlayingPill.vue", () => ({
|
||||
default: { template: "<div />" },
|
||||
}));
|
||||
|
||||
function scrollWindowTo(y: number) {
|
||||
document.documentElement.scrollTop = y;
|
||||
window.dispatchEvent(new Event("scroll"));
|
||||
}
|
||||
|
||||
function mountNav() {
|
||||
return mount(AppNav, {
|
||||
global: { stubs: { RouterLink: true, RImg: true, RSliderBtnGroup: true } },
|
||||
});
|
||||
}
|
||||
|
||||
const SCROLLED = "r-v2-nav-bar--scrolled";
|
||||
|
||||
enableAutoUnmount(afterEach);
|
||||
|
||||
describe("AppNav glass", () => {
|
||||
afterEach(() => {
|
||||
document.documentElement.scrollTop = 0;
|
||||
});
|
||||
|
||||
it("turns to glass once the window scrolls past the threshold", async () => {
|
||||
const wrapper = mountNav();
|
||||
expect(wrapper.classes()).not.toContain(SCROLLED);
|
||||
|
||||
scrollWindowTo(40);
|
||||
await nextTick();
|
||||
expect(wrapper.classes()).toContain(SCROLLED);
|
||||
|
||||
scrollWindowTo(0);
|
||||
await nextTick();
|
||||
expect(wrapper.classes()).not.toContain(SCROLLED);
|
||||
});
|
||||
|
||||
it("stays clear at a scroll within the threshold", async () => {
|
||||
const wrapper = mountNav();
|
||||
|
||||
scrollWindowTo(2);
|
||||
await nextTick();
|
||||
|
||||
expect(wrapper.classes()).not.toContain(SCROLLED);
|
||||
});
|
||||
|
||||
it("starts as glass when mounted on an already-scrolled page", async () => {
|
||||
document.documentElement.scrollTop = 40;
|
||||
const wrapper = mountNav();
|
||||
await nextTick();
|
||||
|
||||
expect(wrapper.classes()).toContain(SCROLLED);
|
||||
});
|
||||
});
|
||||
@@ -11,7 +11,8 @@
|
||||
// destinations: they live in the user menu's Library group, keeping the
|
||||
// primary nav focused on browsing destinations.
|
||||
import { RSliderBtnGroup, RImg } from "@v2/lib";
|
||||
import { computed, onBeforeUnmount, onMounted, ref } from "vue";
|
||||
import { useWindowScroll } from "@vueuse/core";
|
||||
import { computed } from "vue";
|
||||
import { useI18n } from "vue-i18n";
|
||||
import ScanningIndicator from "@/v2/components/AppShell/ScanningIndicator.vue";
|
||||
import UserMenu from "@/v2/components/AppShell/UserMenu.vue";
|
||||
@@ -35,21 +36,10 @@ const { destinations: tabs, activeId: activeTab } = useNavDestinations();
|
||||
// (transitioning `backdrop-filter` directly kept the blur layer alive
|
||||
// and any hover repaint nearby would flash it).
|
||||
const { innerScrolled, innerGlass, handoff, threshold } = useNavGlass();
|
||||
const windowScrolled = ref(false);
|
||||
const scrolled = computed(() => windowScrolled.value || innerScrolled.value);
|
||||
|
||||
function onScroll() {
|
||||
windowScrolled.value = window.scrollY > threshold;
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
window.addEventListener("scroll", onScroll, { passive: true });
|
||||
onScroll();
|
||||
});
|
||||
|
||||
onBeforeUnmount(() => {
|
||||
window.removeEventListener("scroll", onScroll);
|
||||
});
|
||||
const { y: windowScrollY } = useWindowScroll();
|
||||
const scrolled = computed(
|
||||
() => windowScrollY.value > threshold || innerScrolled.value,
|
||||
);
|
||||
</script>
|
||||
|
||||
<template>
|
||||
|
||||
Reference in New Issue
Block a user