Switch Shared Partial

1. Copy shared partial files

Download all 1 shared partial files as a ZIP file.

Download all files (ZIP)

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

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

2. Usage example

<!-- Basic switch without label -->
<%= render "shared/components/switch/switch" %>

<!-- Switch with label -->
<%= render "shared/components/switch/switch",
  label: "Enable notifications" %>

<!-- Switch with name for form submission -->
<%= render "shared/components/switch/switch",
  label: "Dark mode",
  name: "user[dark_mode]",
  checked: true %>

<!-- Switch with custom id -->
<%= render "shared/components/switch/switch",
  label: "Auto-save",
  name: "settings[auto_save]",
  id: "auto-save-switch" %>

<!-- Disabled switch -->
<%= render "shared/components/switch/switch",
  label: "Premium feature",
  disabled: true %>

<!-- Disabled and checked -->
<%= render "shared/components/switch/switch",
  label: "Always enabled",
  checked: true,
  disabled: true %>

<!-- Required switch -->
<%= render "shared/components/switch/switch",
  label: "Accept terms of service",
  name: "accept_terms",
  required: true %>

<!-- With description -->
<%= render "shared/components/switch/switch",
  label: "Email notifications",
  name: "settings[email]",
  description: "Receive updates about your account via email" %>

<!-- With description (checked) -->
<%= render "shared/components/switch/switch",
  label: "Push notifications",
  name: "settings[push]",
  description: "Get real-time alerts in your browser",
  checked: true %>

<!-- With error message -->
<%= render "shared/components/switch/switch",
  label: "Enable two-factor authentication",
  name: "security[2fa]",
  error: "Two-factor authentication is required for admin accounts" %>

<!-- Small size -->
<%= render "shared/components/switch/switch",
  label: "Compact toggle",
  size: "sm" %>

<!-- Large size -->
<%= render "shared/components/switch/switch",
  label: "Prominent toggle",
  size: "lg" %>

<!-- Small with description -->
<%= render "shared/components/switch/switch",
  label: "Compact option",
  description: "A smaller switch with description",
  size: "sm" %>

<!-- Large with description -->
<%= render "shared/components/switch/switch",
  label: "Prominent option",
  description: "A larger switch for emphasis",
  size: "lg" %>

<!-- With icons inside -->
<%= render "shared/components/switch/switch",
  label: "Show icons",
  show_icons: true %>

<!-- With icons (checked) -->
<%= render "shared/components/switch/switch",
  label: "Icons enabled",
  show_icons: true,
  checked: true %>

<!-- Label on left -->
<%= render "shared/components/switch/switch",
  label: "Label on left side",
  label_position: "left" %>

<!-- With custom wrapper classes -->
<%= render "shared/components/switch/switch",
  label: "Custom styled",
  classes: "p-4 bg-neutral-50 dark:bg-neutral-800 rounded-lg" %>

<!-- With custom switch track classes -->
<%= render "shared/components/switch/switch",
  label: "Blue switch",
  checked: true,
  switch_classes: "peer-checked:bg-blue-600 peer-checked:group-hover:bg-blue-500 dark:peer-checked:bg-blue-500" %>

<!-- All options combined -->
<%= render "shared/components/switch/switch",
  label: "Full featured switch",
  name: "full_featured",
  id: "full-featured-switch",
  value: "yes",
  description: "This switch demonstrates all available options",
  size: "md",
  show_icons: true,
  required: true %>

Rendered usage example

Switch ViewComponent

1. Ensure the gem is installed

gem "view_component", "~> 4.2"

2. Copy component files

Download all 2 component files as a ZIP file.

Download all files (ZIP)

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

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

3. Usage example

<!-- Basic switch without label -->
<%= render Switch::Component.new %>

<!-- Switch with label -->
<%= render Switch::Component.new(
  label: "Enable notifications"
) %>

<!-- Switch with name for form submission -->
<%= render Switch::Component.new(
  label: "Dark mode",
  name: "user[dark_mode]",
  checked: true
) %>

