Checkbox

Examples

Default

A control that allows the user to toggle between checked and not checked.

<div class="flex items-center space-x-2">
  <%= render "ui/checkbox", id: "terms", name: "terms", value: "accepted" %>
  <%= render "ui/label", for: "terms" do %>Accept terms and conditions<% end %>
</div>

With Text

Checkbox with a label and description text.

You agree to our Terms of Service and Privacy Policy.

<div class="items-top flex space-x-2">
  <%= render "ui/checkbox", id: "terms-text", name: "terms", value: "accepted" %>
  <div class="grid gap-1.5 leading-none">
    <%= render "ui/label", for: "terms-text" do %>Accept terms and conditions<% end %>
    <p class="text-sm text-muted-foreground">You agree to our Terms of Service and Privacy Policy.</p>
  </div>
</div>

Disabled

A disabled checkbox.

<div class="flex items-center space-x-2">
  <%= render "ui/checkbox", id: "disabled", name: "disabled", value: "yes", disabled: true %>
  <%= render "ui/label", for: "disabled" do %>Disabled<% end %>
</div>

With Card Style

A checkbox inside a styled card with visual feedback on checked state.

<%= render "ui/label", for: "notifications", classes: "hover:bg-accent/50 flex items-start gap-3 rounded-lg border p-3 has-[[aria-checked=true]]:border-blue-600 has-[[aria-checked=true]]:bg-blue-50 dark:has-[[aria-checked=true]]:border-blue-900 dark:has-[[aria-checked=true]]:bg-blue-950" do %>
  <%= render "ui/checkbox", id: "notifications", checked: true, classes: "data-[state=checked]:border-blue-600 data-[state=checked]:bg-blue-600 data-[state=checked]:text-white dark:data-[state=checked]:border-blue-700 dark:data-[state=checked]:bg-blue-700" %>
  <div class="grid gap-1.5 font-normal">
    <p class="text-sm leading-none font-medium">Enable notifications</p>
    <p class="text-muted-foreground text-sm">You can enable or disable notifications at any time.</p>
  </div>
<% end %>

Features

  • Custom styling with Tailwind classes
  • Disabled state support
  • ARIA attributes for accessibility

API Reference

Checkbox

Parameters

NameTypeDefaultDescription
nameStringnilForm field name
idStringnilHTML id attribute
valueStringnilThe current value
checkedBooleanfalseWhether the element is checked
disabledBooleanfalseWhether the element is disabled
requiredBooleanfalseThe required

Accessibility

Implements the WAI-ARIA Checkbox pattern with proper roles, states, and keyboard navigation.

ARIA Attributes

  • aria-checked

JavaScript

Stimulus Controller

ui--checkbox