Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

Split scroll to top button owners

rcourtmanad89bc242b38
diff --git a/docs/release-control/v6/internal/subsystems/frontend-primitives.md b/docs/release-control/v6/internal/subsystems/frontend-primitives.md
index 6543ec57ae..cf2bccde44 100644
--- a/docs/release-control/v6/internal/subsystems/frontend-primitives.md
+++ b/docs/release-control/v6/internal/subsystems/frontend-primitives.md
@@ -246,6 +246,15 @@ container-focus runtime, and tag add/remove/backspace orchestration, and
placeholder policy, remove-title copy, and canonical next-tag derivation.
Future tag-input work should extend those owners instead of pushing DOM reach-in
or tag-mutation policy back into the shell.
+The shared scroll-to-top button now follows that same owner split.
+`frontend-modern/src/components/shared/ScrollToTopButton.tsx` stays the render
+shell, `frontend-modern/src/components/shared/useScrollToTopButtonState.ts`
+owns scroll-listener lifecycle, visible state, and smooth-scroll runtime, and
+`frontend-modern/src/components/shared/scrollToTopButtonModel.ts` owns
+scrollable-ancestor discovery, visibility threshold policy, aria label, and
+button class policy. Future scroll-to-top work should extend those owners
+instead of pushing scroll-container discovery or listener lifecycle back into
+the shell.
The shared dialog now follows that same owner split.
`frontend-modern/src/components/shared/Dialog.tsx` stays the render shell,
`frontend-modern/src/components/shared/useDialogState.ts` owns focus trap,
diff --git a/frontend-modern/src/components/shared/ScrollToTopButton.test.tsx b/frontend-modern/src/components/shared/ScrollToTopButton.test.tsx
new file mode 100644
index 0000000000..f08adf96ed
--- /dev/null
+++ b/frontend-modern/src/components/shared/ScrollToTopButton.test.tsx
@@ -0,0 +1,57 @@
+import { cleanup, fireEvent, render, screen } from '@solidjs/testing-library';
+import { afterEach, describe, expect, it, vi } from 'vitest';
+import { ScrollToTopButton } from './ScrollToTopButton';
+import scrollToTopButtonSource from './ScrollToTopButton.tsx?raw';
+import scrollToTopButtonModelSource from './scrollToTopButtonModel.ts?raw';
+import scrollToTopButtonStateSource from './useScrollToTopButtonState.ts?raw';
+
+describe('ScrollToTopButton', () => {
+ afterEach(() => {
+ cleanup();
+ });
+
+ it('keeps scroll-to-top button on shell, runtime, and model owners', () => {
+ expect(scrollToTopButtonSource).toContain('useScrollToTopButtonState');
+ expect(scrollToTopButtonSource).toContain('SCROLL_TO_TOP_BUTTON_ARIA_LABEL');
+ expect(scrollToTopButtonSource).toContain('getScrollToTopButtonClass');
+ expect(scrollToTopButtonSource).not.toContain('createSignal');
+ expect(scrollToTopButtonSource).not.toContain('onMount');
+ expect(scrollToTopButtonSource).not.toContain('scrollHeight');
+ expect(scrollToTopButtonSource).not.toContain('SCROLL_THRESHOLD');
+
+ expect(scrollToTopButtonStateSource).toContain('export function useScrollToTopButtonState');
+ expect(scrollToTopButtonStateSource).toContain('createSignal');
+ expect(scrollToTopButtonStateSource).toContain('onMount');
+ expect(scrollToTopButtonStateSource).toContain('addEventListener');
+ expect(scrollToTopButtonStateSource).toContain('scrollTo({ top: 0, behavior: \'smooth\' })');
+ expect(scrollToTopButtonStateSource).toContain('findNearestScrollableAncestor');
+
+ expect(scrollToTopButtonModelSource).toContain('SCROLL_TO_TOP_BUTTON_THRESHOLD');
+ expect(scrollToTopButtonModelSource).toContain('SCROLL_TO_TOP_BUTTON_ARIA_LABEL');
+ expect(scrollToTopButtonModelSource).toContain('findNearestScrollableAncestor');
+ expect(scrollToTopButtonModelSource).toContain('isScrollToTopButtonVisible');
+ expect(scrollToTopButtonModelSource).toContain('getScrollToTopButtonClass');
+ });
+
+ it('shows after scrolling past the threshold and scrolls the container to top', async () => {
+ const scroller = document.createElement('div');
+ scroller.style.overflowY = 'auto';
+ Object.defineProperty(scroller, 'scrollHeight', { value: 1000, configurable: true });
+ Object.defineProperty(scroller, 'clientHeight', { value: 200, configurable: true });
+ Object.defineProperty(scroller, 'scrollTop', { value: 0, writable: true, configurable: true });
+ scroller.scrollTo = vi.fn();
+ document.body.appendChild(scroller);
+
+ render(() => <ScrollToTopButton />, { container: scroller });
+
+ const button = screen.getByRole('button', { name: 'Scroll to top' });
+ expect(button.className).toContain('pointer-events-none');
+
+ scroller.scrollTop = 500;
+ fireEvent.scroll(scroller);
+ expect(button.className).toContain('opacity-100');
+
+ fireEvent.click(button);
+ expect(scroller.scrollTo).toHaveBeenCalledWith({ top: 0, behavior: 'smooth' });
+ });
+});
diff --git a/frontend-modern/src/components/shared/ScrollToTopButton.tsx b/frontend-modern/src/components/shared/ScrollToTopButton.tsx
index 817a1574c6..f0721f9367 100644
--- a/frontend-modern/src/components/shared/ScrollToTopButton.tsx
+++ b/frontend-modern/src/components/shared/ScrollToTopButton.tsx
@@ -1,63 +1,21 @@
-import { createSignal, onCleanup, onMount } from 'solid-js';
import ArrowUpIcon from 'lucide-solid/icons/arrow-up';
+import {
+ SCROLL_TO_TOP_BUTTON_ARIA_LABEL,
+ getScrollToTopButtonClass,
+} from '@/components/shared/scrollToTopButtonModel';
+import { useScrollToTopButtonState } from '@/components/shared/useScrollToTopButtonState';
-const SCROLL_THRESHOLD = 400;
-
-/**
- * Floating "back to top" button that appears after scrolling down.
- * Auto-discovers its nearest scrollable ancestor so it works regardless
- * of whether the page uses window scroll or a scrollable div.
- */
export function ScrollToTopButton() {
- const [visible, setVisible] = createSignal(false);
- let sentinel: HTMLDivElement | undefined;
- let scroller: HTMLElement | null = null;
-
- const findScroller = (): HTMLElement | null => {
- let el: HTMLElement | null = sentinel ?? null;
- while (el) {
- const { overflowY } = getComputedStyle(el);
- if ((overflowY === 'auto' || overflowY === 'scroll') && el.scrollHeight > el.clientHeight) {
- return el;
- }
- el = el.parentElement;
- }
- return null;
- };
-
- const onScroll = () => {
- if (!scroller) return;
- setVisible(scroller.scrollTop > SCROLL_THRESHOLD);
- };
-
- const scrollToTop = () => {
- scroller?.scrollTo({ top: 0, behavior: 'smooth' });
- };
-
- onMount(() => {
- scroller = findScroller();
- if (scroller) {
- scroller.addEventListener('scroll', onScroll, { passive: true });
- onScroll();
- }
- });
-
- onCleanup(() => {
- if (scroller) {
- scroller.removeEventListener('scroll', onScroll);
- }
- });
+ const state = useScrollToTopButtonState();
return (
<>
- <div ref={sentinel} class="hidden" />
+ <div ref={state.setSentinelRef} class="hidden" />
<button
type="button"
- onClick={scrollToTop}
- class={`fixed bottom-6 right-6 z-30 flex h-9 w-9 items-center justify-center rounded-full bg-surface text-base-content shadow-sm transition-all duration-200 hover:bg-surface-hover border border-border md:bottom-6 bottom-20 ${
- visible() ? 'opacity-100 translate-y-0' : 'opacity-0 translate-y-2 pointer-events-none'
- }`}
- aria-label="Scroll to top"
+ onClick={state.scrollToTop}
+ class={getScrollToTopButtonClass(state.visible())}
+ aria-label={SCROLL_TO_TOP_BUTTON_ARIA_LABEL}
>
<ArrowUpIcon class="h-4 w-4" />
</button>
diff --git a/frontend-modern/src/components/shared/SharedPrimitives.guardrails.test.ts b/frontend-modern/src/components/shared/SharedPrimitives.guardrails.test.ts
index 83539fa120..bd580994bd 100644
--- a/frontend-modern/src/components/shared/SharedPrimitives.guardrails.test.ts
+++ b/frontend-modern/src/components/shared/SharedPrimitives.guardrails.test.ts
@@ -37,6 +37,8 @@ import searchFieldSource from '@/components/shared/SearchField.tsx?raw';
import searchFieldModelSource from '@/components/shared/searchFieldModel.ts?raw';
import searchInputSource from '@/components/shared/SearchInput.tsx?raw';
import searchInputModelSource from '@/components/shared/searchInputModel.ts?raw';
+import scrollToTopButtonSource from '@/components/shared/ScrollToTopButton.tsx?raw';
+import scrollToTopButtonModelSource from '@/components/shared/scrollToTopButtonModel.ts?raw';
import searchTipsPopoverSource from '@/components/shared/SearchTipsPopover.tsx?raw';
import searchTipsPopoverModelSource from '@/components/shared/searchTipsPopoverModel.ts?raw';
import tooltipSource from '@/components/shared/Tooltip.tsx?raw';
@@ -71,6 +73,7 @@ import pulseDataGridStateSource from '@/components/shared/usePulseDataGridState.
import whatsNewModalStateSource from '@/components/shared/useWhatsNewModalState.ts?raw';
import searchFieldStateSource from '@/components/shared/useSearchFieldState.ts?raw';
import searchInputStateSource from '@/components/shared/useSearchInputState.ts?raw';
+import scrollToTopButtonStateSource from '@/components/shared/useScrollToTopButtonState.ts?raw';
import searchTipsPopoverStateSource from '@/components/shared/useSearchTipsPopoverState.ts?raw';
import tooltipStateSource from '@/components/shared/useTooltipState.ts?raw';
import trialBannerStateSource from '@/components/shared/useTrialBannerState.ts?raw';
@@ -255,6 +258,29 @@ describe('shared primitive guardrails', () => {
expect(tagInputModelSource).toContain('getTagInputRemoveTitle');
});
+ it('keeps scroll-to-top button on shell, runtime, and model owners', () => {
+ expect(scrollToTopButtonSource).toContain('useScrollToTopButtonState');
+ expect(scrollToTopButtonSource).toContain('SCROLL_TO_TOP_BUTTON_ARIA_LABEL');
+ expect(scrollToTopButtonSource).toContain('getScrollToTopButtonClass');
+ expect(scrollToTopButtonSource).not.toContain('createSignal');
+ expect(scrollToTopButtonSource).not.toContain('onMount');
+ expect(scrollToTopButtonSource).not.toContain('scrollHeight');
+ expect(scrollToTopButtonSource).not.toContain('SCROLL_THRESHOLD');
+
+ expect(scrollToTopButtonStateSource).toContain('export function useScrollToTopButtonState');
+ expect(scrollToTopButtonStateSource).toContain('createSignal');
+ expect(scrollToTopButtonStateSource).toContain('onMount');
+ expect(scrollToTopButtonStateSource).toContain('addEventListener');
+ expect(scrollToTopButtonStateSource).toContain("scrollTo({ top: 0, behavior: 'smooth' })");
+ expect(scrollToTopButtonStateSource).toContain('findNearestScrollableAncestor');
+
+ expect(scrollToTopButtonModelSource).toContain('SCROLL_TO_TOP_BUTTON_THRESHOLD');
+ expect(scrollToTopButtonModelSource).toContain('SCROLL_TO_TOP_BUTTON_ARIA_LABEL');
+ expect(scrollToTopButtonModelSource).toContain('findNearestScrollableAncestor');
+ expect(scrollToTopButtonModelSource).toContain('isScrollToTopButtonVisible');
+ expect(scrollToTopButtonModelSource).toContain('getScrollToTopButtonClass');
+ });
+
it('routes settings info callouts through CalloutCard', () => {
expect(calloutCardSource).toContain(
"type CalloutTone = 'danger' | 'info' | 'success' | 'warning'",
diff --git a/frontend-modern/src/components/shared/scrollToTopButtonModel.ts b/frontend-modern/src/components/shared/scrollToTopButtonModel.ts
new file mode 100644
index 0000000000..50da08d9aa
--- /dev/null
+++ b/frontend-modern/src/components/shared/scrollToTopButtonModel.ts
@@ -0,0 +1,31 @@
+export const SCROLL_TO_TOP_BUTTON_THRESHOLD = 400;
+export const SCROLL_TO_TOP_BUTTON_ARIA_LABEL = 'Scroll to top';
+export const SCROLL_TO_TOP_BUTTON_BASE_CLASS =
+ 'fixed bottom-6 right-6 z-30 flex h-9 w-9 items-center justify-center rounded-full bg-surface text-base-content shadow-sm transition-all duration-200 hover:bg-surface-hover border border-border md:bottom-6 bottom-20';
+export const SCROLL_TO_TOP_BUTTON_VISIBLE_CLASS = 'opacity-100 translate-y-0';
+export const SCROLL_TO_TOP_BUTTON_HIDDEN_CLASS = 'opacity-0 translate-y-2 pointer-events-none';
+
+export function findNearestScrollableAncestor(
+ sentinel: HTMLElement | undefined,
+ getStyle: (element: Element) => CSSStyleDeclaration = getComputedStyle,
+): HTMLElement | null {
+ let element: HTMLElement | null = sentinel ?? null;
+ while (element) {
+ const { overflowY } = getStyle(element);
+ if ((overflowY === 'auto' || overflowY === 'scroll') && element.scrollHeight > element.clientHeight) {
+ return element;
+ }
+ element = element.parentElement;
+ }
+ return null;
+}
+
+export function isScrollToTopButtonVisible(scrollTop: number): boolean {
+ return scrollTop > SCROLL_TO_TOP_BUTTON_THRESHOLD;
+}
+
+export function getScrollToTopButtonClass(visible: boolean): string {
+ return `${SCROLL_TO_TOP_BUTTON_BASE_CLASS} ${
+ visible ? SCROLL_TO_TOP_BUTTON_VISIBLE_CLASS : SCROLL_TO_TOP_BUTTON_HIDDEN_CLASS
+ }`;
+}
diff --git a/frontend-modern/src/components/shared/useScrollToTopButtonState.ts b/frontend-modern/src/components/shared/useScrollToTopButtonState.ts
new file mode 100644
index 0000000000..94fba0b970
--- /dev/null
+++ b/frontend-modern/src/components/shared/useScrollToTopButtonState.ts
@@ -0,0 +1,42 @@
+import { createSignal, onCleanup, onMount } from 'solid-js';
+import {
+ findNearestScrollableAncestor,
+ isScrollToTopButtonVisible,
+} from '@/components/shared/scrollToTopButtonModel';
+
+export function useScrollToTopButtonState() {
+ const [visible, setVisible] = createSignal(false);
+ let sentinelRef: HTMLDivElement | undefined;
+ let scroller: HTMLElement | null = null;
+
+ const syncVisibility = () => {
+ if (!scroller) {
+ return;
+ }
+ setVisible(isScrollToTopButtonVisible(scroller.scrollTop));
+ };
+
+ onMount(() => {
+ scroller = findNearestScrollableAncestor(sentinelRef);
+ if (!scroller) {
+ return;
+ }
+
+ scroller.addEventListener('scroll', syncVisibility, { passive: true });
+ syncVisibility();
+ });
+
+ onCleanup(() => {
+ scroller?.removeEventListener('scroll', syncVisibility);
+ });
+
+ return {
+ scrollToTop: () => {
+ scroller?.scrollTo({ top: 0, behavior: 'smooth' });
+ },
+ setSentinelRef: (element: HTMLDivElement) => {
+ sentinelRef = element;
+ },
+ visible,
+ };
+}
diff --git a/frontend-modern/src/utils/__tests__/frontendResourceTypeBoundaries.test.ts b/frontend-modern/src/utils/__tests__/frontendResourceTypeBoundaries.test.ts
index c57bd5048e..22e6c7a5a9 100644
--- a/frontend-modern/src/utils/__tests__/frontendResourceTypeBoundaries.test.ts
+++ b/frontend-modern/src/utils/__tests__/frontendResourceTypeBoundaries.test.ts
@@ -42,6 +42,8 @@ import searchFieldSource from '@/components/shared/SearchField.tsx?raw';
import searchFieldModelSource from '@/components/shared/searchFieldModel.ts?raw';
import searchInputSource from '@/components/shared/SearchInput.tsx?raw';
import searchInputModelSource from '@/components/shared/searchInputModel.ts?raw';
+import scrollToTopButtonSource from '@/components/shared/ScrollToTopButton.tsx?raw';
+import scrollToTopButtonModelSource from '@/components/shared/scrollToTopButtonModel.ts?raw';
import searchTipsPopoverSource from '@/components/shared/SearchTipsPopover.tsx?raw';
import searchTipsPopoverModelSource from '@/components/shared/searchTipsPopoverModel.ts?raw';
import tooltipSource from '@/components/shared/Tooltip.tsx?raw';
@@ -71,6 +73,7 @@ import pulseDataGridStateSource from '@/components/shared/usePulseDataGridState.
import whatsNewModalStateSource from '@/components/shared/useWhatsNewModalState.ts?raw';
import searchFieldStateSource from '@/components/shared/useSearchFieldState.ts?raw';
import searchInputStateSource from '@/components/shared/useSearchInputState.ts?raw';
+import scrollToTopButtonStateSource from '@/components/shared/useScrollToTopButtonState.ts?raw';
import searchTipsPopoverStateSource from '@/components/shared/useSearchTipsPopoverState.ts?raw';
import tooltipStateSource from '@/components/shared/useTooltipState.ts?raw';
import trialBannerStateSource from '@/components/shared/useTrialBannerState.ts?raw';
@@ -2792,6 +2795,23 @@ describe('frontend resource type boundaries', () => {
expect(tagInputModelSource).toContain('getTagInputPlaceholder');
expect(tagInputModelSource).toContain('getNextTagsAfterRemove');
expect(tagInputModelSource).toContain('getTagInputRemoveTitle');
+ expect(scrollToTopButtonSource).toContain('useScrollToTopButtonState');
+ expect(scrollToTopButtonSource).toContain('SCROLL_TO_TOP_BUTTON_ARIA_LABEL');
+ expect(scrollToTopButtonSource).toContain('getScrollToTopButtonClass');
+ expect(scrollToTopButtonSource).not.toContain('createSignal');
+ expect(scrollToTopButtonSource).not.toContain('onMount');
+ expect(scrollToTopButtonSource).not.toContain('scrollHeight');
+ expect(scrollToTopButtonSource).not.toContain('SCROLL_THRESHOLD');
+ expect(scrollToTopButtonStateSource).toContain('createSignal');
+ expect(scrollToTopButtonStateSource).toContain('onMount');
+ expect(scrollToTopButtonStateSource).toContain('addEventListener');
+ expect(scrollToTopButtonStateSource).toContain("scrollTo({ top: 0, behavior: 'smooth' })");
+ expect(scrollToTopButtonStateSource).toContain('findNearestScrollableAncestor');
+ expect(scrollToTopButtonModelSource).toContain('SCROLL_TO_TOP_BUTTON_THRESHOLD');
+ expect(scrollToTopButtonModelSource).toContain('SCROLL_TO_TOP_BUTTON_ARIA_LABEL');
+ expect(scrollToTopButtonModelSource).toContain('findNearestScrollableAncestor');
+ expect(scrollToTopButtonModelSource).toContain('isScrollToTopButtonVisible');
+ expect(scrollToTopButtonModelSource).toContain('getScrollToTopButtonClass');
expect(monitoredSystemLimitWarningBannerSource).toContain(
'useMonitoredSystemLimitWarningBannerState',
);