<!-- Switch with custom id -->
<%= render Switch::Component.new(
  label: "Auto-save",
  name: "settings[auto_save]",
  id: "auto-save-switch"
) %>

<!-- Disabled switch -->
<%= render Switch::Component.new(
  label: "Premium feature",
  disabled: true
) %>

<!-- Disabled and checked -->
<%= render Switch::Component.new(
  label: "Always enabled",
  checked: true,
  disabled: true
) %>

<!-- Required switch -->
<%= render Switch::Component.new(
  label: "Accept terms of service",
  name: "accept_terms",
  required: true
) %>

<!-- With description -->
<%= render Switch::Component.new(
  label: "Email notifications",
  name: "settings[email]",
  description: "Receive updates about your account via email"
) %>

<!-- With description (checked) -->
<%= render Switch::Component.new(
  label: "Push notifications",
  name: "settings[push]",
  description: "Get real-time alerts in your browser",
  checked: true
) %>

<!-- With error message -->
<%= render Switch::Component.new(
  label: "Enable two-factor authentication",
  name: "security[2fa]",
  error: "Two-factor authentication is required for admin accounts"
) %>

<!-- Small size -->
<%= render Switch::Component.new(
  label: "Compact toggle",
  size: :sm
) %>

<!-- Large size -->
<%= render Switch::Component.new(
  label: "Prominent toggle",
  size: :lg
) %>

<!-- Small with description -->
<%= render Switch::Component.new(
  label: "Compact option",
  description: "A smaller switch with description",
  size: :sm
) %>

<!-- Large with description -->
<%= render Switch::Component.new(
  label: "Prominent option",
  description: "A larger switch for emphasis",
  size: :lg
) %>

<!-- With icons inside -->
<%= render Switch::Component.new(
  label: "Show icons",
  show_icons: true
) %>

<!-- With icons (checked) -->
<%= render Switch::Component.new(
  label: "Icons enabled",
  show_icons: true,
  checked: true
) %>

<!-- Label on left -->
<%= render Switch::Component.new(
  label: "Label on left side",
  label_position: :left
) %>

<!-- With custom wrapper classes -->
<%= render Switch::Component.new(
  label: "Custom styled",
  classes: "p-4 bg-neutral-50 dark:bg-neutral-800 rounded-lg"
) %>

<!-- With custom switch track classes -->
<%= render Switch::Component.new(
  label: "Blue switch",
  checked: true,
  switch_classes: "peer-checked:bg-blue-600 peer-checked:group-hover:bg-blue-500 dark:peer-checked:bg-blue-500"
) %>

<!-- All options combined -->
<%= render Switch::Component.new(
  label: "Full featured switch",
  name: "full_featured",
  id: "full-featured-switch",
  value: "yes",
  description: "This switch demonstrates all available options",
  size: :md,
  show_icons: true,
  required: true
) %>

Rendered usage example

Switch Rails Components

Switch component examples for your Ruby on Rails application. A switch is a toggle button that allows the user to turn an option on or off.

Examples

Basic Switch

A simple switch component.

<label class="group flex items-center cursor-pointer">
  <div class="relative">
    <input type="checkbox" class="sr-only peer" name="tailwind_switch" id="tailwind_switch">
    <!-- Background element -->
    <div class="w-10 h-6 bg-neutral-200 border border-black/10 rounded-full transition-all duration-150 ease-in-out cursor-pointer group-hover:bg-[#dcdcdb] peer-checked:bg-[#404040] peer-checked:group-hover:bg-neutral-600 peer-checked:border-white/10 dark:bg-neutral-700 dark:border-white/10 dark:group-hover:bg-neutral-600 dark:peer-checked:bg-neutral-50 dark:peer-checked:group-hover:bg-neutral-100 dark:peer-checked:border-black/20 peer-focus-visible:outline-2 peer-focus-visible:outline-offset-2 peer-focus-visible:outline-neutral-600 dark:peer-focus-visible:outline-neutral-200"></div>
    <!-- Round element with icons inside -->
    <div class="absolute top-[3px] left-[3px] w-[18px] h-[18px] bg-white rounded-full shadow-sm transition-all duration-150 ease-in-out peer-checked:translate-x-4 flex items-center justify-center dark:bg-neutral-200 dark:peer-checked:bg-neutral-800"></div>
  </div>
