Toggle Shared Partial

Don't forget the Stimulus controller!
The shared partial still requires the toggle_controller.js Stimulus controller to be installed. Copy the controller from the Installation section on this page.

1. Copy shared partial files

Download all 2 shared partial files as a ZIP file.

Download all files (ZIP)

After downloading, unzip and copy the toggle/ folder to your app/views/shared/components/ directory.

Copy these files to your app/views/shared/components/toggle/ directory:

2. Usage example

<% bold_icon = capture do %><svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 18 18" fill="none" stroke="currentColor" stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M5.25 2.75h4.5a3 3 0 0 1 0 6h-4.5v-6Z"></path><path d="M5.25 8.75h5.5a3.25 3.25 0 0 1 0 6.5h-5.5v-6.5Z"></path></svg><% end %>
<% italic_icon = capture do %><svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 18 18" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M7.75 2.75h5.5"></path><path d="M4.75 15.25h5.5"></path><path d="M10.5 2.75l-3 12.5"></path></svg><% end %>
<% align_left_icon = capture do %><svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 18 18" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M2.75 4.25h12.5"></path><path d="M2.75 9h7.5"></path><path d="M2.75 13.75h10.5"></path></svg><% end %>
<% align_center_icon = capture do %><svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 18 18" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M2.75 4.25h12.5"></path><path d="M5.25 9h7.5"></path><path d="M3.75 13.75h10.5"></path></svg><% end %>
<% align_right_icon = capture do %><svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 18 18" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M2.75 4.25h12.5"></path><path d="M7.75 9h7.5"></path><path d="M4.75 13.75h10.5"></path></svg><% end %>
<div class="flex flex-col items-start gap-6">
<%# ── 1. Icon-only toggle: `label:` is the accessible name and makes the button square. Pass the icon as a block or content:. %>
<%= render "shared/components/toggle/toggle", label: "Bold", content: bold_icon %>

<%# ── 2. Text toggle, bordered, pressed on the server. Style content with `group-aria-pressed/toggle:`.
     With a model: pressed: current_user.favorited?(@post) %>
<%= render "shared/components/toggle/toggle", variant: :outline, pressed: true do %>
  <svg xmlns="http://www.w3.org/2000/svg" class="size-4 group-aria-pressed/toggle:fill-current" width="18" height="18" viewBox="0 0 18 18" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M9 2.25l2.04 4.14 4.57.66-3.3 3.22.78 4.55L9 12.67l-4.09 2.15.78-4.55-3.3-3.22 4.57-.66L9 2.25Z"></path></svg>
  Favorite
<% end %>

<%# ── 3. Sizes: :sm (32px, 40px tall hit area), :md (36px, default), :lg (44px, touch). %>
<div class="flex items-center gap-2">
  <%= render "shared/components/toggle/toggle", label: "Italic", size: :sm, content: italic_icon %>
  <%= render "shared/components/toggle/toggle", label: "Italic", size: :lg, content: italic_icon %>
</div>

<%# ── 4. Standalone toggle in a form: `name:` adds a hidden input that submits "1" or "0".
     With a model: pressed: @notification_settings.mentions? %>
<%= form_with scope: :notification_settings, url: "#" do |form| %>
  <%= render "shared/components/toggle/toggle", name: form.field_name(:mentions), pressed: true, variant: :outline, content: "Mentions" %>
<% end %>

<%# ── 5. Single-select toolbar: `required: true` keeps one item selected. It renders as a radiogroup
     (role="radio" items with aria-checked): arrow keys move and select, like native radios. %>
<%= render "shared/components/toggle/toggle_group", label: "Text alignment", required: true, items: [
  { value: "left", label: "Align left", pressed: true, content: align_left_icon },
  { value: "center", label: "Align center", content: align_center_icon },
  { value: "right", label: "Align right", content: align_right_icon }
] %>

<%# ── 6. Multi-select row of outline toggles, submitted as "bold,italic". With a model: pressed: @post.styles.include?("bold") %>
<%= render "shared/components/toggle/toggle_group", label: "Text style", variant: :outline, multiple: true, name: "post[styles]", items: [
  { value: "bold", label: "Bold", pressed: true, content: bold_icon },
  { value: "italic", label: "Italic", content: italic_icon }
] %>

<%# ── 7. Segmented control: a sliding indicator, single-select and required by default (a radiogroup). %>
<%= render "shared/components/toggle/toggle_group", label: "Time range", variant: :segmented, size: :sm, name: "range",
  items: %w[1D 1W 1M 1Y].map { |range| { value: range.downcase, content: range, pressed: range == "1M" } } %>

<%# ── 8. React to changes: the group dispatches `toggle-button:change` with item, pressed, value and values. %>
<%= render "shared/components/toggle/toggle_group", label: "Text formatting", multiple: true,
  html: { data: { action: "toggle-button:change->editor#format" } }, items: [
  { value: "bold", label: "Bold", content: bold_icon },
  { value: "italic", label: "Italic", content: italic_icon }
] %>

<%# ── 9. Set the state from your own code. press/release follow the group rules (a single-select group
     releases the other items, a required group keeps its last item) and sync the hidden input, without a change event:
       const toggle = this.application.getControllerForElementAndIdentifier(this.rangeTarget, "toggle-button")
       toggle.press("month")
     Setting aria-pressed (aria-checked for radios) yourself, or a Turbo morph, follows the same rules. %>
<%= render "shared/components/toggle/toggle_group", label: "Report range", variant: :segmented,
  html: { data: { report_target: "range" } }, items: [
  { value: "week", pressed: true, content: "Week" },
  { value: "month", content: "Month" }
] %>
</div>

Rendered usage example

Toggle ViewComponent

Don't forget the Stimulus controller!
The ViewComponent still requires the toggle_controller.js Stimulus controller to be installed. Copy the controller from the Installation section on this page.

1. Ensure the gem is installed

gem "view_component", "~> 4.2"

2. Copy component files

Download all 4 component files as a ZIP file.

Download all files (ZIP)

After downloading, unzip and copy the toggle/ folder to your app/components/ directory.

Copy these files to your app/components/toggle/ directory:

3. Usage example

<% bold_icon = capture do %><svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 18 18" fill="none" stroke="currentColor" stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M5.25 2.75h4.5a3 3 0 0 1 0 6h-4.5v-6Z"></path><path d="M5.25 8.75h5.5a3.25 3.25 0 0 1 0 6.5h-5.5v-6.5Z"></path></svg><% end %>
<% italic_icon = capture do %><svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 18 18" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M7.75 2.75h5.5"></path><path d="M4.75 15.25h5.5"></path><path d="M10.5 2.75l-3 12.5"></path></svg><% end %>
<% align_left_icon = capture do %><svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 18 18" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M2.75 4.25h12.5"></path><path d="M2.75 9h7.5"></path><path d="M2.75 13.75h10.5"></path></svg><% end %>
<% align_center_icon = capture do %><svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 18 18" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M2.75 4.25h12.5"></path><path d="M5.25 9h7.5"></path><path d="M3.75 13.75h10.5"></path></svg><% end %>
<% align_right_icon = capture do %><svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 18 18" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M2.75 4.25h12.5"></path><path d="M7.75 9h7.5"></path><path d="M4.75 13.75h10.5"></path></svg><% end %>
<div class="flex flex-col items-start gap-6">
<%# ── 1. Icon-only toggle: `label:` is the accessible name and makes the button square. Pass the icon as a block or with_content. %>
<%= render Toggle::Component.new(label: "Bold").with_content(bold_icon) %>

<%# ── 2. Text toggle, bordered, pressed on the server. Style content with `group-aria-pressed/toggle:`.
     With a model: pressed: current_user.favorited?(@post) %>
<%= render Toggle::Component.new(variant: :outline, pressed: true) do %>
  <svg xmlns="http://www.w3.org/2000/svg" class="size-4 group-aria-pressed/toggle:fill-current" width="18" height="18" viewBox="0 0 18 18" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M9 2.25l2.04 4.14 4.57.66-3.3 3.22.78 4.55L9 12.67l-4.09 2.15.78-4.55-3.3-3.22 4.57-.66L9 2.25Z"></path></svg>
  Favorite
<% end %>

<%# ── 3. Sizes: :sm (32px, 40px tall hit area), :md (36px, default), :lg (44px, touch). %>
<div class="flex items-center gap-2">
  <%= render Toggle::Component.new(label: "Italic", size: :sm).with_content(italic_icon) %>
  <%= render Toggle::Component.new(label: "Italic", size: :lg).with_content(italic_icon) %>
</div>

<%# ── 4. Standalone toggle in a form: `name:` adds a hidden input that submits "1" or "0".
     With a model: pressed: @notification_settings.mentions? %>
<%= form_with scope: :notification_settings, url: "#" do |form| %>
  <%= render Toggle::Component.new(name: form.field_name(:mentions), pressed: true, variant: :outline).with_content("Mentions") %>
<% end %>

<%# ── 5. Single-select toolbar: `required: true` keeps one item selected. It renders as a radiogroup
     (role="radio" items with aria-checked): arrow keys move and select, like native radios. %>
<%= render Toggle::GroupComponent.new(label: "Text alignment", required: true) do |group| %>
  <% group.with_item(value: "left", label: "Align left", pressed: true).with_content(align_left_icon) %>
  <% group.with_item(value: "center", label: "Align center").with_content(align_center_icon) %>
  <% group.with_item(value: "right", label: "Align right").with_content(align_right_icon) %>
<% end %>

<%# ── 6. Multi-select row of outline toggles, submitted as "bold,italic". With a model: pressed: @post.styles.include?("bold") %>
<%= render Toggle::GroupComponent.new(label: "Text style", variant: :outline, multiple: true, name: "post[styles]") do |group| %>
  <% group.with_item(value: "bold", label: "Bold", pressed: true).with_content(bold_icon) %>
  <% group.with_item(value: "italic", label: "Italic").with_content(italic_icon) %>
<% end %>

<%# ── 7. Segmented control: a sliding indicator, single-select and required by default (a radiogroup). %>
<%= render Toggle::GroupComponent.new(label: "Time range", variant: :segmented, size: :sm, name: "range") do |group| %>
  <% %w[1D 1W 1M 1Y].each do |range| %>
    <% group.with_item(value: range.downcase, pressed: range == "1M").with_content(range) %>
  <% end %>
<% end %>

<%# ── 8. React to changes: the group dispatches `toggle-button:change` with item, pressed, value and values. %>
<%= render Toggle::GroupComponent.new(label: "Text formatting", multiple: true,
                                      html: { data: { action: "toggle-button:change->editor#format" } }) do |group| %>
  <% group.with_item(value: "bold", label: "Bold").with_content(bold_icon) %>
  <% group.with_item(value: "italic", label: "Italic").with_content(italic_icon) %>
<% end %>

<%# ── 9. Set the state from your own code. press/release follow the group rules (a single-select group
     releases the other items, a required group keeps its last item) and sync the hidden input, without a change event:
       const toggle = this.application.getControllerForElementAndIdentifier(this.rangeTarget, "toggle-button")
       toggle.press("month")
     Setting aria-pressed (aria-checked for radios) yourself, or a Turbo morph, follows the same rules. %>
<%= render Toggle::GroupComponent.new(label: "Report range", variant: :segmented, html: { data: { report_target: "range" } }) do |group| %>
  <% group.with_item(value: "week", pressed: true).with_content("Week") %>
  <% group.with_item(value: "month").with_content("Month") %>
<% end %>
</div>

Rendered usage example

Toggle Rails Components

Toggles are two-state buttons that stay pressed, like Bold in an editor or a star on a repository. Group them into toolbars, segmented controls, and filter chips. For an on/off setting that takes effect immediately, use a Switch instead.

Installation

Add the controller to app/javascript/controllers/toggle_button_controller.js. It handles aria-pressed, single and multiple groups, arrow-key navigation, and the sliding indicator. The View Switcher, Filter Chips, and Form examples use native inputs and need no JavaScript at all.

import { Controller } from "@hotwired/stimulus";

