mirror of
https://github.com/rommapp/romm.git
synced 2026-12-02 18:18:47 +01:00
feat(frontend): show compact mobile page titles after scrolling (#5553)
This commit is contained in:
@@ -20,10 +20,12 @@ import NowPlayingPill from "@/components/Soundtrack/NowPlayingPill.vue";
|
||||
import { useBreakpoint } from "@/composables/useBreakpoint";
|
||||
import { useNavDestinations } from "@/composables/useNavDestinations";
|
||||
import { useNavGlass } from "@/composables/useNavGlass";
|
||||
import { useNavTitle } from "@/composables/useNavTitle";
|
||||
import { RSegmentedControl, RImage } from "@/lib";
|
||||
|
||||
defineOptions({ inheritAttrs: false });
|
||||
|
||||
const { navEl, collapsedTitle } = useNavTitle();
|
||||
const { t } = useI18n();
|
||||
const { smAndDown } = useBreakpoint();
|
||||
|
||||
@@ -45,6 +47,7 @@ const scrolled = computed(
|
||||
|
||||
<template>
|
||||
<header
|
||||
ref="navEl"
|
||||
class="r-nav-bar"
|
||||
:class="{
|
||||
'r-nav-bar--scrolled': scrolled,
|
||||
@@ -70,6 +73,14 @@ const scrolled = computed(
|
||||
/>
|
||||
</router-link>
|
||||
|
||||
<span
|
||||
v-if="smAndDown"
|
||||
class="r-nav__title"
|
||||
:class="{ 'r-nav__title--visible': collapsedTitle }"
|
||||
aria-hidden="true"
|
||||
>{{ collapsedTitle }}</span
|
||||
>
|
||||
|
||||
<div class="r-nav__center">
|
||||
<RSegmentedControl
|
||||
:model-value="activeTab"
|
||||
@@ -203,17 +214,9 @@ const scrolled = computed(
|
||||
justify-self: end;
|
||||
}
|
||||
|
||||
/* On sm-and-down (phones + small tablets / landscape, <960px) the four
|
||||
primary destinations move to the bottom tab bar (BottomNav), so the
|
||||
centre pill is dropped from the top nav and the bar keeps only the
|
||||
logo (far left) + user cluster (far right). With just those two
|
||||
children, collapse to a 2-column grid so the cluster tracks the right
|
||||
edge; the default `1fr auto 1fr` would auto-place it into the now-
|
||||
empty centre column instead of the trailing one. The gutter follows
|
||||
`--r-row-pad` (20px sm, 14px xs) so logo + user line up with the Home
|
||||
sections' content edge. */
|
||||
/* Primary destinations move to BottomNav on phones and small tablets. */
|
||||
html[data-bp~="sm-and-down"] .r-nav {
|
||||
grid-template-columns: 1fr auto;
|
||||
grid-template-columns: auto minmax(0, 1fr) auto;
|
||||
}
|
||||
html[data-bp~="sm-and-down"] .r-nav__center {
|
||||
display: none;
|
||||
@@ -227,4 +230,26 @@ html[data-bp~="xs"] .r-nav__logo-word {
|
||||
html[data-bp~="xs"] .r-nav__right {
|
||||
gap: var(--r-space-2);
|
||||
}
|
||||
|
||||
.r-nav__title {
|
||||
min-width: 0;
|
||||
padding: 0 var(--r-space-3);
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
text-align: center;
|
||||
color: var(--r-color-fg-heading);
|
||||
font-size: var(--r-font-size-lg);
|
||||
font-weight: var(--r-font-weight-bold);
|
||||
opacity: 0;
|
||||
transition: opacity var(--r-motion-med) var(--r-motion-ease-out);
|
||||
}
|
||||
.r-nav__title--visible {
|
||||
opacity: 1;
|
||||
}
|
||||
@media (prefers-reduced-motion: reduce) {
|
||||
.r-nav__title {
|
||||
transition: none;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
|
||||
@@ -15,10 +15,12 @@
|
||||
// (Platform / Collection / Search) get the same separator regardless
|
||||
// of which header they render. The InfoPanel just provides the inner
|
||||
// content and its own padding for breathing.
|
||||
import { ref } from "vue";
|
||||
import { useCollapsingTitle } from "@/composables/useNavTitle";
|
||||
|
||||
defineOptions({ inheritAttrs: false });
|
||||
|
||||
withDefaults(
|
||||
const props = withDefaults(
|
||||
defineProps<{
|
||||
title?: string | undefined;
|
||||
}>(),
|
||||
@@ -26,6 +28,8 @@ withDefaults(
|
||||
title: undefined,
|
||||
},
|
||||
);
|
||||
const headingEl = ref<HTMLElement | null>(null);
|
||||
useCollapsingTitle(headingEl, () => props.title);
|
||||
</script>
|
||||
|
||||
<template>
|
||||
@@ -37,7 +41,7 @@ withDefaults(
|
||||
<div v-if="$slots.eyebrow" class="info-panel__eyebrow">
|
||||
<slot name="eyebrow" />
|
||||
</div>
|
||||
<h1 class="info-panel__title">
|
||||
<h1 ref="headingEl" class="info-panel__title">
|
||||
<slot name="title">{{ title }}</slot>
|
||||
</h1>
|
||||
<div v-if="$slots.tags" class="info-panel__tags">
|
||||
|
||||
@@ -10,7 +10,9 @@
|
||||
// shell paints the divider between hero and toolbar so the three
|
||||
// gallery views (Platform / Collection / Search) share one separator
|
||||
// regardless of which header sits above it.
|
||||
import { ref } from "vue";
|
||||
import { useAnimatedNumber } from "@/composables/useAnimatedNumber";
|
||||
import { useCollapsingTitle } from "@/composables/useNavTitle";
|
||||
import { RTag } from "@/lib";
|
||||
|
||||
defineOptions({ inheritAttrs: false });
|
||||
@@ -26,13 +28,15 @@ const props = withDefaults(
|
||||
// A numeric count rolls up to its value; a string one (a range, a label) is
|
||||
// printed as it comes.
|
||||
const countText = useAnimatedNumber(() => props.count);
|
||||
const headingEl = ref<HTMLElement | null>(null);
|
||||
useCollapsingTitle(headingEl, () => props.title);
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<header v-bind="$attrs" class="page-header">
|
||||
<div class="page-header__title-wrap">
|
||||
<slot name="prepend" />
|
||||
<h1 class="page-header__title">
|
||||
<h1 ref="headingEl" class="page-header__title">
|
||||
{{ title }}
|
||||
</h1>
|
||||
<slot name="count">
|
||||
|
||||
74
frontend/src/composables/useNavTitle/index.test.ts
Normal file
74
frontend/src/composables/useNavTitle/index.test.ts
Normal file
@@ -0,0 +1,74 @@
|
||||
import { mount } from "@vue/test-utils";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { defineComponent, h, nextTick, ref } from "vue";
|
||||
import { useCollapsingTitle, useNavTitle } from "./index";
|
||||
|
||||
const { notify } = vi.hoisted(() => ({
|
||||
notify: { current: (_entries: Partial<IntersectionObserverEntry>[]) => {} },
|
||||
}));
|
||||
vi.mock("@vueuse/core", () => ({
|
||||
useElementSize: () => ({ height: ref(58) }),
|
||||
useIntersectionObserver: (
|
||||
_target: unknown,
|
||||
callback: typeof notify.current,
|
||||
) => {
|
||||
notify.current = callback;
|
||||
},
|
||||
}));
|
||||
|
||||
function setup(title = ref("Search")) {
|
||||
const wrapper = mount(
|
||||
defineComponent({
|
||||
setup() {
|
||||
useCollapsingTitle(ref(null), title);
|
||||
return () => h("h1", title.value);
|
||||
},
|
||||
}),
|
||||
);
|
||||
return { wrapper, title };
|
||||
}
|
||||
|
||||
async function observe(bottom: number, isIntersecting = false) {
|
||||
notify.current([
|
||||
{
|
||||
isIntersecting,
|
||||
boundingClientRect: new DOMRect(0, bottom - 40, 100, 40),
|
||||
},
|
||||
]);
|
||||
await nextTick();
|
||||
}
|
||||
|
||||
describe("collapsing navigation title", () => {
|
||||
it("appears only after the heading passes above the navigation bar", async () => {
|
||||
const { wrapper } = setup();
|
||||
const { collapsedTitle } = useNavTitle();
|
||||
expect(collapsedTitle.value).toBe("");
|
||||
await observe(700);
|
||||
expect(collapsedTitle.value).toBe("");
|
||||
await observe(58);
|
||||
expect(collapsedTitle.value).toBe("Search");
|
||||
await observe(90, true);
|
||||
expect(collapsedTitle.value).toBe("");
|
||||
wrapper.unmount();
|
||||
});
|
||||
|
||||
it("updates translated or loaded titles and clears on leaving the page", async () => {
|
||||
const { wrapper, title } = setup();
|
||||
await observe(30);
|
||||
title.value = "Collections";
|
||||
await nextTick();
|
||||
expect(useNavTitle().collapsedTitle.value).toBe("Collections");
|
||||
wrapper.unmount();
|
||||
expect(useNavTitle().collapsedTitle.value).toBe("");
|
||||
});
|
||||
|
||||
it("does not let an outgoing page clear the incoming title", async () => {
|
||||
const old = setup();
|
||||
await observe(30);
|
||||
const incoming = setup(ref("Platforms"));
|
||||
await observe(30);
|
||||
old.wrapper.unmount();
|
||||
expect(useNavTitle().collapsedTitle.value).toBe("Platforms");
|
||||
incoming.wrapper.unmount();
|
||||
});
|
||||
});
|
||||
59
frontend/src/composables/useNavTitle/index.ts
Normal file
59
frontend/src/composables/useNavTitle/index.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
import { useElementSize, useIntersectionObserver } from "@vueuse/core";
|
||||
import {
|
||||
computed,
|
||||
onScopeDispose,
|
||||
ref,
|
||||
shallowRef,
|
||||
toValue,
|
||||
watch,
|
||||
type MaybeRefOrGetter,
|
||||
type Ref,
|
||||
} from "vue";
|
||||
|
||||
const navEl = shallowRef<HTMLElement | null>(null);
|
||||
const collapsedTitle = ref("");
|
||||
let owner: symbol | undefined;
|
||||
|
||||
export function useNavTitle() {
|
||||
return { navEl, collapsedTitle };
|
||||
}
|
||||
|
||||
export function useCollapsingTitle(
|
||||
heading: Ref<HTMLElement | null>,
|
||||
title: MaybeRefOrGetter<string | undefined>,
|
||||
) {
|
||||
const id = Symbol();
|
||||
owner = id;
|
||||
collapsedTitle.value = "";
|
||||
const { height } = useElementSize(navEl, undefined, { box: "border-box" });
|
||||
const hiddenAbove = ref(false);
|
||||
const rootMargin = computed(() => `-${height.value}px 0px 0px 0px`);
|
||||
useIntersectionObserver(
|
||||
heading,
|
||||
([entry]) => {
|
||||
if (entry)
|
||||
hiddenAbove.value =
|
||||
!entry.isIntersecting &&
|
||||
entry.boundingClientRect.bottom <= height.value;
|
||||
},
|
||||
{ rootMargin },
|
||||
);
|
||||
|
||||
// Keep the compact label current when metadata or the language changes.
|
||||
const label = computed(() =>
|
||||
hiddenAbove.value ? (toValue(title) ?? "") : "",
|
||||
);
|
||||
watch(
|
||||
label,
|
||||
(value) => {
|
||||
if (owner === id) collapsedTitle.value = value;
|
||||
},
|
||||
{ immediate: true },
|
||||
);
|
||||
onScopeDispose(() => {
|
||||
if (owner === id) {
|
||||
owner = undefined;
|
||||
collapsedTitle.value = "";
|
||||
}
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user