diff --git a/CHANGELOG.md b/CHANGELOG.md index 69b37af..d60c0a3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,23 @@ Changelog ist die Upgrade-Anleitung für die Tools. Untergrenzen nicht steigen. Die CI testet jeden Dependabot-PR. Für die Tools ändert sich nichts. +### @basicbar/ui (→ wird `ui/v0.10.0`) + +**Neu: `SegmentedControl`** — Auswahl „eins aus wenigen“ als Pill mit +gleich breiten Segmenten und gleitendem Daumen (Klick, Tastatur, Ziehen mit +Finger/Maus; rastet beim Loslassen ein), herausgelöst aus abstimmbar +(`components/ui.tsx`). Optisch klar von An/Aus-Filter-Pills unterschieden. +Neu gegenüber abstimmbar: `role="radiogroup"` statt `group` (die Segmente +sind `role="radio"`), `size="md"` für 44-px-Touch-Ziele, sichtbarer +Fokusring, `transition-transform` für den Daumen (abgeschaltet bei +`prefers-reduced-motion`). + +Migration: Tarball auf `ui/v0.10.0`, Vite-Dep-Cache leeren. abstimmbar kann +die eigene Kopie in `components/ui.tsx` löschen und +`import { SegmentedControl, type SegOption } from "@basicbar/ui"` nutzen +(Props unverändert, `size` ist optional). Die Tailwind-`content`-Zeile für +`node_modules/@basicbar/ui/dist` muss vorhanden sein (ist es in allen Tools). + ### basicbar-pdf (→ wird `pdf/v0.1.0`) **Neues Paket: gemeinsamer PDF-Rahmen** für herunterladbare Dokumente, diff --git a/packages/ui/README.md b/packages/ui/README.md index 5a24cf6..f5375c9 100644 --- a/packages/ui/README.md +++ b/packages/ui/README.md @@ -34,6 +34,9 @@ Tools identisch aussehen, übergeben sie dieselbe Ramp. - **`TranslatableField` / `TranslationFormProvider` / `TranslationControlsSlot`** — Sprach-Tabs pro Feld plus verschiebbare Leiste „alle Felder übersetzen“, optional im Formular angedockt (siehe `TranslatableField.tsx`). +- **`SegmentedControl`** — „eins aus wenigen“ als Pill mit gleitendem + Daumen (Klick, Tastatur, Ziehen); `size="md"` für 44-px-Touch-Ziele. + Für Ansichts-/Typ-Wahl, nicht für An/Aus-Filter. - **`RichText` / `stripHtml` / `isEmptyHtml`** — Rendern und Prüfen des gespeicherten Rich-HTML; **`RichTextEditor`** (TipTap) als **eigener Entry** `@basicbar/ui/rich-text-editor`, damit TipTap/ProseMirror nur in Bundles diff --git a/packages/ui/package-lock.json b/packages/ui/package-lock.json index 1f80fee..33da10a 100644 --- a/packages/ui/package-lock.json +++ b/packages/ui/package-lock.json @@ -1,12 +1,12 @@ { "name": "@basicbar/ui", - "version": "0.9.1", + "version": "0.10.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@basicbar/ui", - "version": "0.9.1", + "version": "0.10.0", "license": "Apache-2.0", "dependencies": { "@tiptap/extension-bold": "^3.27.2", diff --git a/packages/ui/package.json b/packages/ui/package.json index f0cb7bd..253e9ce 100644 --- a/packages/ui/package.json +++ b/packages/ui/package.json @@ -1,7 +1,7 @@ { "name": "@basicbar/ui", - "version": "0.9.1", - "description": "Design-System-Basis der virtUOS -bar-Tools: Tailwind-Preset, Basis-Styles, Theme (Dark Mode), i18n-Bootstrap, contentLang, TranslatableField, Preferences, RichText; RichTextEditor (TipTap) als eigener Entry", + "version": "0.10.0", + "description": "Design-System-Basis der virtUOS -bar-Tools: Tailwind-Preset, Basis-Styles, Theme (Dark Mode), i18n-Bootstrap, contentLang, TranslatableField, Preferences, SegmentedControl, RichText; RichTextEditor (TipTap) als eigener Entry", "license": "Apache-2.0", "author": "Universität Osnabrück (virtUOS)", "type": "module", diff --git a/packages/ui/src/SegmentedControl.tsx b/packages/ui/src/SegmentedControl.tsx new file mode 100644 index 0000000..0bc0d26 --- /dev/null +++ b/packages/ui/src/SegmentedControl.tsx @@ -0,0 +1,165 @@ +// SPDX-License-Identifier: Apache-2.0 +// Copyright 2026 Universität Osnabrück (virtUOS) + +import { + useRef, + useState, + type CSSProperties, + type PointerEvent as ReactPointerEvent, + type ReactNode, +} from "react"; + +export interface SegOption { + value: T; + label: ReactNode; + /** Tooltip; defaults to a string label (see the truncation note below). */ + title?: string; +} + +export interface SegmentedControlProps { + options: SegOption[]; + value: T; + onChange: (value: T) => void; + /** Accessible name of the group. */ + ariaLabel: string; + disabled?: boolean; + /** "sm" (default): compact, text-xs. "md": 44 px touch targets, text-sm. */ + size?: "sm" | "md"; + className?: string; +} + +const DRAG_THRESHOLD = 4; // px of travel before a press becomes a drag + +/** Equal-width segmented control with an animated sliding thumb — a choice + * of one out of a few, visibly different from toggle/filter pills. Supports + * click, keyboard (the segments are real buttons) and pointer-drag: the + * thumb follows the finger and snaps to the nearest segment on release. */ +export function SegmentedControl({ + options, + value, + onChange, + ariaLabel, + disabled = false, + size = "sm", + className = "", +}: SegmentedControlProps) { + const n = options.length; + const activeIndex = Math.max(0, options.findIndex((o) => o.value === value)); + const trackRef = useRef(null); + const [dragX, setDragX] = useState(null); // thumb px offset while dragging + // Pointer state. `startX` marks a pressed pointer; `dragging` flips only + // once it moves past DRAG_THRESHOLD — until then it's a plain press, so we + // do NOT capture the pointer and let the native click reach the button + // (capturing eagerly would redirect the click to the track and kill taps). + const startXRef = useRef(null); + const draggingRef = useRef(false); + const movedRef = useRef(false); + + function seg() { + const track = trackRef.current!; + const inner = track.clientWidth - 4; // p-0.5 = 2px each side + return { left: track.getBoundingClientRect().left, inner, w: inner / n }; + } + function thumbLeftFrom(clientX: number) { + const { left, inner, w } = seg(); + return Math.min(inner - w, Math.max(0, clientX - left - 2 - w / 2)); + } + function indexFrom(left: number) { + return Math.min(n - 1, Math.max(0, Math.round(left / seg().w))); + } + + function onPointerDown(e: ReactPointerEvent) { + if (disabled) return; + startXRef.current = e.clientX; + draggingRef.current = false; + movedRef.current = false; + } + function onPointerMove(e: ReactPointerEvent) { + if (startXRef.current === null) return; + if (!draggingRef.current) { + if (Math.abs(e.clientX - startXRef.current) < DRAG_THRESHOLD) return; + // Real drag begins: now capture the pointer so it keeps tracking even + // if the finger leaves the control, and suppress the trailing click. + draggingRef.current = true; + movedRef.current = true; + e.currentTarget.setPointerCapture(e.pointerId); + } + setDragX(thumbLeftFrom(e.clientX)); + } + function onPointerUp(e: ReactPointerEvent) { + const wasDrag = draggingRef.current; + startXRef.current = null; + draggingRef.current = false; + if (!wasDrag) return; // a plain tap: let the button's onClick handle it + setDragX(null); + const picked = options[indexFrom(thumbLeftFrom(e.clientX))].value; + if (picked !== value) onChange(picked); + } + + const thumbStyle: CSSProperties = { + width: `calc((100% - 4px) / ${n})`, + transform: dragX !== null ? `translateX(${dragX}px)` : `translateX(${activeIndex * 100}%)`, + transition: dragX !== null ? "none" : undefined, + }; + const segSize = size === "md" ? "min-h-[44px] px-2 sm:px-4" : "px-1.5 py-1 sm:px-2.5"; + + return ( +
{ + startXRef.current = null; + draggingRef.current = false; + setDragX(null); + }} + style={{ touchAction: "none" }} + className={`relative grid grid-flow-col auto-cols-[minmax(0,1fr)] items-center rounded-full border border-slate-200 p-0.5 dark:border-slate-700 ${ + size === "md" ? "text-sm font-medium" : "text-xs" + } ${disabled ? "opacity-50" : ""} ${className}`} + > + + {options.map((o) => ( + + ))} +
+ ); +} diff --git a/packages/ui/src/index.ts b/packages/ui/src/index.ts index a49b0b0..d67cd11 100644 --- a/packages/ui/src/index.ts +++ b/packages/ui/src/index.ts @@ -25,6 +25,9 @@ export type { RenderInputArgs, TranslatableFieldProps } from "./TranslatableFiel export { AppearanceControl, LanguageOptions, PreferencesMenu } from "./Preferences"; +export { SegmentedControl } from "./SegmentedControl"; +export type { SegOption, SegmentedControlProps } from "./SegmentedControl"; + // RichTextEditor (TipTap) deliberately lives in its own entry, // "@basicbar/ui/rich-text-editor" — see src/rich-text-editor.ts. export { RichText, richTextClass } from "./RichText";