// Toggle Button
//
// One controller for a standalone two-state button and for toggle groups.
// State lives in `aria-pressed`, so style it with Tailwind's `aria-pressed:` variants.
// In a `role="radiogroup"` (one item always selected), items are `role="radio"` and the
// state lives in `aria-checked` instead: style those with `aria-checked:` variants.
//
// Standalone toggle:
//   <button type="button" aria-pressed="false"
//           data-controller="toggle-button" data-action="toggle-button#toggle">
//
// Toggle group (one tab stop, arrow keys move focus between items):
//   <div role="toolbar" aria-label="Text style"
//        data-controller="toggle-button" data-toggle-button-multiple-value="true">
//     <button type="button" value="bold" aria-pressed="false"
//             data-toggle-button-target="item" data-action="toggle-button#toggle">
//   </div>
//
// Radio group (single choice, one item always selected; arrow keys move and select):
//   <div role="radiogroup" aria-label="Text alignment" data-controller="toggle-button">
//     <button type="button" role="radio" value="left" aria-checked="true"
//             data-toggle-button-target="item" data-action="toggle-button#toggle">
//   </div>
//
// From your own code (no change event, like setting a checkbox from JavaScript):
//   const toggle = application.getControllerForElementAndIdentifier(element, "toggle-button");
//   toggle.press("week"); toggle.release("bold"); toggle.press(); // no value: standalone toggle
// Or set aria-pressed / aria-checked yourself: the group rules still apply.
export default class extends Controller {
  static targets = ["item", "indicator", "input"];

  static values = {
    multiple: { type: Boolean, default: false }, // Groups: allow several pressed items
    required: { type: Boolean, default: false }, // Groups: keep at least one item pressed
  };

  connect() {
    this.items.forEach((item) => this.prepare(item));

    if (this.isGroup) {
      this.updateTabStop();
      this.onKeydown = this.navigate.bind(this);
      this.element.addEventListener("keydown", this.onKeydown);
    }

    if (this.hasIndicatorTarget) {
      this.moveIndicator({ animate: false });
      this.resizeObserver = new ResizeObserver(() => this.moveIndicator({ animate: false }));
      this.resizeObserver.observe(this.element);
    }

    // Stay in sync when something else rewrites the markup (Turbo morphs, other scripts).
    // A morph also drops the tabindex values set here, which would turn every item into a tab stop.
    this.mutationObserver = new MutationObserver((records) => this.observe(records));
    this.mutationObserver.observe(this.element, {
      attributes: true,
      subtree: true,
      attributeFilter: ["aria-pressed", "aria-checked", "style", "tabindex", "disabled"],
    });

    this.syncInput();
    this.warnAboutSetup();
    this.ready = true;
  }

  // Items added or removed later (Turbo Streams, client-side rendering) join the group.
  itemTargetConnected(item) {
    this.prepare(item);
    if (this.ready) this.refresh();
  }

  itemTargetDisconnected() {
    if (this.ready) this.refresh();
  }

  disconnect() {
    this.ready = false;
    if (this.onKeydown) this.element.removeEventListener("keydown", this.onKeydown);
    this.resizeObserver?.disconnect();
    this.mutationObserver?.disconnect();
  }

  // Action: press or release the clicked item.
  toggle(event) {
    const item = event.currentTarget;
    // Keyboard presses (event.detail === 0) jump straight to the new state: no waiting on motion.
    this.userChange(item, !this.isPressed(item), { animate: event.detail > 0 });
  }

  // Public API: press an item by value (or the standalone toggle when no value is given).
  // Also works as an action: data-action="toggle-button#press" data-toggle-button-value-param="week"
  press(value) {
    this.setByValue(value, true);
  }

  // Public API: release an item by value. A required group keeps its last pressed item.
  release(value) {
    this.setByValue(value, false);
  }

  // Roving focus: arrows, Home and End move between enabled items. Space/Enter toggle natively.
  // In a radio group the arrows also select, like native radio buttons.
  navigate(event) {
    // Leave browser and OS shortcuts (Alt+Left to go back, Cmd+Arrow) alone.
    if (event.altKey || event.ctrlKey || event.metaKey) return;

    const items = this.enabledItems;
    const index = items.indexOf(document.activeElement);
    if (index === -1) return;

    const rtl = getComputedStyle(this.element).direction === "rtl";
    const steps = { ArrowRight: rtl ? -1 : 1, ArrowLeft: rtl ? 1 : -1, ArrowDown: 1, ArrowUp: -1 };

    let next;
    if (event.key === "Home") next = 0;
    else if (event.key === "End") next = items.length - 1;
    else if (event.key in steps) next = (index + steps[event.key] + items.length) % items.length;
    else return;

    event.preventDefault();
    this.setTabStop(items[next]);
    items[next].focus();
    if (this.isRadioGroup && !this.isPressed(items[next])) this.userChange(items[next], true, { animate: false });
  }

  // Someone else changed the state (a script, a Turbo morph): apply the single-select rule, then re-sync.
  observe(records) {
    if (this.isSingle) {
      const pressed = records
        .filter((record) => record.attributeName === this.stateAttribute(record.target))
        .map((record) => record.target)
        .filter((item) => this.itemTargets.includes(item) && this.isPressed(item))
        .at(-1);
      if (pressed) this.itemTargets.forEach((other) => other !== pressed && this.setPressed(other, false));
    }
    this.refresh();
  }

  // Re-reads the DOM after an outside change. A no-op after our own updates.
  refresh() {
    if (this.isGroup) this.updateTabStop();
    this.syncInput();
    if (!this.hasIndicatorTarget) return;

    const item = this.pressedItems[0];
    const stale = item !== this.indicatorItem || (item && !this.indicatorTarget.style.width);
    if (stale) this.moveIndicator({ animate: false });
  }

  // A change made by the user: apply it, sync the hidden input and announce it.
  userChange(item, pressed, { animate }) {
    if (!this.apply(item, pressed, { animate })) return;

    if (this.isGroup) this.setTabStop(item);
    this.syncInput({ notify: true });
    this.dispatch("change", { detail: { item, pressed, value: item.value, values: this.values } });
  }

  // Sets one item and enforces the group rules. Returns false when the rules refuse the change.
  apply(item, pressed, { animate = false } = {}) {
    // A required group never ends up empty.
    if (!pressed && this.isRequired && this.isPressed(item) && this.pressedItems.length === 1) return false;

    if (this.isSingle && pressed) {
      this.items.forEach((other) => this.setPressed(other, other === item));
    } else {
      this.setPressed(item, pressed);
    }

    if (this.hasIndicatorTarget) this.moveIndicator({ animate });
    return true;
  }

  setByValue(valueOrEvent, pressed) {
    const fromAction = valueOrEvent instanceof Event;
    const value = fromAction ? valueOrEvent.params.value : valueOrEvent;
    const item = this.isGroup ? this.itemTargets.find((candidate) => candidate.value === String(value)) : this.element;

    if (!item) {
      console.warn(`[toggle-button] No item with value "${value}".`, this.element);
      return;
    }

    // From an action it is a user change (change event, form notified); from code it is silent.
    if (fromAction) {
      this.userChange(item, pressed, { animate: valueOrEvent.detail > 0 });
    } else if (this.apply(item, pressed)) {
      this.syncInput();
    }
  }

  // Slides the optional indicator under the pressed item (single-select groups).
  moveIndicator({ animate }) {
    const indicator = this.indicatorTarget;
    const item = this.pressedItems[0];
    this.indicatorItem = item;

    if (!item) {
      indicator.style.opacity = "0";
      return;
    }

    // Jump (don't slide) on first paint, on resize, and when reappearing.
    const jump = !animate || indicator.style.opacity === "0";
    if (jump) indicator.style.transition = "none";

    indicator.style.width = `${item.offsetWidth}px`;
    indicator.style.height = `${item.offsetHeight}px`;
    indicator.style.translate = `${item.offsetLeft}px ${item.offsetTop}px`;
    indicator.style.opacity = "1";

    if (jump) {
      indicator.offsetWidth; // Flush styles so the jump is not animated
      indicator.style.transition = "";
    }
  }

  // Mirrors the state into an optional hidden input so it submits with a form.
  // Standalone: "1" or "0". Group: pressed values joined with commas.
  syncInput({ notify = false } = {}) {
    if (!this.hasInputTarget) return;

    const value = this.isGroup ? this.values.join(",") : this.isPressed(this.element) ? "1" : "0";
    if (this.inputTarget.value === value) return;

    this.inputTarget.value = value;
    if (notify) this.inputTarget.dispatchEvent(new Event("change", { bubbles: true }));
  }

  setPressed(item, pressed) {
    const value = pressed ? "true" : "false";
    const attribute = this.stateAttribute(item);
    // Only write real changes: the mutation observer reacts to every write.
    if (item.getAttribute(attribute) !== value) item.setAttribute(attribute, value);
  }

  // Radios keep their state in aria-checked, every other toggle in aria-pressed.
  stateAttribute(item) {
    return item.getAttribute("role") === "radio" ? "aria-checked" : "aria-pressed";
  }

  // Setup warnings: common mistakes that are easy to miss because nothing visibly breaks.
  warnAboutSetup() {
    const warn = (message, element = this.element) => console.warn(`[toggle-button] ${message}`, element);
    const named = (element) =>
      element.hasAttribute("aria-label") || element.hasAttribute("aria-labelledby") || element.textContent.trim() !== "";

    this.items.forEach((item) => {
      if (item.tagName !== "BUTTON") warn("Toggles must be <button type=\"button\"> elements.", item);
      else if (item.type !== "button") warn('Add type="button" so the toggle does not submit its form.', item);
      if (!named(item)) warn('Icon-only toggles need an aria-label (for example "Bold").', item);
    });

    if (this.isGroup) {
      if (!this.element.hasAttribute("aria-label") && !this.element.hasAttribute("aria-labelledby")) {
        warn('Give the group an aria-label (for example "Text formatting") so screen readers can name it.');
      }
      if (this.itemTargets.some((item) => !item.value)) {
        warn("Give every item a value attribute: it is what the change event and the hidden input report.");
      }
      if (this.isRadioGroup && this.multipleValue) {
        warn('A radiogroup has one selected item; remove data-toggle-button-multiple-value or use role="toolbar".');
      }
      if (this.isRadioGroup && this.itemTargets.some((item) => item.getAttribute("role") !== "radio")) {
        warn('Items of a role="radiogroup" need role="radio" and aria-checked (not aria-pressed).');
      }
      if (!this.isRadioGroup && this.itemTargets.some((item) => item.getAttribute("role") === "radio")) {
        warn('role="radio" items belong in a role="radiogroup" wrapper.');
      }
      if (this.multipleValue && this.hasIndicatorTarget) {
        warn("The indicator follows a single pressed item; remove data-toggle-button-multiple-value or the indicator.");
      }
    } else if (this.requiredValue || this.multipleValue) {
      warn('"required" and "multiple" only apply to groups. Add item targets, or remove the values.');
    }

    if (this.hasIndicatorTarget && getComputedStyle(this.element).position === "static") {
      warn('The group must be positioned (add "relative") so the indicator lines up with the items.');
    }
  }

  prepare(item) {
    if (!item.hasAttribute(this.stateAttribute(item))) item.setAttribute(this.stateAttribute(item), "false");
  }

  // Keeps exactly one enabled item in the tab order: the current one if it is still usable,
  // otherwise the first pressed item, otherwise the first enabled item.
  updateTabStop() {
    const enabled = this.enabledItems;
    const current = enabled.find((item) => item.getAttribute("tabindex") === "0");
    this.setTabStop(current || enabled.find((item) => this.isPressed(item)) || enabled[0]);
  }

  setTabStop(active) {
    if (!active) return;
    this.items.forEach((item) => {
      const tabindex = item === active ? "0" : "-1";
      // Only write real changes: the mutation observer would otherwise loop forever.
      if (item.getAttribute("tabindex") !== tabindex) item.setAttribute("tabindex", tabindex);
    });
  }

  isPressed(item) {
    return item.getAttribute(this.stateAttribute(item)) === "true";
  }

  get isGroup() {
    return this.hasItemTarget;
  }

  get isRadioGroup() {
    return this.isGroup && this.element.getAttribute("role") === "radiogroup";
  }

  get isSingle() {
    return this.isGroup && !this.multipleValue;
  }

  // A radio group always keeps one item selected.
  get isRequired() {
    return this.requiredValue || this.isRadioGroup;
  }

  get items() {
    return this.isGroup ? this.itemTargets : [this.element];
  }

  get enabledItems() {
    return this.items.filter((item) => !item.disabled);
  }

  get pressedItems() {
    return this.items.filter((item) => this.isPressed(item));
  }

  get values() {
    return this.pressedItems.map((item) => item.value);
  }
}

Examples

Basic Toggle

A two-state button that stays pressed until it is clicked again. The state lives in aria-pressed, so screen readers announce it and Tailwind's aria-pressed: variants style it.

