Icon
An SVG icon from the registry. Corresponds to PHP's app/Component/Icon/IconView.blade.php and IconRegistry.
Rust
rust
use cg_component::{Component, Icon, IconSize};
Icon::new("info").size(IconSize::Lg).label("Help").render(); // meaningful: role="img"
Icon::new("search").render(); // decorative: aria-hidden="true"| Method | Default | Description |
|---|---|---|
Icon::new(name) | — | The name must be registered, or it panics |
.size(IconSize) | Md | cg-icon--{size} |
.label(text) | none | With a label it's meaningful (role="img" aria-label); without, decorative (aria-hidden) |
.class(...) / .attribute(...) / .attributes(...) | — | See Template functions and attributes |
.name() | — | Returns the name |
As a Badge / Button icon, a string name automatically becomes Sm. For another size, pass a configured Icon:
rust
Button::new("Download").icon(Icon::new("download").size(IconSize::Md))Template
jinja
{{ cg_icon("info", size="lg", label="Help") }}
{{ cg_icon("x", attrs={"class": "text-slate-400"}) }}| Parameter | Values |
|---|---|
name (first) | A registered name |
size | xs sm md lg xl |
label | The accessible label |
attrs | A map |
Output
html
<svg class="cg-icon cg-icon--lg" data-component="icon" data-icon="info" viewBox="0 0 24 24"
fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"
focusable="false" role="img" aria-label="Help">
<path d="M12 16v-4" /><path d="M12 8h.01" /><path d="M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z" />
</svg>Line icons: stroke="currentColor", so the color follows the text.
Built-in icons
In sync with PHP's IconRegistry::BUILT_INS, all viewBox="0 0 24 24":
check chevron-down chevron-right copy grip search menu info warning x user image file download audio
IconDefinition::names() returns every name currently available (built-in + your own).
Registering your own icons
rust
use cg_component::IconDefinition;
// at startup (for example in register() in src/cg/mod.rs)
IconDefinition::register("calendar", &["M8 2v4", "M16 2v4", "M3 10h18", "M5 4h14v18H5z"], "0 0 24 24")
.expect("calendar icon");- Names are kebab-case (
^[a-z][a-z0-9-]*$). - At least one path;
viewBoxmust be four numbers. - The same name replaces a built-in.
- Only path
dvalues are accepted, never arbitrary SVG markup: icons are an allow list of names, so business data can never inject arbitrary SVG.
register returns Result<(), String>; use expect at startup so errors surface immediately.
Errors
| Case | Rust | Template |
|---|---|---|
| Unregistered name | panic: 沒有註冊的 Icon [x];可用:… | A template error with the same message |
| Name isn't kebab-case | panic: Icon 名稱要用 kebab-case:[x] | A template error |