</label>

Basic Switch with Active State

A simple switch component with active state, you will see the switch grow in size when you click on it, and shrink when you release your click.

<label class="group flex items-center cursor-pointer">
  <div class="relative">
    <input type="checkbox" class="sr-only peer" name="tailwind_switch" id="tailwind_switch">
    <!-- Background element -->
    <div class="w-10 h-6 bg-neutral-200 border border-black/10 rounded-full transition-all duration-150 ease-in-out cursor-pointer group-hover:bg-[#dcdcdb] peer-checked:bg-[#404040] peer-checked:group-hover:bg-neutral-600 peer-checked:border-white/10 dark:bg-neutral-700 dark:border-white/10 dark:group-hover:bg-neutral-600 dark:peer-checked:bg-neutral-50 dark:peer-checked:group-hover:bg-neutral-100 dark:peer-checked:border-black/20 peer-focus-visible:outline-2 peer-focus-visible:outline-offset-2 peer-focus-visible:outline-neutral-600 dark:peer-focus-visible:outline-neutral-200"></div>
    <!-- Round element with icons inside -->
    <div class="absolute top-[3px] left-[3px] w-[18px] h-[18px] bg-white rounded-full shadow-sm transition-all duration-150 ease-in-out group-active:w-[22px] peer-checked:group-active:-ml-1 peer-checked:translate-x-4 flex items-center justify-center dark:bg-neutral-200 dark:peer-checked:bg-neutral-800"></div>
  </div>
</label>

Switch with Icon

Switch with an On/Off icon.

<label class="group flex items-center cursor-pointer">
  <div class="relative">
    <input type="checkbox" class="sr-only peer" name="tailwind_switch" id="tailwind_switch">
    <!-- Background element -->
    <div class="w-10 h-6 bg-neutral-200 border border-black/10 rounded-full transition-all duration-150 ease-in-out cursor-pointer group-hover:bg-[#dcdcdb] peer-checked:bg-[#404040] peer-checked:group-hover:bg-neutral-600 peer-checked:border-white/10 dark:bg-neutral-700 dark:border-white/10 dark:group-hover:bg-neutral-600 dark:peer-checked:bg-neutral-50 dark:peer-checked:group-hover:bg-neutral-100 dark:peer-checked:border-black/20 peer-focus-visible:outline-2 peer-focus-visible:outline-offset-2 peer-focus-visible:outline-neutral-600 dark:peer-focus-visible:outline-neutral-200"></div>
    <!-- Round element with icons inside -->
    <div class="absolute top-[3px] left-[3px] w-[18px] h-[18px] bg-white rounded-full shadow-sm transition-all duration-150 ease-in-out peer-checked:translate-x-4 flex items-center justify-center dark:bg-neutral-200 dark:peer-checked:bg-neutral-800">
      <!-- X icon for unchecked state -->
      <svg xmlns="http://www.w3.org/2000/svg" class="size-4 text-neutral-400 transition-all duration-150 ease-in-out group-has-[:checked]:opacity-0 dark:text-neutral-500" width="20" height="20" viewBox="0 0 20 20"><g fill="currentColor"><line x1="7" y1="7" x2="13" y2="13" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2"></line><line x1="7" y1="13" x2="13" y2="7" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2"></line></g></svg>
      <!-- Checkmark icon for checked state -->
      <svg xmlns="http://www.w3.org/2000/svg" class="absolute size-4 text-neutral-700 transition-all duration-150 ease-in-out opacity-0 group-has-[:checked]:opacity-100 dark:text-neutral-100" width="20" height="20" viewBox="0 0 20 20"><g fill="currentColor"><polyline points="6.5 10.5 8.75 13 13.5 7" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2"></polyline></g></svg>
    </div>
  </div>
</label>

Table of contents

Powered by

Get notified when new components come out