<button type="button" aria-label="Bold" aria-pressed="false" data-controller="toggle-button" data-action="toggle-button#toggle" class="inline-flex size-9 shrink-0 items-center justify-center rounded-lg text-neutral-600 hover:bg-neutral-100 hover:text-neutral-900 aria-pressed:bg-neutral-200/80 aria-pressed:text-neutral-950 dark:text-neutral-400 dark:hover:bg-white/10 dark:hover:text-neutral-100 dark:aria-pressed:bg-white/15 dark:aria-pressed:text-white transition duration-150 ease-out select-none disabled:pointer-events-none disabled:opacity-50 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-neutral-600 dark:focus-visible:outline-neutral-200 forced-colors:aria-pressed:bg-[Highlight] forced-colors:aria-pressed:text-[HighlightText] forced-colors:aria-pressed:forced-color-adjust-none">
  <svg xmlns="http://www.w3.org/2000/svg" class="size-4" width="18" height="18" viewBox="0 0 18 18" fill="none" stroke="currentColor" stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M5.25 2.75h4.5a3 3 0 0 1 0 6h-4.5v-6Z"></path><path d="M5.25 8.75h5.5a3.25 3.25 0 0 1 0 6.5h-5.5v-6.5Z"></path></svg>
</button>

Outline Toggle

A bordered variant whose pressed state sits in with an inset shadow. Render aria-pressed="true" on the server to start in the pressed state.

<div class="flex items-center gap-2">
  <button type="button" aria-label="Italic" aria-pressed="false" data-controller="toggle-button" data-action="toggle-button#toggle" class="inline-flex size-9 shrink-0 items-center justify-center rounded-lg border border-black/10 bg-white text-neutral-600 shadow-xs hover:bg-neutral-50 hover:text-neutral-900 aria-pressed:border-black/15 aria-pressed:bg-neutral-100 aria-pressed:text-neutral-950 aria-pressed:shadow-none aria-pressed:inset-shadow-xs dark:border-white/10 dark:bg-neutral-800 dark:text-neutral-400 dark:hover:bg-neutral-700/60 dark:hover:text-neutral-100 dark:aria-pressed:border-white/20 dark:aria-pressed:bg-neutral-600 dark:aria-pressed:text-white transition duration-150 ease-out select-none disabled:pointer-events-none disabled:opacity-50 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-neutral-600 dark:focus-visible:outline-neutral-200 forced-colors:aria-pressed:bg-[Highlight] forced-colors:aria-pressed:text-[HighlightText] forced-colors:aria-pressed:forced-color-adjust-none">
    <svg xmlns="http://www.w3.org/2000/svg" class="size-4" width="18" height="18" viewBox="0 0 18 18" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M7.75 2.75h5.5"></path><path d="M4.75 15.25h5.5"></path><path d="M10.5 2.75l-3 12.5"></path></svg>
  </button>

  <%# Pressed by default: render aria-pressed="true" on the server %>
  <button type="button" aria-label="Underline" aria-pressed="true" data-controller="toggle-button" data-action="toggle-button#toggle" class="inline-flex size-9 shrink-0 items-center justify-center rounded-lg border border-black/10 bg-white text-neutral-600 shadow-xs hover:bg-neutral-50 hover:text-neutral-900 aria-pressed:border-black/15 aria-pressed:bg-neutral-100 aria-pressed:text-neutral-950 aria-pressed:shadow-none aria-pressed:inset-shadow-xs dark:border-white/10 dark:bg-neutral-800 dark:text-neutral-400 dark:hover:bg-neutral-700/60 dark:hover:text-neutral-100 dark:aria-pressed:border-white/20 dark:aria-pressed:bg-neutral-600 dark:aria-pressed:text-white transition duration-150 ease-out select-none disabled:pointer-events-none disabled:opacity-50 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-neutral-600 dark:focus-visible:outline-neutral-200 forced-colors:aria-pressed:bg-[Highlight] forced-colors:aria-pressed:text-[HighlightText] forced-colors:aria-pressed:forced-color-adjust-none">
    <svg xmlns="http://www.w3.org/2000/svg" class="size-4" width="18" height="18" viewBox="0 0 18 18" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M5.25 2.75v5a3.75 3.75 0 0 0 7.5 0v-5"></path><path d="M3.75 15.25h10.5"></path></svg>
  </button>
</div>

Toggle with Text

An icon and a label together. The icon fills in softly when the toggle is on, so the state does not rely on background color alone.

<div class="flex flex-wrap items-center justify-center gap-2">
  <%# The icon fills in when the toggle is pressed %>
  <button type="button" aria-pressed="false" data-controller="toggle-button" data-action="toggle-button#toggle" class="group/toggle inline-flex h-9 gap-1.5 px-3 text-sm font-medium shrink-0 items-center justify-center rounded-lg text-neutral-600 hover:bg-neutral-100 hover:text-neutral-900 aria-pressed:bg-neutral-200/80 aria-pressed:text-neutral-950 dark:text-neutral-400 dark:hover:bg-white/10 dark:hover:text-neutral-100 dark:aria-pressed:bg-white/15 dark:aria-pressed:text-white transition duration-150 ease-out select-none disabled:pointer-events-none disabled:opacity-50 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-neutral-600 dark:focus-visible:outline-neutral-200 forced-colors:aria-pressed:bg-[Highlight] forced-colors:aria-pressed:text-[HighlightText] forced-colors:aria-pressed:forced-color-adjust-none">
    <svg xmlns="http://www.w3.org/2000/svg" class="size-4 transition-[fill] duration-150 ease-out group-aria-pressed/toggle:fill-neutral-950/15 dark:group-aria-pressed/toggle:fill-white/25" width="18" height="18" viewBox="0 0 18 18" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M1.75 9S4.25 4 9 4s7.25 5 7.25 5-2.5 5-7.25 5-7.25-5-7.25-5Z"></path><circle cx="9" cy="9" r="2.25"></circle></svg>
    Watch
  </button>

  <button type="button" aria-pressed="true" data-controller="toggle-button" data-action="toggle-button#toggle" class="group/toggle inline-flex h-9 gap-1.5 px-3 text-sm font-medium shrink-0 items-center justify-center rounded-lg border border-black/10 bg-white text-neutral-600 shadow-xs hover:bg-neutral-50 hover:text-neutral-900 aria-pressed:border-black/15 aria-pressed:bg-neutral-100 aria-pressed:text-neutral-950 aria-pressed:shadow-none aria-pressed:inset-shadow-xs dark:border-white/10 dark:bg-neutral-800 dark:text-neutral-400 dark:hover:bg-neutral-700/60 dark:hover:text-neutral-100 dark:aria-pressed:border-white/20 dark:aria-pressed:bg-neutral-600 dark:aria-pressed:text-white transition duration-150 ease-out select-none disabled:pointer-events-none disabled:opacity-50 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-neutral-600 dark:focus-visible:outline-neutral-200 forced-colors:aria-pressed:bg-[Highlight] forced-colors:aria-pressed:text-[HighlightText] forced-colors:aria-pressed:forced-color-adjust-none">
    <svg xmlns="http://www.w3.org/2000/svg" class="size-4 transition-[fill] duration-150 ease-out group-aria-pressed/toggle:fill-neutral-950/15 dark:group-aria-pressed/toggle:fill-white/25" width="18" height="18" viewBox="0 0 18 18" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M9 2.25a4.25 4.25 0 0 0-4.25 4.25c0 3.75-1.5 5.25-1.5 5.25h11.5s-1.5-1.5-1.5-5.25A4.25 4.25 0 0 0 9 2.25Z"></path><path d="M7.5 14.5a1.6 1.6 0 0 0 3 0"></path></svg>
    Notify me
  </button>
</div>

Toggle Sizes

Small (32px), default (36px), and large (44px) toggles. Use the large size where touch is the primary input.

<div class="flex items-center gap-3">
  <%# Small: 32px, with an invisible 40px hit area %>
  <button type="button" aria-label="Bold" aria-pressed="false" data-controller="toggle-button" data-action="toggle-button#toggle" class="inline-flex relative size-8 after:absolute after:inset-x-0 after:-inset-y-1 shrink-0 items-center justify-center rounded-md border border-black/10 bg-white text-neutral-600 shadow-xs hover:bg-neutral-50 hover:text-neutral-900 aria-pressed:border-black/15 aria-pressed:bg-neutral-100 aria-pressed:text-neutral-950 aria-pressed:shadow-none aria-pressed:inset-shadow-xs dark:border-white/10 dark:bg-neutral-800 dark:text-neutral-400 dark:hover:bg-neutral-700/60 dark:hover:text-neutral-100 dark:aria-pressed:border-white/20 dark:aria-pressed:bg-neutral-600 dark:aria-pressed:text-white transition duration-150 ease-out select-none disabled:pointer-events-none disabled:opacity-50 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-neutral-600 dark:focus-visible:outline-neutral-200 forced-colors:aria-pressed:bg-[Highlight] forced-colors:aria-pressed:text-[HighlightText] forced-colors:aria-pressed:forced-color-adjust-none">
    <svg xmlns="http://www.w3.org/2000/svg" class="size-3.5" width="18" height="18" viewBox="0 0 18 18" fill="none" stroke="currentColor" stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M5.25 2.75h4.5a3 3 0 0 1 0 6h-4.5v-6Z"></path><path d="M5.25 8.75h5.5a3.25 3.25 0 0 1 0 6.5h-5.5v-6.5Z"></path></svg>
  </button>

  <%# Default: 36px %>
  <button type="button" aria-label="Bold" aria-pressed="true" data-controller="toggle-button" data-action="toggle-button#toggle" class="inline-flex size-9 shrink-0 items-center justify-center rounded-lg border border-black/10 bg-white text-neutral-600 shadow-xs hover:bg-neutral-50 hover:text-neutral-900 aria-pressed:border-black/15 aria-pressed:bg-neutral-100 aria-pressed:text-neutral-950 aria-pressed:shadow-none aria-pressed:inset-shadow-xs dark:border-white/10 dark:bg-neutral-800 dark:text-neutral-400 dark:hover:bg-neutral-700/60 dark:hover:text-neutral-100 dark:aria-pressed:border-white/20 dark:aria-pressed:bg-neutral-600 dark:aria-pressed:text-white transition duration-150 ease-out select-none disabled:pointer-events-none disabled:opacity-50 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-neutral-600 dark:focus-visible:outline-neutral-200 forced-colors:aria-pressed:bg-[Highlight] forced-colors:aria-pressed:text-[HighlightText] forced-colors:aria-pressed:forced-color-adjust-none">
    <svg xmlns="http://www.w3.org/2000/svg" class="size-4" width="18" height="18" viewBox="0 0 18 18" fill="none" stroke="currentColor" stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M5.25 2.75h4.5a3 3 0 0 1 0 6h-4.5v-6Z"></path><path d="M5.25 8.75h5.5a3.25 3.25 0 0 1 0 6.5h-5.5v-6.5Z"></path></svg>
  </button>

  <%# Large: 44px, a comfortable touch target %>
  <button type="button" aria-label="Bold" aria-pressed="false" data-controller="toggle-button" data-action="toggle-button#toggle" class="inline-flex size-11 shrink-0 items-center justify-center rounded-xl border border-black/10 bg-white text-neutral-600 shadow-xs hover:bg-neutral-50 hover:text-neutral-900 aria-pressed:border-black/15 aria-pressed:bg-neutral-100 aria-pressed:text-neutral-950 aria-pressed:shadow-none aria-pressed:inset-shadow-xs dark:border-white/10 dark:bg-neutral-800 dark:text-neutral-400 dark:hover:bg-neutral-700/60 dark:hover:text-neutral-100 dark:aria-pressed:border-white/20 dark:aria-pressed:bg-neutral-600 dark:aria-pressed:text-white transition duration-150 ease-out select-none disabled:pointer-events-none disabled:opacity-50 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-neutral-600 dark:focus-visible:outline-neutral-200 forced-colors:aria-pressed:bg-[Highlight] forced-colors:aria-pressed:text-[HighlightText] forced-colors:aria-pressed:forced-color-adjust-none">
    <svg xmlns="http://www.w3.org/2000/svg" class="size-5" width="18" height="18" viewBox="0 0 18 18" fill="none" stroke="currentColor" stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M5.25 2.75h4.5a3 3 0 0 1 0 6h-4.5v-6Z"></path><path d="M5.25 8.75h5.5a3.25 3.25 0 0 1 0 6.5h-5.5v-6.5Z"></path></svg>
  </button>
</div>

Disabled Toggle

Disabled toggles are skipped by the keyboard and still show whether they are on or off.

