feat(ui): 适配手机端布局与触控交互
窄屏改用抽屉导航和卡片列表,加大触控区域并处理安全区,避免表格横向溢出。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -1,25 +1,61 @@
|
||||
"use client";
|
||||
|
||||
import { createContext, useContext, useState } from "react";
|
||||
import { createContext, useCallback, useContext, useEffect, useMemo, useState } from "react";
|
||||
|
||||
type SidebarCtx = {
|
||||
collapsed: boolean;
|
||||
toggle: () => void;
|
||||
setCollapsed: (v: boolean) => void;
|
||||
mobileOpen: boolean;
|
||||
openMobile: () => void;
|
||||
closeMobile: () => void;
|
||||
toggleMobile: () => void;
|
||||
};
|
||||
|
||||
const Ctx = createContext<SidebarCtx | null>(null);
|
||||
|
||||
/** 折叠状态提供者:包裹侧边栏 + 主区,供二者共享 collapsed */
|
||||
/** 折叠状态提供者:包裹侧边栏 + 主区,供二者共享 collapsed / 手机抽屉 */
|
||||
export function SidebarProvider({ children }: { children: React.ReactNode }) {
|
||||
const [collapsed, setCollapsed] = useState(false);
|
||||
return (
|
||||
<Ctx.Provider
|
||||
value={{ collapsed, toggle: () => setCollapsed((v) => !v), setCollapsed }}
|
||||
>
|
||||
{children}
|
||||
</Ctx.Provider>
|
||||
const [mobileOpen, setMobileOpen] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
const mq = window.matchMedia("(min-width: 768px)");
|
||||
const onChange = () => {
|
||||
if (mq.matches) setMobileOpen(false);
|
||||
};
|
||||
mq.addEventListener("change", onChange);
|
||||
return () => mq.removeEventListener("change", onChange);
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
if (!mobileOpen) return;
|
||||
const prev = document.body.style.overflow;
|
||||
document.body.style.overflow = "hidden";
|
||||
return () => {
|
||||
document.body.style.overflow = prev;
|
||||
};
|
||||
}, [mobileOpen]);
|
||||
|
||||
const toggle = useCallback(() => setCollapsed((v) => !v), []);
|
||||
const openMobile = useCallback(() => setMobileOpen(true), []);
|
||||
const closeMobile = useCallback(() => setMobileOpen(false), []);
|
||||
const toggleMobile = useCallback(() => setMobileOpen((v) => !v), []);
|
||||
|
||||
const value = useMemo(
|
||||
() => ({
|
||||
collapsed,
|
||||
toggle,
|
||||
setCollapsed,
|
||||
mobileOpen,
|
||||
openMobile,
|
||||
closeMobile,
|
||||
toggleMobile,
|
||||
}),
|
||||
[collapsed, toggle, mobileOpen, openMobile, closeMobile, toggleMobile]
|
||||
);
|
||||
|
||||
return <Ctx.Provider value={value}>{children}</Ctx.Provider>;
|
||||
}
|
||||
|
||||
export function useSidebar() {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect } from "react";
|
||||
import Link from "next/link";
|
||||
import { usePathname } from "next/navigation";
|
||||
import { cn } from "@/lib/utils";
|
||||
@@ -11,60 +12,76 @@ import { useSidebar } from "./sidebar-context";
|
||||
export function Sidebar({ isSuperAdmin }: { isSuperAdmin: boolean }) {
|
||||
const pathname = usePathname();
|
||||
const active = activeHref(pathname);
|
||||
const { collapsed } = useSidebar();
|
||||
const { collapsed, mobileOpen, closeMobile } = useSidebar();
|
||||
const sections = navSections.filter((s) => !s.superAdminOnly || isSuperAdmin);
|
||||
|
||||
useEffect(() => {
|
||||
closeMobile();
|
||||
}, [pathname, closeMobile]);
|
||||
|
||||
return (
|
||||
<aside
|
||||
className={cn(
|
||||
"shrink-0 overflow-hidden transition-[width] duration-200 ease-in-out",
|
||||
collapsed ? "w-0" : "w-60"
|
||||
<>
|
||||
{mobileOpen && (
|
||||
<button
|
||||
type="button"
|
||||
aria-label="关闭菜单"
|
||||
className="fixed inset-0 z-30 bg-black/40 md:hidden"
|
||||
onClick={closeMobile}
|
||||
/>
|
||||
)}
|
||||
>
|
||||
{/* 固定宽度内容容器:折叠时被外层裁剪,形成平滑滑动而非挤压 */}
|
||||
<div className="flex h-full w-60 flex-col bg-[var(--color-sidebar)] text-[var(--color-sidebar-foreground)]">
|
||||
{/* 品牌 */}
|
||||
<div className="flex h-16 items-center px-5">
|
||||
<Link href="/">
|
||||
<BrandLogo size={36} invert />
|
||||
</Link>
|
||||
</div>
|
||||
|
||||
<nav className="flex-1 space-y-6 overflow-y-auto px-3 py-4">
|
||||
{sections.map((section) => (
|
||||
<div key={section.title}>
|
||||
<div className="px-3 pb-2 text-[11px] font-medium uppercase tracking-wider text-[var(--color-sidebar-muted)]">
|
||||
{section.title}
|
||||
</div>
|
||||
<div className="space-y-1">
|
||||
{section.items.map((item) => {
|
||||
const Icon = item.icon;
|
||||
const isActive = active === item.href;
|
||||
return (
|
||||
<Link
|
||||
key={item.href}
|
||||
href={item.href}
|
||||
className={cn(
|
||||
"flex items-center gap-3 rounded-lg px-3 py-2 text-sm transition-colors",
|
||||
isActive
|
||||
? "bg-[var(--color-primary)] text-white shadow-sm"
|
||||
: "text-[var(--color-sidebar-muted)] hover:bg-[var(--color-sidebar-hover)] hover:text-white"
|
||||
)}
|
||||
>
|
||||
<Icon className="h-[18px] w-[18px]" />
|
||||
{item.label}
|
||||
</Link>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
<aside
|
||||
className={cn(
|
||||
"z-40 overflow-hidden bg-[var(--color-sidebar)] text-[var(--color-sidebar-foreground)]",
|
||||
"fixed inset-y-0 left-0 w-60 transition-transform duration-200 ease-in-out",
|
||||
mobileOpen ? "translate-x-0" : "-translate-x-full",
|
||||
"md:static md:translate-x-0 md:shrink-0 md:transition-[width]",
|
||||
collapsed ? "md:w-0" : "md:w-60"
|
||||
)}
|
||||
>
|
||||
<div className="flex h-full w-60 flex-col pt-[env(safe-area-inset-top)] pb-[env(safe-area-inset-bottom)]">
|
||||
<div className="flex h-16 items-center px-5">
|
||||
<Link href="/" onClick={closeMobile}>
|
||||
<BrandLogo size={36} invert />
|
||||
</Link>
|
||||
</div>
|
||||
))}
|
||||
</nav>
|
||||
|
||||
<div className="px-5 py-4 text-[11px] text-[var(--color-sidebar-muted)]">
|
||||
{copyright()}
|
||||
</div>
|
||||
</div>
|
||||
</aside>
|
||||
<nav className="flex-1 space-y-6 overflow-y-auto overscroll-contain px-3 py-4">
|
||||
{sections.map((section) => (
|
||||
<div key={section.title}>
|
||||
<div className="px-3 pb-2 text-[11px] font-medium uppercase tracking-wider text-[var(--color-sidebar-muted)]">
|
||||
{section.title}
|
||||
</div>
|
||||
<div className="space-y-1">
|
||||
{section.items.map((item) => {
|
||||
const Icon = item.icon;
|
||||
const isActive = active === item.href;
|
||||
return (
|
||||
<Link
|
||||
key={item.href}
|
||||
href={item.href}
|
||||
onClick={closeMobile}
|
||||
className={cn(
|
||||
"flex min-h-11 items-center gap-3 rounded-lg px-3 py-2.5 text-sm transition-colors md:min-h-0 md:py-2",
|
||||
isActive
|
||||
? "bg-[var(--color-primary)] text-white shadow-sm"
|
||||
: "text-[var(--color-sidebar-muted)] hover:bg-[var(--color-sidebar-hover)] hover:text-white"
|
||||
)}
|
||||
>
|
||||
<Icon className="h-[18px] w-[18px]" />
|
||||
{item.label}
|
||||
</Link>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</nav>
|
||||
|
||||
<div className="px-5 py-4 text-[11px] text-[var(--color-sidebar-muted)]">
|
||||
{copyright()}
|
||||
</div>
|
||||
</div>
|
||||
</aside>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -29,36 +29,44 @@ export function Topbar({
|
||||
const pathname = usePathname();
|
||||
const [open, setOpen] = useState(false);
|
||||
const ref = useRef<HTMLDivElement>(null);
|
||||
const { toggle } = useSidebar();
|
||||
const { toggle, toggleMobile, mobileOpen } = useSidebar();
|
||||
|
||||
function onMenu() {
|
||||
if (window.matchMedia("(min-width: 768px)").matches) toggle();
|
||||
else toggleMobile();
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
function onClick(e: MouseEvent) {
|
||||
function onClick(e: Event) {
|
||||
if (ref.current && !ref.current.contains(e.target as Node)) setOpen(false);
|
||||
}
|
||||
document.addEventListener("mousedown", onClick);
|
||||
return () => document.removeEventListener("mousedown", onClick);
|
||||
document.addEventListener("pointerdown", onClick);
|
||||
return () => document.removeEventListener("pointerdown", onClick);
|
||||
}, []);
|
||||
|
||||
const roleLabel = user.roles.includes("super admin") ? "超级管理员" : "业务员";
|
||||
|
||||
return (
|
||||
<header className="sticky top-0 z-20 flex h-16 items-center justify-between border-b bg-[var(--color-surface)]/90 px-6 backdrop-blur">
|
||||
<div className="flex items-center gap-3">
|
||||
<header className="sticky top-0 z-20 flex h-[calc(4rem+env(safe-area-inset-top))] items-center justify-between border-b bg-[var(--color-surface)]/90 px-3 pt-[env(safe-area-inset-top)] backdrop-blur md:px-6">
|
||||
<div className="flex min-w-0 items-center gap-2 md:gap-3">
|
||||
<button
|
||||
type="button"
|
||||
onClick={toggle}
|
||||
aria-label="收起/展开菜单"
|
||||
className="-ml-1.5 rounded-md p-1.5 text-[var(--color-muted-foreground)] transition-colors hover:bg-[var(--color-muted)] hover:text-[var(--color-foreground)]"
|
||||
onClick={onMenu}
|
||||
aria-label="打开/关闭菜单"
|
||||
aria-expanded={mobileOpen}
|
||||
className="-ml-1 flex h-11 w-11 items-center justify-center rounded-md text-[var(--color-muted-foreground)] transition-colors hover:bg-[var(--color-muted)] hover:text-[var(--color-foreground)] md:h-auto md:w-auto md:p-1.5"
|
||||
>
|
||||
<PanelLeft className="h-[18px] w-[18px]" />
|
||||
</button>
|
||||
<h1 className="text-base font-semibold">{pageTitle(pathname)}</h1>
|
||||
<h1 className="truncate text-base font-semibold">{pageTitle(pathname)}</h1>
|
||||
</div>
|
||||
|
||||
<div className="relative" ref={ref}>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setOpen((v) => !v)}
|
||||
className="flex items-center gap-2.5 rounded-full py-1 pl-1 pr-2.5 transition-colors hover:bg-[var(--color-muted)]"
|
||||
className="flex min-h-11 items-center gap-2.5 rounded-full py-1 pl-1 pr-2.5 transition-colors hover:bg-[var(--color-muted)]"
|
||||
aria-expanded={open}
|
||||
>
|
||||
<Avatar name={user.name ?? user.username} src={user.image} size={32} />
|
||||
<div className="hidden text-left leading-tight sm:block">
|
||||
@@ -73,7 +81,7 @@ export function Topbar({
|
||||
<Link
|
||||
href="/me"
|
||||
onClick={() => setOpen(false)}
|
||||
className="flex items-center gap-2.5 px-4 py-2 text-sm hover:bg-[var(--color-muted)]"
|
||||
className="flex min-h-11 items-center gap-2.5 px-4 py-2 text-sm hover:bg-[var(--color-muted)] md:min-h-0"
|
||||
>
|
||||
<User className="h-4 w-4" /> 个人中心
|
||||
</Link>
|
||||
@@ -85,7 +93,7 @@ export function Topbar({
|
||||
window.location.assign("/login");
|
||||
});
|
||||
}}
|
||||
className="flex w-full items-center gap-2.5 px-4 py-2 text-sm text-[var(--color-danger)] hover:bg-[var(--color-danger-soft)]"
|
||||
className="flex min-h-11 w-full items-center gap-2.5 px-4 py-2 text-sm text-[var(--color-danger)] hover:bg-[var(--color-danger-soft)] md:min-h-0"
|
||||
>
|
||||
<LogOut className="h-4 w-4" /> 退出登录
|
||||
</button>
|
||||
|
||||
@@ -2,7 +2,7 @@ import * as React from "react";
|
||||
import { cn } from "@/lib/utils";
|
||||
|
||||
const base =
|
||||
"inline-flex items-center justify-center gap-2 rounded-[var(--radius)] text-sm font-medium transition-all focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[var(--color-ring)] disabled:pointer-events-none disabled:opacity-50 whitespace-nowrap active:scale-[0.98]";
|
||||
"inline-flex items-center justify-center gap-2 rounded-[var(--radius)] text-sm font-medium transition-all focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[var(--color-ring)] disabled:pointer-events-none disabled:opacity-50 whitespace-nowrap active:scale-[0.98] touch-manipulation";
|
||||
|
||||
const variants = {
|
||||
primary:
|
||||
@@ -18,10 +18,10 @@ const variants = {
|
||||
} as const;
|
||||
|
||||
const sizes = {
|
||||
default: "h-9 px-4",
|
||||
sm: "h-8 px-3 text-[13px]",
|
||||
lg: "h-10 px-5",
|
||||
icon: "h-9 w-9",
|
||||
default: "h-11 px-4 md:h-9",
|
||||
sm: "h-9 px-3 text-[13px] md:h-8",
|
||||
lg: "h-12 px-5 md:h-10",
|
||||
icon: "h-11 w-11 md:h-9 md:w-9",
|
||||
} as const;
|
||||
|
||||
export interface ButtonProps
|
||||
|
||||
@@ -19,7 +19,7 @@ export function CardHeader({
|
||||
}: React.HTMLAttributes<HTMLDivElement>) {
|
||||
return (
|
||||
<div
|
||||
className={cn("flex items-center justify-between border-b px-5 py-4", className)}
|
||||
className={cn("flex flex-wrap items-center justify-between gap-2 border-b px-4 py-3 md:px-5 md:py-4", className)}
|
||||
{...props}
|
||||
/>
|
||||
);
|
||||
@@ -29,12 +29,12 @@ export function CardTitle({
|
||||
className,
|
||||
...props
|
||||
}: React.HTMLAttributes<HTMLHeadingElement>) {
|
||||
return <h3 className={cn("text-sm font-semibold", className)} {...props} />;
|
||||
return <h3 className={cn("min-w-0 text-sm font-semibold break-words", className)} {...props} />;
|
||||
}
|
||||
|
||||
export function CardContent({
|
||||
className,
|
||||
...props
|
||||
}: React.HTMLAttributes<HTMLDivElement>) {
|
||||
return <div className={cn("p-5", className)} {...props} />;
|
||||
return <div className={cn("p-4 md:p-5", className)} {...props} />;
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ import * as React from "react";
|
||||
import { cn } from "@/lib/utils";
|
||||
|
||||
const fieldCls =
|
||||
"flex h-9 w-full rounded-[var(--radius)] border bg-[var(--color-surface)] px-3 text-sm shadow-sm transition-colors placeholder:text-[var(--color-muted-foreground)] focus-visible:outline-none focus-visible:border-[var(--color-primary)] focus-visible:ring-2 focus-visible:ring-[var(--color-ring)] disabled:cursor-not-allowed disabled:opacity-50";
|
||||
"flex h-11 w-full rounded-[var(--radius)] border bg-[var(--color-surface)] px-3 text-base shadow-sm transition-colors placeholder:text-[var(--color-muted-foreground)] focus-visible:outline-none focus-visible:border-[var(--color-primary)] focus-visible:ring-2 focus-visible:ring-[var(--color-ring)] disabled:cursor-not-allowed disabled:opacity-50 md:h-9 md:text-sm";
|
||||
|
||||
export const Input = React.forwardRef<
|
||||
HTMLInputElement,
|
||||
|
||||
@@ -14,8 +14,8 @@ export function Table({
|
||||
return (
|
||||
<div className="w-full overflow-x-auto">
|
||||
<table
|
||||
className={cn(
|
||||
"w-full text-sm",
|
||||
className={cn(
|
||||
"w-full min-w-max text-sm",
|
||||
bordered &&
|
||||
"border border-[var(--color-border)] [&_tr]:divide-x [&_tr]:divide-[var(--color-border)]",
|
||||
striped && "[&_tbody_tr:nth-child(even)]:bg-[var(--color-muted)]/50",
|
||||
@@ -46,9 +46,9 @@ export function TR({ className, ...props }: React.HTMLAttributes<HTMLTableRowEle
|
||||
}
|
||||
|
||||
export function TH({ className, ...props }: React.ThHTMLAttributes<HTMLTableCellElement>) {
|
||||
return <th className={cn("px-4 py-3 font-medium", className)} {...props} />;
|
||||
return <th className={cn("px-3 py-2.5 font-medium md:px-4 md:py-3", className)} {...props} />;
|
||||
}
|
||||
|
||||
export function TD({ className, ...props }: React.TdHTMLAttributes<HTMLTableCellElement>) {
|
||||
return <td className={cn("px-4 py-3 align-middle", className)} {...props} />;
|
||||
return <td className={cn("px-3 py-2.5 align-middle md:px-4 md:py-3", className)} {...props} />;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user