feat(webui): refine interactive controls
Separate the custom context menu preference and enable it by default. Bring dropdowns and settings choices into a consistent, responsive interaction language.
This commit is contained in:
16 files changed
+904
-283
No files matched your search
@@ -100,8 +100,10 @@ export interface UiAppearance {
|
||||
reduceMotion: boolean;
|
||||
/** Hard-disable all UI animations (stronger than reduceMotion). */
|
||||
disableMotion: boolean;
|
||||
/** Use SnowLuma's adaptive cursor and custom context menu. */
|
||||
/** Use SnowLuma's adaptive cursor. */
|
||||
customPointerSystem: boolean;
|
||||
/** Replace the browser context menu with SnowLuma's context menu. */
|
||||
customContextMenu: boolean;
|
||||
highContrast: boolean;
|
||||
sidebarPinned: boolean;
|
||||
timeFormat: TimeFormat;
|
||||
@@ -229,6 +231,7 @@ const DEFAULT_APPEARANCE: UiAppearance = {
|
||||
reduceMotion: false,
|
||||
disableMotion: false,
|
||||
customPointerSystem: false,
|
||||
customContextMenu: true,
|
||||
highContrast: false,
|
||||
sidebarPinned: false,
|
||||
timeFormat: '24h',
|
||||
@@ -524,6 +527,7 @@ export function normalizeAppearance(value: unknown, imageState: ServerImageState
|
||||
reduceMotion: boolOr(v.reduceMotion, DEFAULT_APPEARANCE.reduceMotion),
|
||||
disableMotion: boolOr(v.disableMotion, DEFAULT_APPEARANCE.disableMotion),
|
||||
customPointerSystem: boolOr(v.customPointerSystem, DEFAULT_APPEARANCE.customPointerSystem),
|
||||
customContextMenu: boolOr(v.customContextMenu, DEFAULT_APPEARANCE.customContextMenu),
|
||||
highContrast: boolOr(v.highContrast, DEFAULT_APPEARANCE.highContrast),
|
||||
sidebarPinned: boolOr(v.sidebarPinned, DEFAULT_APPEARANCE.sidebarPinned),
|
||||
timeFormat: oneOf<TimeFormat>(v.timeFormat, ['12h', '24h'], DEFAULT_APPEARANCE.timeFormat),
|
||||
|
||||
Binary file not shown.
@@ -5,7 +5,7 @@
|
||||
import { useEffect, useMemo, useState } from 'react';
|
||||
import { motion } from 'motion/react';
|
||||
import { Loader2, MessageSquarePlus, Send } from 'lucide-react';
|
||||
import { Select } from '@/components/ui/select';
|
||||
import { Dropdown } from '@/components/interior/dropdown';
|
||||
import { Picker } from '@/components/ui/picker';
|
||||
import { JsonTree } from '@/components/ui/json-tree';
|
||||
import { Segmented } from '@/components/debug/segmented';
|
||||
@@ -97,10 +97,17 @@ export function MessageComposer({ accounts }: { accounts: QQInfo[] }) {
|
||||
<div className="flex h-8 items-center">
|
||||
<span className="text-xs font-medium text-muted-foreground">账号</span>
|
||||
</div>
|
||||
<Select value={uin} onChange={(e) => setUin(e.target.value)} className="w-full sm:w-44">
|
||||
{accounts.length === 0 && <option value="">(无在线账号)</option>}
|
||||
{accounts.map((a) => <option key={a.uin} value={a.uin}>{a.nickname || a.uin}</option>)}
|
||||
</Select>
|
||||
<Dropdown
|
||||
value={uin}
|
||||
onChange={setUin}
|
||||
label="账号"
|
||||
placeholder={accounts.length === 0 ? '(无在线账号)' : '选择账号'}
|
||||
items={accounts.map((account) => ({
|
||||
value: account.uin,
|
||||
label: account.nickname || account.uin,
|
||||
}))}
|
||||
className="w-full sm:w-44"
|
||||
/>
|
||||
</div>
|
||||
<div className="flex flex-col gap-1.5">
|
||||
<div className="flex h-8 items-center justify-between">
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
// group_id, an image a FileSource, a bool a switch, an enum a select.
|
||||
import { useState } from 'react';
|
||||
import { Input } from '@/components/ui/input';
|
||||
import { Select } from '@/components/ui/select';
|
||||
import { Dropdown } from '@/components/interior/dropdown';
|
||||
import { ToggleSwitch } from '@/components/ui/toggle-switch';
|
||||
import { Picker } from '@/components/ui/picker';
|
||||
import { FaceGrid } from '@/components/ui/face-grid';
|
||||
@@ -84,13 +84,20 @@ export function ParamField({ param, value, onChange, uin, groupContext }: {
|
||||
// ── type-driven widgets ──
|
||||
if (param.values && param.values.length > 0) {
|
||||
return (
|
||||
// Always render the empty option so a controlled <select value=""> has a
|
||||
// matching option (otherwise the browser shows the first value while state
|
||||
// stays '' — a "looks selected but sends nothing" trap for required enums).
|
||||
<Select value={value} onChange={(e) => onChange(e.target.value)}>
|
||||
<option value="">{param.required ? '(请选择)' : '(默认)'}</option>
|
||||
{param.values.map((v) => <option key={String(v)} value={String(v)}>{String(v)}</option>)}
|
||||
</Select>
|
||||
<Dropdown
|
||||
value={value}
|
||||
onChange={onChange}
|
||||
label={param.name}
|
||||
placeholder={param.required ? '(请选择)' : '(默认)'}
|
||||
items={[
|
||||
{ value: '', label: param.required ? '(请选择)' : '(默认)' },
|
||||
...param.values.map((nextValue) => ({
|
||||
value: String(nextValue),
|
||||
label: String(nextValue),
|
||||
})),
|
||||
]}
|
||||
className="w-full"
|
||||
/>
|
||||
);
|
||||
}
|
||||
if (param.type === 'bool' || param.type === 'boolean') {
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
"use client";
|
||||
|
||||
import { type ReactNode, type RefObject } from "react";
|
||||
import { createPortal } from "react-dom";
|
||||
import { AnimatePresence, motion, useReducedMotion } from "motion/react";
|
||||
|
||||
const EASE = [0.23, 1, 0.32, 1] as const;
|
||||
const EXIT = [0.4, 0, 1, 1] as const;
|
||||
|
||||
export type ContextMenuPlacement = {
|
||||
left: number;
|
||||
top: number;
|
||||
width: number;
|
||||
maxHeight: number;
|
||||
transformOrigin: string;
|
||||
visible?: boolean;
|
||||
};
|
||||
|
||||
export type ContextMenuSurfaceProps = {
|
||||
openKey: number | string | null;
|
||||
menuRef: RefObject<HTMLDivElement | null>;
|
||||
placement: ContextMenuPlacement;
|
||||
label: string;
|
||||
children: ReactNode;
|
||||
};
|
||||
|
||||
/**
|
||||
* Interior's context-menu surface, kept separate from action discovery so the
|
||||
* WebUI can build native browser-style actions from the element under pointer.
|
||||
*/
|
||||
export function ContextMenuSurface({
|
||||
openKey,
|
||||
menuRef,
|
||||
placement,
|
||||
label,
|
||||
children,
|
||||
}: ContextMenuSurfaceProps) {
|
||||
const reduced = useReducedMotion();
|
||||
|
||||
return createPortal(
|
||||
<AnimatePresence initial={false}>
|
||||
{openKey !== null ? (
|
||||
<motion.div
|
||||
key={openKey}
|
||||
ref={menuRef}
|
||||
role="menu"
|
||||
aria-label={label}
|
||||
initial={reduced ? { opacity: 0 } : { opacity: 0, scale: 0.96 }}
|
||||
animate={{ opacity: 1, scale: 1 }}
|
||||
exit={
|
||||
reduced
|
||||
? { opacity: 0, transition: { duration: 0 } }
|
||||
: { opacity: 0, scale: 0.98, transition: { duration: 0.14, ease: EXIT } }
|
||||
}
|
||||
transition={reduced ? { duration: 0 } : { duration: 0.2, ease: EASE }}
|
||||
style={{
|
||||
position: "fixed",
|
||||
left: placement.left,
|
||||
top: placement.top,
|
||||
width: placement.width,
|
||||
maxHeight: placement.maxHeight,
|
||||
transformOrigin: placement.transformOrigin,
|
||||
visibility: placement.visible === false ? "hidden" : "visible",
|
||||
zIndex: 2147483646,
|
||||
}}
|
||||
className="global-context-menu"
|
||||
>
|
||||
{children}
|
||||
</motion.div>
|
||||
) : null}
|
||||
</AnimatePresence>,
|
||||
document.body,
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,422 @@
|
||||
"use client";
|
||||
|
||||
import { useCallback, useEffect, useId, useRef, useState } from "react";
|
||||
import { AnimatePresence, motion, useReducedMotion } from "motion/react";
|
||||
|
||||
const EASE = [0.23, 1, 0.32, 1] as const;
|
||||
const EXIT = [0.4, 0, 1, 1] as const;
|
||||
const CELL = { type: "spring", stiffness: 520, damping: 34, mass: 0.45 } as const;
|
||||
const NUDGE = { type: "spring", stiffness: 700, damping: 46, mass: 0.5 } as const;
|
||||
const NONE = { duration: 0 } as const;
|
||||
const SLIDE = { type: "spring", stiffness: 700, damping: 46, mass: 0.5 } as const;
|
||||
const ROW_H = 32;
|
||||
const OPEN = { type: "spring", stiffness: 620, damping: 38, mass: 0.6 } as const;
|
||||
|
||||
export type DropdownItem = {
|
||||
value: string;
|
||||
label: string;
|
||||
hint?: string;
|
||||
disabled?: boolean;
|
||||
};
|
||||
|
||||
export type UseDropdownOptions = {
|
||||
items: DropdownItem[];
|
||||
value?: string;
|
||||
defaultValue?: string;
|
||||
onChange?: (value: string) => void;
|
||||
disabled?: boolean;
|
||||
typeaheadDelay?: number;
|
||||
};
|
||||
|
||||
export function useDropdown({
|
||||
items,
|
||||
value,
|
||||
defaultValue,
|
||||
onChange,
|
||||
disabled = false,
|
||||
typeaheadDelay = 600,
|
||||
}: UseDropdownOptions) {
|
||||
const uid = useId();
|
||||
const listId = `${uid}-list`;
|
||||
const itemId = useCallback((i: number) => `${uid}-opt-${i}`, [uid]);
|
||||
|
||||
const [uncontrolled, setUncontrolled] = useState<string | null>(defaultValue ?? null);
|
||||
const selectedValue = value !== undefined ? value : uncontrolled;
|
||||
const selectedIndex = items.findIndex((it) => it.value === selectedValue);
|
||||
|
||||
const [open, setOpen] = useState(false);
|
||||
const [activeIndex, setActiveIndex] = useState(-1);
|
||||
|
||||
const rootRef = useRef<HTMLDivElement>(null);
|
||||
const triggerRef = useRef<HTMLButtonElement>(null);
|
||||
const listRef = useRef<HTMLUListElement>(null);
|
||||
const itemRefs = useRef<(HTMLLIElement | null)[]>([]);
|
||||
const viaKey = useRef(false);
|
||||
const buffer = useRef("");
|
||||
const bufferTimer = useRef<ReturnType<typeof setTimeout> | null>(null);
|
||||
const emit = useRef(onChange);
|
||||
|
||||
useEffect(() => {
|
||||
emit.current = onChange;
|
||||
}, [onChange]);
|
||||
|
||||
const step = useCallback(
|
||||
(from: number, dir: 1 | -1) => {
|
||||
const n = items.length;
|
||||
if (n === 0) return -1;
|
||||
let i = from;
|
||||
for (let k = 0; k < n; k += 1) {
|
||||
i = (i + dir + n) % n;
|
||||
if (!items[i].disabled) return i;
|
||||
}
|
||||
return from;
|
||||
},
|
||||
[items],
|
||||
);
|
||||
|
||||
const edge = useCallback(
|
||||
(dir: 1 | -1) => step(dir === 1 ? -1 : items.length, dir),
|
||||
[step, items.length],
|
||||
);
|
||||
|
||||
const openMenu = useCallback(
|
||||
(index?: number) => {
|
||||
if (disabled || items.length === 0) return;
|
||||
const usable = selectedIndex >= 0 && !items[selectedIndex].disabled;
|
||||
viaKey.current = true;
|
||||
setActiveIndex(index ?? (usable ? selectedIndex : edge(1)));
|
||||
setOpen(true);
|
||||
},
|
||||
[disabled, items, selectedIndex, edge],
|
||||
);
|
||||
|
||||
const close = useCallback((restoreFocus = true) => {
|
||||
buffer.current = "";
|
||||
setOpen(false);
|
||||
setActiveIndex(-1);
|
||||
if (restoreFocus) triggerRef.current?.focus();
|
||||
}, []);
|
||||
|
||||
const select = useCallback(
|
||||
(index: number) => {
|
||||
const item = items[index];
|
||||
if (!item || item.disabled) return;
|
||||
if (value === undefined) setUncontrolled(item.value);
|
||||
emit.current?.(item.value);
|
||||
close();
|
||||
},
|
||||
[items, value, close],
|
||||
);
|
||||
|
||||
const typeahead = useCallback(
|
||||
(char: string) => {
|
||||
if (bufferTimer.current) clearTimeout(bufferTimer.current);
|
||||
buffer.current += char.toLowerCase();
|
||||
bufferTimer.current = setTimeout(() => {
|
||||
buffer.current = "";
|
||||
}, typeaheadDelay);
|
||||
|
||||
const q = buffer.current;
|
||||
const n = items.length;
|
||||
const from = activeIndex < 0 ? 0 : activeIndex;
|
||||
const start = q.length > 1 ? from : from + 1;
|
||||
for (let k = 0; k < n; k += 1) {
|
||||
const i = (start + k) % n;
|
||||
const it = items[i];
|
||||
if (!it.disabled && it.label.toLowerCase().startsWith(q)) {
|
||||
viaKey.current = true;
|
||||
setActiveIndex(i);
|
||||
return;
|
||||
}
|
||||
}
|
||||
},
|
||||
[items, activeIndex, typeaheadDelay],
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
if (open) listRef.current?.focus();
|
||||
}, [open]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!open) return;
|
||||
const onDown = (event: PointerEvent) => {
|
||||
if (!rootRef.current?.contains(event.target as Node)) close(false);
|
||||
};
|
||||
const onWindowBlur = () => close(false);
|
||||
document.addEventListener("pointerdown", onDown, true);
|
||||
window.addEventListener("blur", onWindowBlur);
|
||||
return () => {
|
||||
document.removeEventListener("pointerdown", onDown, true);
|
||||
window.removeEventListener("blur", onWindowBlur);
|
||||
};
|
||||
}, [open, close]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!open || activeIndex < 0 || !viaKey.current) return;
|
||||
viaKey.current = false;
|
||||
itemRefs.current[activeIndex]?.scrollIntoView({ block: "nearest" });
|
||||
}, [open, activeIndex]);
|
||||
|
||||
useEffect(
|
||||
() => () => {
|
||||
if (bufferTimer.current) clearTimeout(bufferTimer.current);
|
||||
},
|
||||
[],
|
||||
);
|
||||
|
||||
const triggerProps = {
|
||||
ref: triggerRef,
|
||||
type: "button" as const,
|
||||
disabled,
|
||||
"aria-haspopup": "listbox" as const,
|
||||
"aria-expanded": open,
|
||||
"aria-controls": open ? listId : undefined,
|
||||
onClick: () => (open ? close() : openMenu()),
|
||||
onKeyDown: (event: React.KeyboardEvent<HTMLButtonElement>) => {
|
||||
if (event.key === "ArrowDown" || event.key === "Enter" || event.key === " ") {
|
||||
event.preventDefault();
|
||||
openMenu();
|
||||
} else if (event.key === "ArrowUp") {
|
||||
event.preventDefault();
|
||||
openMenu(edge(-1));
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
const listProps = {
|
||||
ref: listRef,
|
||||
id: listId,
|
||||
role: "listbox" as const,
|
||||
tabIndex: -1,
|
||||
"aria-activedescendant": activeIndex >= 0 ? itemId(activeIndex) : undefined,
|
||||
onKeyDown: (event: React.KeyboardEvent<HTMLUListElement>) => {
|
||||
if (event.key === "ArrowDown" || event.key === "ArrowUp") {
|
||||
event.preventDefault();
|
||||
const dir = event.key === "ArrowDown" ? 1 : -1;
|
||||
viaKey.current = true;
|
||||
setActiveIndex((i) => step(i, dir));
|
||||
} else if (event.key === "Home" || event.key === "End") {
|
||||
event.preventDefault();
|
||||
viaKey.current = true;
|
||||
setActiveIndex(edge(event.key === "Home" ? 1 : -1));
|
||||
} else if (event.key === "Enter" || event.key === " ") {
|
||||
event.preventDefault();
|
||||
select(activeIndex);
|
||||
} else if (event.key === "Escape") {
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
close();
|
||||
} else if (event.key === "Tab") {
|
||||
close(false);
|
||||
} else if (
|
||||
event.key.length === 1
|
||||
&& !event.metaKey
|
||||
&& !event.ctrlKey
|
||||
&& !event.altKey
|
||||
) {
|
||||
event.preventDefault();
|
||||
typeahead(event.key);
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
const getItemProps = useCallback(
|
||||
(index: number) => ({
|
||||
id: itemId(index),
|
||||
role: "option" as const,
|
||||
"aria-selected": index === selectedIndex,
|
||||
"aria-disabled": items[index]?.disabled ? (true as const) : undefined,
|
||||
ref: (element: HTMLLIElement | null) => {
|
||||
itemRefs.current[index] = element;
|
||||
},
|
||||
onPointerMove: () => {
|
||||
if (items[index]?.disabled) return;
|
||||
viaKey.current = false;
|
||||
setActiveIndex(index);
|
||||
},
|
||||
onClick: () => select(index),
|
||||
}),
|
||||
[itemId, items, selectedIndex, select],
|
||||
);
|
||||
|
||||
return {
|
||||
open,
|
||||
activeIndex,
|
||||
selectedIndex,
|
||||
selectedItem: selectedIndex >= 0 ? items[selectedIndex] : null,
|
||||
rootRef,
|
||||
triggerProps,
|
||||
listProps,
|
||||
getItemProps,
|
||||
};
|
||||
}
|
||||
|
||||
export type DropdownProps = {
|
||||
items: DropdownItem[];
|
||||
value?: string;
|
||||
defaultValue?: string;
|
||||
onChange?: (value: string) => void;
|
||||
label?: string;
|
||||
placeholder?: string;
|
||||
disabled?: boolean;
|
||||
emptyLabel?: string;
|
||||
className?: string;
|
||||
};
|
||||
|
||||
export function Dropdown({
|
||||
items,
|
||||
value,
|
||||
defaultValue,
|
||||
onChange,
|
||||
label = "Options",
|
||||
placeholder = "Select an option",
|
||||
disabled = false,
|
||||
emptyLabel = "Nothing to choose",
|
||||
className = "",
|
||||
}: DropdownProps) {
|
||||
const reduced = useReducedMotion();
|
||||
const {
|
||||
open,
|
||||
activeIndex,
|
||||
selectedIndex,
|
||||
selectedItem,
|
||||
rootRef,
|
||||
triggerProps,
|
||||
listProps,
|
||||
getItemProps,
|
||||
} = useDropdown({ items, value, defaultValue, onChange, disabled });
|
||||
|
||||
const cell = reduced ? NONE : CELL;
|
||||
|
||||
return (
|
||||
<div ref={rootRef} className={`relative inline-block text-left ${className}`}>
|
||||
<button
|
||||
{...triggerProps}
|
||||
className={`flex h-9 w-full select-none items-center gap-2 whitespace-nowrap rounded-[9px] border border-border bg-background px-3 text-[13px] font-medium text-foreground outline-none transition-[box-shadow,border-color] duration-150 disabled:opacity-50 ${
|
||||
open
|
||||
? "shadow-[inset_0_1px_2px_rgb(0_0_0/0.09)]"
|
||||
: "shadow-xs hover:border-foreground/20 hover:shadow-sm focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/40"
|
||||
}`}
|
||||
>
|
||||
<span className="sr-only">
|
||||
{label}: {selectedItem ? selectedItem.label : placeholder}
|
||||
</span>
|
||||
<span aria-hidden className="min-w-0 flex-1 truncate text-left">
|
||||
{selectedItem ? selectedItem.label : placeholder}
|
||||
</span>
|
||||
<motion.svg
|
||||
aria-hidden
|
||||
viewBox="0 0 12 12"
|
||||
className="size-3 shrink-0 text-muted-foreground"
|
||||
initial={false}
|
||||
animate={{ rotate: open ? 180 : 0 }}
|
||||
transition={reduced ? NONE : NUDGE}
|
||||
>
|
||||
<path
|
||||
d="M3 4.75 6 7.75 9 4.75"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
strokeWidth="1.4"
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
/>
|
||||
</motion.svg>
|
||||
</button>
|
||||
<AnimatePresence>
|
||||
{open ? (
|
||||
<motion.div
|
||||
initial={reduced ? { opacity: 0 } : { opacity: 0, scale: 0.94, y: -8 }}
|
||||
animate={{ opacity: 1, scale: 1, y: 0 }}
|
||||
exit={{
|
||||
opacity: 0,
|
||||
scale: 0.97,
|
||||
y: -6,
|
||||
transition: reduced ? NONE : { duration: 0.12, ease: EXIT },
|
||||
}}
|
||||
transition={
|
||||
reduced
|
||||
? NONE
|
||||
: { ...OPEN, opacity: { duration: 0.12, ease: EASE } }
|
||||
}
|
||||
style={{ transformOrigin: "top left" }}
|
||||
className="absolute left-0 top-[calc(100%+6px)] z-50 min-w-full whitespace-nowrap rounded-[11px] border border-border bg-popover p-[5px] text-popover-foreground shadow-[0_1px_2px_rgb(0_0_0/0.06),0_16px_36px_-18px_rgb(0_0_0/0.5)]"
|
||||
>
|
||||
<ul
|
||||
{...listProps}
|
||||
aria-label={label}
|
||||
className="relative max-h-[216px] overflow-y-auto outline-none [scrollbar-gutter:stable]"
|
||||
>
|
||||
<motion.span
|
||||
aria-hidden
|
||||
className="pointer-events-none absolute inset-x-0 top-0 h-8 rounded-[7px] bg-accent"
|
||||
initial={false}
|
||||
animate={{
|
||||
y: activeIndex < 0 ? 0 : activeIndex * ROW_H,
|
||||
opacity: activeIndex < 0 ? 0 : 1,
|
||||
}}
|
||||
transition={
|
||||
reduced
|
||||
? NONE
|
||||
: { ...SLIDE, opacity: { duration: 0.1, ease: EASE } }
|
||||
}
|
||||
/>
|
||||
{items.map((item, index) => {
|
||||
const active = index === activeIndex && !item.disabled;
|
||||
const picked = index === selectedIndex;
|
||||
return (
|
||||
<li
|
||||
key={item.value}
|
||||
{...getItemProps(index)}
|
||||
className={`relative flex h-8 cursor-default select-none items-center rounded-[7px] px-2.5 text-[13px] ${
|
||||
item.disabled
|
||||
? "text-muted-foreground/70"
|
||||
: active
|
||||
? "text-accent-foreground"
|
||||
: "text-popover-foreground"
|
||||
}`}
|
||||
>
|
||||
<span className="relative flex min-w-0 flex-1 items-center gap-3">
|
||||
<span className="truncate">{item.label}</span>
|
||||
{item.hint ? (
|
||||
<span className="ml-auto shrink-0 font-mono text-[10.5px] text-muted-foreground">
|
||||
{item.hint}
|
||||
</span>
|
||||
) : null}
|
||||
</span>
|
||||
<motion.span
|
||||
aria-hidden
|
||||
initial={false}
|
||||
animate={{ opacity: picked ? 1 : 0, scale: picked ? 1 : 0.7 }}
|
||||
transition={cell}
|
||||
className="relative ml-2 flex size-[14px] shrink-0 items-center justify-center"
|
||||
>
|
||||
<svg viewBox="0 0 14 14" className="size-[14px]">
|
||||
<path
|
||||
d="M3 7.4 5.8 10.2 11 4.4"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
strokeWidth="1.5"
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
/>
|
||||
</svg>
|
||||
</motion.span>
|
||||
</li>
|
||||
);
|
||||
})}
|
||||
|
||||
{items.length === 0 ? (
|
||||
<li
|
||||
role="presentation"
|
||||
className="flex h-8 items-center px-2.5 text-[13px] text-muted-foreground"
|
||||
>
|
||||
{emptyLabel}
|
||||
</li>
|
||||
) : null}
|
||||
</ul>
|
||||
</motion.div>
|
||||
) : null}
|
||||
</AnimatePresence>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,196 @@
|
||||
"use client";
|
||||
|
||||
import { useCallback, useEffect, useRef, useState, type ReactNode } from "react";
|
||||
import {
|
||||
animate,
|
||||
motion,
|
||||
useMotionValue,
|
||||
useReducedMotion,
|
||||
useTransform,
|
||||
} from "motion/react";
|
||||
|
||||
const CELL = { type: "spring", stiffness: 520, damping: 34, mass: 0.45 } as const;
|
||||
const SEG =
|
||||
"px-3 py-[7px] text-center text-[13px] font-medium leading-[18px] tracking-[-0.01em] whitespace-nowrap";
|
||||
|
||||
export type SegmentedOption = {
|
||||
value: string;
|
||||
label: string;
|
||||
content?: ReactNode;
|
||||
disabled?: boolean;
|
||||
};
|
||||
|
||||
export type SegmentedControlProps = {
|
||||
options: SegmentedOption[];
|
||||
label: string;
|
||||
value?: string;
|
||||
defaultValue?: string;
|
||||
onValueChange?: (value: string) => void;
|
||||
className?: string;
|
||||
};
|
||||
|
||||
export function SegmentedControl({
|
||||
options,
|
||||
label,
|
||||
value,
|
||||
defaultValue,
|
||||
onValueChange,
|
||||
className = "",
|
||||
}: SegmentedControlProps) {
|
||||
const count = Math.max(1, options.length);
|
||||
const template = `repeat(${count}, minmax(0, 1fr))`;
|
||||
|
||||
const [internal, setInternal] = useState(
|
||||
() => defaultValue ?? options[0]?.value ?? "",
|
||||
);
|
||||
const [hovered, setHovered] = useState(-1);
|
||||
|
||||
const controlled = value !== undefined;
|
||||
const current = controlled ? value : internal;
|
||||
const found = options.findIndex((option) => option.value === current);
|
||||
const index = found < 0 ? 0 : found;
|
||||
|
||||
const buttons = useRef<(HTMLButtonElement | null)[]>([]);
|
||||
const emit = useRef(onValueChange);
|
||||
useEffect(() => {
|
||||
emit.current = onValueChange;
|
||||
}, [onValueChange]);
|
||||
|
||||
const reduced = useReducedMotion();
|
||||
const pos = useMotionValue(index);
|
||||
const thumbX = useTransform(pos, (next) => `${next * 100}%`);
|
||||
const maskX = useTransform(pos, (next) => `${next * -100}%`);
|
||||
|
||||
useEffect(() => {
|
||||
if (reduced) {
|
||||
pos.set(index);
|
||||
return;
|
||||
}
|
||||
const controls = animate(pos, index, CELL);
|
||||
return () => controls.stop();
|
||||
}, [index, reduced, pos]);
|
||||
|
||||
const select = useCallback(
|
||||
(next: string) => {
|
||||
if (!controlled) setInternal(next);
|
||||
if (next !== current) emit.current?.(next);
|
||||
},
|
||||
[controlled, current],
|
||||
);
|
||||
|
||||
const seek = useCallback(
|
||||
(from: number, direction: number) => {
|
||||
let next = from;
|
||||
for (let offset = 0; offset < count; offset += 1) {
|
||||
next = (next + direction + count) % count;
|
||||
if (!options[next]?.disabled) return next;
|
||||
}
|
||||
return from;
|
||||
},
|
||||
[count, options],
|
||||
);
|
||||
|
||||
const go = useCallback(
|
||||
(next: number) => {
|
||||
const option = options[next];
|
||||
if (!option || option.disabled) return;
|
||||
buttons.current[next]?.focus();
|
||||
select(option.value);
|
||||
},
|
||||
[options, select],
|
||||
);
|
||||
|
||||
const onKeyDown = (event: React.KeyboardEvent, optionIndex: number) => {
|
||||
if (event.key === "ArrowRight" || event.key === "ArrowDown") {
|
||||
event.preventDefault();
|
||||
go(seek(optionIndex, 1));
|
||||
} else if (event.key === "ArrowLeft" || event.key === "ArrowUp") {
|
||||
event.preventDefault();
|
||||
go(seek(optionIndex, -1));
|
||||
} else if (event.key === "Home") {
|
||||
event.preventDefault();
|
||||
go(seek(count - 1, 1));
|
||||
} else if (event.key === "End") {
|
||||
event.preventDefault();
|
||||
go(seek(0, -1));
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div
|
||||
role="radiogroup"
|
||||
aria-label={label}
|
||||
className={`relative inline-block select-none rounded-[9px] border border-border bg-muted/70 p-[3px] shadow-[inset_0_1px_2px_rgb(0_0_0/0.07)] ${className}`}
|
||||
>
|
||||
<div
|
||||
className="relative grid"
|
||||
style={{ gridTemplateColumns: template, touchAction: "manipulation" }}
|
||||
>
|
||||
{options.map((option, optionIndex) => (
|
||||
<span
|
||||
key={option.value}
|
||||
aria-hidden
|
||||
className={`${SEG} pointer-events-none ${
|
||||
option.disabled
|
||||
? "text-muted-foreground/40"
|
||||
: hovered === optionIndex && optionIndex !== index
|
||||
? "text-foreground"
|
||||
: "text-muted-foreground"
|
||||
}`}
|
||||
>
|
||||
{option.content ?? option.label}
|
||||
</span>
|
||||
))}
|
||||
|
||||
<motion.div
|
||||
aria-hidden
|
||||
className="pointer-events-none absolute inset-y-0 left-0 overflow-hidden rounded-[6px] bg-foreground text-background shadow-[0_1px_2px_rgb(0_0_0/0.28)]"
|
||||
style={{ width: `${100 / count}%`, x: thumbX }}
|
||||
initial={false}
|
||||
>
|
||||
<motion.div
|
||||
className="absolute inset-0"
|
||||
style={{ x: maskX }}
|
||||
initial={false}
|
||||
>
|
||||
<div
|
||||
className="absolute inset-y-0 left-0 grid"
|
||||
style={{ width: `${count * 100}%`, gridTemplateColumns: template }}
|
||||
>
|
||||
{options.map((option) => (
|
||||
<span key={option.value} className={`${SEG} text-background`}>
|
||||
{option.content ?? option.label}
|
||||
</span>
|
||||
))}
|
||||
</div>
|
||||
</motion.div>
|
||||
</motion.div>
|
||||
<div
|
||||
className="absolute inset-0 grid"
|
||||
style={{ gridTemplateColumns: template }}
|
||||
onPointerLeave={() => setHovered(-1)}
|
||||
>
|
||||
{options.map((option, optionIndex) => (
|
||||
<button
|
||||
key={option.value}
|
||||
ref={(node) => {
|
||||
buttons.current[optionIndex] = node;
|
||||
}}
|
||||
type="button"
|
||||
role="radio"
|
||||
aria-checked={optionIndex === index}
|
||||
aria-disabled={option.disabled || undefined}
|
||||
tabIndex={optionIndex === index ? 0 : -1}
|
||||
onClick={() => !option.disabled && select(option.value)}
|
||||
onKeyDown={(event) => onKeyDown(event, optionIndex)}
|
||||
onPointerEnter={() => !option.disabled && setHovered(optionIndex)}
|
||||
className="cursor-default rounded-[6px] outline-none focus-visible:bg-primary/[0.06] focus-visible:shadow-[inset_0_0_0_1px_var(--ring)]"
|
||||
>
|
||||
<span className="sr-only">{option.label}</span>
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -2,7 +2,7 @@ import { createContext, useContext, useId, useRef, useState, type ComponentProps
|
||||
import { motion } from 'motion/react';
|
||||
import {
|
||||
Accessibility, AlertTriangle, Bell, Bug, Check, Clock, Code2, Download, ExternalLink, Github, Image as ImageIcon,
|
||||
Info, KeyRound, Loader2, Monitor, Moon, Palette, PanelTop, Plus, RefreshCw, RotateCcw, Server, ShieldCheck,
|
||||
Info, KeyRound, Loader2, Monitor, Moon, MousePointer2, Palette, PanelTop, Plus, RefreshCw, RotateCcw, Server, ShieldCheck,
|
||||
SlidersHorizontal, Sparkles, Star, Sun, Tag, Upload, Trash2, HardDrive,
|
||||
} from 'lucide-react';
|
||||
import { Card, CardContent, CardHeader, CardTitle, CardDescription } from '@/components/ui/card';
|
||||
@@ -28,6 +28,7 @@ import {
|
||||
type DarkIntensity,
|
||||
type Density,
|
||||
type FontSpec,
|
||||
type Palette as PaletteId,
|
||||
type SidebarStyle,
|
||||
type ThemeMode,
|
||||
type TimeFormat,
|
||||
@@ -39,6 +40,7 @@ import { TOPBAR_CATALOGUE } from '@/components/layout/top-bar';
|
||||
import { ChangePasswordDialog } from '@/components/change-password-dialog';
|
||||
import { ConfirmDialog } from '@/components/confirm-dialog';
|
||||
import { SkeletonSwap } from '@/components/interior/skeleton-swap';
|
||||
import { SegmentedControl } from '@/components/interior/segmented-control';
|
||||
import { useApi } from '@/lib/api';
|
||||
import { useAppState } from '@/contexts/AppStateContext';
|
||||
import { cn } from '@/lib/utils';
|
||||
@@ -181,42 +183,29 @@ interface Opt<T> { value: T; label: string; icon?: typeof Sun }
|
||||
|
||||
// Associates a SettingRow's visible label with the control inside it (so the
|
||||
// segmented radiogroup gets an accessible name without prop-threading).
|
||||
const RowLabelContext = createContext<string | undefined>(undefined);
|
||||
const RowLabelContext = createContext<{ id: string; label: string } | undefined>(undefined);
|
||||
|
||||
// iOS/macOS segmented control: a recessed track with a raised pill on the
|
||||
// selected segment. Single-select → exposed as a radiogroup. Wraps gracefully.
|
||||
function Segmented<T extends string | number>({
|
||||
value, options, onChange, disabled,
|
||||
}: { value: T; options: Opt<T>[]; onChange: (v: T) => void; disabled?: boolean }) {
|
||||
const labelledBy = useContext(RowLabelContext);
|
||||
const rowLabel = useContext(RowLabelContext);
|
||||
return (
|
||||
<div
|
||||
role="radiogroup"
|
||||
aria-labelledby={labelledBy}
|
||||
aria-disabled={disabled || undefined}
|
||||
className={cn('inline-flex flex-wrap items-center gap-1 rounded-lg bg-muted/60 p-1', disabled && 'opacity-50')}
|
||||
>
|
||||
{options.map((opt) => {
|
||||
const active = value === opt.value;
|
||||
const Icon = opt.icon;
|
||||
return (
|
||||
<button
|
||||
key={String(opt.value)}
|
||||
type="button"
|
||||
role="radio"
|
||||
aria-checked={active}
|
||||
disabled={disabled}
|
||||
onClick={() => onChange(opt.value)}
|
||||
className={cn(
|
||||
'inline-flex items-center gap-1.5 rounded-md px-3 py-1.5 text-sm transition-[background-color,color,box-shadow] duration-150 ease-out cursor-pointer outline-none focus-visible:ring-[3px] focus-visible:ring-ring/40 disabled:cursor-not-allowed',
|
||||
active ? 'bg-card font-semibold text-foreground shadow-sm ring-1 ring-border' : 'font-medium text-muted-foreground hover:text-foreground',
|
||||
)}
|
||||
>
|
||||
{Icon && <Icon className="size-4" />}
|
||||
{opt.label}
|
||||
</button>
|
||||
);
|
||||
})}
|
||||
<div aria-labelledby={rowLabel?.id} aria-disabled={disabled || undefined} className={cn('max-w-full overflow-x-auto', disabled && 'opacity-50')}>
|
||||
<SegmentedControl
|
||||
label={rowLabel?.label ?? '选项'}
|
||||
value={String(value)}
|
||||
options={options.map((option) => ({
|
||||
value: String(option.value),
|
||||
label: option.label,
|
||||
disabled,
|
||||
}))}
|
||||
onValueChange={(next) => {
|
||||
const option = options.find((candidate) => String(candidate.value) === next);
|
||||
if (option) onChange(option.value);
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -237,7 +226,7 @@ function SettingRow({
|
||||
{hint && <p className="mt-0.5 text-[12px] leading-snug text-muted-foreground">{hint}</p>}
|
||||
</div>
|
||||
);
|
||||
const control = <RowLabelContext.Provider value={labelId}>{children}</RowLabelContext.Provider>;
|
||||
const control = <RowLabelContext.Provider value={{ id: labelId, label }}>{children}</RowLabelContext.Provider>;
|
||||
if (layout === 'stack') {
|
||||
return <div className="flex flex-col gap-3 px-5 py-4">{head}{control}</div>;
|
||||
}
|
||||
@@ -407,33 +396,29 @@ function AppearancePanel() {
|
||||
<div className="flex flex-col gap-5">
|
||||
<Group title="主题" icon={Sun} description="配色方案、明暗模式与深色风格。所有改动立即生效并保存到服务器,跨设备同步。">
|
||||
<SettingRow label="配色方案" hint="“默认”跟随下方明暗模式;Catppuccin 为整套配色,会自行决定明暗。" layout="stack">
|
||||
<div className="flex flex-wrap gap-2.5" role="radiogroup" aria-label="配色方案">
|
||||
{PALETTE_OPTIONS.map((p) => {
|
||||
const active = a.palette === p.id;
|
||||
return (
|
||||
<button
|
||||
key={p.id}
|
||||
type="button"
|
||||
role="radio"
|
||||
aria-checked={active}
|
||||
aria-label={p.label}
|
||||
onClick={() => setAppearance({ palette: p.id })}
|
||||
className={cn(
|
||||
'flex w-[5.5rem] flex-col items-center gap-1.5 rounded-xl border p-1.5 transition-[border-color,box-shadow] duration-150 ease-out cursor-pointer outline-none focus-visible:ring-[3px] focus-visible:ring-ring/40',
|
||||
active ? 'border-primary ring-2 ring-primary/30' : 'border-border hover:border-foreground/30',
|
||||
)}
|
||||
>
|
||||
<span className="flex h-11 w-full items-center justify-center rounded-lg" style={{ backgroundColor: p.preview.bg }}>
|
||||
<span className="flex gap-1 rounded-md px-1.5 py-1 shadow-sm" style={{ backgroundColor: p.preview.surface }}>
|
||||
{p.preview.dots.map((d, i) => (
|
||||
<span key={i} className="size-1.5 rounded-full" style={{ backgroundColor: d }} />
|
||||
))}
|
||||
<div className="max-w-full overflow-x-auto pb-1">
|
||||
<SegmentedControl
|
||||
label="配色方案"
|
||||
value={a.palette}
|
||||
onValueChange={(palette) => setAppearance({ palette: palette as PaletteId })}
|
||||
className="min-w-max"
|
||||
options={PALETTE_OPTIONS.map((p) => ({
|
||||
value: p.id,
|
||||
label: p.label,
|
||||
content: (
|
||||
<span className="flex w-[5.5rem] flex-col items-center gap-1.5">
|
||||
<span className="flex h-11 w-full items-center justify-center rounded-lg" style={{ backgroundColor: p.preview.bg }}>
|
||||
<span className="flex gap-1 rounded-md px-1.5 py-1 shadow-sm" style={{ backgroundColor: p.preview.surface }}>
|
||||
{p.preview.dots.map((dot, dotIndex) => (
|
||||
<span key={dotIndex} className="size-1.5 rounded-full" style={{ backgroundColor: dot }} />
|
||||
))}
|
||||
</span>
|
||||
</span>
|
||||
<span className="text-xs font-medium">{p.label}</span>
|
||||
</span>
|
||||
<span className="text-xs font-medium">{p.label}</span>
|
||||
</button>
|
||||
);
|
||||
})}
|
||||
),
|
||||
}))}
|
||||
/>
|
||||
</div>
|
||||
</SettingRow>
|
||||
<SettingRow label="显示模式" hint={paletteFixed ? '当前由 Catppuccin 配色决定明暗。' : undefined}>
|
||||
@@ -543,12 +528,22 @@ function AppearancePanel() {
|
||||
|
||||
<TopbarPanel />
|
||||
|
||||
<Group title="交互与无障碍" icon={Accessibility} description="光标、右键菜单、动效、对比度与侧栏行为。">
|
||||
<SettingRow label="自定义光标与右键菜单" hint="默认关闭;开启后使用主题化光标与 SnowLuma 右键菜单,关闭后立即恢复浏览器原生行为。" layout="inline">
|
||||
<Group title="右键菜单" icon={MousePointer2} description="控制网页内右键操作的呈现方式。">
|
||||
<SettingRow label="自定义右键菜单" hint="默认开启;关闭后立即恢复浏览器原生右键菜单。" layout="inline">
|
||||
<ToggleSwitch
|
||||
value={a.customContextMenu}
|
||||
onChange={(customContextMenu) => setAppearance({ customContextMenu })}
|
||||
ariaLabel="自定义右键菜单"
|
||||
/>
|
||||
</SettingRow>
|
||||
</Group>
|
||||
|
||||
<Group title="交互与无障碍" icon={Accessibility} description="光标、动效、对比度与侧栏行为。">
|
||||
<SettingRow label="自定义光标" hint="默认关闭;开启后使用主题化自适应光标。" layout="inline">
|
||||
<ToggleSwitch
|
||||
value={a.customPointerSystem}
|
||||
onChange={(customPointerSystem) => setAppearance({ customPointerSystem })}
|
||||
ariaLabel="自定义光标与右键菜单"
|
||||
ariaLabel="自定义光标"
|
||||
/>
|
||||
</SettingRow>
|
||||
<SettingRow label="减弱动效" hint="弱化页面切换、弹簧等装饰性动画(保留轻微淡入),对低端设备与晕动敏感者更友好。" layout="inline">
|
||||
|
||||
@@ -10,10 +10,10 @@ import { AlertTriangle, Database, Download, Loader2, Lock, Pencil, Save, ShieldC
|
||||
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
|
||||
import { Badge } from '@/components/ui/badge';
|
||||
import { SkeletonSwap } from '@/components/interior/skeleton-swap';
|
||||
import { Dropdown } from '@/components/interior/dropdown';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Input } from '@/components/ui/input';
|
||||
import { Label } from '@/components/ui/label';
|
||||
import { Select } from '@/components/ui/select';
|
||||
import { Textarea } from '@/components/ui/textarea';
|
||||
import { ToggleSwitch } from '@/components/ui/toggle-switch';
|
||||
import { ConfirmDialog } from '@/components/confirm-dialog';
|
||||
@@ -281,10 +281,16 @@ export function SystemPanel() {
|
||||
</div>
|
||||
<div className="flex flex-col gap-1.5">
|
||||
<Label className="flex items-center">绑定地址<EnvBadge field="webuiHost" /></Label>
|
||||
<Select value={host} onChange={(e) => setHost(e.target.value)}>
|
||||
<option value="0.0.0.0">0.0.0.0(所有网卡)</option>
|
||||
<option value="127.0.0.1">127.0.0.1(仅本机)</option>
|
||||
</Select>
|
||||
<Dropdown
|
||||
value={host}
|
||||
onChange={setHost}
|
||||
label="绑定地址"
|
||||
items={[
|
||||
{ value: '0.0.0.0', label: '0.0.0.0(所有网卡)' },
|
||||
{ value: '127.0.0.1', label: '127.0.0.1(仅本机)' },
|
||||
]}
|
||||
className="w-full"
|
||||
/>
|
||||
</div>
|
||||
<div className="flex flex-col gap-1.5">
|
||||
<Label className="flex items-center">信任代理 (trust-proxy)<EnvBadge field="trustProxy" /></Label>
|
||||
|
||||
@@ -1,7 +1,4 @@
|
||||
import { useEffect, useRef, useState } from 'react';
|
||||
import { AnimatePresence, motion } from 'motion/react';
|
||||
import { Check, ChevronDown } from 'lucide-react';
|
||||
import { cn } from '@/lib/utils';
|
||||
import { Dropdown } from '@/components/interior/dropdown';
|
||||
|
||||
export interface DropdownOption<T extends string> {
|
||||
value: T;
|
||||
@@ -31,113 +28,13 @@ export function DropdownSelect<T extends string>({
|
||||
ariaLabel,
|
||||
className,
|
||||
}: DropdownSelectProps<T>) {
|
||||
const [open, setOpen] = useState(false);
|
||||
const [active, setActive] = useState(0);
|
||||
const rootRef = useRef<HTMLDivElement>(null);
|
||||
|
||||
const selected = options.find((o) => o.value === value);
|
||||
|
||||
useEffect(() => {
|
||||
if (!open) return;
|
||||
// Seed the keyboard highlight on the current selection each time we open.
|
||||
setActive(Math.max(0, options.findIndex((o) => o.value === value)));
|
||||
const onPointer = (e: PointerEvent) => {
|
||||
if (rootRef.current && !rootRef.current.contains(e.target as Node)) setOpen(false);
|
||||
};
|
||||
document.addEventListener('pointerdown', onPointer);
|
||||
return () => document.removeEventListener('pointerdown', onPointer);
|
||||
}, [open]); // eslint-disable-line react-hooks/exhaustive-deps
|
||||
|
||||
const choose = (v: T) => {
|
||||
onChange(v);
|
||||
setOpen(false);
|
||||
};
|
||||
|
||||
const onKeyDown = (e: React.KeyboardEvent) => {
|
||||
if (e.key === 'Escape' && open) {
|
||||
// Swallow so the surrounding Dialog doesn't also close on the same key.
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
setOpen(false);
|
||||
return;
|
||||
}
|
||||
if (e.key === 'ArrowDown' || e.key === 'ArrowUp') {
|
||||
e.preventDefault();
|
||||
if (!open) {
|
||||
setOpen(true);
|
||||
return;
|
||||
}
|
||||
setActive((i) => {
|
||||
const n = options.length;
|
||||
return e.key === 'ArrowDown' ? (i + 1) % n : (i - 1 + n) % n;
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (e.key === 'Enter' || e.key === ' ') {
|
||||
e.preventDefault();
|
||||
if (!open) setOpen(true);
|
||||
else if (options[active]) choose(options[active].value);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div ref={rootRef} className={cn('relative', className)}>
|
||||
<button
|
||||
type="button"
|
||||
aria-haspopup="listbox"
|
||||
aria-expanded={open}
|
||||
aria-label={ariaLabel}
|
||||
onClick={() => setOpen((o) => !o)}
|
||||
onKeyDown={onKeyDown}
|
||||
className={cn(
|
||||
'flex h-9 w-full items-center justify-between gap-2 rounded-lg border border-border bg-transparent px-3 text-sm shadow-xs outline-none transition-[color,box-shadow]',
|
||||
'hover:bg-accent/40 focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/40',
|
||||
)}
|
||||
>
|
||||
<span className="truncate">{selected?.label ?? ''}</span>
|
||||
<ChevronDown
|
||||
className={cn(
|
||||
'size-4 shrink-0 text-muted-foreground transition-transform duration-200',
|
||||
open && 'rotate-180',
|
||||
)}
|
||||
/>
|
||||
</button>
|
||||
|
||||
<AnimatePresence>
|
||||
{open && (
|
||||
<motion.ul
|
||||
role="listbox"
|
||||
aria-label={ariaLabel}
|
||||
initial={{ opacity: 0, y: -4, scale: 0.97 }}
|
||||
animate={{ opacity: 1, y: 0, scale: 1 }}
|
||||
exit={{ opacity: 0, y: -4, scale: 0.97 }}
|
||||
transition={{ type: 'spring', stiffness: 600, damping: 38 }}
|
||||
className="absolute inset-x-0 top-[calc(100%+4px)] z-50 origin-top overflow-hidden rounded-xl border border-border/60 bg-card p-1 shadow-lg backdrop-blur-sm"
|
||||
>
|
||||
{options.map((o, i) => {
|
||||
const isSel = o.value === value;
|
||||
const isActive = i === active;
|
||||
return (
|
||||
<li key={o.value} role="option" aria-selected={isSel}>
|
||||
<button
|
||||
type="button"
|
||||
tabIndex={-1}
|
||||
onMouseEnter={() => setActive(i)}
|
||||
onClick={() => choose(o.value)}
|
||||
className={cn(
|
||||
'flex w-full items-center justify-between gap-2 rounded-lg px-2.5 py-1.5 text-left text-sm transition-colors',
|
||||
isActive ? 'bg-accent text-accent-foreground' : 'text-foreground',
|
||||
)}
|
||||
>
|
||||
<span className="truncate">{o.label}</span>
|
||||
{isSel && <Check className="size-4 shrink-0 text-primary" />}
|
||||
</button>
|
||||
</li>
|
||||
);
|
||||
})}
|
||||
</motion.ul>
|
||||
)}
|
||||
</AnimatePresence>
|
||||
</div>
|
||||
<Dropdown
|
||||
value={value}
|
||||
items={options.map((option) => ({ ...option }))}
|
||||
onChange={(next) => onChange(next as T)}
|
||||
label={ariaLabel}
|
||||
className={className}
|
||||
/>
|
||||
);
|
||||
}
|
||||
@@ -1,6 +1,4 @@
|
||||
import * as React from 'react';
|
||||
import { createPortal } from 'react-dom';
|
||||
import { AnimatePresence, motion } from 'motion/react';
|
||||
import {
|
||||
ArrowLeft,
|
||||
ClipboardPaste,
|
||||
@@ -13,6 +11,7 @@ import {
|
||||
TextSelect,
|
||||
type LucideIcon,
|
||||
} from 'lucide-react';
|
||||
import { ContextMenuSurface } from '@/components/interior/context-menu';
|
||||
import { useTheme } from '@/contexts/ThemeContext';
|
||||
|
||||
type TextEditor = HTMLInputElement | HTMLTextAreaElement | HTMLElement;
|
||||
@@ -315,7 +314,7 @@ function buildActionGroups(snapshot: ContextSnapshot): MenuAction[][] {
|
||||
|
||||
export function GlobalContextMenu() {
|
||||
const { appearance } = useTheme();
|
||||
const enabled = appearance.customPointerSystem;
|
||||
const enabled = appearance.customContextMenu;
|
||||
const [menu, setMenu] = React.useState<MenuState | null>(null);
|
||||
const [position, setPosition] = React.useState({ x: -9999, y: -9999, ready: false });
|
||||
const [busyAction, setBusyAction] = React.useState<string | null>(null);
|
||||
@@ -448,26 +447,24 @@ export function GlobalContextMenu() {
|
||||
}
|
||||
};
|
||||
|
||||
return createPortal(
|
||||
<AnimatePresence initial={false}>
|
||||
{enabled && menu && (
|
||||
<motion.div
|
||||
key={menu.id}
|
||||
ref={menuRef}
|
||||
role="menu"
|
||||
aria-label="右键菜单"
|
||||
className="global-context-menu"
|
||||
style={{
|
||||
left: position.x,
|
||||
top: position.y,
|
||||
visibility: position.ready ? 'visible' : 'hidden',
|
||||
transformOrigin: `${menu.x > window.innerWidth / 2 ? 'right' : 'left'} ${menu.y > window.innerHeight / 2 ? 'bottom' : 'top'}`,
|
||||
}}
|
||||
initial={{ opacity: 0, scale: 0.96, y: -4 }}
|
||||
animate={{ opacity: 1, scale: 1, y: 0 }}
|
||||
exit={{ opacity: 0, scale: 0.98, y: -2, transition: { duration: 0.12, ease: 'easeIn' } }}
|
||||
transition={{ type: 'spring', duration: 0.3, bounce: 0 }}
|
||||
>
|
||||
return (
|
||||
<ContextMenuSurface
|
||||
openKey={enabled && menu ? menu.id : null}
|
||||
menuRef={menuRef}
|
||||
label="右键菜单"
|
||||
placement={{
|
||||
left: position.x,
|
||||
top: position.y,
|
||||
width: Math.min(224, Math.max(160, window.innerWidth - 16)),
|
||||
maxHeight: Math.max(42, window.innerHeight - 16),
|
||||
transformOrigin: menu
|
||||
? `${clamp(menu.x - position.x, 0, 224)}px ${clamp(menu.y - position.y, 0, window.innerHeight)}px`
|
||||
: '0 0',
|
||||
visible: position.ready,
|
||||
}}
|
||||
>
|
||||
{enabled && menu ? (
|
||||
<>
|
||||
{groups.map((group, groupIndex) => (
|
||||
<React.Fragment key={group.map((action) => action.id).join(':')}>
|
||||
{groupIndex > 0 && <div className="global-context-menu-separator" role="separator" />}
|
||||
@@ -502,9 +499,8 @@ export function GlobalContextMenu() {
|
||||
{error}
|
||||
</div>
|
||||
)}
|
||||
</motion.div>
|
||||
)}
|
||||
</AnimatePresence>,
|
||||
document.body,
|
||||
</>
|
||||
) : null}
|
||||
</ContextMenuSurface>
|
||||
);
|
||||
}
|
||||
@@ -6,8 +6,8 @@
|
||||
// can still commit it as a raw value. Loading / error / empty states degrade to
|
||||
// the raw-value input so the picker never traps you.
|
||||
import { useEffect, useMemo, useRef, useState } from 'react';
|
||||
import { AnimatePresence, motion } from 'motion/react';
|
||||
import { ChevronDown, RefreshCw, Search } from 'lucide-react';
|
||||
import { AnimatePresence, motion, useReducedMotion } from 'motion/react';
|
||||
import { RefreshCw, Search } from 'lucide-react';
|
||||
import { SkeletonSwap } from '@/components/interior/skeleton-swap';
|
||||
import { cn } from '@/lib/utils';
|
||||
|
||||
@@ -39,11 +39,18 @@ interface PickerProps {
|
||||
const ROW_H = 44; // px — fixed row height drives the windowing math
|
||||
const OVERSCAN = 6;
|
||||
const PANEL_MAX = 264; // px of scroll viewport
|
||||
const EASE = [0.23, 1, 0.32, 1] as const;
|
||||
const EXIT = [0.4, 0, 1, 1] as const;
|
||||
const NUDGE = { type: 'spring', stiffness: 700, damping: 46, mass: 0.5 } as const;
|
||||
const SLIDE = { type: 'spring', stiffness: 700, damping: 46, mass: 0.5 } as const;
|
||||
const OPEN = { type: 'spring', stiffness: 620, damping: 38, mass: 0.6 } as const;
|
||||
const NONE = { duration: 0 } as const;
|
||||
|
||||
export function Picker({
|
||||
value, onChange, options, loading, error, onRefresh,
|
||||
placeholder = '选择…', validateRaw, ariaLabel, className, disabled,
|
||||
}: PickerProps) {
|
||||
const reduced = useReducedMotion();
|
||||
const [open, setOpen] = useState(false);
|
||||
const [query, setQuery] = useState('');
|
||||
const [scrollTop, setScrollTop] = useState(0);
|
||||
@@ -145,17 +152,39 @@ export function Picker({
|
||||
{selected ? selected.label : value || placeholder}
|
||||
{selected?.sub && <span className="ml-1.5 text-xs text-muted-foreground tabular-nums">{selected.sub}</span>}
|
||||
</span>
|
||||
<ChevronDown className={cn('h-4 w-4 shrink-0 text-muted-foreground transition-transform', open && 'rotate-180')} />
|
||||
<motion.svg
|
||||
aria-hidden
|
||||
viewBox="0 0 12 12"
|
||||
className="size-3 shrink-0 text-muted-foreground"
|
||||
initial={false}
|
||||
animate={{ rotate: open ? 180 : 0 }}
|
||||
transition={reduced ? NONE : NUDGE}
|
||||
>
|
||||
<path
|
||||
d="M3 4.75 6 7.75 9 4.75"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
strokeWidth="1.4"
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
/>
|
||||
</motion.svg>
|
||||
</button>
|
||||
|
||||
<AnimatePresence>
|
||||
{open && (
|
||||
<motion.div
|
||||
initial={{ opacity: 0, y: -4, scale: 0.98 }}
|
||||
initial={reduced ? { opacity: 0 } : { opacity: 0, scale: 0.94, y: -8 }}
|
||||
animate={{ opacity: 1, y: 0, scale: 1 }}
|
||||
exit={{ opacity: 0, y: -4, scale: 0.98 }}
|
||||
transition={{ type: 'spring', stiffness: 500, damping: 38 }}
|
||||
className="absolute z-50 mt-1.5 w-full overflow-hidden rounded-xl border border-border/60 bg-popover shadow-lg ring-1 ring-black/5"
|
||||
exit={{
|
||||
opacity: 0,
|
||||
scale: 0.97,
|
||||
y: -6,
|
||||
transition: reduced ? NONE : { duration: 0.12, ease: EXIT },
|
||||
}}
|
||||
transition={reduced ? NONE : { ...OPEN, opacity: { duration: 0.12, ease: EASE } }}
|
||||
style={{ transformOrigin: 'top left' }}
|
||||
className="absolute z-50 mt-1.5 w-full overflow-hidden rounded-[11px] border border-border bg-popover shadow-[0_1px_2px_rgb(0_0_0/0.06),0_16px_36px_-18px_rgb(0_0_0/0.5)]"
|
||||
>
|
||||
<div className="flex items-center gap-2 border-b border-border/60 px-2.5 py-2">
|
||||
<Search className="h-3.5 w-3.5 shrink-0 text-muted-foreground" />
|
||||
@@ -204,6 +233,17 @@ export function Picker({
|
||||
style={{ height: viewH }}
|
||||
>
|
||||
<div style={{ height: totalRows * ROW_H, position: 'relative' }}>
|
||||
<motion.span
|
||||
aria-hidden
|
||||
className="pointer-events-none absolute inset-x-0 top-0 rounded-[7px] bg-accent"
|
||||
style={{ height: ROW_H }}
|
||||
initial={false}
|
||||
animate={{
|
||||
y: active * ROW_H,
|
||||
opacity: totalRows > 0 ? 1 : 0,
|
||||
}}
|
||||
transition={reduced ? NONE : { ...SLIDE, opacity: { duration: 0.1, ease: EASE } }}
|
||||
/>
|
||||
{rows.map(({ idx, opt }) => (
|
||||
<div
|
||||
key={opt ? opt.value : `__raw-${idx}`}
|
||||
@@ -216,8 +256,8 @@ export function Picker({
|
||||
choose(idx);
|
||||
}}
|
||||
className={cn(
|
||||
'absolute left-0 right-0 flex cursor-pointer items-center gap-2.5 px-3',
|
||||
idx === active ? 'bg-accent text-accent-foreground' : 'hover:bg-accent/50',
|
||||
'absolute left-0 right-0 z-[1] flex cursor-pointer items-center gap-2.5 px-3',
|
||||
idx === active ? 'text-accent-foreground' : 'hover:bg-accent/50',
|
||||
)}
|
||||
style={{ top: idx * ROW_H, height: ROW_H }}
|
||||
>
|
||||
|
||||
@@ -1,30 +0,0 @@
|
||||
import * as React from 'react';
|
||||
import { ChevronDown } from 'lucide-react';
|
||||
import { cn } from '@/lib/utils';
|
||||
|
||||
// A styled native <select> matching Input's design language. Native keeps it
|
||||
// accessible + zero-dep; we hide the OS arrow (appearance-none) and draw our
|
||||
// own chevron for a consistent look. Pass <option>s as children.
|
||||
const Select = React.forwardRef<HTMLSelectElement, React.SelectHTMLAttributes<HTMLSelectElement>>(
|
||||
({ className, children, ...props }, ref) => (
|
||||
<div className="relative w-full">
|
||||
<select
|
||||
ref={ref}
|
||||
data-ui-control=""
|
||||
className={cn(
|
||||
'flex h-10 w-full min-w-0 appearance-none rounded-md border border-border bg-transparent px-3 py-1 pr-8 text-sm shadow-xs transition-[color,box-shadow] outline-none',
|
||||
'focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/40',
|
||||
'disabled:cursor-not-allowed disabled:opacity-50',
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
>
|
||||
{children}
|
||||
</select>
|
||||
<ChevronDown className="pointer-events-none absolute right-2.5 top-1/2 h-4 w-4 -translate-y-1/2 text-muted-foreground" />
|
||||
</div>
|
||||
)
|
||||
);
|
||||
Select.displayName = 'Select';
|
||||
|
||||
export { Select };
|
||||
@@ -112,6 +112,7 @@ export const DEFAULT_APPEARANCE: UiAppearance = {
|
||||
reduceMotion: false,
|
||||
disableMotion: false,
|
||||
customPointerSystem: false,
|
||||
customContextMenu: true,
|
||||
highContrast: false,
|
||||
sidebarPinned: false,
|
||||
timeFormat: '24h',
|
||||
@@ -484,6 +485,17 @@ function authToken(): string | null {
|
||||
try { return localStorage.getItem(TOKEN_KEY); } catch { return null; }
|
||||
}
|
||||
|
||||
function withAppearanceDefaults(value: Partial<UiAppearance>): UiAppearance {
|
||||
return {
|
||||
...DEFAULT_APPEARANCE,
|
||||
...value,
|
||||
background: {
|
||||
...DEFAULT_APPEARANCE.background,
|
||||
...value.background,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
async function fetchAppearance(): Promise<UiAppearance | null> {
|
||||
const token = authToken();
|
||||
try {
|
||||
@@ -492,15 +504,15 @@ async function fetchAppearance(): Promise<UiAppearance | null> {
|
||||
if (token) {
|
||||
const res = await fetch('/api/ui', { headers: { Authorization: `Bearer ${token}` } });
|
||||
if (res.ok) {
|
||||
const data = (await res.json()) as { config?: { appearance?: UiAppearance } };
|
||||
if (data.config?.appearance) return data.config.appearance;
|
||||
const data = (await res.json()) as { config?: { appearance?: Partial<UiAppearance> } };
|
||||
if (data.config?.appearance) return withAppearanceDefaults(data.config.appearance);
|
||||
}
|
||||
// 401/expired → fall through to the public subset.
|
||||
}
|
||||
const res = await fetch('/api/ui/public');
|
||||
if (!res.ok) return null;
|
||||
const data = (await res.json()) as { appearance?: UiAppearance };
|
||||
return data.appearance ?? null;
|
||||
const data = (await res.json()) as { appearance?: Partial<UiAppearance> };
|
||||
return data.appearance ? withAppearanceDefaults(data.appearance) : null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
@@ -593,7 +605,7 @@ function readCache(): UiAppearance {
|
||||
const parsed = JSON.parse(raw) as Partial<UiAppearance>;
|
||||
// Shallow merge over defaults so a cache written by an older build still
|
||||
// yields a complete object (the server is the real validator).
|
||||
return { ...DEFAULT_APPEARANCE, ...parsed, background: { ...DEFAULT_APPEARANCE.background, ...parsed.background } };
|
||||
return withAppearanceDefaults(parsed);
|
||||
} catch {
|
||||
return DEFAULT_APPEARANCE;
|
||||
}
|
||||
|
||||
@@ -653,52 +653,45 @@ html[data-cursor-system="custom"] .adaptive-cursor[data-visible="true"] {
|
||||
Global context menu
|
||||
────────────────────────────────────────────── */
|
||||
.global-context-menu {
|
||||
position: fixed;
|
||||
z-index: 2147483646;
|
||||
width: min(15.5rem, calc(100vw - 1rem));
|
||||
padding: 0.375rem;
|
||||
border-radius: calc(var(--radius) + 0.25rem);
|
||||
background: color-mix(in oklab, var(--popover) 92%, transparent);
|
||||
padding: 5px;
|
||||
border: 1px solid color-mix(in oklab, var(--border) 74%, transparent);
|
||||
border-radius: 14px;
|
||||
background: var(--popover);
|
||||
color: var(--popover-foreground);
|
||||
box-shadow:
|
||||
0 0 0 1px color-mix(in oklab, var(--foreground) 9%, transparent),
|
||||
0 2px 5px -2px oklch(0% 0 0 / 0.18),
|
||||
0 18px 48px -20px oklch(0% 0 0 / 0.42);
|
||||
0 1px 2px color-mix(in oklab, var(--foreground) 6%, transparent),
|
||||
0 16px 36px -18px color-mix(in oklab, var(--foreground) 50%, transparent);
|
||||
outline: none;
|
||||
overflow: hidden;
|
||||
backdrop-filter: blur(20px) saturate(1.15);
|
||||
overflow-y: auto;
|
||||
overscroll-behavior: contain;
|
||||
}
|
||||
|
||||
.global-context-menu-group {
|
||||
display: grid;
|
||||
gap: 0.125rem;
|
||||
}
|
||||
|
||||
.global-context-menu-separator {
|
||||
height: 1px;
|
||||
margin: 0.375rem 0.5rem;
|
||||
background: color-mix(in oklab, var(--border) 74%, transparent);
|
||||
margin: 4px;
|
||||
background: var(--border);
|
||||
}
|
||||
|
||||
.global-context-menu-item {
|
||||
display: grid;
|
||||
grid-template-columns: 1.125rem minmax(0, 1fr) auto;
|
||||
grid-template-columns: 1rem minmax(0, 1fr) auto;
|
||||
align-items: center;
|
||||
gap: 0.625rem;
|
||||
gap: 0.5rem;
|
||||
width: 100%;
|
||||
min-height: 2.5rem;
|
||||
height: 32px;
|
||||
padding: 0 0.625rem;
|
||||
border: 0;
|
||||
border-radius: calc(var(--radius) - 0.125rem);
|
||||
border-radius: 7px;
|
||||
background: transparent;
|
||||
color: var(--popover-foreground);
|
||||
font: inherit;
|
||||
text-align: left;
|
||||
scale: 1;
|
||||
outline: none;
|
||||
transition-property: color, background-color, box-shadow, scale, opacity;
|
||||
transition-duration: 140ms;
|
||||
transition-timing-function: ease-out;
|
||||
}
|
||||
.global-context-menu-item:hover,
|
||||
.global-context-menu-item:focus-visible {
|
||||
@@ -709,7 +702,7 @@ html[data-cursor-system="custom"] .adaptive-cursor[data-visible="true"] {
|
||||
box-shadow: inset 0 0 0 1px color-mix(in oklab, var(--ring) 45%, transparent);
|
||||
}
|
||||
.global-context-menu-item:active:not(:disabled) {
|
||||
scale: 0.96;
|
||||
background: color-mix(in oklab, var(--primary) 16%, var(--accent));
|
||||
}
|
||||
.global-context-menu-item:disabled {
|
||||
color: color-mix(in oklab, var(--muted-foreground) 62%, transparent);
|
||||
|
||||
@@ -512,8 +512,10 @@ export interface UiAppearance {
|
||||
density: Density;
|
||||
reduceMotion: boolean;
|
||||
disableMotion: boolean;
|
||||
/** Master switch for the adaptive cursor and custom context menu. */
|
||||
/** Use SnowLuma's adaptive cursor. */
|
||||
customPointerSystem: boolean;
|
||||
/** Replace the browser context menu with SnowLuma's context menu. */
|
||||
customContextMenu: boolean;
|
||||
highContrast: boolean;
|
||||
/** Pin the sidebar permanently expanded, opting out of the hover-expand rail. */
|
||||
sidebarPinned: boolean;
|
||||
|
||||
Reference in new issue
Block a user