feat(ui): overlay page scrollbar that takes no layout space
Continuous Integration / build_web (push) Failing after 1s
Continuous Integration / build (macos-latest) (push) Skipped
Continuous Integration / build (ubuntu-22.04) (push) Skipped
Continuous Integration / build (windows-11-arm) (push) Skipped
Continuous Integration / build (windows-latest) (push) Skipped
Continuous Integration / nightly_release (push) Skipped
Continuous Integration / build_web (push) Failing after 1s
Continuous Integration / build (macos-latest) (push) Skipped
Continuous Integration / build (ubuntu-22.04) (push) Skipped
Continuous Integration / build (windows-11-arm) (push) Skipped
Continuous Integration / build (windows-latest) (push) Skipped
Continuous Integration / nightly_release (push) Skipped
Hides the native page scrollbar and draws a fixed overlay thumb, so content no longer shifts when it appears. The thumb is driven by a CSS scroll timeline where supported, with a lightweight scroll-listener fallback, to keep scrolling smooth.
This commit is contained in:
1 parent
e39a5c70c0
commit
affbd00157
4 files changed
+327
-1
No files matched your search
@@ -0,0 +1,243 @@
|
|||||||
|
import React, { useEffect, useRef } from "react";
|
||||||
|
|
||||||
|
import "./overlayScrollbar.scss";
|
||||||
|
|
||||||
|
const MIN_THUMB_HEIGHT = 28;
|
||||||
|
const HIDE_DELAY = 1100;
|
||||||
|
const EDGE_ZONE = 28;
|
||||||
|
const SHOW_THROTTLE = 120;
|
||||||
|
|
||||||
|
type Metrics = {
|
||||||
|
maxScroll: number;
|
||||||
|
maxTop: number;
|
||||||
|
scrollable: boolean;
|
||||||
|
};
|
||||||
|
|
||||||
|
const getPageScroller = (): HTMLElement =>
|
||||||
|
(document.scrollingElement as HTMLElement | null) ?? document.documentElement;
|
||||||
|
|
||||||
|
const clamp = (value: number, min: number, max: number) =>
|
||||||
|
Math.min(Math.max(value, min), max);
|
||||||
|
|
||||||
|
// Where supported the thumb is driven by a CSS scroll timeline, so it moves
|
||||||
|
// with the compositor and never trails the page. Older engines fall back to
|
||||||
|
// the scroll listener below.
|
||||||
|
const supportsScrollTimeline =
|
||||||
|
typeof CSS !== "undefined" &&
|
||||||
|
CSS.supports("animation-timeline", "scroll(root block)");
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Overlay scrollbar for the page scroller. It sits on top of the content
|
||||||
|
* instead of taking layout space, so pages no longer shift when the native
|
||||||
|
* scrollbar appears or disappears.
|
||||||
|
*
|
||||||
|
* Measuring (track size, thumb size) happens only when the page or window
|
||||||
|
* size changes. Scrolling just paints the cached thumb position, which keeps
|
||||||
|
* the thumb moving with the page instead of lagging behind it.
|
||||||
|
*/
|
||||||
|
const OverlayScrollbar = () => {
|
||||||
|
const trackRef = useRef<HTMLDivElement>(null);
|
||||||
|
const thumbRef = useRef<HTMLDivElement>(null);
|
||||||
|
const metrics = useRef<Metrics>({ maxScroll: 0, maxTop: 0, scrollable: false });
|
||||||
|
const dragging = useRef(false);
|
||||||
|
const hideTimer = useRef<number | undefined>(undefined);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const track = trackRef.current;
|
||||||
|
const thumb = thumbRef.current;
|
||||||
|
if (!track || !thumb) return;
|
||||||
|
|
||||||
|
const scroller = getPageScroller();
|
||||||
|
document.documentElement.classList.add("has-overlay-scrollbar");
|
||||||
|
document.body.classList.add("has-overlay-scrollbar");
|
||||||
|
|
||||||
|
let frame = 0;
|
||||||
|
let lastShow = 0;
|
||||||
|
|
||||||
|
const setVisible = (visible: boolean) => {
|
||||||
|
const next = visible ? "true" : "false";
|
||||||
|
if (track.dataset.visible !== next) track.dataset.visible = next;
|
||||||
|
};
|
||||||
|
|
||||||
|
const scheduleHide = () => {
|
||||||
|
if (dragging.current) return;
|
||||||
|
window.clearTimeout(hideTimer.current);
|
||||||
|
hideTimer.current = window.setTimeout(() => {
|
||||||
|
if (!dragging.current) setVisible(false);
|
||||||
|
}, HIDE_DELAY);
|
||||||
|
};
|
||||||
|
|
||||||
|
const show = () => {
|
||||||
|
if (!metrics.current.scrollable) return;
|
||||||
|
setVisible(true);
|
||||||
|
const now = Date.now();
|
||||||
|
if (now - lastShow > SHOW_THROTTLE) {
|
||||||
|
lastShow = now;
|
||||||
|
scheduleHide();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Cheap path: only reads scrollTop and writes a compositor-friendly
|
||||||
|
// transform. No layout reads, no track/thumb size writes.
|
||||||
|
const paint = () => {
|
||||||
|
if (supportsScrollTimeline) return;
|
||||||
|
const { scrollable, maxScroll, maxTop } = metrics.current;
|
||||||
|
if (!scrollable || maxScroll <= 0) return;
|
||||||
|
const top = clamp(scroller.scrollTop, 0, maxScroll);
|
||||||
|
const offset = (top / maxScroll) * maxTop;
|
||||||
|
thumb.style.transform = `translate3d(0, ${offset}px, 0)`;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Expensive path: runs on mount and when page/window geometry changes.
|
||||||
|
const measure = () => {
|
||||||
|
const header = document.querySelector("header");
|
||||||
|
const insetTop = Math.max(
|
||||||
|
0,
|
||||||
|
Math.round(header?.getBoundingClientRect().bottom ?? 0) + 4,
|
||||||
|
);
|
||||||
|
const trackHeight = Math.max(0, window.innerHeight - insetTop - 6);
|
||||||
|
const topStyle = `${insetTop}px`;
|
||||||
|
const heightStyle = `${trackHeight}px`;
|
||||||
|
if (track.style.top !== topStyle) track.style.top = topStyle;
|
||||||
|
if (track.style.height !== heightStyle) track.style.height = heightStyle;
|
||||||
|
|
||||||
|
const viewport = scroller.clientHeight;
|
||||||
|
const total = scroller.scrollHeight;
|
||||||
|
const scrollable = total - viewport > 1 && trackHeight > 0;
|
||||||
|
|
||||||
|
if (!scrollable) {
|
||||||
|
metrics.current = { maxScroll: 0, maxTop: 0, scrollable: false };
|
||||||
|
track.dataset.scrollable = "false";
|
||||||
|
setVisible(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const thumbHeight = Math.min(
|
||||||
|
trackHeight,
|
||||||
|
Math.max(
|
||||||
|
MIN_THUMB_HEIGHT,
|
||||||
|
Math.round((viewport / total) * trackHeight),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
const thumbHeightStyle = `${thumbHeight}px`;
|
||||||
|
if (thumb.style.height !== thumbHeightStyle) {
|
||||||
|
thumb.style.height = thumbHeightStyle;
|
||||||
|
}
|
||||||
|
|
||||||
|
const maxTop = trackHeight - thumbHeight;
|
||||||
|
metrics.current = {
|
||||||
|
maxScroll: total - viewport,
|
||||||
|
maxTop,
|
||||||
|
scrollable: true,
|
||||||
|
};
|
||||||
|
track.style.setProperty("--overlay-thumb-travel", `${maxTop}px`);
|
||||||
|
track.dataset.scrollable = "true";
|
||||||
|
paint();
|
||||||
|
};
|
||||||
|
|
||||||
|
const requestMeasure = () => {
|
||||||
|
if (frame) return;
|
||||||
|
frame = window.requestAnimationFrame(() => {
|
||||||
|
frame = 0;
|
||||||
|
measure();
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const onScroll = () => {
|
||||||
|
paint();
|
||||||
|
show();
|
||||||
|
};
|
||||||
|
|
||||||
|
const onResize = () => {
|
||||||
|
requestMeasure();
|
||||||
|
show();
|
||||||
|
};
|
||||||
|
|
||||||
|
const onPointerMove = (event: PointerEvent) => {
|
||||||
|
if (event.clientX >= window.innerWidth - EDGE_ZONE) show();
|
||||||
|
};
|
||||||
|
|
||||||
|
const onVisibilityChange = () => {
|
||||||
|
if (document.hidden) return;
|
||||||
|
measure();
|
||||||
|
show();
|
||||||
|
};
|
||||||
|
|
||||||
|
const onThumbPointerDown = (event: PointerEvent) => {
|
||||||
|
if (event.pointerType === "mouse" && event.button !== 0) return;
|
||||||
|
const { maxScroll, maxTop } = metrics.current;
|
||||||
|
if (maxScroll <= 0 || maxTop <= 0) return;
|
||||||
|
|
||||||
|
event.preventDefault();
|
||||||
|
dragging.current = true;
|
||||||
|
window.clearTimeout(hideTimer.current);
|
||||||
|
setVisible(true);
|
||||||
|
|
||||||
|
const startY = event.clientY;
|
||||||
|
const startScroll = scroller.scrollTop;
|
||||||
|
const ratio = maxScroll / maxTop;
|
||||||
|
|
||||||
|
const onMove = (moveEvent: PointerEvent) => {
|
||||||
|
scroller.scrollTop = startScroll + (moveEvent.clientY - startY) * ratio;
|
||||||
|
paint();
|
||||||
|
};
|
||||||
|
|
||||||
|
const onUp = () => {
|
||||||
|
dragging.current = false;
|
||||||
|
window.removeEventListener("pointermove", onMove);
|
||||||
|
window.removeEventListener("pointerup", onUp);
|
||||||
|
window.removeEventListener("pointercancel", onUp);
|
||||||
|
scheduleHide();
|
||||||
|
};
|
||||||
|
|
||||||
|
window.addEventListener("pointermove", onMove);
|
||||||
|
window.addEventListener("pointerup", onUp);
|
||||||
|
window.addEventListener("pointercancel", onUp);
|
||||||
|
};
|
||||||
|
|
||||||
|
measure();
|
||||||
|
show();
|
||||||
|
scheduleHide();
|
||||||
|
|
||||||
|
scroller.addEventListener("scroll", onScroll, { passive: true });
|
||||||
|
window.addEventListener("resize", onResize);
|
||||||
|
window.addEventListener("pointermove", onPointerMove, { passive: true });
|
||||||
|
document.addEventListener("visibilitychange", onVisibilityChange);
|
||||||
|
thumb.addEventListener("pointerdown", onThumbPointerDown);
|
||||||
|
|
||||||
|
const content = document.getElementById("root");
|
||||||
|
const resizeObserver = content ? new ResizeObserver(requestMeasure) : null;
|
||||||
|
if (content && resizeObserver) resizeObserver.observe(content);
|
||||||
|
|
||||||
|
const mutationObserver = new MutationObserver(requestMeasure);
|
||||||
|
mutationObserver.observe(document.body, { childList: true, subtree: true });
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
window.clearTimeout(hideTimer.current);
|
||||||
|
if (frame) window.cancelAnimationFrame(frame);
|
||||||
|
scroller.removeEventListener("scroll", onScroll);
|
||||||
|
window.removeEventListener("resize", onResize);
|
||||||
|
window.removeEventListener("pointermove", onPointerMove);
|
||||||
|
document.removeEventListener("visibilitychange", onVisibilityChange);
|
||||||
|
thumb.removeEventListener("pointerdown", onThumbPointerDown);
|
||||||
|
resizeObserver?.disconnect();
|
||||||
|
mutationObserver.disconnect();
|
||||||
|
document.documentElement.classList.remove("has-overlay-scrollbar");
|
||||||
|
document.body.classList.remove("has-overlay-scrollbar");
|
||||||
|
};
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
ref={trackRef}
|
||||||
|
className="overlay-scrollbar"
|
||||||
|
data-visible="false"
|
||||||
|
data-scrollable="false"
|
||||||
|
aria-hidden="true"
|
||||||
|
>
|
||||||
|
<div ref={thumbRef} className="overlay-scrollbar__thumb" />
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default OverlayScrollbar;
|
||||||
@@ -0,0 +1,82 @@
|
|||||||
|
// Hide the native page scrollbar only. Nested panels keep their own styling.
|
||||||
|
html.has-overlay-scrollbar,
|
||||||
|
body.has-overlay-scrollbar {
|
||||||
|
scrollbar-width: none;
|
||||||
|
-ms-overflow-style: none;
|
||||||
|
|
||||||
|
&::-webkit-scrollbar {
|
||||||
|
display: none;
|
||||||
|
width: 0;
|
||||||
|
height: 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
.overlay-scrollbar {
|
||||||
|
--overlay-thumb-travel: 0px;
|
||||||
|
|
||||||
|
position: fixed;
|
||||||
|
top: 70px;
|
||||||
|
right: 2px;
|
||||||
|
z-index: 95;
|
||||||
|
width: 12px;
|
||||||
|
height: calc(100vh - 76px);
|
||||||
|
pointer-events: none;
|
||||||
|
opacity: 0;
|
||||||
|
transition: opacity 0.2s ease;
|
||||||
|
|
||||||
|
&[data-scrollable="true"] {
|
||||||
|
opacity: 0.45;
|
||||||
|
}
|
||||||
|
|
||||||
|
&[data-visible="true"] {
|
||||||
|
opacity: 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
&__thumb {
|
||||||
|
position: absolute;
|
||||||
|
top: 0;
|
||||||
|
right: 3px;
|
||||||
|
width: 6px;
|
||||||
|
min-height: 28px;
|
||||||
|
border-radius: 999px;
|
||||||
|
background: rgb(255 255 255 / 0.28);
|
||||||
|
box-shadow: 0 0 0 1px rgb(0 0 0 / 0.18);
|
||||||
|
pointer-events: auto;
|
||||||
|
cursor: grab;
|
||||||
|
will-change: transform;
|
||||||
|
transition:
|
||||||
|
width 0.15s ease,
|
||||||
|
right 0.15s ease,
|
||||||
|
background-color 0.15s ease;
|
||||||
|
|
||||||
|
&:hover {
|
||||||
|
right: 2px;
|
||||||
|
width: 8px;
|
||||||
|
background: rgb(255 255 255 / 0.46);
|
||||||
|
}
|
||||||
|
|
||||||
|
&:active {
|
||||||
|
cursor: grabbing;
|
||||||
|
background: rgb(255 255 255 / 0.58);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Scroll-driven thumb for engines that support it: the compositor moves it in
|
||||||
|
// lockstep with the page, so scrolling stays smooth. The JS listener is the
|
||||||
|
// fallback for older engines.
|
||||||
|
@supports (animation-timeline: scroll(root block)) {
|
||||||
|
.overlay-scrollbar__thumb {
|
||||||
|
animation: overlay-scrollbar-thumb linear both;
|
||||||
|
animation-timeline: scroll(root block);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@keyframes overlay-scrollbar-thumb {
|
||||||
|
from {
|
||||||
|
transform: translate3d(0, 0, 0);
|
||||||
|
}
|
||||||
|
to {
|
||||||
|
transform: translate3d(0, var(--overlay-thumb-travel), 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -24,6 +24,7 @@ import { DndProvider } from "react-dnd";
|
|||||||
import { HTML5Backend } from "react-dnd-html5-backend";
|
import { HTML5Backend } from "react-dnd-html5-backend";
|
||||||
import Backdrop from "@/components/backdrop";
|
import Backdrop from "@/components/backdrop";
|
||||||
import { ErrorNotice } from "@/components/notices/errorNotice/errorNotice";
|
import { ErrorNotice } from "@/components/notices/errorNotice/errorNotice";
|
||||||
|
import OverlayScrollbar from "@/components/overlayScrollbar";
|
||||||
import RemoteControlDialog from "@/components/remoteControl";
|
import RemoteControlDialog from "@/components/remoteControl";
|
||||||
import RouterLoading from "@/components/routerLoading";
|
import RouterLoading from "@/components/routerLoading";
|
||||||
import Search from "@/components/search";
|
import Search from "@/components/search";
|
||||||
@@ -57,6 +58,7 @@ export const Route = createRootRouteWithContext<ApiContext>()({
|
|||||||
<RemoteControlDialog />
|
<RemoteControlDialog />
|
||||||
<AppBar />
|
<AppBar />
|
||||||
<AudioPlayer />
|
<AudioPlayer />
|
||||||
|
<OverlayScrollbar />
|
||||||
<Outlet />
|
<Outlet />
|
||||||
</SnackbarProvider>
|
</SnackbarProvider>
|
||||||
</ThemeProvider>
|
</ThemeProvider>
|
||||||
|
|||||||
@@ -171,7 +171,6 @@ h1 {
|
|||||||
box-sizing: border-box;
|
box-sizing: border-box;
|
||||||
min-height: 100vh;
|
min-height: 100vh;
|
||||||
padding: ($page-margin - 1.2em) $page-margin;
|
padding: ($page-margin - 1.2em) $page-margin;
|
||||||
scrollbar-gutter: stable;
|
|
||||||
&.padded-top {
|
&.padded-top {
|
||||||
padding-top: 4.4em;
|
padding-top: 4.4em;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user