Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply VueUse composables in Vue 3/Nuxt projects to replace custom implementations with battle-tested utilities.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/useColorMode.md
1---2category: Browser3related:4- useDark5- usePreferredDark6- useStorage7---89# useColorMode1011Reactive color mode (dark / light / customs) with auto data persistence.1213## Basic Usage1415```ts16import { useColorMode } from '@vueuse/core'1718const mode = useColorMode() // Ref<'dark' | 'light'>19```2021By default, it will match with users' browser preference using `usePreferredDark` (a.k.a `auto` mode). When reading the ref, it will by default return the current color mode (`dark`, `light` or your custom modes). The `auto` mode can be included in the returned modes by enabling the `emitAuto` option. When writing to the ref, it will trigger DOM updates and persist the color mode to local storage (or your custom storage). You can pass `auto` to set back to auto mode.2223```ts24import { useColorMode } from '@vueuse/core'2526const mode = useColorMode()27// ---cut---28mode.value // 'dark' | 'light'2930mode.value = 'dark' // change to dark mode and persist3132mode.value = 'auto' // change to auto mode33```3435## Config3637```ts38import { useColorMode } from '@vueuse/core'3940const mode = useColorMode({41attribute: 'theme',42modes: {43// custom colors44dim: 'dim',45cafe: 'cafe',46},47}) // Ref<'dark' | 'light' | 'dim' | 'cafe'>48```4950## Advanced Usage5152You can also explicit access to the system preference and storaged user override mode.5354```ts55import { useColorMode } from '@vueuse/core'5657const { system, store } = useColorMode()5859system.value // 'dark' | 'light'60store.value // 'dark' | 'light' | 'auto'6162const myColorMode = computed(() => store.value === 'auto' ? system.value : store.value)63```6465## Component Usage6667```vue68<template>69<UseColorMode v-slot="color">70<button @click="color.mode = color.mode === 'dark' ? 'light' : 'dark'">71Mode {{ color.mode }}72</button>73</UseColorMode>74</template>75```7677## Type Declarations7879```ts80export type BasicColorMode = "light" | "dark"81export type BasicColorSchema = BasicColorMode | "auto"82export interface UseColorModeOptions<83T extends string = BasicColorMode,84> extends UseStorageOptions<T | BasicColorMode> {85/**86* CSS Selector for the target element applying to87*88* @default 'html'89*/90selector?: string | MaybeElementRef91/**92* HTML attribute applying the target element93*94* @default 'class'95*/96attribute?: string97/**98* The initial color mode99*100* @default 'auto'101*/102initialValue?: MaybeRefOrGetter<T | BasicColorSchema>103/**104* Prefix when adding value to the attribute105*/106modes?: Partial<Record<T | BasicColorSchema, string>>107/**108* A custom handler for handle the updates.109* When specified, the default behavior will be overridden.110*111* @default undefined112*/113onChanged?: (114mode: T | BasicColorMode,115defaultHandler: (mode: T | BasicColorMode) => void,116) => void117/**118* Custom storage ref119*120* When provided, `useStorage` will be skipped121*/122storageRef?: Ref<T | BasicColorSchema>123/**124* Key to persist the data into localStorage/sessionStorage.125*126* Pass `null` to disable persistence127*128* @default 'vueuse-color-scheme'129*/130storageKey?: string | null131/**132* Storage object, can be localStorage or sessionStorage133*134* @default localStorage135*/136storage?: StorageLike137/**138* Emit `auto` mode from state139*140* When set to `true`, preferred mode won't be translated into `light` or `dark`.141* This is useful when the fact that `auto` mode was selected needs to be known.142*143* @default undefined144* @deprecated use `store.value` when `auto` mode needs to be known145* @see https://vueuse.org/core/useColorMode/#advanced-usage146*/147emitAuto?: boolean148/**149* Disable transition on switch150*151* @see https://paco.me/writing/disable-theme-transitions152* @default true153*/154disableTransition?: boolean155}156export type UseColorModeReturn<T extends string = BasicColorMode> = Ref<157T | BasicColorSchema158> & {159store: Ref<T | BasicColorSchema>160system: ComputedRef<BasicColorMode>161state: ComputedRef<T | BasicColorMode>162}163/**164* Reactive color mode with auto data persistence.165*166* @see https://vueuse.org/useColorMode167* @param options168*/169export declare function useColorMode<T extends string = BasicColorMode>(170options?: UseColorModeOptions<T>,171): UseColorModeReturn<T>172```173