From f159b2f41a3b0513d0acb4e84f4d339b06be61b8 Mon Sep 17 00:00:00 2001
From: DrSmoothl <1787882683@qq.com>
Date: Fri, 31 Jul 2026 16:56:27 +0800
Subject: [PATCH] feat(webui): morph the mobile menu control
Give mobile navigation a clear transition that follows the real drawer state.
Preserve accessible open and close labels.
---
.../src/components/interior/icon-morph.tsx | 268 ++++++++++++++++++
.../src/components/layout/main-layout.tsx | 3 +-
.../webui/src/components/layout/top-bar.tsx | 20 +-
packages/webui/tests/icon-morph.test.tsx | 42 +++
4 files changed, 326 insertions(+), 7 deletions(-)
create mode 100644 packages/webui/src/components/interior/icon-morph.tsx
create mode 100644 packages/webui/tests/icon-morph.test.tsx
diff --git a/packages/webui/src/components/interior/icon-morph.tsx b/packages/webui/src/components/interior/icon-morph.tsx
new file mode 100644
index 00000000..efa9936d
--- /dev/null
+++ b/packages/webui/src/components/interior/icon-morph.tsx
@@ -0,0 +1,268 @@
+'use client';
+
+import { useCallback, useMemo, useState } from 'react';
+import { motion, useReducedMotion } from 'motion/react';
+
+const CELL = { type: 'spring', stiffness: 520, damping: 34, mass: 0.45 } as const;
+const CROSSFADE = { type: 'spring', stiffness: 260, damping: 34, mass: 0.8 } as const;
+const INSTANT = { duration: 0 } as const;
+
+const NUMBER = /-?\d*\.?\d+/g;
+const CENTER = '12';
+
+export type MorphShape = {
+ d: readonly string[];
+ rotate?: number;
+};
+
+export type IconMorphMode = 'stroke' | 'fill';
+
+export type IconMorphPreset =
+ | 'menu-close'
+ | 'play-pause'
+ | 'plus-minus'
+ | 'check-close';
+
+export type IconMorphSlot = {
+ key: number;
+ d: string;
+ visible: boolean;
+};
+
+export type IconMorphSemantics = 'label' | 'pressed' | 'expanded';
+
+export const iconMorphPresets: Record<
+ IconMorphPreset,
+ { mode: IconMorphMode; labels: readonly string[]; shapes: readonly MorphShape[] }
+> = {
+ 'menu-close': {
+ mode: 'stroke',
+ labels: ['Menu', 'Close'],
+ shapes: [
+ {
+ rotate: 0,
+ d: ['M 4 7 L 20 7', 'M 4 12 L 20 12', 'M 4 17 L 20 17'],
+ },
+ {
+ rotate: 90,
+ d: ['M 6.5 6.5 L 17.5 17.5', 'M 12 12 L 12 12', 'M 6.5 17.5 L 17.5 6.5'],
+ },
+ ],
+ },
+ 'play-pause': {
+ mode: 'fill',
+ labels: ['Play', 'Pause'],
+ shapes: [
+ {
+ d: [
+ 'M 8 5 L 14 8.5 L 14 15.5 L 8 19 Z',
+ 'M 14 8.5 L 20 12 L 20 12 L 14 15.5 Z',
+ ],
+ },
+ {
+ d: [
+ 'M 8 5 L 11.5 5 L 11.5 19 L 8 19 Z',
+ 'M 15 5 L 18.5 5 L 18.5 19 L 15 19 Z',
+ ],
+ },
+ ],
+ },
+ 'plus-minus': {
+ mode: 'stroke',
+ labels: ['Add', 'Remove'],
+ shapes: [
+ { rotate: 0, d: ['M 5 12 L 19 12', 'M 12 5 L 12 19'] },
+ { rotate: 180, d: ['M 5 12 L 19 12', 'M 5 12 L 19 12'] },
+ ],
+ },
+ 'check-close': {
+ mode: 'stroke',
+ labels: ['Confirm', 'Cancel'],
+ shapes: [
+ { d: ['M 5 12.5 L 10 17.5 L 19.5 7', 'M 12 12 L 12 12 L 12 12'] },
+ { d: ['M 6.5 6.5 L 12 12 L 17.5 17.5', 'M 17.5 6.5 L 12 12 L 6.5 17.5'] },
+ ],
+ },
+};
+
+function isCollapsed(d: string): boolean {
+ const nums = d.match(NUMBER);
+ if (!nums || nums.length < 4) return false;
+ return nums.every((n, i) => n === nums[i % 2]);
+}
+
+function normalize(shapes: readonly MorphShape[]): IconMorphSlot[][] {
+ const slots = shapes.reduce((most, s) => Math.max(most, s.d.length), 0);
+
+ return shapes.map((shape) =>
+ Array.from({ length: slots }, (_, i) => {
+ const own = shape.d[i];
+ const sibling = shapes.find((s) => s.d[i] !== undefined)?.d[i] ?? '';
+ const d = own ?? sibling.replace(NUMBER, CENTER);
+ return { key: i, d, visible: !isCollapsed(d) };
+ }),
+ );
+}
+
+function toIndex(value: number | boolean): number {
+ return typeof value === 'boolean' ? (value ? 1 : 0) : Math.trunc(value);
+}
+
+export type UseIconMorphOptions = {
+ preset?: IconMorphPreset;
+ shapes?: readonly MorphShape[];
+ mode?: IconMorphMode;
+ labels?: readonly string[];
+ active?: number | boolean;
+ defaultActive?: number | boolean;
+ onActiveChange?: (index: number) => void;
+};
+
+export function useIconMorph({
+ preset = 'menu-close',
+ shapes,
+ mode,
+ labels,
+ active,
+ defaultActive = 0,
+ onActiveChange,
+}: UseIconMorphOptions = {}) {
+ const base = iconMorphPresets[preset];
+ const source = shapes ?? base.shapes;
+ const names = labels ?? base.labels;
+ const count = source.length;
+
+ const [internal, setInternal] = useState(() => toIndex(defaultActive));
+ const reduced = useReducedMotion();
+
+ const raw = active === undefined ? internal : toIndex(active);
+ const index = count === 0 ? 0 : Math.min(Math.max(raw, 0), count - 1);
+
+ const frames = useMemo(() => normalize(source), [source]);
+
+ const setIndex = useCallback(
+ (next: number) => {
+ if (count === 0) return;
+ const wrapped = ((next % count) + count) % count;
+ if (active === undefined) setInternal(wrapped);
+ onActiveChange?.(wrapped);
+ },
+ [active, count, onActiveChange],
+ );
+
+ const toggle = useCallback(() => setIndex(index + 1), [setIndex, index]);
+
+ return {
+ index,
+ count,
+ slots: frames[index] ?? [],
+ rotate: source[index]?.rotate ?? 0,
+ mode: mode ?? base.mode,
+ label: names[index] ?? '',
+ labels: names,
+ transition: reduced ? INSTANT : CELL,
+ labelTransition: reduced ? INSTANT : CROSSFADE,
+ setIndex,
+ toggle,
+ };
+}
+
+export type IconMorphProps = UseIconMorphOptions & {
+ size?: number;
+ strokeWidth?: number;
+ showLabel?: boolean;
+ semantics?: IconMorphSemantics;
+ disabled?: boolean;
+ className?: string;
+};
+
+export function IconMorph({
+ size = 20,
+ strokeWidth = 1.75,
+ showLabel = false,
+ semantics = 'label',
+ disabled = false,
+ className = '',
+ ...options
+}: IconMorphProps) {
+ const {
+ index,
+ slots,
+ rotate,
+ mode,
+ label,
+ labels,
+ transition,
+ labelTransition,
+ toggle,
+ } = useIconMorph(options);
+
+ const stroked = mode === 'stroke';
+
+ return (
+
+
+
+
+
+ {showLabel && (
+
+ {labels.map((text, i) => (
+
+ {text}
+
+ ))}
+
+ )}
+
+ );
+}
diff --git a/packages/webui/src/components/layout/main-layout.tsx b/packages/webui/src/components/layout/main-layout.tsx
index cdb5fb3e..8e3425ea 100644
--- a/packages/webui/src/components/layout/main-layout.tsx
+++ b/packages/webui/src/components/layout/main-layout.tsx
@@ -118,7 +118,8 @@ export function MainLayout({ status, onLogout, notice, children }: MainLayoutPro
{!kiosk && (
setMobileOpen(true)}
+ mobileOpen={mobileOpen}
+ onMobileOpenChange={setMobileOpen}
onLogout={onLogout}
isMobile={isMobile}
/>
diff --git a/packages/webui/src/components/layout/top-bar.tsx b/packages/webui/src/components/layout/top-bar.tsx
index 5846706d..386b90c6 100644
--- a/packages/webui/src/components/layout/top-bar.tsx
+++ b/packages/webui/src/components/layout/top-bar.tsx
@@ -1,7 +1,8 @@
import { useState } from 'react';
-import { LogOut, Menu, Monitor } from 'lucide-react';
+import { LogOut, Monitor } from 'lucide-react';
import { motion, AnimatePresence } from 'motion/react';
import { useRouterState } from '@tanstack/react-router';
+import { IconMorph } from '@/components/interior/icon-morph';
import { Button } from '@/components/ui/button';
import { Badge } from '@/components/ui/badge';
import { ThemeToggle } from '@/components/theme-toggle';
@@ -20,14 +21,16 @@ export const TOPBAR_CATALOGUE: { id: string; label: string }[] = [
interface TopBarProps {
status: string;
- onOpenMobile: () => void;
+ mobileOpen: boolean;
+ onMobileOpenChange: (open: boolean) => void;
onLogout: () => void;
isMobile: boolean;
}
export function TopBar({
status,
- onOpenMobile,
+ mobileOpen,
+ onMobileOpenChange,
onLogout,
isMobile,
}: TopBarProps) {
@@ -53,9 +56,14 @@ export function TopBar({
sidebar auto-expands on hover/focus, and its boundary with the content
is a soft surface-tone shift, not a hard border. */}
{isMobile && (
-
+ onMobileOpenChange(index === 1)}
+ className="ui-button ui-button-icon topbar-control cursor-pointer border-0 bg-transparent text-foreground hover:bg-accent hover:text-accent-foreground focus-visible:ring-[3px] focus-visible:ring-ring/40 dark:border-0 dark:bg-transparent dark:text-foreground dark:focus-visible:ring-ring/40"
+ />
)}
{/* Page title */}
diff --git a/packages/webui/tests/icon-morph.test.tsx b/packages/webui/tests/icon-morph.test.tsx
new file mode 100644
index 00000000..f199ba4b
--- /dev/null
+++ b/packages/webui/tests/icon-morph.test.tsx
@@ -0,0 +1,42 @@
+import { renderToStaticMarkup } from 'react-dom/server';
+import { describe, expect, it } from 'vitest';
+import { IconMorph, iconMorphPresets } from '../src/components/interior/icon-morph';
+
+describe('mobile menu icon morph', () => {
+ it('keeps the Interior menu and close geometry unchanged', () => {
+ expect(iconMorphPresets['menu-close'].shapes).toEqual([
+ {
+ rotate: 0,
+ d: ['M 4 7 L 20 7', 'M 4 12 L 20 12', 'M 4 17 L 20 17'],
+ },
+ {
+ rotate: 90,
+ d: ['M 6.5 6.5 L 17.5 17.5', 'M 12 12 L 12 12', 'M 6.5 17.5 L 17.5 6.5'],
+ },
+ ]);
+ });
+
+ it('reflects the controlled sheet state in its accessible semantics', () => {
+ const menu = renderToStaticMarkup(
+ ,
+ );
+ const close = renderToStaticMarkup(
+ ,
+ );
+
+ expect(menu).toContain('aria-label="打开菜单"');
+ expect(menu).toContain('aria-expanded="false"');
+ expect(close).toContain('aria-label="关闭菜单"');
+ expect(close).toContain('aria-expanded="true"');
+ });
+});