Components overview
cg-component started as a PHP (Laravel + Blade) component framework: server-rendered HTML, state in the GET query string, usable without JavaScript. Laravel for Rust templates are MiniJinja and can't run Blade, so here the components are Rust structs that write their own HTML. The output has the same structure as the PHP version, so resources/css/cg-component.css and the JS are shared unchanged.
Ported components
| Component | Rust | Template function | PHP source |
|---|---|---|---|
| Badge | Badge | cg_badge(label, …) | app/Component/Badge/ |
| Button | Button | cg_button(label, …) | app/Component/Button/ (extends Action) |
| Icon | Icon, IconDefinition | cg_icon(name, …) | app/Component/Icon/ |
tests/php_parity.rs takes the same component from the PHP visual snapshots and compares tags, attributes, classes and text with the Rust output: Badge (on the overview and catalog pages), a Button with an icon, a Button with state_set, and an icon-only Button. Icon is compared as part of those Badges and Buttons.
The PHP version has 61 components. The browser behavior of the others (modal.js, tabs.js, table.js, chart/, datePicker.js…) is already in resources/js/cg/, and their CSS is in cg-component.css; only the Rust builders are missing. To use one, port it by following Porting a new component.
Two ways to use them
Build in Rust
use cg_component::{Badge, Button, ButtonType, Color, Component};
use laravel::prelude::*;
use laravel::view::View;
pub async fn show() -> View {
let status = Badge::new("Paid").color(Color::Success).icon("check").render();
let save = Button::new("Save").icon("check").button_type(ButtonType::Submit).render();
view("orders.show", ()).with_html("status", status).with_html("save", save)
}render()returns Laravel'sHtml. Pass it to a template withView::with_htmlso it isn't escaped; you can also return it directly as a response, or put it inside other HTML (html.as_str()).- Colors, sizes and variants are enums, so typos are caught at compile time.
- Where the PHP version throws (an icon-only button without an icon, an icon name that doesn't exist), Rust panics.
Button::validate()checks first.
Call from templates
{{ cg_badge("Draft", color="warning", icon="warning") }}
{{ cg_button("Next", variant="outline", icon="chevron-right", icon_position="after") }}
{{ cg_button("Close", variant="ghost", color="neutral", size="sm", icon="x", icon_only=true) }}
{{ cg_icon("info", size="lg", label="Help") }}Text is always escaped; only values marked safe (|safe, caller()) are treated as HTML. Unknown parameters and invalid option values return a template error instead of being silently ignored. All parameters are in Template functions and attributes.
Shared options
| Type | Values | Used by |
|---|---|---|
Color | Neutral Primary Success Warning Danger Info | Badge (default Neutral), Button (default Primary) |
Size | Sm Md Lg | Badge, Button (default Md) |
Variant | Solid Outline Ghost | Button (default Solid) |
IconSize | Xs Sm Md Lg Xl | Icon (default Md; Sm as a Badge / Button icon) |
IconPosition | Before After | Button |
ButtonType | Button Submit Reset | Button (default Button) |
Every enum has as_str(), ALL, Display and FromStr (templates use lowercase strings, such as "warning"). When FromStr fails, the message lists the valid values.
The shared attribute API
Every component has:
Badge::new("x")
.class("ml-2 shrink-0") // append classes
.attribute("data-state-text", "matchCount") // a single attribute
.attributes(runtime::render_object(...)); // merge a set of AttributesThe rules are those of PHP's HasAttributes: the component's own classes first, then yours; other attributes in the order they were set; a later attribute with the same name replaces the earlier one; true outputs name="" and false outputs nothing. Attribute names must match ^[A-Za-z_:][A-Za-z0-9:._-]*$; an invalid one panics. See Template functions and attributes.
Works without JavaScript
Components output plain HTML: Badge and Icon have no behavior; Button is a real <button>, and type="submit" submits the form normally without JS. Interaction (render fragments, Linker) is progressive enhancement on top. Page state goes in the GET query string, for example a filter form with method="GET", so reloads and shared URLs keep it.