<div class="flex items-center gap-2">
  <button type="button" aria-label="Bold" aria-pressed="false" disabled data-controller="toggle-button" data-action="toggle-button#toggle" class="inline-flex size-9 shrink-0 items-center justify-center rounded-lg border border-black/10 bg-white text-neutral-600 shadow-xs hover:bg-neutral-50 hover:text-neutral-900 aria-pressed:border-black/15 aria-pressed:bg-neutral-100 aria-pressed:text-neutral-950 aria-pressed:shadow-none aria-pressed:inset-shadow-xs dark:border-white/10 dark:bg-neutral-800 dark:text-neutral-400 dark:hover:bg-neutral-700/60 dark:hover:text-neutral-100 dark:aria-pressed:border-white/20 dark:aria-pressed:bg-neutral-600 dark:aria-pressed:text-white transition duration-150 ease-out select-none disabled:pointer-events-none disabled:opacity-50 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-neutral-600 dark:focus-visible:outline-neutral-200 forced-colors:aria-pressed:bg-[Highlight] forced-colors:aria-pressed:text-[HighlightText] forced-colors:aria-pressed:forced-color-adjust-none">
    <svg xmlns="http://www.w3.org/2000/svg" class="size-4" width="18" height="18" viewBox="0 0 18 18" fill="none" stroke="currentColor" stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M5.25 2.75h4.5a3 3 0 0 1 0 6h-4.5v-6Z"></path><path d="M5.25 8.75h5.5a3.25 3.25 0 0 1 0 6.5h-5.5v-6.5Z"></path></svg>
  </button>

  <%# Disabled toggles keep showing their pressed state %>
  <button type="button" aria-label="Italic" aria-pressed="true" disabled data-controller="toggle-button" data-action="toggle-button#toggle" class="inline-flex size-9 shrink-0 items-center justify-center rounded-lg border border-black/10 bg-white text-neutral-600 shadow-xs hover:bg-neutral-50 hover:text-neutral-900 aria-pressed:border-black/15 aria-pressed:bg-neutral-100 aria-pressed:text-neutral-950 aria-pressed:shadow-none aria-pressed:inset-shadow-xs dark:border-white/10 dark:bg-neutral-800 dark:text-neutral-400 dark:hover:bg-neutral-700/60 dark:hover:text-neutral-100 dark:aria-pressed:border-white/20 dark:aria-pressed:bg-neutral-600 dark:aria-pressed:text-white transition duration-150 ease-out select-none disabled:pointer-events-none disabled:opacity-50 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-neutral-600 dark:focus-visible:outline-neutral-200 forced-colors:aria-pressed:bg-[Highlight] forced-colors:aria-pressed:text-[HighlightText] forced-colors:aria-pressed:forced-color-adjust-none">
    <svg xmlns="http://www.w3.org/2000/svg" class="size-4" width="18" height="18" viewBox="0 0 18 18" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M7.75 2.75h5.5"></path><path d="M4.75 15.25h5.5"></path><path d="M10.5 2.75l-3 12.5"></path></svg>
  </button>
</div>

Single Toggle Group

A toolbar where one option is always selected, here for text alignment. Because one item is always selected, it is a radio group: role="radiogroup" with role="radio" items whose state lives in aria-checked, so screen readers announce "radio button, 2 of 4, checked". The group is a single tab stop: arrow keys, Home, and End move between items and select them, like native radio buttons.

Good typography is invisible. Readers should notice what the words say, never how the paragraph was set.

<%# One item at a time, always one selected: a radio group. Items are role="radio" with aria-checked, so style them with aria-checked: %>
<%# Arrow keys move and select, like native radio buttons; clicking the selected item keeps it selected. %>
<div class="group/align flex w-full max-w-sm flex-col items-center gap-4">
  <div role="radiogroup" aria-label="Text alignment" data-controller="toggle-button" class="inline-flex items-center gap-0.5 rounded-xl border border-black/10 bg-white p-1 shadow-xs dark:border-white/10 dark:bg-neutral-800">
    <button type="button" value="left" aria-label="Align left" role="radio" aria-checked="true" data-toggle-button-target="item" data-action="toggle-button#toggle" class="inline-flex size-9 shrink-0 items-center justify-center rounded-lg text-neutral-600 hover:bg-neutral-100 hover:text-neutral-900 aria-checked:bg-neutral-200/80 aria-checked:text-neutral-950 dark:text-neutral-400 dark:hover:bg-white/10 dark:hover:text-neutral-100 dark:aria-checked:bg-white/15 dark:aria-checked:text-white transition duration-150 ease-out select-none disabled:pointer-events-none disabled:opacity-50 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-neutral-600 dark:focus-visible:outline-neutral-200 forced-colors:aria-checked:bg-[Highlight] forced-colors:aria-checked:text-[HighlightText] forced-colors:aria-checked:forced-color-adjust-none">
      <svg xmlns="http://www.w3.org/2000/svg" class="size-4" width="18" height="18" viewBox="0 0 18 18" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M2.75 4.25h12.5"></path><path d="M2.75 9h7.5"></path><path d="M2.75 13.75h10.5"></path></svg>
    </button>
    <button type="button" value="center" aria-label="Align center" role="radio" aria-checked="false" data-toggle-button-target="item" data-action="toggle-button#toggle" class="inline-flex size-9 shrink-0 items-center justify-center rounded-lg text-neutral-600 hover:bg-neutral-100 hover:text-neutral-900 aria-checked:bg-neutral-200/80 aria-checked:text-neutral-950 dark:text-neutral-400 dark:hover:bg-white/10 dark:hover:text-neutral-100 dark:aria-checked:bg-white/15 dark:aria-checked:text-white transition duration-150 ease-out select-none disabled:pointer-events-none disabled:opacity-50 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-neutral-600 dark:focus-visible:outline-neutral-200 forced-colors:aria-checked:bg-[Highlight] forced-colors:aria-checked:text-[HighlightText] forced-colors:aria-checked:forced-color-adjust-none">
      <svg xmlns="http://www.w3.org/2000/svg" class="size-4" width="18" height="18" viewBox="0 0 18 18" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M2.75 4.25h12.5"></path><path d="M5.25 9h7.5"></path><path d="M3.75 13.75h10.5"></path></svg>
    </button>
    <button type="button" value="right" aria-label="Align right" role="radio" aria-checked="false" data-toggle-button-target="item" data-action="toggle-button#toggle" class="inline-flex size-9 shrink-0 items-center justify-center rounded-lg text-neutral-600 hover:bg-neutral-100 hover:text-neutral-900 aria-checked:bg-neutral-200/80 aria-checked:text-neutral-950 dark:text-neutral-400 dark:hover:bg-white/10 dark:hover:text-neutral-100 dark:aria-checked:bg-white/15 dark:aria-checked:text-white transition duration-150 ease-out select-none disabled:pointer-events-none disabled:opacity-50 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-neutral-600 dark:focus-visible:outline-neutral-200 forced-colors:aria-checked:bg-[Highlight] forced-colors:aria-checked:text-[HighlightText] forced-colors:aria-checked:forced-color-adjust-none">
      <svg xmlns="http://www.w3.org/2000/svg" class="size-4" width="18" height="18" viewBox="0 0 18 18" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M2.75 4.25h12.5"></path><path d="M7.75 9h7.5"></path><path d="M4.75 13.75h10.5"></path></svg>
    </button>
    <button type="button" value="justify" aria-label="Justify" role="radio" aria-checked="false" data-toggle-button-target="item" data-action="toggle-button#toggle" class="inline-flex size-9 shrink-0 items-center justify-center rounded-lg text-neutral-600 hover:bg-neutral-100 hover:text-neutral-900 aria-checked:bg-neutral-200/80 aria-checked:text-neutral-950 dark:text-neutral-400 dark:hover:bg-white/10 dark:hover:text-neutral-100 dark:aria-checked:bg-white/15 dark:aria-checked:text-white transition duration-150 ease-out select-none disabled:pointer-events-none disabled:opacity-50 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-neutral-600 dark:focus-visible:outline-neutral-200 forced-colors:aria-checked:bg-[Highlight] forced-colors:aria-checked:text-[HighlightText] forced-colors:aria-checked:forced-color-adjust-none">
      <svg xmlns="http://www.w3.org/2000/svg" class="size-4" width="18" height="18" viewBox="0 0 18 18" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M2.75 4.25h12.5"></path><path d="M2.75 9h12.5"></path><path d="M2.75 13.75h12.5"></path></svg>
    </button>
  </div>

  <%# Optional: the sample text follows the pressed item with CSS only %>
  <p class="w-full text-sm text-pretty text-neutral-600 group-has-[[value=center][aria-checked=true]]/align:text-center group-has-[[value=justify][aria-checked=true]]/align:text-justify group-has-[[value=right][aria-checked=true]]/align:text-right dark:text-neutral-400">
    Good typography is invisible. Readers should notice what the words say, never how the paragraph was set.
  </p>
</div>

Multiple Toggle Group

A formatting toolbar where any combination can be pressed. Set data-toggle-button-multiple-value="true" on the group.

The quick brown fox jumps over the lazy dog.

<%# Any number of items can be pressed. Tab enters the group once, arrow keys move between items. %>
<div class="group/format flex w-full max-w-sm flex-col items-center gap-4">
  <div role="toolbar" aria-label="Text formatting" data-controller="toggle-button" data-toggle-button-multiple-value="true" class="inline-flex items-center gap-0.5 rounded-xl border border-black/10 bg-white p-1 shadow-xs dark:border-white/10 dark:bg-neutral-800">
    <button type="button" value="bold" aria-label="Bold" aria-pressed="true" data-toggle-button-target="item" data-action="toggle-button#toggle" class="inline-flex size-9 shrink-0 items-center justify-center rounded-lg text-neutral-600 hover:bg-neutral-100 hover:text-neutral-900 aria-pressed:bg-neutral-200/80 aria-pressed:text-neutral-950 dark:text-neutral-400 dark:hover:bg-white/10 dark:hover:text-neutral-100 dark:aria-pressed:bg-white/15 dark:aria-pressed:text-white transition duration-150 ease-out select-none disabled:pointer-events-none disabled:opacity-50 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-neutral-600 dark:focus-visible:outline-neutral-200 forced-colors:aria-pressed:bg-[Highlight] forced-colors:aria-pressed:text-[HighlightText] forced-colors:aria-pressed:forced-color-adjust-none">
      <svg xmlns="http://www.w3.org/2000/svg" class="size-4" width="18" height="18" viewBox="0 0 18 18" fill="none" stroke="currentColor" stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M5.25 2.75h4.5a3 3 0 0 1 0 6h-4.5v-6Z"></path><path d="M5.25 8.75h5.5a3.25 3.25 0 0 1 0 6.5h-5.5v-6.5Z"></path></svg>
    </button>
    <button type="button" value="italic" aria-label="Italic" aria-pressed="false" data-toggle-button-target="item" data-action="toggle-button#toggle" class="inline-flex size-9 shrink-0 items-center justify-center rounded-lg text-neutral-600 hover:bg-neutral-100 hover:text-neutral-900 aria-pressed:bg-neutral-200/80 aria-pressed:text-neutral-950 dark:text-neutral-400 dark:hover:bg-white/10 dark:hover:text-neutral-100 dark:aria-pressed:bg-white/15 dark:aria-pressed:text-white transition duration-150 ease-out select-none disabled:pointer-events-none disabled:opacity-50 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-neutral-600 dark:focus-visible:outline-neutral-200 forced-colors:aria-pressed:bg-[Highlight] forced-colors:aria-pressed:text-[HighlightText] forced-colors:aria-pressed:forced-color-adjust-none">
      <svg xmlns="http://www.w3.org/2000/svg" class="size-4" width="18" height="18" viewBox="0 0 18 18" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M7.75 2.75h5.5"></path><path d="M4.75 15.25h5.5"></path><path d="M10.5 2.75l-3 12.5"></path></svg>
    </button>
    <button type="button" value="underline" aria-label="Underline" aria-pressed="false" data-toggle-button-target="item" data-action="toggle-button#toggle" class="inline-flex size-9 shrink-0 items-center justify-center rounded-lg text-neutral-600 hover:bg-neutral-100 hover:text-neutral-900 aria-pressed:bg-neutral-200/80 aria-pressed:text-neutral-950 dark:text-neutral-400 dark:hover:bg-white/10 dark:hover:text-neutral-100 dark:aria-pressed:bg-white/15 dark:aria-pressed:text-white transition duration-150 ease-out select-none disabled:pointer-events-none disabled:opacity-50 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-neutral-600 dark:focus-visible:outline-neutral-200 forced-colors:aria-pressed:bg-[Highlight] forced-colors:aria-pressed:text-[HighlightText] forced-colors:aria-pressed:forced-color-adjust-none">
      <svg xmlns="http://www.w3.org/2000/svg" class="size-4" width="18" height="18" viewBox="0 0 18 18" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M5.25 2.75v5a3.75 3.75 0 0 0 7.5 0v-5"></path><path d="M3.75 15.25h10.5"></path></svg>
    </button>
  </div>

  <%# Optional: the sample text reflects the pressed items with CSS only %>
  <p class="text-center text-sm text-neutral-600 underline-offset-4 group-has-[[value=bold][aria-pressed=true]]/format:font-semibold group-has-[[value=italic][aria-pressed=true]]/format:italic group-has-[[value=underline][aria-pressed=true]]/format:underline dark:text-neutral-400">
    The quick brown fox jumps over the lazy dog.
  </p>
