/** * 图标选择面板。按来源、分类与文本查询筛选目录,使用固定五列虚拟列表只挂载视窗附近的图标按钮。 * 仅保存搜索/滚动/焦点状态;choose把名称和标签交给画布创建节点,实际SVG按需加载。 */ "use client"; import { useEffect, useMemo, useRef, useState } from "react"; import { motion, useReducedMotion } from "motion/react"; import { createPortal } from "react-dom"; import { X } from "lucide-react"; import { CanvasIcon } from "./canvas-icon"; import { iconCatalog, iconCategories, searchIcons, type IconSource } from "./icon-catalog"; import styles from "./canvas.module.css"; /** 固定网格参数:每行五项、每行48px;占位总高与绝对定位必须使用同一组参数。 */ const columns = 5, rowHeight = 48; /** 通过Portal挂载到body,调用方在浏览器中打开;Escape或关闭按钮只触发close。 */ export function IconPicker({ close, choose }: { close: () => void; choose: (name: string, label: string) => void }) { const [query, setQuery] = useState(""); const [category, setCategory] = useState("common"); const [source, setSource] = useState("all"); const [scrollTop, setScrollTop] = useState(0); const [height, setHeight] = useState(240); const [focused, setFocused] = useState(0); const search = useRef(null); const grid = useRef(null); const reduce = useReducedMotion(); // 有非空查询时搜索当前来源的全部分类;无查询才使用分类按钮值,useMemo避免无关渲染重复过滤。 const matches = useMemo(() => searchIcons(query, query.trim() ? "all" : category, source), [query, category, source]); // 挂载时聚焦搜索框并观察列表实际高度;卸载断开观察器,尝试把焦点还给打开前的元素。 useEffect(() => { const previous = document.activeElement as HTMLElement | null; search.current?.focus(); const observer = new ResizeObserver(([entry]) => setHeight(entry.contentRect.height)); if (grid.current) observer.observe(grid.current); return () => { observer.disconnect(); previous?.focus(); }; }, []); // 任一筛选条件变化都回到列表顶部与首项焦点索引,避免沿用上次结果中的越界位置。 useEffect(() => { if (grid.current) grid.current.scrollTop = 0; setScrollTop(0); setFocused(0); }, [query, category, source]); // 根据总行数撑起滚动区域,上下各多挂载一行缓冲;slice只取可见范围的目录项。 const rows = Math.ceil(matches.length / columns); const firstRow = Math.max(0, Math.floor(scrollTop / rowHeight) - 1); const lastRow = Math.min(rows, Math.ceil((scrollTop + height) / rowHeight) + 1); // 虚拟化只限制挂载按钮,matches 仍保留完整同步筛选结果;不是分批解析/下载目录。 const visible = matches.slice(firstRow * columns, lastRow * columns); /** 先钳制目标索引,再按行把目标滚入视口;等下一帧虚拟按钮挂载后调用focus。 * 空结果没有可聚焦按钮,最终可选链自然不执行focus;不凭空创建占位项目。 */ function focusIcon(index: number) { const next = Math.max(0, Math.min(matches.length - 1, index)); setFocused(next); const top = Math.floor(next / columns) * rowHeight; if (grid.current) { if (top < grid.current.scrollTop) grid.current.scrollTop = top; else if (top + rowHeight > grid.current.scrollTop + height) grid.current.scrollTop = top + rowHeight - height; setScrollTop(grid.current.scrollTop); } // 帧未单独取消且不带筛选代次;执行时读最新 grid,筛选变化可能让同一 index 指向另一条结果。 requestAnimationFrame(() => grid.current?.querySelector(`[data-index="${next}"]`)?.focus()); } // Portal 直接使用 document.body,仅供客户端挂载;dialog 角色不自动提供焦点陷阱或背景 inert。 // 分类点击同时清空搜索,来源选择保留查询;网格按钮按全局结果索引绝对定位,不按切片局部索引。 // 左右移动一项、上下移动一行,Home/End跳至结果首尾;只有当前focused项进入Tab顺序。 // 每个按钮由CanvasIcon加载路径,页脚结果数来自筛选数组;choose后是否关闭由调用方决定。 return createPortal( { event.stopPropagation(); if (event.key === "Escape") close(); }}>
图标
setQuery(event.target.value)} />
{iconCategories.map(([id, label]) => )}
setScrollTop(event.currentTarget.scrollTop)}>
{visible.map(({ name, label, source }, offset) => { // 加回首行偏移取得完整结果索引,保证滚动后的定位、Tab状态与键盘导航仍对应同一条记录。 const index = firstRow * columns + offset; return ; })}
{!matches.length &&

未找到图标

}
, document.body); }