SuperAPI AI 工具集

viewcomponent-coder

通用与其它

查看项目

更新于 10-16 00:03

技能介绍

用途:使用 ViewComponent 与 Evil Martians 的 view_component-contrib 模式构建组件化 UI,适用于创建可复用组件、实现插槽与样式变体、构建 Lookbook 预览、隔离测试组件,以及将 partial 重构为组件。核心原则是优先用 ViewComponent 替代 partial,以获得更好的封装、可隔离测试、显式契约、IDE 支持与编译模板性能。

主要能力:通过 dry-initializer 声明式初始化组件;使用 StyleVariants DSL 定义 base、variants、compound 与 defaults,替代手写 VARIANTS 哈希;用 renders_one/renders_many 实现插槽;用 render? 控制条件渲染;用 Lookbook 生成预览;用 RSpec 的 type: :component 与 render_inline 做隔离测试。

使用方式:在 Gemfile 添加 view_component、view_component-contrib、dry-initializer、lookbook、inline_svg,可用 rails app:template LOCATION="https://railsbytes.com/script/zJosO5" 安装。定义 ApplicationViewComponent 继承 ViewComponentContrib::Base 并 extend Dry::Initializer,预览基类继承 ViewComponentContrib::Preview::Base。组件中调用 Rails 辅助方法需加 helpers. 前缀(如 helpers.link_to、helpers.image_tag、helpers.inline_svg_tag),t() 除外。SVG 图标存于 app/assets/images/icons/ 并用 inline_svg 渲染,不要内联 SVG 标记。Lookbook 预览访问 http://localhost:3000/lookbook。

限制:资料未提供版本要求、兼容性、性能数据或收费信息;具体实现细节需参考 references/patterns.md 与 references/style-variants.md。