Switch

An on/off toggle built on a native checkbox.

Install

If this is the first component in the project, set up once. This writes ermine.json and copies the theme stylesheet. The installation guide covers Vue, Nuxt and Astro.

npx ermineui init
pnpm dlx ermineui init
yarn dlx ermineui init
bunx ermineui init

Then add the component:

npx ermineui add switch
pnpm dlx ermineui add switch
yarn dlx ermineui add switch
bunx ermineui add switch

That copies 2 files into src/components/ui/switch/. Files you already have are kept.

Use

import { Switch } from '@/components/ui/switch'
<script setup lang="ts">
import { Switch } from '@/components/ui/switch'
</script>

<template>
  <Switch />
</template>

In Nuxt, with the auto-import setting, the tags work in templates without the import. In Astro, use it inside a Vue island. The props are documented in the source below, on each one.

What gets copied

FileWhere it goes
Switch.vuesrc/components/ui/switch/Switch.vue
index.tssrc/components/ui/switch/index.ts

The folders are the suggested ones. ermine.json says where yours are.

Source

This is exactly what the command copies. To install by hand, create each file below and the helpers listed above.

Switch.vue 59 lines
<script setup lang="ts">
const model = defineModel<boolean>()
</script>

<template>
  <input v-model="model" type="checkbox" role="switch" class="er-switch" />
</template>

<style>
@layer components {
  /* A native checkbox announced as a switch, so it submits with forms and needs no JavaScript. */
  .er-switch {
    appearance: none;
    position: relative;
    flex-shrink: 0;
    width: 2rem;
    height: 1.15rem;
    margin: 0;
    border: 1px solid transparent;
    border-radius: 999px;
    background: var(--er-input);
    box-shadow: var(--er-shadow-xs);
    cursor: pointer;
    transition: background-color var(--er-duration);
  }

  .er-switch::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    height: 100%;
    aspect-ratio: 1;
    border-radius: 50%;
    background: var(--er-background);
    box-shadow: var(--er-shadow-sm);
    transition: translate var(--er-duration) var(--er-ease);
  }

  .er-switch:checked {
    background: var(--er-primary);
  }

  .er-switch:checked::before {
    translate: calc(2rem - 2px - 100%) 0;
  }

  .er-switch:focus-visible {
    outline: 2px solid var(--er-ring);
    outline-offset: 2px;
  }

  .er-switch:disabled {
    opacity: 0.5;
    cursor: not-allowed;
  }
}
</style>
index.ts 3 lines
/** Switch — an on/off toggle built on a native checkbox. */
export { default as Switch } from './Switch.vue'