Files
nanobot/webui/src/components/SidebarSelectionHighlight.tsx
T
chengyongru 9261e731ce feat(webui): improve model and provider settings UX (#6145)
* feat(webui): improve model and provider settings UX

* refactor(webui): organize provider forms and model picker interactions

* fix(webui): separate OAuth credentials from provider configuration

* fix(webui): stabilize shared combobox navigation

* refactor(config): store provider connections as sparse entries

* fix(webui): preserve provider validation and isolate catalog tests

* refactor(config): manage explicit provider connections

* refactor(webui): decouple settings UX from provider removal

* fix(webui): select current combobox options during rapid input

* feat(webui): refine model preset and provider management

* style(webui): refine preset footer actions

* fix(webui): align model settings with responsive layout rules

* refactor(webui): finalize preset dialogs and simplify provider state

* fix(webui): keep fallback heading stable while sorting presets

Animate card dimensions without changing layout height and keep the section heading outside sortable cards. Remove proxy normalization from this PR.

* docs: update WebUI model and provider setup instructions

* docs: start model setup from new preset

* docs: remove duplicate WebUI model setup instructions
2026-10-11 04:30:43 +08:00

129 lines
4.5 KiB
TypeScript

import {
type HTMLAttributes,
type RefObject,
useLayoutEffect,
useRef,
} from "react";
import { cn } from "@/lib/utils";
interface SidebarSelectionHighlightProps extends HTMLAttributes<HTMLDivElement> {
targetRef?: RefObject<HTMLElement>;
targetSelector?: string;
activeId: string | null;
scope: string;
highlightClassName?: string;
}
export const SIDEBAR_SELECTION_ITEM_CLASS =
"relative z-[1] transition-[color,background-color] duration-150 ease-out motion-reduce:transition-none";
// During a drag, animate only the shared highlight, not its measured target as well.
export const SIDEBAR_SELECTION_ACTION_ITEM_CLASS =
"relative z-[1] transition-[width,padding,color,background-color] [transition-duration:300ms,300ms,150ms,150ms] ease-out group-data-[resizing=true]/sidebar:transition-none motion-reduce:transition-none";
export function SidebarSelectionHighlight({
targetRef,
targetSelector,
activeId,
scope,
highlightClassName,
children,
...containerProps
}: SidebarSelectionHighlightProps) {
const containerRef = useRef<HTMLDivElement>(null);
const highlightRef = useRef<HTMLDivElement>(null);
const positionedRef = useRef(false);
useLayoutEffect(() => {
const highlight = highlightRef.current;
const container = containerRef.current;
const target = targetRef?.current ?? (targetSelector
? container?.querySelector<HTMLElement>(targetSelector)
: null);
if (!highlight) return;
if (!activeId || !container || !target) {
highlight.style.opacity = "0";
positionedRef.current = false;
return;
}
let restoreTransitionFrame: number | null = null;
let positionFrame: number | null = null;
const position = () => {
const containerRect = container.getBoundingClientRect();
const targetRect = target.getBoundingClientRect();
if (targetRect.width === 0 || targetRect.height === 0) {
highlight.style.opacity = "0";
positionedRef.current = false;
return;
}
const firstPosition = !positionedRef.current;
if (firstPosition) highlight.style.transitionProperty = "none";
// Preserve fractional layout coordinates so the highlight shares the
// target's edges, including where it meets an adjacent hover background.
highlight.style.left = "0px";
highlight.style.top = "0px";
highlight.style.width = `${targetRect.width}px`;
highlight.style.height = `${targetRect.height}px`;
highlight.style.transform = `translate3d(${targetRect.left - containerRect.left}px, ${
targetRect.top - containerRect.top
}px, 0)`;
highlight.style.opacity = "1";
positionedRef.current = true;
if (firstPosition) {
restoreTransitionFrame = window.requestAnimationFrame(() => {
highlight.style.removeProperty("transition-property");
restoreTransitionFrame = null;
});
}
};
// Measure once per animation frame so React rerenders do not repeatedly
// retarget a transition before the browser has advanced its current frame.
const schedulePosition = () => {
if (positionFrame !== null) return;
positionFrame = window.requestAnimationFrame(() => {
positionFrame = null;
position();
});
};
if (!positionedRef.current) position();
else schedulePosition();
const resizeObserver =
typeof ResizeObserver === "undefined" ? null : new ResizeObserver(schedulePosition);
resizeObserver?.observe(container);
resizeObserver?.observe(target);
window.addEventListener("resize", schedulePosition);
return () => {
if (restoreTransitionFrame !== null) {
window.cancelAnimationFrame(restoreTransitionFrame);
}
if (positionFrame !== null) {
window.cancelAnimationFrame(positionFrame);
}
highlight?.style.removeProperty("transition-property");
resizeObserver?.disconnect();
window.removeEventListener("resize", schedulePosition);
};
});
return (
<div {...containerProps} ref={containerRef}>
{children}
<div
ref={highlightRef}
data-testid={`${scope}-selection-highlight`}
data-active-id={activeId ?? undefined}
aria-hidden="true"
className={cn("pointer-events-none absolute left-0 top-0 z-0 !mt-0 rounded-control bg-sidebar-foreground/[0.055] opacity-0 transition-[transform,width,height] duration-300 ease-out will-change-transform motion-reduce:transition-none dark:bg-white/[0.07]", highlightClassName)}
/>
</div>
);
}