fix(nav): put the prerelease badge beside the logo and never let the actions cover the tab pill

Both side columns keep their content width, so the pill gives way: it drops
its labels to icons when they no longer fit (measured, since plugins and
languages change the width) and scrolls only after that. The username is no
longer cut at 96px.
This commit is contained in:
Maurice
2026-10-14 10:45:09 +02:00
committed by Maurice
parent db531d2bef
commit b3ef13616f
4 changed files with 219 additions and 22 deletions
@@ -0,0 +1,109 @@
// FE-HOOK-COLLAPSE-001 to FE-HOOK-COLLAPSE-006
import { act, renderHook } from '@testing-library/react';
import { useCollapseOnOverflow } from './useCollapseOnOverflow';
function strip(scrollWidth: number, clientWidth: number): HTMLDivElement {
const el = document.createElement('div');
setWidths(el, scrollWidth, clientWidth);
return el;
}
function setWidths(el: HTMLElement, scrollWidth: number, clientWidth: number): void {
Object.defineProperty(el, 'scrollWidth', { value: scrollWidth, configurable: true });
Object.defineProperty(el, 'clientWidth', { value: clientWidth, configurable: true });
}
function resizeWindow(width: number): void {
act(() => {
window.innerWidth = width;
window.dispatchEvent(new Event('resize'));
});
}
function mount(el: HTMLDivElement, key = 'a|b|c') {
return renderHook(
({ k }) => {
const result = useCollapseOnOverflow<HTMLDivElement>(k);
result.ref.current = el;
return result;
},
{ initialProps: { k: key } }
);
}
const originalWidth = window.innerWidth;
afterEach(() => {
window.innerWidth = originalWidth;
});
describe('useCollapseOnOverflow', () => {
it('FE-HOOK-COLLAPSE-001: keeps the labels while the strip fits', () => {
window.innerWidth = 1600;
const { result } = mount(strip(500, 500));
resizeWindow(1500);
expect(result.current.collapsed).toBe(false);
});
it('FE-HOOK-COLLAPSE-002: collapses once the strip overflows', () => {
window.innerWidth = 1600;
const el = strip(500, 500);
const { result } = mount(el);
setWidths(el, 740, 640);
resizeWindow(1200);
expect(result.current.collapsed).toBe(true);
});
it('FE-HOOK-COLLAPSE-003: brings the labels back only once the window has the missing width', () => {
window.innerWidth = 1200;
const el = strip(740, 640);
const { result } = mount(el);
expect(result.current.collapsed).toBe(true);
// Collapsed, the strip fits by definition, so only the window width counts.
setWidths(el, 320, 320);
resizeWindow(1250);
expect(result.current.collapsed).toBe(true);
setWidths(el, 740, 740);
resizeWindow(1300);
expect(result.current.collapsed).toBe(false);
});
it('FE-HOOK-COLLAPSE-004: measures again when the set of items changes', () => {
window.innerWidth = 1200;
const el = strip(740, 640);
const { result, rerender } = mount(el);
expect(result.current.collapsed).toBe(true);
// One tab fewer: the labels fit at the same window width.
setWidths(el, 600, 600);
rerender({ k: 'a|b' });
expect(result.current.collapsed).toBe(false);
});
it('FE-HOOK-COLLAPSE-005: stops listening on unmount', () => {
const remove = vi.spyOn(window, 'removeEventListener');
const { unmount } = mount(strip(500, 500));
unmount();
expect(remove).toHaveBeenCalledWith('resize', expect.any(Function));
remove.mockRestore();
});
it('FE-HOOK-COLLAPSE-006: measures again when the strip itself changes size, a font or text size with it', () => {
let resized: (() => void) | null = null;
vi.stubGlobal(
'ResizeObserver',
class {
constructor(cb: () => void) {
resized = cb;
}
observe() {}
disconnect() {}
}
);
window.innerWidth = 1600;
const el = strip(500, 500);
const { result } = mount(el);
expect(result.current.collapsed).toBe(false);
setWidths(el, 700, 600);
act(() => resized?.());
expect(result.current.collapsed).toBe(true);
vi.unstubAllGlobals();
});
});
+54
View File
@@ -0,0 +1,54 @@
import { useLayoutEffect, useRef, useState } from 'react';
/**
* Tells a horizontal strip when to drop its labels and show icons only.
*
* A fixed breakpoint cannot do this for the navbar pill: how wide the labels
* are depends on how many addons and page plugins are enabled and on the
* language. So the strip is measured instead. When it overflows with labels,
* the viewport width it would need to fit them is remembered, and the labels
* come back once the window is at least that wide. Measuring the collapsed
* strip would not work, since without labels it always fits.
*
* `itemsKey` names the current set of items. When it changes the labels are
* shown and measured again. Besides a window resize, a change in the strip's
* own size (a web font arriving, a larger text size) is measured too.
*/
export function useCollapseOnOverflow<T extends HTMLElement = HTMLElement>(
itemsKey: string
): {
ref: React.RefObject<T | null>;
collapsed: boolean;
} {
const ref = useRef<T | null>(null);
const [state, setState] = useState<{ key: string; fitsFrom: number } | null>(null);
const fitsFrom = state?.key === itemsKey ? state.fitsFrom : null;
useLayoutEffect(() => {
const check = () => {
if (fitsFrom !== null) {
if (window.innerWidth >= fitsFrom) setState(null);
return;
}
const el = ref.current;
if (!el) return;
const overflow = el.scrollWidth - el.clientWidth;
if (overflow > 0) setState({ key: itemsKey, fitsFrom: window.innerWidth + overflow });
};
check();
window.addEventListener('resize', check);
const el = ref.current;
const observer = el && typeof ResizeObserver !== 'undefined' ? new ResizeObserver(check) : null;
if (observer && el) {
// The strip stops growing once it is squeezed; its items keep reporting.
observer.observe(el);
for (const child of Array.from(el.children)) observer.observe(child);
}
return () => {
window.removeEventListener('resize', check);
observer?.disconnect();
};
}, [itemsKey, fitsFrom]);
return { ref, collapsed: fitsFrom !== null };
}