refactor(webui): animate segmented control indicator

This commit is contained in:
chengyongru authored and chengyongru committed 2026-09-16 00:43:03 +08:00
1 parent 0374f74bb7
commit 0c4583b7f5
6 files changed
+270 -31

No files matched your search

@@ -954,6 +954,7 @@ function ModelAdvancedFields({
</span>
<SegmentedControl
value={String(contextWindowTokens)}
animateIndicator={false}
options={contextWindowOptions.map((tokens) => ({
value: String(tokens),
label: formatModelContextWindow(tokens),
@@ -38,7 +38,6 @@ import type {
} from "@/lib/local-preferences";
import { providerBrand, providerDisplayLabel } from "@/lib/provider-brand";
import type { SettingsPayload } from "@/lib/types";
import { cn } from "@/lib/utils";
import { useClient } from "@/providers/ClientProvider";
export function OverviewSettings({
@@ -275,30 +274,19 @@ export function AppearanceSettings({
<SettingsSectionTitle>{t("settings.sections.interface")}</SettingsSectionTitle>
<SettingsGroup>
<SettingsRow title={t("settings.rows.theme")}>
<button
type="button"
onClick={onToggleTheme}
className="inline-flex h-8 items-center rounded-full bg-muted p-0.5 text-[12px] font-medium text-muted-foreground"
>
<span
className={cn(
"rounded-full px-3 py-1 transition-colors",
theme === "light" &&
"bg-background text-foreground ring-1 ring-inset ring-border/45",
)}
>
{t("settings.values.light")}
</span>
<span
className={cn(
"rounded-full px-3 py-1 transition-colors",
theme === "dark" &&
"bg-background text-foreground ring-1 ring-inset ring-border/45",
)}
>
{t("settings.values.dark")}
</span>
</button>
<SegmentedControl
value={theme}
ariaLabel={t("settings.rows.theme")}
className="bg-muted p-0.5"
itemClassName="px-3"
options={[
{ value: "light", label: t("settings.values.light") },
{ value: "dark", label: t("settings.values.dark") },
]}
onChange={(nextTheme) => {
if (nextTheme !== theme) onToggleTheme();
}}
/>
</SettingsRow>
<SettingsRow title={t("settings.rows.language")}>
@@ -254,6 +254,7 @@ export function AutomationsSettings({
value={view}
className="rounded-[var(--radius-panel)] bg-[var(--automation-view-track)]"
itemClassName="min-h-8 rounded-[calc(var(--radius-panel)-0.25rem)]"
indicatorClassName="rounded-[calc(var(--radius-panel)-0.25rem)]"
options={[
{ value: "tasks", label: tx("settings.automations.views.tasks", "Tasks") },
{ value: "calendar", label: tx("settings.automations.views.calendar", "Calendar") },
+125 -6
View File
@@ -1,4 +1,10 @@
import type { ReactNode } from "react";
import {
useLayoutEffect,
useRef,
useState,
type KeyboardEvent,
type ReactNode,
} from "react";
import { cn } from "@/lib/utils";
@@ -15,6 +21,26 @@ interface SegmentedControlProps<T extends string> {
mode?: "buttons" | "tabs";
className?: string;
itemClassName?: string;
indicatorClassName?: string;
animateIndicator?: boolean;
}
interface IndicatorGeometry {
left: number;
top: number;
width: number;
height: number;
animate: boolean;
}
function sameGeometry(
previous: IndicatorGeometry,
next: Omit<IndicatorGeometry, "animate">,
): boolean {
return previous.left === next.left
&& previous.top === next.top
&& previous.width === next.width
&& previous.height === next.height;
}
export function SegmentedControl<T extends string>({
@@ -25,30 +51,123 @@ export function SegmentedControl<T extends string>({
mode = "buttons",
className,
itemClassName,
indicatorClassName,
animateIndicator = true,
}: SegmentedControlProps<T>) {
const tabs = mode === "tabs";
const rootRef = useRef<HTMLDivElement>(null);
const itemRefs = useRef<Array<HTMLButtonElement | null>>([]);
const previousValueRef = useRef<T | undefined>(undefined);
const [indicator, setIndicator] = useState<IndicatorGeometry | null>(null);
const selectedIndex = options.findIndex((option) => option.value === value);
const optionValuesKey = options.map((option) => option.value).join("\u0000");
useLayoutEffect(() => {
const measure = (animate: boolean) => {
const item = itemRefs.current[selectedIndex];
if (!item) {
setIndicator(null);
return;
}
const next = {
left: item.offsetLeft,
top: item.offsetTop,
width: item.offsetWidth,
height: item.offsetHeight,
};
setIndicator((previous) => {
if (previous && sameGeometry(previous, next)) return previous;
return { ...next, animate: Boolean(previous) && animate };
});
};
const valueChanged = previousValueRef.current !== undefined
&& previousValueRef.current !== value;
measure(valueChanged);
previousValueRef.current = value;
const handleResize = () => measure(false);
window.addEventListener("resize", handleResize);
const observer = typeof ResizeObserver === "undefined"
? null
: new ResizeObserver(handleResize);
if (observer) {
if (rootRef.current) observer.observe(rootRef.current);
itemRefs.current.forEach((item) => {
if (item) observer.observe(item);
});
}
return () => {
window.removeEventListener("resize", handleResize);
observer?.disconnect();
};
}, [optionValuesKey, selectedIndex, value]);
const handleTabKeyDown = (event: KeyboardEvent<HTMLButtonElement>, index: number) => {
if (!tabs) return;
let nextIndex: number | null = null;
if (event.key === "ArrowRight") nextIndex = (index + 1) % options.length;
if (event.key === "ArrowLeft") nextIndex = (index - 1 + options.length) % options.length;
if (event.key === "Home") nextIndex = 0;
if (event.key === "End") nextIndex = options.length - 1;
if (nextIndex === null) return;
event.preventDefault();
const next = options[nextIndex];
if (!next) return;
onChange(next.value);
itemRefs.current[nextIndex]?.focus();
};
return (
<div
role={tabs ? "tablist" : undefined}
ref={rootRef}
role={tabs ? "tablist" : ariaLabel ? "group" : undefined}
aria-label={ariaLabel}
className={cn(
"segmented-control inline-flex min-h-8 max-w-full flex-nowrap items-center gap-1 rounded-full bg-muted/65 p-1 text-[12px] font-medium text-muted-foreground",
"segmented-control relative isolate inline-flex min-h-8 max-w-full flex-nowrap items-center gap-1 rounded-full bg-muted/65 p-1 text-[12px] font-medium text-muted-foreground",
className,
)}
>
{options.map((option) => {
{indicator ? (
<span
aria-hidden
data-segmented-control-indicator
data-animate={indicator.animate && animateIndicator ? "true" : "false"}
className={cn(
"segmented-control-indicator pointer-events-none absolute left-0 z-0 rounded-full bg-background shadow-[0_1px_2px_hsl(var(--foreground)/0.08)] ring-1 ring-inset ring-border/25",
indicatorClassName,
)}
style={{
top: indicator.top,
width: indicator.width,
height: indicator.height,
transform: `translate3d(${indicator.left}px, 0, 0)`,
}}
/>
) : null}
{options.map((option, index) => {
const selected = value === option.value;
return (
<button
ref={(item) => {
itemRefs.current[index] = item;
}}
key={option.value}
type="button"
role={tabs ? "tab" : undefined}
aria-selected={tabs ? selected : undefined}
aria-pressed={tabs ? undefined : selected}
tabIndex={tabs ? (selected || (selectedIndex < 0 && index === 0) ? 0 : -1) : undefined}
onClick={() => onChange(option.value)}
onKeyDown={(event) => handleTabKeyDown(event, index)}
className={cn(
"min-w-0 truncate whitespace-nowrap rounded-full px-2 py-1 text-muted-foreground transition-colors",
selected ? "bg-background text-foreground" : "hover:text-foreground",
"relative z-[1] min-w-0 truncate whitespace-nowrap rounded-full px-2 py-1 text-muted-foreground transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring",
selected ? "text-foreground" : "hover:text-foreground",
itemClassName,
)}
>