Files
siftlode/frontend/src/components/ui/NativeControls.tsx
T
peter 8cf8c4139c feat(playlists): select rows with a box, Shift for a range, x and Esc
A box first in every row and one over the column for the rows shown, the
selected row tinted. A filter keeps what it hides selected; another list
starts over; a row that left the list drops out. Watch later has none.

A box no longer counts as a typing target: a click on one left the focus
there and the page's keys went dead.
2026-11-01 20:49:59 +01:00

112 lines
3.2 KiB
TypeScript

import clsx from "clsx";
import type { InputHTMLAttributes, ReactNode, RefObject } from "react";
// A6 (docs/DESIGN.md): checkbox, slider and colour well are the BROWSER's controls, tinted
// with the chosen colour (the accent; the media amber on the player chrome) — keyboard, screen
// reader and forced-colours support come free, and a drawn copy
// would have to rebuild all three. What the browser does not give is the 44 touch target: the labelled
// row (`CheckRow`) or the slider's own box grows to it on a coarse pointer. Like `Input`, a raw
// `<input>` for any of them anywhere else is a lint error.
type Native = Omit<InputHTMLAttributes<HTMLInputElement>, "type" | "onChange">;
/** The bare checkbox: inside your own `<label>`, or with an `aria-label`. Prefer `CheckRow`.
* `onChange` also says whether Shift was held, for a range. `mixed` is the third state, some of what
* it covers: the DOM has no attribute for it, only the property. */
export function Checkbox({
onChange,
className,
ref,
mixed = false,
...rest
}: Native & {
onChange?: (checked: boolean, shift: boolean) => void;
ref?: RefObject<HTMLInputElement | null>;
mixed?: boolean;
}) {
return (
<input
ref={(el) => {
if (ref) ref.current = el;
if (el) el.indeterminate = mixed;
}}
type="checkbox"
onChange={(e) =>
onChange?.(e.target.checked, e.nativeEvent instanceof MouseEvent && e.nativeEvent.shiftKey)
}
className={clsx(
"size-4 shrink-0 cursor-pointer accent-chosen disabled:cursor-not-allowed",
className,
)}
{...rest}
/>
);
}
/** A checkbox and its label as one row, the whole row the target (44 on touch). */
export function CheckRow({
checked,
onChange,
disabled,
className,
children,
}: {
checked: boolean;
onChange: (checked: boolean) => void;
disabled?: boolean;
/** Lays the row out (text size, colour, margin); the target geometry is the primitive's. */
className?: string;
children: ReactNode;
}) {
return (
<label
className={clsx(
"inline-flex min-h-6 cursor-pointer items-center gap-2 select-none pointer-coarse:min-h-(--touch-target)",
disabled && "cursor-not-allowed opacity-50",
className,
)}
>
<Checkbox checked={checked} onChange={(on) => onChange(on)} disabled={disabled} />
{children}
</label>
);
}
/** A range slider; `onChange` hands back the number. */
export function Slider({
onChange,
className,
...rest
}: Native & { onChange: (value: number) => void }) {
return (
<input
type="range"
onChange={(e) => onChange(Number(e.target.value))}
className={clsx(
"min-h-6 w-full cursor-pointer accent-chosen pointer-coarse:min-h-(--touch-target)",
className,
)}
{...rest}
/>
);
}
/** The browser's colour well, a small swatch. */
export function ColorInput({
onChange,
className,
...rest
}: Native & { onChange: (value: string) => void }) {
return (
<input
type="color"
onChange={(e) => onChange(e.target.value)}
className={clsx(
"h-6 w-10 cursor-pointer rounded-sm bg-transparent pointer-coarse:h-(--touch-target) pointer-coarse:w-14",
className,
)}
{...rest}
/>
);
}