Skip to main content

arks_server/web/ui/
icons.rs

1//! Typed Lucide SVG sprite icon primitives (`web::ui::icon`, `Icon`, `IconSize`)
2//! governed by `web-platform-baseline.sot.md` WP-ICON-1..3.
3//!
4//! All icons come from the single vendored `/assets/icons.svg` sprite (Lucide v1.84.0, ISC,
5//! plus the existing A2-R2 arks mark `i-arks`). Emoji and Unicode symbol glyphs are banned
6//! as UI icons.
7
8use crate::web::core::assets::core_asset_url;
9use crate::web::core::html::{Attr, HtmlBuf, Render};
10
11/// Standard icon sizing classes matching `web_ui.css`.
12#[derive(Debug, Clone, Copy, PartialEq, Eq)]
13pub enum IconSize {
14    /// 12px (inside pills/badges).
15    Xs,
16    /// 14px (dense rows, secondary controls).
17    Sm,
18    /// 16px (default navigation & buttons).
19    Md,
20    /// 18px (section headers).
21    Lg,
22    /// 28px (A2-R2 brand mark in topbar).
23    Brand,
24}
25
26impl IconSize {
27    pub const fn class(self) -> &'static str {
28        match self {
29            Self::Xs => "ico ico-xs",
30            Self::Sm => "ico ico-sm",
31            Self::Md => "ico",
32            Self::Lg => "ico ico-lg",
33            Self::Brand => "ico ico-brand",
34        }
35    }
36}
37
38/// Complete typed catalog of symbols embedded in `/assets/icons.svg`.
39#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
40pub enum Icon {
41    Activity,
42    Archive,
43    Arks,
44    ArrowLeft,
45    ArrowRight,
46    BadgeCheck,
47    Bell,
48    BookOpen,
49    Bot,
50    Box,
51    ChartColumn,
52    Check,
53    ChevronDown,
54    ChevronRight,
55    CircleAlert,
56    CircleCheck,
57    CircleDot,
58    CirclePlay,
59    CircleX,
60    Clock,
61    Cloud,
62    Code,
63    Copy,
64    Cpu,
65    Download,
66    Ellipsis,
67    ExternalLink,
68    Eye,
69    File,
70    FileText,
71    Folder,
72    GitBranch,
73    GitCommitHorizontal,
74    GitCompare,
75    GitFork,
76    GitGraph,
77    GitMerge,
78    GitPullRequest,
79    GitPullRequestArrow,
80    History,
81    KeyRound,
82    Layers,
83    Link,
84    ListFilter,
85    Lock,
86    LogOut,
87    Menu,
88    MessageSquare,
89    Moon,
90    Network,
91    Pencil,
92    Plus,
93    Radio,
94    RefreshCw,
95    Search,
96    Settings,
97    Shield,
98    ShieldCheck,
99    Sparkles,
100    SquareKanban,
101    Star,
102    Sun,
103    Tag,
104    Terminal,
105    Trash2,
106    TriangleAlert,
107    Undo2,
108    User,
109    Users,
110    Webhook,
111    Workflow,
112    X,
113}
114
115impl Icon {
116    /// Every variant in `Icon`, verified against `/assets/icons.svg` by unit tests.
117    pub const ALL: &'static [Icon] = &[
118        Self::Activity,
119        Self::Archive,
120        Self::Arks,
121        Self::ArrowLeft,
122        Self::ArrowRight,
123        Self::BadgeCheck,
124        Self::Bell,
125        Self::BookOpen,
126        Self::Bot,
127        Self::Box,
128        Self::ChartColumn,
129        Self::Check,
130        Self::ChevronDown,
131        Self::ChevronRight,
132        Self::CircleAlert,
133        Self::CircleCheck,
134        Self::CircleDot,
135        Self::CirclePlay,
136        Self::CircleX,
137        Self::Clock,
138        Self::Cloud,
139        Self::Code,
140        Self::Copy,
141        Self::Cpu,
142        Self::Download,
143        Self::Ellipsis,
144        Self::ExternalLink,
145        Self::Eye,
146        Self::File,
147        Self::FileText,
148        Self::Folder,
149        Self::GitBranch,
150        Self::GitCommitHorizontal,
151        Self::GitCompare,
152        Self::GitFork,
153        Self::GitGraph,
154        Self::GitMerge,
155        Self::GitPullRequest,
156        Self::GitPullRequestArrow,
157        Self::History,
158        Self::KeyRound,
159        Self::Layers,
160        Self::Link,
161        Self::ListFilter,
162        Self::Lock,
163        Self::LogOut,
164        Self::Menu,
165        Self::MessageSquare,
166        Self::Moon,
167        Self::Network,
168        Self::Pencil,
169        Self::Plus,
170        Self::Radio,
171        Self::RefreshCw,
172        Self::Search,
173        Self::Settings,
174        Self::Shield,
175        Self::ShieldCheck,
176        Self::Sparkles,
177        Self::SquareKanban,
178        Self::Star,
179        Self::Sun,
180        Self::Tag,
181        Self::Terminal,
182        Self::Trash2,
183        Self::TriangleAlert,
184        Self::Undo2,
185        Self::User,
186        Self::Users,
187        Self::Webhook,
188        Self::Workflow,
189        Self::X,
190    ];
191
192    /// Symbol ID suffix (`i-<slug>`) inside `/assets/icons.svg`.
193    pub const fn symbol_id(self) -> &'static str {
194        match self {
195            Self::Activity => "i-activity",
196            Self::Archive => "i-archive",
197            Self::Arks => "i-arks",
198            Self::ArrowLeft => "i-arrow-left",
199            Self::ArrowRight => "i-arrow-right",
200            Self::BadgeCheck => "i-badge-check",
201            Self::Bell => "i-bell",
202            Self::BookOpen => "i-book-open",
203            Self::Bot => "i-bot",
204            Self::Box => "i-box",
205            Self::ChartColumn => "i-chart-column",
206            Self::Check => "i-check",
207            Self::ChevronDown => "i-chevron-down",
208            Self::ChevronRight => "i-chevron-right",
209            Self::CircleAlert => "i-circle-alert",
210            Self::CircleCheck => "i-circle-check",
211            Self::CircleDot => "i-circle-dot",
212            Self::CirclePlay => "i-circle-play",
213            Self::CircleX => "i-circle-x",
214            Self::Clock => "i-clock",
215            Self::Cloud => "i-cloud",
216            Self::Code => "i-code",
217            Self::Copy => "i-copy",
218            Self::Cpu => "i-cpu",
219            Self::Download => "i-download",
220            Self::Ellipsis => "i-ellipsis",
221            Self::ExternalLink => "i-external-link",
222            Self::Eye => "i-eye",
223            Self::File => "i-file",
224            Self::FileText => "i-file-text",
225            Self::Folder => "i-folder",
226            Self::GitBranch => "i-git-branch",
227            Self::GitCommitHorizontal => "i-git-commit-horizontal",
228            Self::GitCompare => "i-git-compare",
229            Self::GitFork => "i-git-fork",
230            Self::GitGraph => "i-git-graph",
231            Self::GitMerge => "i-git-merge",
232            Self::GitPullRequest => "i-git-pull-request",
233            Self::GitPullRequestArrow => "i-git-pull-request-arrow",
234            Self::History => "i-history",
235            Self::KeyRound => "i-key-round",
236            Self::Layers => "i-layers",
237            Self::Link => "i-link",
238            Self::ListFilter => "i-list-filter",
239            Self::Lock => "i-lock",
240            Self::LogOut => "i-log-out",
241            Self::Menu => "i-menu",
242            Self::MessageSquare => "i-message-square",
243            Self::Moon => "i-moon",
244            Self::Network => "i-network",
245            Self::Pencil => "i-pencil",
246            Self::Plus => "i-plus",
247            Self::Radio => "i-radio",
248            Self::RefreshCw => "i-refresh-cw",
249            Self::Search => "i-search",
250            Self::Settings => "i-settings",
251            Self::Shield => "i-shield",
252            Self::ShieldCheck => "i-shield-check",
253            Self::Sparkles => "i-sparkles",
254            Self::SquareKanban => "i-square-kanban",
255            Self::Star => "i-star",
256            Self::Sun => "i-sun",
257            Self::Tag => "i-tag",
258            Self::Terminal => "i-terminal",
259            Self::Trash2 => "i-trash-2",
260            Self::TriangleAlert => "i-triangle-alert",
261            Self::Undo2 => "i-undo-2",
262            Self::User => "i-user",
263            Self::Users => "i-users",
264            Self::Webhook => "i-webhook",
265            Self::Workflow => "i-workflow",
266            Self::X => "i-x",
267        }
268    }
269}
270
271/// Renderable strict-CSP SVG `<use>` icon primitive.
272#[derive(Debug, Clone, Copy)]
273pub struct IconEl {
274    pub icon: Icon,
275    pub size: IconSize,
276}
277
278/// Render a default 16px Lucide sprite icon (`aria-hidden="true"`).
279pub const fn icon(icon: Icon) -> IconEl {
280    IconEl {
281        icon,
282        size: IconSize::Md,
283    }
284}
285
286/// Render a sized Lucide sprite icon (`aria-hidden="true"`).
287pub const fn icon_sized(icon: Icon, size: IconSize) -> IconEl {
288    IconEl { icon, size }
289}
290
291impl Render for IconEl {
292    fn render_to(&self, out: &mut HtmlBuf) {
293        let href = format!(
294            "{}#{}",
295            core_asset_url("/assets/icons.svg"),
296            self.icon.symbol_id()
297        );
298        let cls = if self.icon == Icon::Arks {
299            match self.size {
300                IconSize::Brand => "ico ico-brand ico-fill",
301                _ => "ico ico-fill",
302            }
303        } else {
304            self.size.class()
305        };
306        out.el(
307            "svg",
308            &[Attr::Class(cls), Attr::Aria("hidden", "true")],
309            |svg: &mut HtmlBuf| {
310                svg.el("use", &[Attr::Href(&href)], ());
311            },
312        );
313    }
314}
315
316/// Forbidden emoji/Unicode symbol glyphs banned as UI icons (WP-ICON-1).
317pub const FORBIDDEN_GLYPH_ICONS: &[char] = &[
318    '⚙', '👥', '⚿', '◇', '⑂', '☆', '▸', '≡', '⌕', '🔔', '◐', '⟲', '✎', '✓', '✕', '▾',
319];
320
321#[cfg(test)]
322mod tests {
323    use super::*;
324
325    const SPRITE: &str = include_str!("../../assets/icons.svg");
326
327    #[test]
328    fn every_icon_variant_exists_in_lucide_sprite() {
329        for &ic in Icon::ALL {
330            let needle = format!("id=\"{}\"", ic.symbol_id());
331            assert!(
332                SPRITE.contains(&needle),
333                "Icon::{ic:?} missing symbol {needle} in assets/icons.svg"
334            );
335        }
336        assert_eq!(Icon::ALL.len(), 72);
337    }
338
339    #[test]
340    fn icon_renders_fingerprinted_svg_use_without_inline_style() {
341        let mut buf = HtmlBuf::new();
342        icon(Icon::GitBranch).render_to(&mut buf);
343        let html = buf.finish();
344        assert!(html.contains("class=\"ico\""));
345        assert!(html.contains("aria-hidden=\"true\""));
346        assert!(html.contains("/assets/icons."));
347        assert!(html.contains("#i-git-branch"));
348        assert!(!html.contains("style="));
349    }
350}