Skip to content

ThemeToggle

An animated sun/moon toggle switch for controlling light/dark mode. Uses CSS transitions for smooth thumb movement and icon state changes.

Basic Usage

Demo

Current theme: ☀️ Light

Props

Props
PropTypeDefaultDescription
modelValuebooleanfalseCurrent state of the toggle. true = dark mode, false = light mode. Use with v-model.

Emits

Emits
EventPayloadDescription
update:modelValuebooleanEmitted when the toggle is clicked. Value is the new boolean state.
togglebooleanAlso emitted on toggle. Useful for triggering global theme logic without v-model.

Usage with Dark Mode

Pair ThemeToggle with a global dark mode state manager. A common pattern:

ts
// composables/useTheme.ts
import { ref, watch } from 'vue'

const isDark = ref(false)

export function useTheme() {
  const toggle = (val: boolean) => {
    isDark.value = val
    document.documentElement.classList.toggle('dark', val)
  }
  return { isDark, toggle }
}
vue
<template>
  <ThemeToggle v-model="isDark" @toggle="toggle" />
</template>

Built with VitePress