</div>

Segmented Control

A radio group with an indicator that slides and resizes to the selected segment. Segments can have different widths, the indicator stays aligned when the control is resized, and keyboard presses skip the slide so they feel instant. Short labels like "1D" stay the accessible name, so voice control users can say what they see; the long form is in title.

<%# The indicator target is measured and moved under the selected item, so segments can have any width. %>
<%# One option is always selected, so this is a radio group: role="radio" items with aria-checked (style them with aria-checked:). %>
<%# The visible text ("1D") is the accessible name, so voice control users can say what they see. `title` adds the long form. %>
<div role="radiogroup" aria-label="Time range" data-controller="toggle-button" class="relative inline-flex rounded-[10px] bg-neutral-100 p-0.5 inset-ring inset-ring-black/5 dark:bg-neutral-800 dark:inset-ring-white/5">
  <%# Sliding indicator: keep it absolutely positioned at the top left of a relative parent %>
  <span data-toggle-button-target="indicator" aria-hidden="true" class="pointer-events-none absolute top-0 left-0 rounded-lg bg-white shadow-sm ring-1 ring-black/5 transition-[translate,width] duration-250 ease-[cubic-bezier(0.23,1,0.32,1)] motion-reduce:transition-none dark:bg-neutral-600 dark:ring-white/10 forced-colors:bg-[Highlight] forced-colors:forced-color-adjust-none"></span>

  <button type="button" value="1d" title="1 day" role="radio" aria-checked="false" data-toggle-button-target="item" data-action="toggle-button#toggle" class="relative h-8 rounded-lg px-3 text-sm font-medium text-neutral-500 transition duration-150 ease-out select-none after:absolute after:inset-x-0 after:-inset-y-1 hover:text-neutral-900 aria-checked:text-neutral-950 dark:text-neutral-400 dark:hover:text-neutral-100 dark:aria-checked:text-white focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-neutral-600 dark:focus-visible:outline-neutral-200 forced-colors:aria-checked:text-[HighlightText] forced-colors:aria-checked:forced-color-adjust-none">1D</button>
  <button type="button" value="1w" title="1 week" role="radio" aria-checked="false" data-toggle-button-target="item" data-action="toggle-button#toggle" class="relative h-8 rounded-lg px-3 text-sm font-medium text-neutral-500 transition duration-150 ease-out select-none after:absolute after:inset-x-0 after:-inset-y-1 hover:text-neutral-900 aria-checked:text-neutral-950 dark:text-neutral-400 dark:hover:text-neutral-100 dark:aria-checked:text-white focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-neutral-600 dark:focus-visible:outline-neutral-200 forced-colors:aria-checked:text-[HighlightText] forced-colors:aria-checked:forced-color-adjust-none">1W</button>
  <button type="button" value="1m" title="1 month" role="radio" aria-checked="true" data-toggle-button-target="item" data-action="toggle-button#toggle" class="relative h-8 rounded-lg px-3 text-sm font-medium text-neutral-500 transition duration-150 ease-out select-none after:absolute after:inset-x-0 after:-inset-y-1 hover:text-neutral-900 aria-checked:text-neutral-950 dark:text-neutral-400 dark:hover:text-neutral-100 dark:aria-checked:text-white focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-neutral-600 dark:focus-visible:outline-neutral-200 forced-colors:aria-checked:text-[HighlightText] forced-colors:aria-checked:forced-color-adjust-none">1M</button>
  <button type="button" value="3m" title="3 months" role="radio" aria-checked="false" data-toggle-button-target="item" data-action="toggle-button#toggle" class="relative h-8 rounded-lg px-3 text-sm font-medium text-neutral-500 transition duration-150 ease-out select-none after:absolute after:inset-x-0 after:-inset-y-1 hover:text-neutral-900 aria-checked:text-neutral-950 dark:text-neutral-400 dark:hover:text-neutral-100 dark:aria-checked:text-white focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-neutral-600 dark:focus-visible:outline-neutral-200 forced-colors:aria-checked:text-[HighlightText] forced-colors:aria-checked:forced-color-adjust-none">3M</button>
  <button type="button" value="1y" title="1 year" role="radio" aria-checked="false" data-toggle-button-target="item" data-action="toggle-button#toggle" class="relative h-8 rounded-lg px-3 text-sm font-medium text-neutral-500 transition duration-150 ease-out select-none after:absolute after:inset-x-0 after:-inset-y-1 hover:text-neutral-900 aria-checked:text-neutral-950 dark:text-neutral-400 dark:hover:text-neutral-100 dark:aria-checked:text-white focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-neutral-600 dark:focus-visible:outline-neutral-200 forced-colors:aria-checked:text-[HighlightText] forced-colors:aria-checked:forced-color-adjust-none">1Y</button>
  <button type="button" value="all" role="radio" aria-checked="false" data-toggle-button-target="item" data-action="toggle-button#toggle" class="relative h-8 rounded-lg px-3 text-sm font-medium text-neutral-500 transition duration-150 ease-out select-none after:absolute after:inset-x-0 after:-inset-y-1 hover:text-neutral-900 aria-checked:text-neutral-950 dark:text-neutral-400 dark:hover:text-neutral-100 dark:aria-checked:text-white focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-neutral-600 dark:focus-visible:outline-neutral-200 forced-colors:aria-checked:text-[HighlightText] forced-colors:aria-checked:forced-color-adjust-none">All time</button>
</div>

Favorite Toggles

Star, like, and save buttons with small rewards on press: the icon pops in, a ring bursts outward, the count rolls, and the label swaps in place. Everything is CSS transitions driven by aria-pressed and is skipped for reduced motion.

<%# The counter shows two numbers and rolls between them: the count without and with the current user's star. %>
<%# Pass the real values from your app: `stars` is the stored total, `starred` whether this user has starred. %>
<% stars = local_assigns.fetch(:stars, 1284) %>
<% starred = local_assigns.fetch(:starred, false) %>
<% unstarred_count = starred ? stars - 1 : stars %>
<div class="flex flex-wrap items-center justify-center gap-3">
  <%# Star with a rolling count %>
  <button type="button" aria-pressed="<%= starred %>" data-controller="toggle-button" data-action="toggle-button#toggle" class="group/toggle inline-flex h-9 gap-2 pr-3 pl-2.5 text-sm font-medium shrink-0 items-center justify-center rounded-lg border border-black/10 bg-white text-neutral-600 shadow-xs hover:bg-neutral-50 hover:text-neutral-900 aria-pressed:border-black/15 aria-pressed:text-neutral-950 dark:border-white/10 dark:bg-neutral-800 dark:text-neutral-400 dark:hover:bg-neutral-700/60 dark:hover:text-neutral-100 dark:aria-pressed:border-white/20 dark:aria-pressed:text-white transition duration-150 ease-out select-none disabled:pointer-events-none disabled:opacity-50 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-neutral-600 dark:focus-visible:outline-neutral-200">
    <span class="relative size-4">
      <svg xmlns="http://www.w3.org/2000/svg" class="size-4 transition-opacity duration-150 group-aria-pressed/toggle:opacity-0" width="18" height="18" viewBox="0 0 18 18" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M9 1.75l2.24 4.54 5.01.73-3.63 3.53.86 4.99L9 13.18l-4.48 2.36.86-4.99-3.63-3.53 5.01-.73L9 1.75Z"></path></svg>
      <svg xmlns="http://www.w3.org/2000/svg" class="absolute inset-0 size-4 scale-25 -rotate-45 text-amber-400 opacity-0 blur-[4px] transition-[scale,rotate,opacity,filter] duration-300 ease-[cubic-bezier(0.34,1.36,0.64,1)] group-aria-pressed/toggle:scale-100 group-aria-pressed/toggle:rotate-0 group-aria-pressed/toggle:opacity-100 group-aria-pressed/toggle:blur-none motion-reduce:transition-none" width="18" height="18" viewBox="0 0 18 18" fill="currentColor" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M9 1.75l2.24 4.54 5.01.73-3.63 3.53.86 4.99L9 13.18l-4.48 2.36.86-4.99-3.63-3.53 5.01-.73L9 1.75Z"></path></svg>
      <span aria-hidden="true" class="pointer-events-none absolute inset-0 scale-0 rounded-full border-2 border-amber-400 opacity-70 group-aria-pressed/toggle:scale-[1.9] group-aria-pressed/toggle:opacity-0 group-aria-pressed/toggle:transition-[scale,opacity] group-aria-pressed/toggle:duration-500 group-aria-pressed/toggle:ease-out motion-reduce:hidden"></span>
    </span>
    Star
    <span aria-hidden="true" class="-my-1 h-5 overflow-hidden border-l border-black/10 pl-2 text-neutral-500 tabular-nums dark:border-white/10 dark:text-neutral-400">
      <span class="block transition-[translate] duration-300 ease-[cubic-bezier(0.23,1,0.32,1)] group-aria-pressed/toggle:-translate-y-1/2 motion-reduce:transition-none">
        <span class="block h-5 leading-5"><%= number_with_delimiter(unstarred_count) %></span>
        <span class="block h-5 leading-5"><%= number_with_delimiter(unstarred_count + 1) %></span>
      </span>
    </span>
    <%# Screen readers get the count too: only the number for the current state is in the accessibility tree. %>
    <span class="sr-only group-aria-pressed/toggle:hidden">, <%= pluralize(number_with_delimiter(unstarred_count), "star") %></span>
    <span class="sr-only hidden group-aria-pressed/toggle:inline">, <%= pluralize(number_with_delimiter(unstarred_count + 1), "star") %></span>
  </button>

  <%# Like %>
  <button type="button" aria-label="Like" aria-pressed="false" data-controller="toggle-button" data-action="toggle-button#toggle" class="group/toggle inline-flex size-9 shrink-0 items-center justify-center rounded-full text-neutral-600 hover:bg-neutral-100 hover:text-neutral-900 aria-pressed:bg-rose-500/10 dark:text-neutral-400 dark:hover:bg-white/10 dark:hover:text-neutral-100 dark:aria-pressed:bg-rose-500/15 transition duration-150 ease-out select-none disabled:pointer-events-none disabled:opacity-50 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-neutral-600 dark:focus-visible:outline-neutral-200 forced-colors:aria-pressed:bg-[Highlight] forced-colors:aria-pressed:text-[HighlightText] forced-colors:aria-pressed:forced-color-adjust-none">
    <span class="relative size-4">
      <svg xmlns="http://www.w3.org/2000/svg" class="size-4 transition-opacity duration-150 group-aria-pressed/toggle:opacity-0" width="18" height="18" viewBox="0 0 18 18" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M9 15.25c-.3 0-6.75-3.6-6.75-8.25A3.75 3.75 0 0 1 9 4.75 3.75 3.75 0 0 1 15.75 7c0 4.65-6.45 8.25-6.75 8.25Z"></path></svg>
      <svg xmlns="http://www.w3.org/2000/svg" class="absolute inset-0 size-4 scale-25 text-rose-500 opacity-0 blur-[4px] transition-[scale,rotate,opacity,filter] duration-300 ease-[cubic-bezier(0.34,1.36,0.64,1)] group-aria-pressed/toggle:scale-100 group-aria-pressed/toggle:rotate-0 group-aria-pressed/toggle:opacity-100 group-aria-pressed/toggle:blur-none motion-reduce:transition-none" width="18" height="18" viewBox="0 0 18 18" fill="currentColor" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M9 15.25c-.3 0-6.75-3.6-6.75-8.25A3.75 3.75 0 0 1 9 4.75 3.75 3.75 0 0 1 15.75 7c0 4.65-6.45 8.25-6.75 8.25Z"></path></svg>
      <span aria-hidden="true" class="pointer-events-none absolute inset-0 scale-0 rounded-full border-2 border-rose-500 opacity-70 group-aria-pressed/toggle:scale-[1.9] group-aria-pressed/toggle:opacity-0 group-aria-pressed/toggle:transition-[scale,opacity] group-aria-pressed/toggle:duration-500 group-aria-pressed/toggle:ease-out motion-reduce:hidden"></span>
    </span>
  </button>

  <%# Save: the label swaps without changing the button width %>
  <button type="button" aria-pressed="false" data-controller="toggle-button" data-action="toggle-button#toggle" class="group/toggle inline-flex h-9 gap-1.5 pr-3 pl-2.5 text-sm font-medium shrink-0 items-center justify-center rounded-lg border border-black/10 bg-white text-neutral-600 shadow-xs hover:bg-neutral-50 hover:text-neutral-900 aria-pressed:border-black/15 aria-pressed:bg-neutral-100 aria-pressed:text-neutral-950 aria-pressed:shadow-none aria-pressed:inset-shadow-xs dark:border-white/10 dark:bg-neutral-800 dark:text-neutral-400 dark:hover:bg-neutral-700/60 dark:hover:text-neutral-100 dark:aria-pressed:border-white/20 dark:aria-pressed:bg-neutral-600 dark:aria-pressed:text-white transition duration-150 ease-out select-none disabled:pointer-events-none disabled:opacity-50 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-neutral-600 dark:focus-visible:outline-neutral-200 forced-colors:aria-pressed:bg-[Highlight] forced-colors:aria-pressed:text-[HighlightText] forced-colors:aria-pressed:forced-color-adjust-none">
    <svg xmlns="http://www.w3.org/2000/svg" class="size-4 transition-[fill] duration-200 ease-out group-aria-pressed/toggle:fill-current" width="18" height="18" viewBox="0 0 18 18" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M4.75 3.75a1 1 0 0 1 1-1h6.5a1 1 0 0 1 1 1v11.5L9 12.25l-4.25 3V3.75Z"></path></svg>
    <span class="grid">
      <span class="col-start-1 row-start-1 transition-[opacity,translate,filter] duration-200 ease-out group-aria-pressed/toggle:-translate-y-1 group-aria-pressed/toggle:opacity-0 group-aria-pressed/toggle:blur-[2px] motion-reduce:transition-none">Save</span>
      <span aria-hidden="true" class="col-start-1 row-start-1 translate-y-1 opacity-0 blur-[2px] transition-[opacity,translate,filter] duration-200 ease-out group-aria-pressed/toggle:translate-y-0 group-aria-pressed/toggle:opacity-100 group-aria-pressed/toggle:blur-none motion-reduce:transition-none">Saved</span>
    </span>
  </button>
