feat(canvas): fix rotation, clarify frame membership and load icons on demand
This commit is contained in:
@@ -1,9 +1,24 @@
|
||||
"use client";
|
||||
import { DynamicIcon } from "lucide-react/dynamic";
|
||||
import { Star, type LucideProps } from "lucide-react";
|
||||
import { iconsById } from "./icon-catalog";
|
||||
export function CanvasIcon({ name, ...props }: LucideProps & { name?: string }) {
|
||||
const icon = iconsById.get(name || "Star");
|
||||
if (!icon) return <Star {...props} />;
|
||||
return <DynamicIcon name={icon.slug} {...props} fallback={() => <Star {...props} />} />;
|
||||
import { createElement, useEffect, useState } from "react";
|
||||
import type { LucideProps } from "lucide-react";
|
||||
import { loadIcon, peekIcon, type IconPaths } from "@/lib/canvas-icon-loader";
|
||||
|
||||
export function CanvasIcon({ name = "Star", size = 24, width = size, height = size, strokeWidth = 2, absoluteStrokeWidth, ...props }: LucideProps & { name?: string }) {
|
||||
const [result, setResult] = useState<{ name: string; paths?: IconPaths; error?: boolean }>(() => ({ name, paths: peekIcon(name) }));
|
||||
const [retry, setRetry] = useState(0);
|
||||
useEffect(() => {
|
||||
const controller = new AbortController();
|
||||
setResult({ name, paths: peekIcon(name) });
|
||||
loadIcon(name, controller.signal).then((paths) => setResult({ name, paths }), (error) => {
|
||||
if (error.name !== "AbortError") setResult({ name, error: true });
|
||||
});
|
||||
return () => controller.abort();
|
||||
}, [name, retry]);
|
||||
const paths = result.name === name ? result.paths : undefined;
|
||||
const failed = result.name === name && result.error;
|
||||
return <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width={width} height={height} fill="none" stroke="currentColor" strokeWidth={absoluteStrokeWidth && typeof size === "number" ? Number(strokeWidth) * 24 / size : strokeWidth} strokeLinecap="round" strokeLinejoin="round"
|
||||
{...props} data-icon-id={name} data-icon-state={paths ? "loaded" : failed ? "error" : "loading"}
|
||||
onClick={failed ? (event) => { event.stopPropagation(); setRetry((value) => value + 1); } : props.onClick}>
|
||||
{paths ? paths.map(([tag, attributes], index) => createElement(tag, { ...attributes, key: attributes.key ?? index })) : failed ? <><title>图标加载失败,点击重试</title><path d="m8 8 8 8m0-8-8 8" opacity=".5" /></> : <rect x="5" y="5" width="14" height="14" rx="3" opacity=".2" strokeDasharray="2 3" />}
|
||||
</svg>;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user