Files
community-rule/app/components/asset/DiagramGlyph/DiagramGlyph.tsx
T

160 lines
3.9 KiB
TypeScript

"use client";
/**
* Figma: "Asset / Diagram Glyph" (25359:65776)
*/
import Image from "next/image";
import { memo } from "react";
import AdviceGlyph from "./advice.svg";
import AuthorityGlyph from "./authority.svg";
import ConflictGlyph from "./conflict.svg";
import ConsensusGlyph from "./consensus.svg";
import DiscussionGlyph from "./discussion.svg";
import GroupGlyph from "./group.svg";
import LadderGlyph from "./ladder.svg";
import MembraneGlyph from "./membrane.svg";
import MoneyGlyph from "./money.svg";
import PersonGlyph from "./person.svg";
import PodGlyph from "./pod.svg";
import PrivacyGlyph from "./privacy.svg";
import RecordsGlyph from "./records.svg";
import RotationGlyph from "./rotation.svg";
import SortitionGlyph from "./sortition.svg";
import TermGlyph from "./term.svg";
import VoteGlyph from "./vote.svg";
export const DIAGRAM_GLYPH_OPTIONS = [
"group",
"pod",
"membrane",
"person",
"authority",
"advice",
"rotation",
"ladder",
"conflict",
"consensus",
"vote",
"sortition",
"term",
"discussion",
"records",
"privacy",
"money",
] as const;
export type DiagramGlyphName = (typeof DIAGRAM_GLYPH_OPTIONS)[number];
export const DIAGRAM_GLYPH_SIZE_OPTIONS = [24, 48] as const;
export type DiagramGlyphSize = (typeof DIAGRAM_GLYPH_SIZE_OPTIONS)[number];
type SvgComponent =
| React.ComponentType<React.SVGProps<SVGSVGElement>>
| { default: React.ComponentType<React.SVGProps<SVGSVGElement>> };
const glyphMap: Record<DiagramGlyphName, SvgComponent> = {
group: GroupGlyph,
pod: PodGlyph,
membrane: MembraneGlyph,
person: PersonGlyph,
authority: AuthorityGlyph,
advice: AdviceGlyph,
rotation: RotationGlyph,
ladder: LadderGlyph,
conflict: ConflictGlyph,
consensus: ConsensusGlyph,
vote: VoteGlyph,
sortition: SortitionGlyph,
term: TermGlyph,
discussion: DiscussionGlyph,
records: RecordsGlyph,
privacy: PrivacyGlyph,
money: MoneyGlyph,
};
function resolveSvgComponent(module: SvgComponent) {
if (typeof module === "object" && module !== null && "default" in module) {
return (
module as {
default: React.ComponentType<React.SVGProps<SVGSVGElement>>;
}
).default;
}
return module as React.ComponentType<React.SVGProps<SVGSVGElement>>;
}
export interface DiagramGlyphProps {
glyph: DiagramGlyphName;
className?: string;
/** Width and height. Figma grid is 48; diagrams also use 24. */
size?: DiagramGlyphSize | number;
/**
* Accessible name when the glyph sits in a diagram. Omit for a decorative
* mark (empty alt / `aria-hidden`).
*/
title?: string;
/** Follows `title` when the glyph is named in a diagram. */
description?: string;
}
function DiagramGlyphComponent({
glyph,
className = "",
size = 48,
title,
description,
}: DiagramGlyphProps) {
const SvgModule = glyphMap[glyph];
if (!SvgModule) return null;
const resolved = resolveSvgComponent(SvgModule);
const decorative = !title;
const label = [title, description].filter(Boolean).join(". ");
if (typeof resolved === "string") {
return (
<Image
src={resolved}
width={size}
height={size}
className={className}
alt={decorative ? "" : label}
unoptimized
aria-hidden={decorative || undefined}
/>
);
}
if (resolved == null) return null;
const Svg = resolved as React.ComponentType<React.SVGProps<SVGSVGElement>>;
if (decorative) {
return (
<Svg
width={size}
height={size}
className={className}
aria-hidden
data-figma-node="25359:65776"
/>
);
}
return (
<span
role="img"
aria-label={label}
className={`inline-flex ${className}`.trim()}
data-figma-node="25359:65776"
>
<Svg width={size} height={size} aria-hidden />
</span>
);
}
const DiagramGlyph = memo(DiagramGlyphComponent);
DiagramGlyph.displayName = "DiagramGlyph";
export default DiagramGlyph;