feat(canvas): fix rotation, clarify frame membership and load icons on demand
This commit is contained in:
@@ -1,26 +1,60 @@
|
||||
"use client";
|
||||
import { useEffect, useMemo, useRef, useState } from "react";
|
||||
import { AnimatePresence, motion, useReducedMotion } from "motion/react";
|
||||
import { motion, useReducedMotion } from "motion/react";
|
||||
import { createPortal } from "react-dom";
|
||||
import { X } from "lucide-react";
|
||||
import { CanvasIcon } from "./canvas-icon";
|
||||
import { iconCatalog, searchIcons } from "./icon-catalog";
|
||||
import { iconCatalog, iconCategories, searchIcons, type IconSource } from "./icon-catalog";
|
||||
import styles from "./canvas.module.css";
|
||||
export function IconPicker({ open, close, choose }: { open: boolean; close: () => void; choose: (name: string, label: string) => void }) {
|
||||
|
||||
const columns = 5, rowHeight = 48;
|
||||
export function IconPicker({ close, choose }: { close: () => void; choose: (name: string, label: string) => void }) {
|
||||
const [query, setQuery] = useState("");
|
||||
const [limit, setLimit] = useState(80);
|
||||
const matches = useMemo(() => searchIcons(query), [query]);
|
||||
const [category, setCategory] = useState("common");
|
||||
const [source, setSource] = useState<IconSource>("all");
|
||||
const [scrollTop, setScrollTop] = useState(0);
|
||||
const [height, setHeight] = useState(240);
|
||||
const [focused, setFocused] = useState(0);
|
||||
const search = useRef<HTMLInputElement>(null);
|
||||
const grid = useRef<HTMLDivElement>(null);
|
||||
const reduce = useReducedMotion();
|
||||
useEffect(() => { if (open) { search.current?.focus(); setLimit(80); } }, [open]);
|
||||
if (typeof document === "undefined") return null;
|
||||
return createPortal(<AnimatePresence>{open && <motion.aside key="icons" initial={{ opacity: 0, x: reduce ? 0 : -12 }} animate={{ opacity: 1, x: 0 }} exit={{ opacity: 0, x: reduce ? 0 : -12 }} transition={{ duration: reduce ? 0 : 0.16 }} className={styles.iconPicker} role="dialog" aria-label="添加图标" onKeyDown={(event) => { event.stopPropagation(); if (event.key === "Escape") close(); }}>
|
||||
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]);
|
||||
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);
|
||||
const visible = matches.slice(firstRow * columns, lastRow * columns);
|
||||
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);
|
||||
}
|
||||
requestAnimationFrame(() => grid.current?.querySelector<HTMLButtonElement>(`[data-index="${next}"]`)?.focus());
|
||||
}
|
||||
return createPortal(<motion.aside initial={{ opacity: 0, x: reduce ? 0 : -12 }} animate={{ opacity: 1, x: 0 }} transition={{ duration: reduce ? 0 : 0.16 }} className={styles.iconPicker} role="dialog" aria-label="添加图标" onKeyDown={(event) => { event.stopPropagation(); if (event.key === "Escape") close(); }}>
|
||||
<header><strong>图标</strong><button aria-label="关闭图标面板" onClick={close}><X size={16} /></button></header>
|
||||
<input ref={search} type="search" aria-label="搜索图标" placeholder="搜索图标…" value={query} onChange={(event) => { setQuery(event.target.value); setLimit(80); }} />
|
||||
<div className={styles.iconGrid}>{matches.slice(0, limit).map(({ name, label }) => <button key={name} title={label} aria-label={`添加${label}图标`} onClick={() => choose(name, label)}><CanvasIcon name={name} size={24} /></button>)}</div>
|
||||
<label className={styles.iconSource}>图标库<select aria-label="图标库" value={source} onChange={(event) => setSource(event.target.value as IconSource)}><option value="all">全部图标库</option><option value="lucide">Lucide · 2313</option><option value="tabler">Tabler · 24 个试用</option></select></label>
|
||||
<input ref={search} type="search" aria-label="搜索图标" placeholder="搜索当前库的全部图标…" value={query} onChange={(event) => setQuery(event.target.value)} />
|
||||
<div className={styles.iconCategories} aria-label="图标分类">{iconCategories.map(([id, label]) => <button key={id} aria-pressed={!query.trim() && category === id} onClick={() => { setCategory(id); setQuery(""); }}>{label}</button>)}</div>
|
||||
<div ref={grid} className={styles.virtualIconGrid} aria-label="图标列表" onScroll={(event) => setScrollTop(event.currentTarget.scrollTop)}>
|
||||
<div style={{ height: rows * rowHeight, position: "relative" }}>{visible.map(({ name, label, source }, offset) => {
|
||||
const index = firstRow * columns + offset;
|
||||
return <button key={name} type="button" data-index={index} tabIndex={focused === index ? 0 : -1} title={`${label} · ${source === "tabler" ? "Tabler" : "Lucide"}`} aria-label={`添加${label}图标`} style={{ position: "absolute", width: "20%", height: rowHeight, left: `${index % columns * 20}%`, top: Math.floor(index / columns) * rowHeight }} onFocus={() => setFocused(index)} onClick={() => choose(name, label)}
|
||||
onKeyDown={(event) => { const delta = { ArrowRight: 1, ArrowLeft: -1, ArrowDown: columns, ArrowUp: -columns, Home: -index, End: matches.length - 1 - index }[event.key]; if (delta !== undefined) { event.preventDefault(); focusIcon(index + delta); } }}><CanvasIcon name={name} size={24} /></button>;
|
||||
})}</div>
|
||||
</div>
|
||||
{!matches.length && <p>未找到图标</p>}
|
||||
<footer>Lucide · {iconCatalog.length} 个图标 · 已显示 {Math.min(limit, matches.length)} / {matches.length}
|
||||
{limit < matches.length && <button type="button" onClick={() => setLimit(limit + 80)}>加载更多</button>}
|
||||
</footer>
|
||||
</motion.aside>}</AnimatePresence>, document.body);
|
||||
<footer>{matches.length} 个结果 · 共 {iconCatalog.length} 个 · 随滚动加载</footer>
|
||||
</motion.aside>, document.body);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user