Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions packages/ui/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions packages/ui/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/ui/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
165 changes: 165 additions & 0 deletions packages/ui/src/SegmentedControl.tsx
Original file line number Diff line number Diff line change
@@ -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<T extends string> {
value: T;
label: ReactNode;
/** Tooltip; defaults to a string label (see the truncation note below). */
title?: string;
}

export interface SegmentedControlProps<T extends string> {
options: SegOption<T>[];
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<T extends string>({
options,
value,
onChange,
ariaLabel,
disabled = false,
size = "sm",
className = "",
}: SegmentedControlProps<T>) {
const n = options.length;
const activeIndex = Math.max(0, options.findIndex((o) => o.value === value));
const trackRef = useRef<HTMLDivElement>(null);
const [dragX, setDragX] = useState<number | null>(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<number | null>(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<HTMLDivElement>) {
if (disabled) return;
startXRef.current = e.clientX;
draggingRef.current = false;
movedRef.current = false;
}
function onPointerMove(e: ReactPointerEvent<HTMLDivElement>) {
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<HTMLDivElement>) {
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 (
<div
ref={trackRef}
role="radiogroup"
aria-label={ariaLabel}
aria-disabled={disabled || undefined}
onPointerDown={onPointerDown}
onPointerMove={onPointerMove}
onPointerUp={onPointerUp}
onPointerCancel={() => {
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}`}
>
<span
aria-hidden
className="pointer-events-none absolute bottom-0.5 left-0.5 top-0.5 rounded-full bg-brand-100 transition-transform duration-150 motion-reduce:transition-none dark:bg-brand-900"
style={thumbStyle}
/>
{options.map((o) => (
<button
key={o.value}
type="button"
role="radio"
aria-checked={o.value === value}
disabled={disabled}
onClick={() => {
if (movedRef.current) {
movedRef.current = false; // ignore the click synthesized after a drag
return;
}
if (o.value !== value) onChange(o.value);
}}
// Too narrow (phones): the label is cut with an ellipsis instead
// of breaking mid-word; the full text stays in the title.
title={o.title ?? (typeof o.label === "string" ? o.label : undefined)}
className={`relative z-10 inline-flex min-w-0 items-center justify-center gap-1.5 rounded-full text-center transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-brand-500 ${segSize} ${
o.value === value ? "text-brand-800 dark:text-brand-200" : "text-slate-500 dark:text-slate-400"
}`}
>
{/* Tailwind's preflight makes <svg> display:block, so an icon inside a
plain block span stacked above its text. A string label keeps the
ellipsis; a node label (icon + text) is laid out as a flex row. */}
{typeof o.label === "string" ? (
<span className="min-w-0 truncate">{o.label}</span>
) : (
<span className="inline-flex min-w-0 items-center justify-center gap-1.5 whitespace-nowrap">
{o.label}
</span>
)}
</button>
))}
</div>
);
}
3 changes: 3 additions & 0 deletions packages/ui/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Loading