</div>

View Switcher

A grid and list switcher built on native radio buttons with no JavaScript. The :has() selector moves the indicator and changes the layout below.

Brand assets

  • Logo.svg 24 KB
  • Guidelines.pdf 1.8 MB
  • Palette.fig 4.2 MB
<%# No JavaScript: native radio buttons drive both the sliding indicator and the layout with :has(). %>
<%# Radios that share a name are one group across the whole page, so give every switcher its own name. %>
<% name = local_assigns.fetch(:name, "files_view") %>
<div class="group/files w-full max-w-sm">
  <div class="mb-3 flex items-center justify-between">
    <p class="text-sm font-medium text-neutral-900 dark:text-neutral-100">Brand assets</p>

    <div role="radiogroup" aria-label="View" class="relative grid grid-cols-2 rounded-lg bg-neutral-100 p-0.5 inset-ring inset-ring-black/5 dark:bg-neutral-800 dark:inset-ring-white/5">
      <span aria-hidden="true" class="pointer-events-none absolute inset-y-0.5 start-0.5 w-10 rounded-md bg-white shadow-sm ring-1 ring-black/5 transition-[translate] duration-250 ease-[cubic-bezier(0.23,1,0.32,1)] group-has-[[value=list]:checked]/files:translate-x-full motion-reduce:transition-none rtl:group-has-[[value=list]:checked]/files:-translate-x-full dark:bg-neutral-600 dark:ring-white/10 forced-colors:bg-[Highlight] forced-colors:forced-color-adjust-none"></span>

      <label class="relative cursor-pointer">
        <input type="radio" name="<%= name %>" value="grid" class="peer sr-only" checked>
        <span class="relative flex h-8 w-10 items-center justify-center rounded-md text-neutral-500 transition duration-150 ease-out hover:text-neutral-900 peer-checked:text-neutral-950 dark:text-neutral-400 dark:hover:text-neutral-100 dark:peer-checked:text-white peer-focus-visible:outline-2 peer-focus-visible:outline-offset-2 peer-focus-visible:outline-neutral-600 dark:peer-focus-visible:outline-neutral-200 forced-colors:peer-checked:text-[HighlightText] forced-colors:peer-checked:forced-color-adjust-none">
          <svg xmlns="http://www.w3.org/2000/svg" class="size-4" width="18" height="18" viewBox="0 0 18 18" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><rect x="2.75" y="2.75" width="5" height="5" rx="1"></rect><rect x="10.25" y="2.75" width="5" height="5" rx="1"></rect><rect x="2.75" y="10.25" width="5" height="5" rx="1"></rect><rect x="10.25" y="10.25" width="5" height="5" rx="1"></rect></svg>
          <span class="sr-only">Grid view</span>
        </span>
      </label>
      <label class="relative cursor-pointer">
        <input type="radio" name="<%= name %>" value="list" class="peer sr-only">
        <span class="relative flex h-8 w-10 items-center justify-center rounded-md text-neutral-500 transition duration-150 ease-out hover:text-neutral-900 peer-checked:text-neutral-950 dark:text-neutral-400 dark:hover:text-neutral-100 dark:peer-checked:text-white peer-focus-visible:outline-2 peer-focus-visible:outline-offset-2 peer-focus-visible:outline-neutral-600 dark:peer-focus-visible:outline-neutral-200 forced-colors:peer-checked:text-[HighlightText] forced-colors:peer-checked:forced-color-adjust-none">
          <svg xmlns="http://www.w3.org/2000/svg" class="size-4" width="18" height="18" viewBox="0 0 18 18" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M2.75 4.75h12.5"></path><path d="M2.75 9h12.5"></path><path d="M2.75 13.25h12.5"></path></svg>
          <span class="sr-only">List view</span>
        </span>
      </label>
    </div>
  </div>

  <ul class="grid grid-cols-3 gap-2 group-has-[[value=list]:checked]/files:grid-cols-1 group-has-[[value=list]:checked]/files:gap-1.5">
    <li class="flex flex-col gap-2 rounded-lg border border-black/10 bg-white p-2.5 group-has-[[value=list]:checked]/files:flex-row group-has-[[value=list]:checked]/files:items-center group-has-[[value=list]:checked]/files:gap-3 group-has-[[value=list]:checked]/files:px-3 group-has-[[value=list]:checked]/files:py-2 dark:border-white/10 dark:bg-neutral-800">
      <span class="flex h-12 items-center justify-center rounded-md bg-sky-500/10 text-sky-600 dark:text-sky-400 group-has-[[value=list]:checked]/files:size-7 group-has-[[value=list]:checked]/files:shrink-0">
        <svg xmlns="http://www.w3.org/2000/svg" class="size-4" width="18" height="18" viewBox="0 0 18 18" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M4.75 2.75h5.5l3 3v8.5a1 1 0 0 1-1 1h-7.5a1 1 0 0 1-1-1V3.75a1 1 0 0 1 1-1Z"></path><path d="M10.25 2.75v3h3"></path></svg>
      </span>
      <span class="min-w-0 truncate text-xs font-medium text-neutral-900 group-has-[[value=list]:checked]/files:grow group-has-[[value=list]:checked]/files:text-sm dark:text-neutral-100">Logo.svg</span>
      <span class="text-xs text-neutral-500 dark:text-neutral-400">24 KB</span>
    </li>
    <li class="flex flex-col gap-2 rounded-lg border border-black/10 bg-white p-2.5 group-has-[[value=list]:checked]/files:flex-row group-has-[[value=list]:checked]/files:items-center group-has-[[value=list]:checked]/files:gap-3 group-has-[[value=list]:checked]/files:px-3 group-has-[[value=list]:checked]/files:py-2 dark:border-white/10 dark:bg-neutral-800">
      <span class="flex h-12 items-center justify-center rounded-md bg-rose-500/10 text-rose-600 dark:text-rose-400 group-has-[[value=list]:checked]/files:size-7 group-has-[[value=list]:checked]/files:shrink-0">
        <svg xmlns="http://www.w3.org/2000/svg" class="size-4" width="18" height="18" viewBox="0 0 18 18" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M4.75 2.75h5.5l3 3v8.5a1 1 0 0 1-1 1h-7.5a1 1 0 0 1-1-1V3.75a1 1 0 0 1 1-1Z"></path><path d="M10.25 2.75v3h3"></path></svg>
      </span>
      <span class="min-w-0 truncate text-xs font-medium text-neutral-900 group-has-[[value=list]:checked]/files:grow group-has-[[value=list]:checked]/files:text-sm dark:text-neutral-100">Guidelines.pdf</span>
      <span class="text-xs text-neutral-500 dark:text-neutral-400">1.8 MB</span>
    </li>
    <li class="flex flex-col gap-2 rounded-lg border border-black/10 bg-white p-2.5 group-has-[[value=list]:checked]/files:flex-row group-has-[[value=list]:checked]/files:items-center group-has-[[value=list]:checked]/files:gap-3 group-has-[[value=list]:checked]/files:px-3 group-has-[[value=list]:checked]/files:py-2 dark:border-white/10 dark:bg-neutral-800">
      <span class="flex h-12 items-center justify-center rounded-md bg-violet-500/10 text-violet-600 dark:text-violet-400 group-has-[[value=list]:checked]/files:size-7 group-has-[[value=list]:checked]/files:shrink-0">
        <svg xmlns="http://www.w3.org/2000/svg" class="size-4" width="18" height="18" viewBox="0 0 18 18" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M4.75 2.75h5.5l3 3v8.5a1 1 0 0 1-1 1h-7.5a1 1 0 0 1-1-1V3.75a1 1 0 0 1 1-1Z"></path><path d="M10.25 2.75v3h3"></path></svg>
      </span>
      <span class="min-w-0 truncate text-xs font-medium text-neutral-900 group-has-[[value=list]:checked]/files:grow group-has-[[value=list]:checked]/files:text-sm dark:text-neutral-100">Palette.fig</span>
      <span class="text-xs text-neutral-500 dark:text-neutral-400">4.2 MB</span>
    </li>
  </ul>
</div>

Filter Chips

Multi-select tags built on native checkboxes with no JavaScript. A check mark slides in when a chip is selected.

