mcpField
G
gpui-component

gpui-component

github.com/aprilnea开发947 次安装40 周前更新
代码仓库

关于 gpui-component

为 GPUI 应用使用 gpui-component 构建可复用 UI 组件的实践指南,涵盖组件结构、特征、主题、弹层、表单与动画。

主要功能

  1. 01组件架构模式
  2. 02Trait 实现指南
  3. 03主题化样式
  4. 04对话框与弹出层模式
  5. 05表单状态与验证
  6. 06动画指南

适用场景

  1. 01设计可复用的 GPUI 组件
  2. 02应用统一主题与尺寸
  3. 03构建对话框、弹出层或侧边面板
  4. 04实现表单与动画行为

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/aprilnea/gpui-skills --skill gpui-component

安装后在 Claude Code 中输入/gpui-component确认已加载。

包含的文件

除 SKILL.md 外,安装时一起下载的参考资料和脚本。

rules/
_sections.md
anim-with-animation.md
comp-renderonce.md
comp-stateful.md
comp-structure.md
dialog-comparison.md
dialog-root.md
dialog-window-ext.md
form-input-state.md
theme-colors.md
theme-variants.md
trait-disableable.md
trait-sizable.md
trait-styled.md

详细介绍

gpui-component 最佳实践

使用 gpui-component 构建 UI 组件的综合指南。gpui-component 是一个用于 GPUI 应用的组件库。

适用场景

在以下情况下参考这些指南:

  • 创建可复用的 UI 组件
  • 实现主题感知样式
  • 构建对话框、弹出框或面板
  • 创建表单组件(Input、Checkbox 等)
  • 实现组件动画
  • 遵循 Longbridge 组件模式

按优先级划分的规则类别

优先级类别影响前缀
1组件架构严重comp-
2Trait 系统严重trait-
3主题系统高theme-
4对话框与弹出框高dialog-
5表单组件中form-
6动画中anim-

快速参考

1. 组件架构(严重)
  • comp-renderonce - 使用 RenderOnce 和 #[derive(IntoElement)]
  • comp-structure - 标准组件结构模板
  • comp-builder - 组件的流式构建器模式
  • comp-stateful - 为有状态组件使用 Entity + RenderOnce
  • comp-callbacks - 使用 Rc 处理事件回调
2. Trait 系统(严重)
  • trait-styled - 实现 Styled 以自定义样式
  • trait-disableable - 实现 Disableable 以设置禁用状态
  • trait-selectable - 实现 Selectable 以设置选中状态
  • trait-sizable - 使用 Size 枚举实现 Sizable
  • trait-parent-element - 实现 ParentElement 以添加子元素
  • trait-interactive - 实现 InteractiveElement 以处理事件
3. 主题系统(高)
  • theme-colors - 使用 ThemeColor 确保颜色一致
  • theme-active - 使用 ActiveTheme trait 访问主题
  • theme-variants - 实现变体枚举(ButtonVariant 等)
  • theme-size-system - 使用 StyleSized 实现基于尺寸的样式
4. 对话框与弹出框(高)
  • dialog-root - 使用 Root 组件作为窗口根节点
  • dialog-window-ext - 使用 WindowExt 管理对话框
  • dialog-confirm - 实现确认/警告对话框
  • dialog-form - 使用输入状态构建表单对话框
  • popover-pattern - 选择 Popover、PopupMenu 或 Sheet
5. 表单组件(中)
  • form-input-state - 基于 Entity 管理输入状态
  • form-focus - 使用带键状态管理焦点
  • form-validation - 输入验证模式
6. 动画(中)
  • anim-with-animation - 使用 with_animation 实现过渡
  • anim-keyed-state - 使用 use_keyed_state 保留动画状态
  • anim-easing - 使用 cubic_bezier 实现平滑动画

组件结构模板

#[derive(IntoElement)]
pub struct MyComponent {
    // 1. Identifier
    id: ElementId,

    // 2. Base element (for event delegation)
    base: Div,

    // 3. Style override
    style: StyleRefinement,

    // 4. Content
    label: Option<SharedString>,
    children: Vec<AnyElement>,

    // 5. State configuration
    disabled: bool,
    selected: bool,
    size: Size,

    // 6. Callbacks
    on_click: Option<Rc<dyn Fn(&ClickEvent, &mut Window, &mut App)>>,
}

架构概览

┌─────────────────────────────────────────────────────────────┐
│  Window                                                      │
│  ├── Root (Entity, manages overlay state)                   │
│  │   ├── view: AnyView (user's main view)                   │
│  │   ├── active_dialogs: Vec<ActiveDialog>                  │
│  │   ├── active_sheet: Option<ActiveSheet>                  │
│  │   └── notification: Entity<NotificationList>             │
│  │                                                          │
│  └── Render Layers                                          │
│      ├── Layer 0: Root.view (main content)                  │
│      ├── Layer 1: Sheet (side drawer)                       │
│      ├── Layer 2: Dialogs (stackable)                       │
│      └── Layer 3: Notifications                             │
└─────────────────────────────────────────────────────────────┘

弹出组件对比

特性对话框PopoverPopupMenuSheet
位置居中锚定锚定侧边
遮罩层是否否是
状态Rootkeyed_stateEntityRoot
堆叠多个单个子菜单单个
关闭ESC/点击/按钮ESC/外部点击ESC/选择ESC/遮罩层

使用方法

阅读各个规则文件,了解详细说明和代码示例:

rules/comp-renderonce.md
rules/trait-styled.md
rules/dialog-root.md

每个规则文件都包含:

  • 简要说明其重要性
  • 展示正确模式的代码示例
  • 需要避免的常见错误

评论

支持 Markdown