Button
A <button> action. Corresponds to PHP's app/Component/Button/ButtonView.blade.php, which is built on PHP's Action base, so its classes are cg-action cg-button.
Rust
use cg_component::{Button, ButtonType, Color, Component, IconPosition, Size, Variant};
Button::new("Save").icon("check").button_type(ButtonType::Submit).render();
Button::new("Next").variant(Variant::Outline).icon("chevron-right").icon_position(IconPosition::After).render();
Button::new("Close").variant(Variant::Ghost).color(Color::Neutral).size(Size::Sm).icon("x").icon_only(None).render();
Button::new("Delete").disabled(true).name("action").value("delete").render();| Method | Default | Description |
|---|---|---|
Button::new(label) | — | &str / String (escaped) or Html |
.color(Color) | Primary | cg-action--{color} |
.size(Size) | Md | cg-action--{size} |
.variant(Variant) | Solid | cg-action--{variant} |
.icon(name or Icon) | none | A string becomes an Sm Icon |
.icon_position(IconPosition) | Before | Icon before or after the label |
.icon_only(Option<&str>) | no | Shows only the icon and visually hides the label; the argument is the accessible label, None uses the label text |
.disabled(bool) | false | Outputs disabled aria-disabled="true" |
.button_type(ButtonType) | Button | The type attribute |
.name(..) / .value(..) | none | The value the form submits; value takes strings, numbers and booleans (true → 1) |
.state_set(key, value) | — | data-state-set="key=<JSON>" (PHP stateSet) |
.state_toggle(key) | — | data-state-toggle="key" |
.opens_modal(key) / .closes_modal(key) | — | state_set(key, true / false) |
.validate() | — | Checks the rules PHP throws for first; returns Result<(), String> |
.class(...) / .attribute(...) / .attributes(...) | — | See Template functions and attributes |
When it panics
Wherever PHP throws, render() runs validate() first:
| Rule | Message |
|---|---|
icon_only without an icon | iconOnly Action 必須設定 icon。 |
icon_only without a plain-text accessible label (the label is Html with no argument given, or blank) | iconOnly Action 需要純文字 accessible label。 |
An invalid state_set / state_toggle key | 不合法的 state key [...] |
| An unregistered icon name | 沒有註冊的 Icon [...] |
For data-driven buttons (with a label from the user, say), call validate() first and show something else if it fails, rather than letting the handler panic.
Template
{{ 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_button("Search", icon="search", icon_only="Search customers") }}
{{ cg_button("Submit", type="submit", name="action", value="save", attrs={"class": "w-full"}) }}| Parameter | Values |
|---|---|
label (first) | Text; safe values are treated as HTML |
color / size / variant | As in Rust, lowercase strings |
icon | A registered icon name |
icon_position | before / after |
icon_only | true (the label is the accessible label) or a string (the accessible label) |
disabled | Boolean |
type | button / submit / reset |
name / value | Strings; a boolean value outputs 1 / 0 |
attrs | A map |
When those rules are broken, the template function returns a template error (it doesn't panic).
Output
<button class="cg-action cg-button cg-action--ghost cg-action--neutral cg-action--sm cg-action--icon-only"
data-component="button" type="button" aria-label="Close">
<svg class="cg-icon cg-icon--sm" data-component="icon" data-icon="x" … aria-hidden="true">…</svg>
<span class="cg-action__label cg-visually-hidden">Close</span>
</button>- Attribute order:
class, the attributes you added,data-component,type,name,value,disabled,aria-label. Snapshot comparisons ignore attribute order. - The icon-only label is still in the DOM (
cg-visually-hidden), so screen readers read it;aria-labelis output too.
With render fragments and Linker
Buttons are where interaction attributes most often go:
// redraw a fragment on click
Button::new("Refresh")
.variant(Variant::Outline)
.attributes(runtime::render_trigger("clock", RenderMethod::Components, Some("#clock"), &[]));
// call a Linker method, with confirmation
Button::new("Reset")
.variant(Variant::Ghost)
.color(Color::Danger)
.attribute("data-linker-call", "reset")
.attribute("data-linker-confirm", "Reset to zero?");
// submit the whole form (on the submit button)
Button::new("Say hello")
.button_type(ButtonType::Submit)
.attribute("data-linker-call", "greet")
.attribute("data-linker-args", "form");All taken from the example project's src/http/handlers/runtime_demo.rs.
Switching client state
Button::new("List").state_set("tab", "list"); // data-state-set="tab="list""
Button::new("Expand").state_toggle("open");
Button::new("Edit").opens_modal("editOpen");Values are JSON-encoded, so the browser reads back the string "list" and the boolean true. Prefix the key with @ to write a global key. The button must be inside a render object with StateWrite.
Tests
tests/php_parity.rs: an_icon_button_matches_the_php_fields_snapshot, a_state_button_matches_the_php_interactions_snapshot, an_icon_only_button_matches_the_php_interactions_snapshot.