Viewing Mode

UI Components

The app ships a set of accessible, on-brand UI building blocks as ViewComponents under the UI:: namespace (app/components/ui/). They’re generated by the dev-only modelrails_ui gem, then vendored — committed to this app and owned by you. Production loads only view_component plus these vendored files; modelrails_ui is a development tool and is excluded from production.

Use them as prescribed and accessibility (WCAG 2.2 AAA) and styling come for free. Browse them live — every state, the source, and the a11y contract — at /lookbook (development).

Using a component

The ergonomic entry point is the ui helper:

<%= ui :button, "Save changes", variant: :solid, tone: :primary %>
<%= ui :dialog, title: "Edit profile" do |d| %>
  <% d.with_trigger { tag.button("Open", type: "button") } %>
  Body content
<% end %>

ui(:name, …) renders UI::NameComponent, forwarding all arguments. The component class stays an implementation detail; an unknown name raises NameError (a boundary guard).

Prescribed paths (use these first)

Several components have a higher-level entry point that wires up labels, ARIA, and model logic for you. Reach for it before ui:

Need Use Renders
Form field f.text_field :email / f.text_area / f.file_field (TailwindFormBuilder) UI::Input / Textarea / FileInput with label, help, error, ARIA
Modal render "shared/modal", title: "…" do … end UI::DialogComponent + the modal Stimulus controller
User avatar avatar_for(user, size: :md) UI::AvatarComponent (handles upload / gravatar / initials / hue)

Use ui :input / ui :dialog / ui :avatar directly only for standalone cases outside those flows (a filter box outside a form, a non-user avatar, etc.).

Where the component docs live

Each vendored component’s class comment is three lines: a summary and a pointer. The full reference — usage, options, when to use it, and the accessibility contract — is docs/components/<name>.md in the modelrails_ui gem (bundle show modelrails_ui), and the live examples are in Lookbook at /lookbook. Sub-components share their parent’s file: accordion items point to accordion.md, list-group items to list_group.md, menubar menus to menubar.md, tab items to tabs.md, and the five card parts (content, description, footer, header, title) to card.md. Do not grow the class comment back into prose; the component_headers_are_pointers code-smell spec fails when one does.

A handful of vendored components carry a genuine fork-local divergence from the gem’s shipped version — a real difference in behavior or markup, not just rewording — that has no home in the gem doc because it would misdescribe the gem’s default component. Those live here instead.

Fork notes

  • Badge — the [:soft, :neutral] cell (the tenth) is proven AAA in this app, not in the gem, since gem CI disables color-contrast (see docs/testing.md in the modelrails_ui gem): its neutral preview is audited in both themes by spec/system/ui/badge_component_spec.rb.
  • Button — this is an app-local copy that intentionally diverges from modelrails_ui’s self-contained (raw-utility) ButtonComponent: this app owns its design tokens, so the component applies the app’s button-family CSS classes (app/assets/tailwind/application.css @layer components) instead of re-listing them. The proven (variant, tone) cells map to those classes — see the COMBOS constant in button_component.rb for the exact mapping (solid/primary → the primary fill, solid/danger → the danger fill, outline/neutral → the secondary bordered style, text/primary and text/danger → the two text-style treatments). This page deliberately names none of those CSS classes: the btn_text_is_action_row_only code-smell spec (underline doctrine #772) fails any app/docs page that contains the de-emphasized text button’s class name, so COMBOS is the source of truth for the mapping instead.
  • Chart — bin/importmap pin chart.js downloads the library to vendor/javascript, since production CSP allows no CDN.

Adding a component

  1. Discover — bin/rails g modelrails_ui:list shows the catalog and which are installed. Or browse /lookbook.
  2. Generate (vendor) — bin/rails g modelrails_ui:add badge writes app/components/ui/badge_component.rb (and a Stimulus controller for interactive components, which auto-registers). You own and commit it.
  3. Use it — ui :badge, "New" in new markup, or point an adapter at it to replace an existing seam (call sites unchanged). It already uses the app’s semantic tokens.
  4. Document it — add spec/components/previews/ui/badge_component_preview.rb following the teaching pattern (class-level overview / Use-when / Don’t-use-when / a11y contract, per-scenario notes, a labeled Don't, scenarios calling ui). include UIHelper.
  5. Lock the contract — add spec/components/ui/badge_component_spec.rb (markup/ARIA/parity), test-first.
  6. Commit the component (+ controller + preview + spec).

Accessibility & guardrails

  • Baked in: AAA contrast tokens, focus rings, and 44px touch targets live inside the components — using them as prescribed keeps you compliant.
  • Boundary validation: components validate their inputs and fail loud in development (e.g. an unproven button (variant, tone) cell raises), while degrading safely in production.
  • Outcome gate: the axe-core system specs audit rendered pages at WCAG 2.2 AAA in both light and dark themes, so a regression fails CI regardless of how the markup was written.

How it ships

view_component is a runtime dependency (the vendored components are ViewComponents loaded in production). modelrails_ui lives in the :development group and only generates — it is never loaded in production. See Extending the template for the broader philosophy.