From 0c4583b7f53db394e088e639683c003c16c43b51 Mon Sep 17 00:00:00 2001 From: chengyongru <2755839590@qq.com> Date: Tue, 15 Sep 2026 23:58:22 +0800 Subject: [PATCH] refactor(webui): animate segmented control indicator --- .../settings/models/ModelsSettings.tsx | 1 + .../settings/overview/OverviewSettings.tsx | 38 ++--- .../settings/system/AutomationsSettings.tsx | 1 + webui/src/components/ui/segmented-control.tsx | 131 +++++++++++++++++- webui/src/globals.css | 12 ++ webui/src/tests/segmented-control.test.tsx | 118 ++++++++++++++++ 6 files changed, 270 insertions(+), 31 deletions(-) create mode 100644 webui/src/tests/segmented-control.test.tsx diff --git a/webui/src/components/settings/models/ModelsSettings.tsx b/webui/src/components/settings/models/ModelsSettings.tsx index d99ec31f4..1e83f09e7 100644 --- a/webui/src/components/settings/models/ModelsSettings.tsx +++ b/webui/src/components/settings/models/ModelsSettings.tsx @@ -954,6 +954,7 @@ function ModelAdvancedFields({ ({ value: String(tokens), label: formatModelContextWindow(tokens), diff --git a/webui/src/components/settings/overview/OverviewSettings.tsx b/webui/src/components/settings/overview/OverviewSettings.tsx index 01c96b480..cbb514cac 100644 --- a/webui/src/components/settings/overview/OverviewSettings.tsx +++ b/webui/src/components/settings/overview/OverviewSettings.tsx @@ -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({ {t("settings.sections.interface")} - + { + if (nextTheme !== theme) onToggleTheme(); + }} + /> diff --git a/webui/src/components/settings/system/AutomationsSettings.tsx b/webui/src/components/settings/system/AutomationsSettings.tsx index e9f600750..c5f47c9b9 100644 --- a/webui/src/components/settings/system/AutomationsSettings.tsx +++ b/webui/src/components/settings/system/AutomationsSettings.tsx @@ -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") }, diff --git a/webui/src/components/ui/segmented-control.tsx b/webui/src/components/ui/segmented-control.tsx index 59a3d2a10..841cff6a6 100644 --- a/webui/src/components/ui/segmented-control.tsx +++ b/webui/src/components/ui/segmented-control.tsx @@ -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 { 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, +): boolean { + return previous.left === next.left + && previous.top === next.top + && previous.width === next.width + && previous.height === next.height; } export function SegmentedControl({ @@ -25,30 +51,123 @@ export function SegmentedControl({ mode = "buttons", className, itemClassName, + indicatorClassName, + animateIndicator = true, }: SegmentedControlProps) { const tabs = mode === "tabs"; + const rootRef = useRef(null); + const itemRefs = useRef>([]); + const previousValueRef = useRef(undefined); + const [indicator, setIndicator] = useState(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, 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 (
- {options.map((option) => { + {indicator ? ( + + ) : null} + {options.map((option, index) => { const selected = value === option.value; return (