Files
TREK/client/src/mobile/components/useActivePill.ts
T
Maurice b7951baf23 fix(ci): green the dev gates after the native and popup rounds
- RTL: the measured pills and centred controls use logical forms or a marked line
- i18n: the two native widget numbers are positions, not counts (NOT_PLURAL)
- Types: styleOverride on the lazy GL map, the location and shared-file payloads,
  the capabilities fallback, the transport sheet's stay metadata
- ESLint: latest-value refs move to layout effects, usePresence keeps the last
  open state in state, the booking import opens clean with a shared document
  kept in the list (the reset effect used to clear it again)
- Layers: the phone import sheet lives under mobile/ and reads through tripRepo;
  the offline host check moves to utils/tileUrl
- Sizes: plugin layer geometry, hardNavigate and the exit animations get
  modules of their own
- Tests follow the status radios, the stay chip overview, the dock + button
  for the first list and the field token; store registry lists the new stores
2026-11-06 18:24:25 +01:00

61 lines
2.4 KiB
TypeScript

import type { CSSProperties, RefObject } from 'react';
import { useLayoutEffect, useState } from 'react';
/**
* The pill that glides under the active item of a segmented control or a chip
* rail, the way a native segmented control moves its thumb instead of
* repainting two buttons.
*
* Measures the active item inside `trackRef` (the first match of `selector`)
* and hands back the pill's style: positioned by transform, so the glide runs
* on the compositor. The first placement does not animate, so a screen opens
* with the pill already in place rather than sliding in from the edge. The
* pill sits inside the track's scroll content, so a rail that scrolls carries
* it along.
*/
export function useActivePill(
/** The track, as a ref or as the element itself (a callback-ref'd rail). */
trackRef: RefObject<HTMLElement | null> | HTMLElement | null,
selector: string,
/** What moves the active item, as one value: a change re-measures the pill. */
key: string | number,
/** Follow the item across rows too (a grid of chips): the pill then also takes
* the item's top and height, and the caller leaves top/bottom unset. */
rows = false
): CSSProperties | null {
const [box, setBox] = useState<{ x: number; y: number; width: number; height: number; animate: boolean } | null>(
null
);
useLayoutEffect(() => {
const track = trackRef instanceof HTMLElement ? trackRef : (trackRef?.current ?? null);
if (!track) return;
const measure = () => {
const active = track.querySelector<HTMLElement>(selector);
if (!active) {
setBox(null);
return;
}
setBox((prev) => ({
x: active.offsetLeft,
y: active.offsetTop,
width: active.offsetWidth,
height: active.offsetHeight,
animate: prev !== null,
}));
};
measure();
if (typeof ResizeObserver === 'undefined') return;
const observer = new ResizeObserver(measure);
observer.observe(track);
return () => observer.disconnect();
}, [trackRef, selector, key]);
if (!box) return null;
const transition = box.animate ? 'transform 320ms var(--ease-drawer), width 320ms var(--ease-drawer)' : 'none';
if (rows) {
return { top: 0, width: box.width, height: box.height, transform: `translate(${box.x}px, ${box.y}px)`, transition };
}
return { width: box.width, transform: `translateX(${box.x}px)`, transition };
}