Amenities
<%# No JavaScript: each chip is a real checkbox, so it submits with a form and works with a keyboard out of the box. %>
<fieldset class="w-full max-w-sm">
  <legend class="mb-3 text-sm font-medium text-neutral-900 dark:text-neutral-100">Amenities</legend>

  <div class="flex flex-wrap gap-2">
    <label class="group/chip relative cursor-pointer">
      <input type="checkbox" name="amenities[]" value="wifi" class="peer sr-only" checked>
      <span class="relative inline-flex h-8 items-center after:absolute after:inset-x-0 after:-inset-y-1 rounded-full border border-black/10 bg-white px-3 text-sm font-medium text-neutral-700 shadow-xs transition duration-150 ease-out select-none peer-not-checked:hover:bg-neutral-50 peer-checked:border-neutral-900 peer-checked:bg-neutral-900 peer-checked:hover:bg-neutral-800 peer-checked:text-white peer-disabled:pointer-events-none peer-disabled:opacity-50 dark:border-white/10 dark:bg-neutral-800 dark:text-neutral-300 dark:peer-not-checked:hover:bg-neutral-700/60 dark:peer-checked:border-white dark:peer-checked:bg-white dark:peer-checked:hover:bg-neutral-200 dark:peer-checked:text-neutral-900 peer-focus-visible:outline-2 peer-focus-visible:outline-offset-2 peer-focus-visible:outline-neutral-600 dark:peer-focus-visible:outline-neutral-200 forced-colors:peer-checked:bg-[Highlight] forced-colors:peer-checked:text-[HighlightText] forced-colors:peer-checked:forced-color-adjust-none">
        <svg xmlns="http://www.w3.org/2000/svg" class="-ml-0.5 h-3.5 w-0 scale-25 opacity-0 blur-[4px] transition-[width,margin,scale,opacity,filter] duration-200 ease-[cubic-bezier(0.23,1,0.32,1)] group-has-[:checked]/chip:mr-1.5 group-has-[:checked]/chip:w-3.5 group-has-[:checked]/chip:scale-100 group-has-[:checked]/chip:opacity-100 group-has-[:checked]/chip:blur-none motion-reduce:transition-none" width="18" height="18" viewBox="0 0 18 18" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M3.75 9.5l3.25 3.5 7.25-8.25"></path></svg>
        Fast Wi-Fi
      </span>
    </label>
    <label class="group/chip relative cursor-pointer">
      <input type="checkbox" name="amenities[]" value="kitchen" class="peer sr-only">
      <span class="relative inline-flex h-8 items-center after:absolute after:inset-x-0 after:-inset-y-1 rounded-full border border-black/10 bg-white px-3 text-sm font-medium text-neutral-700 shadow-xs transition duration-150 ease-out select-none peer-not-checked:hover:bg-neutral-50 peer-checked:border-neutral-900 peer-checked:bg-neutral-900 peer-checked:hover:bg-neutral-800 peer-checked:text-white peer-disabled:pointer-events-none peer-disabled:opacity-50 dark:border-white/10 dark:bg-neutral-800 dark:text-neutral-300 dark:peer-not-checked:hover:bg-neutral-700/60 dark:peer-checked:border-white dark:peer-checked:bg-white dark:peer-checked:hover:bg-neutral-200 dark:peer-checked:text-neutral-900 peer-focus-visible:outline-2 peer-focus-visible:outline-offset-2 peer-focus-visible:outline-neutral-600 dark:peer-focus-visible:outline-neutral-200 forced-colors:peer-checked:bg-[Highlight] forced-colors:peer-checked:text-[HighlightText] forced-colors:peer-checked:forced-color-adjust-none">
        <svg xmlns="http://www.w3.org/2000/svg" class="-ml-0.5 h-3.5 w-0 scale-25 opacity-0 blur-[4px] transition-[width,margin,scale,opacity,filter] duration-200 ease-[cubic-bezier(0.23,1,0.32,1)] group-has-[:checked]/chip:mr-1.5 group-has-[:checked]/chip:w-3.5 group-has-[:checked]/chip:scale-100 group-has-[:checked]/chip:opacity-100 group-has-[:checked]/chip:blur-none motion-reduce:transition-none" width="18" height="18" viewBox="0 0 18 18" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M3.75 9.5l3.25 3.5 7.25-8.25"></path></svg>
        Kitchen
      </span>
    </label>
    <label class="group/chip relative cursor-pointer">
      <input type="checkbox" name="amenities[]" value="parking" class="peer sr-only" checked>
      <span class="relative inline-flex h-8 items-center after:absolute after:inset-x-0 after:-inset-y-1 rounded-full border border-black/10 bg-white px-3 text-sm font-medium text-neutral-700 shadow-xs transition duration-150 ease-out select-none peer-not-checked:hover:bg-neutral-50 peer-checked:border-neutral-900 peer-checked:bg-neutral-900 peer-checked:hover:bg-neutral-800 peer-checked:text-white peer-disabled:pointer-events-none peer-disabled:opacity-50 dark:border-white/10 dark:bg-neutral-800 dark:text-neutral-300 dark:peer-not-checked:hover:bg-neutral-700/60 dark:peer-checked:border-white dark:peer-checked:bg-white dark:peer-checked:hover:bg-neutral-200 dark:peer-checked:text-neutral-900 peer-focus-visible:outline-2 peer-focus-visible:outline-offset-2 peer-focus-visible:outline-neutral-600 dark:peer-focus-visible:outline-neutral-200 forced-colors:peer-checked:bg-[Highlight] forced-colors:peer-checked:text-[HighlightText] forced-colors:peer-checked:forced-color-adjust-none">
        <svg xmlns="http://www.w3.org/2000/svg" class="-ml-0.5 h-3.5 w-0 scale-25 opacity-0 blur-[4px] transition-[width,margin,scale,opacity,filter] duration-200 ease-[cubic-bezier(0.23,1,0.32,1)] group-has-[:checked]/chip:mr-1.5 group-has-[:checked]/chip:w-3.5 group-has-[:checked]/chip:scale-100 group-has-[:checked]/chip:opacity-100 group-has-[:checked]/chip:blur-none motion-reduce:transition-none" width="18" height="18" viewBox="0 0 18 18" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M3.75 9.5l3.25 3.5 7.25-8.25"></path></svg>
        Free parking
      </span>
    </label>
    <label class="group/chip relative cursor-pointer">
      <input type="checkbox" name="amenities[]" value="pool" class="peer sr-only">
      <span class="relative inline-flex h-8 items-center after:absolute after:inset-x-0 after:-inset-y-1 rounded-full border border-black/10 bg-white px-3 text-sm font-medium text-neutral-700 shadow-xs transition duration-150 ease-out select-none peer-not-checked:hover:bg-neutral-50 peer-checked:border-neutral-900 peer-checked:bg-neutral-900 peer-checked:hover:bg-neutral-800 peer-checked:text-white peer-disabled:pointer-events-none peer-disabled:opacity-50 dark:border-white/10 dark:bg-neutral-800 dark:text-neutral-300 dark:peer-not-checked:hover:bg-neutral-700/60 dark:peer-checked:border-white dark:peer-checked:bg-white dark:peer-checked:hover:bg-neutral-200 dark:peer-checked:text-neutral-900 peer-focus-visible:outline-2 peer-focus-visible:outline-offset-2 peer-focus-visible:outline-neutral-600 dark:peer-focus-visible:outline-neutral-200 forced-colors:peer-checked:bg-[Highlight] forced-colors:peer-checked:text-[HighlightText] forced-colors:peer-checked:forced-color-adjust-none">
        <svg xmlns="http://www.w3.org/2000/svg" class="-ml-0.5 h-3.5 w-0 scale-25 opacity-0 blur-[4px] transition-[width,margin,scale,opacity,filter] duration-200 ease-[cubic-bezier(0.23,1,0.32,1)] group-has-[:checked]/chip:mr-1.5 group-has-[:checked]/chip:w-3.5 group-has-[:checked]/chip:scale-100 group-has-[:checked]/chip:opacity-100 group-has-[:checked]/chip:blur-none motion-reduce:transition-none" width="18" height="18" viewBox="0 0 18 18" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M3.75 9.5l3.25 3.5 7.25-8.25"></path></svg>
        Pool
      </span>
    </label>
    <label class="group/chip relative cursor-pointer">
      <input type="checkbox" name="amenities[]" value="pets" class="peer sr-only">
      <span class="relative inline-flex h-8 items-center after:absolute after:inset-x-0 after:-inset-y-1 rounded-full border border-black/10 bg-white px-3 text-sm font-medium text-neutral-700 shadow-xs transition duration-150 ease-out select-none peer-not-checked:hover:bg-neutral-50 peer-checked:border-neutral-900 peer-checked:bg-neutral-900 peer-checked:hover:bg-neutral-800 peer-checked:text-white peer-disabled:pointer-events-none peer-disabled:opacity-50 dark:border-white/10 dark:bg-neutral-800 dark:text-neutral-300 dark:peer-not-checked:hover:bg-neutral-700/60 dark:peer-checked:border-white dark:peer-checked:bg-white dark:peer-checked:hover:bg-neutral-200 dark:peer-checked:text-neutral-900 peer-focus-visible:outline-2 peer-focus-visible:outline-offset-2 peer-focus-visible:outline-neutral-600 dark:peer-focus-visible:outline-neutral-200 forced-colors:peer-checked:bg-[Highlight] forced-colors:peer-checked:text-[HighlightText] forced-colors:peer-checked:forced-color-adjust-none">
        <svg xmlns="http://www.w3.org/2000/svg" class="-ml-0.5 h-3.5 w-0 scale-25 opacity-0 blur-[4px] transition-[width,margin,scale,opacity,filter] duration-200 ease-[cubic-bezier(0.23,1,0.32,1)] group-has-[:checked]/chip:mr-1.5 group-has-[:checked]/chip:w-3.5 group-has-[:checked]/chip:scale-100 group-has-[:checked]/chip:opacity-100 group-has-[:checked]/chip:blur-none motion-reduce:transition-none" width="18" height="18" viewBox="0 0 18 18" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M3.75 9.5l3.25 3.5 7.25-8.25"></path></svg>
        Pets allowed
      </span>
    </label>
    <label class="group/chip relative cursor-pointer">
      <input type="checkbox" name="amenities[]" value="workspace" class="peer sr-only">
      <span class="relative inline-flex h-8 items-center after:absolute after:inset-x-0 after:-inset-y-1 rounded-full border border-black/10 bg-white px-3 text-sm font-medium text-neutral-700 shadow-xs transition duration-150 ease-out select-none peer-not-checked:hover:bg-neutral-50 peer-checked:border-neutral-900 peer-checked:bg-neutral-900 peer-checked:hover:bg-neutral-800 peer-checked:text-white peer-disabled:pointer-events-none peer-disabled:opacity-50 dark:border-white/10 dark:bg-neutral-800 dark:text-neutral-300 dark:peer-not-checked:hover:bg-neutral-700/60 dark:peer-checked:border-white dark:peer-checked:bg-white dark:peer-checked:hover:bg-neutral-200 dark:peer-checked:text-neutral-900 peer-focus-visible:outline-2 peer-focus-visible:outline-offset-2 peer-focus-visible:outline-neutral-600 dark:peer-focus-visible:outline-neutral-200 forced-colors:peer-checked:bg-[Highlight] forced-colors:peer-checked:text-[HighlightText] forced-colors:peer-checked:forced-color-adjust-none">
        <svg xmlns="http://www.w3.org/2000/svg" class="-ml-0.5 h-3.5 w-0 scale-25 opacity-0 blur-[4px] transition-[width,margin,scale,opacity,filter] duration-200 ease-[cubic-bezier(0.23,1,0.32,1)] group-has-[:checked]/chip:mr-1.5 group-has-[:checked]/chip:w-3.5 group-has-[:checked]/chip:scale-100 group-has-[:checked]/chip:opacity-100 group-has-[:checked]/chip:blur-none motion-reduce:transition-none" width="18" height="18" viewBox="0 0 18 18" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M3.75 9.5l3.25 3.5 7.25-8.25"></path></svg>
        Workspace
      </span>
    </label>
    <label class="group/chip relative cursor-pointer">
      <input type="checkbox" name="amenities[]" value="ev" class="peer sr-only">
      <span class="relative inline-flex h-8 items-center after:absolute after:inset-x-0 after:-inset-y-1 rounded-full border border-black/10 bg-white px-3 text-sm font-medium text-neutral-700 shadow-xs transition duration-150 ease-out select-none peer-not-checked:hover:bg-neutral-50 peer-checked:border-neutral-900 peer-checked:bg-neutral-900 peer-checked:hover:bg-neutral-800 peer-checked:text-white peer-disabled:pointer-events-none peer-disabled:opacity-50 dark:border-white/10 dark:bg-neutral-800 dark:text-neutral-300 dark:peer-not-checked:hover:bg-neutral-700/60 dark:peer-checked:border-white dark:peer-checked:bg-white dark:peer-checked:hover:bg-neutral-200 dark:peer-checked:text-neutral-900 peer-focus-visible:outline-2 peer-focus-visible:outline-offset-2 peer-focus-visible:outline-neutral-600 dark:peer-focus-visible:outline-neutral-200 forced-colors:peer-checked:bg-[Highlight] forced-colors:peer-checked:text-[HighlightText] forced-colors:peer-checked:forced-color-adjust-none">
        <svg xmlns="http://www.w3.org/2000/svg" class="-ml-0.5 h-3.5 w-0 scale-25 opacity-0 blur-[4px] transition-[width,margin,scale,opacity,filter] duration-200 ease-[cubic-bezier(0.23,1,0.32,1)] group-has-[:checked]/chip:mr-1.5 group-has-[:checked]/chip:w-3.5 group-has-[:checked]/chip:scale-100 group-has-[:checked]/chip:opacity-100 group-has-[:checked]/chip:blur-none motion-reduce:transition-none" width="18" height="18" viewBox="0 0 18 18" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M3.75 9.5l3.25 3.5 7.25-8.25"></path></svg>
        EV charger
      </span>
    </label>
  </div>
</fieldset>

Toggles in a Form

Day toggles and a segmented format picker that submit like regular fields because they are regular fields, built with form.collection_check_boxes and form.collection_radio_buttons. Press Save to see the params the server receives.

Send my digest on
Format

Submit to see the params the server receives.

<%# Native checkboxes and radio buttons styled as toggles, built with the Rails form builder: no JavaScript needed. %>
<%# In your app, `digest` is your record (e.g. current_user.digest_setting) and the form posts to its controller. %>
<%# This demo has no model: it submits to the current page inside a Turbo Frame and reads the values back from params. %>
<% digest_params = params.fetch(:digest, {}).then { |value| value.respond_to?(:key?) ? value : {} } %>
<% submitted = digest_params.key?(:days) %>
<% digest = Data.define(:days, :digest_format).new(
     days: submitted ? Array(digest_params[:days]).compact_blank : ["mon", "wed", "fri"],
     digest_format: digest_params[:digest_format].presence_in(["summary", "full"]) || "summary"
   ) %>
<% weekdays = [["mon", "Monday"], ["tue", "Tuesday"], ["wed", "Wednesday"], ["thu", "Thursday"], ["fri", "Friday"], ["sat", "Saturday"], ["sun", "Sunday"]] %>

