Skip to content

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 ​

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();
MethodDefaultDescription
Button::new(label)—&str / String (escaped) or Html
.color(Color)Primarycg-action--{color}
.size(Size)Mdcg-action--{size}
.variant(Variant)Solidcg-action--{variant}
.icon(name or Icon)noneA string becomes an Sm Icon
.icon_position(IconPosition)BeforeIcon before or after the label
.icon_only(Option<&str>)noShows only the icon and visually hides the label; the argument is the accessible label, None uses the label text
.disabled(bool)falseOutputs disabled aria-disabled="true"
.button_type(ButtonType)ButtonThe type attribute
.name(..) / .value(..)noneThe 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:

RuleMessage
icon_only without an iconiconOnly 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 ​

jinja
{{ 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"}) }}
ParameterValues
label (first)Text; safe values are treated as HTML
color / size / variantAs in Rust, lowercase strings
iconA registered icon name
icon_positionbefore / after
icon_onlytrue (the label is the accessible label) or a string (the accessible label)
disabledBoolean
typebutton / submit / reset
name / valueStrings; a boolean value outputs 1 / 0
attrsA map

When those rules are broken, the template function returns a template error (it doesn't panic).

Output ​

html
<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-label is output too.

With render fragments and Linker ​

Buttons are where interaction attributes most often go:

rust
// 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 ​

rust
Button::new("List").state_set("tab", "list");      // data-state-set="tab=&quot;list&quot;"
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.