Components

Button

Displays a button or a component that looks like a button.

Overview

Default / Secondary / Destructive / Outline / Ghost / Link 六种变体,hover/active/focus-visible 都有反馈。

GPUI · Live previewGPUI WASM

Installation

Button 是单个 Rust 文件(src/button.rs),样式层自绘,交互语义交给 gpui_base::Button 原语 —— focus、Tab 顺序、Enter/Space 激活、disabled 惰性全部免费。

Cargo.toml
[dependencies]gpui-kit = "0.6.6"# button.rs builds on gpui_base::Button — focus, keyboard# activation and disabled behavior come from the primitive.

Download runnable example

Terminal
# Download the runnable example crate, then:cargo run --locked -- --button# Other demos:cargo run --locked -- --button variantscargo run --locked -- --button sizescargo run --locked -- --button iconscargo run --locked -- --button roundedcargo run --locked -- --button spinnercargo run --locked -- --button group

Usage

Button 是 render-once 元素(IntoElement),在 render() 里内联构建即可,不需要实体。一个 on_click 同时覆盖鼠标与键盘。

Example.rs
use justdo_command::button::{Button, ButtonVariant, ButtonSize, ButtonIcon, IconPos};// Button is a render-once element — build it inline inside your view's// render(), it is not an entity and needs no cx.new.div().child(    Button::new("save")        .label("Save")        .icon(ButtonIcon::Plus, IconPos::Start)        .variant(ButtonVariant::Default)        .on_click(|_, _, cx| { /* activate */ }),)

网页预览与桌面端运行同一份 Rust / GPUI 组件。

Composition

组合结构对齐 shadcn/ui 的 Button —— variant / size / icon slot / pill / button group / spinner。

Structure
Button                    element — gpui_base::Button owns focus/keys/disabled├── label                 .label(..) — sm/xs medium, nowrap├── icon                  .icon(icon, pos) — inline-start | inline-end│     ArrowUp | ArrowUpRight | GitBranch | Plus | Spinner (animating arc)├── variant               Default | Secondary | Destructive | Outline | Ghost | Link├── size                  Xs | Sm | Default | Lg | IconXs | IconSm | Icon | IconLg├── pill                  .pill(true) — rounded-full├── join                  .join(Start|Middle|End) — ButtonGroup corners├── disabled              .disabled(true) — inert, dimmed└── on_click              pointer + Enter + Space activation
Example.rs
// on_click fires for pointer AND Enter/Space — the base// primitive synthesizes a keyboard ClickEvent, so one handler// covers both input paths.Button::new("save")    .label("Save")    .on_click(|_, _, cx| save(cx));

Notes

Keyboard for free. gpui_base::Button 把 Enter/Space 合成 ClickEvent::Keyboard 派发进同一个 on_click —— 不需要手写 key handler,也不用担心重复触发。

Spinner clock. WASM 上 std::time::Instant 不可用 —— 起始时刻存在 use_keyed_state 里(key 取自 a11y label,因为按钮 id 已被底层原语的 FocusHandle 占用),渲染期用 cx.background_executor().now() 相减;reduced-motion 下静止且不再请求逐帧。

Icon-only. Icon* 尺寸没有可见文本,用 .a11y_label(..) 给无障碍树命名。

Accessibility

Role & focus

底层原语标记 role="button"、管理 Tab 顺序与 focus-visible 描边;disabled 按钮既不可聚焦也不响应指针,父级点击也不会被穿透。

Contrast

六种变体在明暗主题下分别取色;Link 变体带下划线,不只靠颜色表达可点击;icon-only 按钮必须提供 a11y_label。

API Reference

元素暴露的 builder 方法。

APISignatureNotes
Button::new(id)-> ButtonElement id — used for focus state.
.label(..)impl Into<SharedString>Visible label; also the default a11y name.
.a11y_label(..)impl Into<SharedString>Required for icon-only buttons.
.variant(..)ButtonVariantDefault | Secondary | Destructive | Outline | Ghost | Link.
.size(..)ButtonSizeXs | Sm | Default | Lg | IconXs | IconSm | Icon | IconLg.
.icon(..)ButtonIcon, IconPosArrowUp | ArrowUpRight | GitBranch | Plus | Spinner.
.pill(..)boolrounded-full.
.join(..)ButtonJoinSolo | Start | Middle | End — joined group corners.
.disabled(..)boolInert: no pointer/keyboard activation, dimmed.
.on_click(..)Fn(&ClickEvent, ..)Pointer, Enter, and Space share one handler.

Source & inspiration

结构与视觉参考 shadcn/ui Button 。本页交互由 Rust / GPUI 绘制,基础样式来自 gpui-kit 。