<turbo-frame id="toggle_form_example" class="block w-full max-w-sm">
  <%= form_with model: digest, scope: :digest, url: request.path, method: :get, data: { turbo_frame: "toggle_form_example" }, class: "rounded-xl border border-black/10 bg-white p-5 shadow-xs dark:border-white/10 dark:bg-neutral-800/50" do |form| %>
    <fieldset>
      <legend class="mb-2 text-sm font-medium text-neutral-900 dark:text-neutral-100">Send my digest on</legend>
      <div class="grid grid-cols-7 gap-1.5">
        <%# collection_check_boxes adds a hidden empty value, so the param is still sent when every day is off %>
        <%= form.collection_check_boxes :days, weekdays, :first, :last do |day| %>
          <label class="relative cursor-pointer">
            <%= day.check_box(class: "peer sr-only") %>
            <span class="flex h-9 items-center justify-center rounded-lg border border-black/10 bg-white text-sm font-medium text-neutral-600 shadow-xs transition duration-150 ease-out select-none peer-not-checked:hover:bg-neutral-50 peer-checked:border-neutral-900 peer-checked:bg-neutral-900 peer-checked:hover:bg-neutral-800 peer-checked:text-white dark:border-white/10 dark:bg-neutral-800 dark:text-neutral-400 dark:peer-not-checked:hover:bg-neutral-700/60 dark:peer-checked:border-white dark:peer-checked:bg-white dark:peer-checked:hover:bg-neutral-200 dark:peer-checked:text-neutral-900 peer-focus-visible:outline-2 peer-focus-visible:outline-offset-2 peer-focus-visible:outline-neutral-600 dark:peer-focus-visible:outline-neutral-200 forced-colors:peer-checked:bg-[Highlight] forced-colors:peer-checked:text-[HighlightText] forced-colors:peer-checked:forced-color-adjust-none">
              <span aria-hidden="true"><%= day.text.first %></span>
              <span class="sr-only"><%= day.text %></span>
            </span>
          </label>
        <% end %>
      </div>
    </fieldset>

    <fieldset class="group/format mt-5">
      <legend class="mb-2 text-sm font-medium text-neutral-900 dark:text-neutral-100">Format</legend>
      <div class="relative grid grid-cols-2 rounded-[10px] bg-neutral-100 p-0.5 inset-ring inset-ring-black/5 dark:bg-neutral-900 dark:inset-ring-white/5">
        <span aria-hidden="true" class="pointer-events-none absolute inset-y-0.5 start-0.5 w-[calc(50%-2px)] rounded-lg bg-white shadow-sm ring-1 ring-black/5 transition-[translate] duration-250 ease-[cubic-bezier(0.23,1,0.32,1)] group-has-[[value=full]:checked]/format:translate-x-full motion-reduce:transition-none rtl:group-has-[[value=full]:checked]/format:-translate-x-full dark:bg-neutral-600 dark:ring-white/10 forced-colors:bg-[Highlight] forced-colors:forced-color-adjust-none"></span>
        <%= form.collection_radio_buttons :digest_format, [["summary", "Summary"], ["full", "Full report"]], :first, :last do |option| %>
          <label class="relative cursor-pointer">
            <%= option.radio_button(class: "peer sr-only") %>
            <span class="relative flex h-8 items-center justify-center rounded-lg text-sm font-medium text-neutral-500 transition duration-150 ease-out select-none hover:text-neutral-900 peer-checked:text-neutral-950 dark:text-neutral-400 dark:hover:text-neutral-100 dark:peer-checked:text-white peer-focus-visible:outline-2 peer-focus-visible:outline-offset-2 peer-focus-visible:outline-neutral-600 dark:peer-focus-visible:outline-neutral-200 forced-colors:peer-checked:text-[HighlightText] forced-colors:peer-checked:forced-color-adjust-none"><%= option.text %></span>
          </label>
        <% end %>
      </div>
    </fieldset>

    <div class="mt-5 flex items-center justify-between gap-3">
      <p class="min-w-0 text-xs text-neutral-500 dark:text-neutral-400" role="status">
        <% if submitted %>
          Saved: <span class="font-mono text-neutral-900 dark:text-neutral-100"><%= digest.days.any? ? digest.days.join(", ") : "no days" %> · <%= digest.digest_format %></span>
        <% else %>
          Submit to see the params the server receives.
        <% end %>
      </p>
      <%= form.button "Save", type: "submit", class: "flex shrink-0 items-center justify-center gap-1.5 rounded-lg border border-neutral-400/30 bg-neutral-800 px-3.5 py-2 text-sm font-medium whitespace-nowrap text-white shadow-sm transition-all duration-100 ease-in-out select-none hover:bg-neutral-700 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-neutral-600 disabled:cursor-not-allowed disabled:opacity-50 dark:bg-white dark:text-neutral-800 dark:hover:bg-neutral-100 dark:focus-visible:outline-neutral-200" %>
    </div>
  <% end %>
</turbo-frame>

ViewComponent

Prefer Ruby? Toggle::Component renders one toggle and Toggle::GroupComponent renders toolbars, outline rows and segmented controls with items as slots. They write the classes, aria-pressed (or the radio group roles when one item is always selected), labels and Stimulus wiring for you, size icons to match, and raise a clear error for a missing group label, a missing item value or an unknown option. The shared partials (shared/components/toggle/toggle and toggle_group) take the same options as locals and render the same markup, for apps without ViewComponent. Both are behind the ViewComponent and ERB Template buttons at the top of the page.

<%# Toggle::Component and Toggle::GroupComponent write the classes, ARIA and Stimulus wiring for you. %>
<div class="flex flex-col items-center gap-5">
  <div class="flex flex-wrap items-center justify-center gap-3">
    <%= render Toggle::GroupComponent.new(label: "Text formatting", multiple: true) do |group| %>
      <% group.with_item(value: "bold", label: "Bold", pressed: true) do %>
        <svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 18 18" fill="none" stroke="currentColor" stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M5.25 2.75h4.5a3 3 0 0 1 0 6h-4.5v-6Z"></path><path d="M5.25 8.75h5.5a3.25 3.25 0 0 1 0 6.5h-5.5v-6.5Z"></path></svg>
      <% end %>
      <% group.with_item(value: "italic", label: "Italic") do %>
        <svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 18 18" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M7.75 2.75h5.5"></path><path d="M4.75 15.25h5.5"></path><path d="M10.5 2.75l-3 12.5"></path></svg>
      <% end %>
      <% group.with_item(value: "underline", label: "Underline") do %>
        <svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 18 18" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M5.25 2.75v5a3.75 3.75 0 0 0 7.5 0v-5"></path><path d="M3.75 15.25h10.5"></path></svg>
      <% end %>
    <% end %>

    <%= render Toggle::Component.new(variant: :outline, pressed: true) do %>
      <svg xmlns="http://www.w3.org/2000/svg" class="size-4 transition-[fill] duration-150 ease-out group-aria-pressed/toggle:fill-neutral-950/15 dark:group-aria-pressed/toggle:fill-white/25" width="18" height="18" viewBox="0 0 18 18" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M9 2.25a4.25 4.25 0 0 0-4.25 4.25c0 3.75-1.5 5.25-1.5 5.25h11.5s-1.5-1.5-1.5-5.25A4.25 4.25 0 0 0 9 2.25Z"></path><path d="M7.5 14.5a1.6 1.6 0 0 0 3 0"></path></svg>
      Notify me
    <% end %>
  </div>

  <%= render Toggle::GroupComponent.new(label: "Calendar view", variant: :segmented) do |group| %>
    <% group.with_item(value: "day").with_content("Day") %>
    <% group.with_item(value: "week", pressed: true).with_content("Week") %>
    <% group.with_item(value: "month").with_content("Month") %>
    <% group.with_item(value: "year").with_content("Year") %>
  <% end %>
</div>

Configuration

Put data-controller="toggle-button" on a single button for a standalone toggle, or on a wrapper with item targets for a group.

Values

Value Type Default Description
multiple Boolean false Groups only. Lets several items be pressed at once. Without it, pressing an item releases the others.
required Boolean false Groups only. Keeps at least one item pressed, so clicking the pressed item again does nothing.

Targets

Target Description
item A button in a group. Give each one a value attribute and aria-pressed (in a radio group, role="radio" and aria-checked).
indicator Optional. An absolutely positioned element that slides under the pressed item of a single-select group. Its parent must be relative.
input Optional hidden input kept in sync for form submission: 1 or 0 for a standalone toggle, comma-separated values for a group.

Actions and Events

Name Type Description
toggle-button#toggle Action Presses or releases the clicked button. Add it to the standalone button or to every item.
toggle-button#press / #release Action / Method Presses or releases the item with that value, following the group rules. As an action, name the item with data-toggle-button-value-param; it counts as a user change. Called from code (press("week")), it does not dispatch a change event.
toggle-button:change Event Dispatched after every press with event.detail.item, pressed, value, and values (all pressed values in the group). It bubbles, so you can listen on any ancestor.

Keyboard

Key Action
Tab Moves into a group, to the pressed item or the first one, and out of it again. A group is a single tab stop.
Arrow keys Move focus between the enabled items of a group and wrap around. Left and right flip in right-to-left layouts. In a radio group they also select. Browser shortcuts with Alt, Ctrl or Cmd keep working.
Home / End Move focus to the first or last enabled item.
Space / Enter Press or release the focused toggle.

Saving Changes

The examples toggle in the browser only. To save a star, a like or a preference, listen for toggle-button:change and send the new state to your server. Alternatively, add a hidden input target inside a form and call requestSubmit() on change (for example with data-action="change->autosubmit#submit" and the autosubmit controller on the form): the input fires a bubbling change event on every press.

// app/javascript/controllers/star_controller.js
// <button type="button" aria-pressed="false" data-controller="toggle-button star"
//         data-action="toggle-button#toggle toggle-button:change->star#save" data-star-url-value="/repositories/42/star">
import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
  static values = { url: String }

  async save({ detail: { item, pressed } }) {
    const response = await fetch(this.urlValue, {
      method: pressed ? "POST" : "DELETE",
      headers: { "X-CSRF-Token": document.querySelector("meta[name=csrf-token]")?.content }
    })
    // Roll back if the server refused. Setting aria-pressed directly does not fire another change event.
    if (!response.ok) item.setAttribute("aria-pressed", String(!pressed))
  }
}

Setting State From Outside

From another controller, call press(value) or release(value). They follow the group rules: a single-select group releases the other items and a required group keeps its last one. You can also set aria-pressed (aria-checked on radios) from a Turbo Stream, a morph or a script, and the same rules apply. Either way the controller moves the indicator, updates the hidden input and keeps the group to a single tab stop, without dispatching toggle-button:change, because the user did not change anything. Items added or removed later join the group automatically.

// In your controller, with the group as a target: <div data-report-target="range" data-controller="toggle-button" …>
const toggle = this.application.getControllerForElementAndIdentifier(this.rangeTarget, "toggle-button")
toggle.press("month")  // a group item, by value
toggle.release("bold")
toggle.press()         // a standalone toggle

Styling Hooks

  • aria-pressed: on the button styles its pressed look (aria-checked: on the items of a radio group). disabled: styles disabled toggles, which keep showing their state.
  • group-aria-pressed/toggle: styles content inside a toggle, which carries group/toggle in the ViewComponent, the partials and the examples. Inside radio group items, use group-aria-checked/toggle:.
  • group-has-[[value=center][aria-pressed=true]]/align: lets nearby elements follow a group with CSS only. Always use a named group: an unnamed group also matches any outer element with the group class.

Accessibility

  • Give icon-only toggles an aria-label and every group an aria-label. Toolbars use role="toolbar", which tells screen reader users that arrow keys move between items. Groups with one item always selected (alignment, segmented controls) use role="radiogroup" with role="radio" items and aria-checked: a pressed button that can't be released would be misleading.
  • In Windows High Contrast (forced colors) mode, background colors are replaced by the system palette. The forced-colors:aria-pressed: classes switch pressed items to the system highlight colors so the state stays visible. Keep them when you restyle a toggle.
  • The controller logs a [toggle-button] console warning for setup mistakes such as a missing label, an item without a value, or an indicator in a group that is not positioned.
  • Keep the label the same in both states. "Save" with aria-pressed="true" is read as "Save, toggle button, pressed". That is why the Favorite example hides its swapped "Saved" label from screen readers.
  • Use a Switch for settings that apply immediately, a checkbox for choices submitted with a form, and Tabs when the control shows and hides panels of content.
  • The CSS-only examples use :has() (Chrome 105+, Safari 15.4+, Firefox 121+). In older browsers their indicator and check marks stay put, but the inputs still work and submit.

Table of contents

Powered by

Get notified when new components come out