useColorMode
Category
Export Size
2.69 kB
Last Changed
3 hours ago
Related
使用自动数据持久化的响应式颜色模式 (dark / light / customs)。
Demo
← 点击更改颜色模式
基本用法
js
import { useColorMode } from '@vueuse/core'
const mode = useColorMode() // Ref<'dark' | 'light'>
默认情况下,它将使用 usePreferredDark
(也称为 auto
模式)匹配用户浏览器的偏好。在读取 ref 时,默认将返回当前的颜色模式(dark
、light
或您的自定义模式)。可以通过启用 emitAuto
选项将 auto
模式包含在返回的模式中。在写入 ref 时,它将触发 DOM 更新并将颜色模式持久化到本地存储(或您的自定义存储)。您可以传递 auto
来设置回自动模式。
ts
mode.value // 'dark' | 'light'
mode.value = 'dark' // 切换到暗模式并持久化
mode.value = 'auto' // 切换到自动模式
配置
js
import { useColorMode } from '@vueuse/core'
const mode = useColorMode({
attribute: 'theme',
modes: {
// 自定义颜色
dim: 'dim',
cafe: 'cafe',
},
}) // Ref<'dark' | 'light' | 'dim' | 'cafe'>
高级用法
您还可以明确访问系统偏好和存储的用户覆盖模式。
js
import { useColorMode } from '@vueuse/core'
const { system, store } = useColorMode()
system.value // 'dark' | 'light'
store.value // 'dark' | 'light' | 'auto'
const myColorMode = computed(() => store.value === 'auto' ? system.value : store.value)
组件用法
vue
<template>
<UseColorMode v-slot="{ mode }">
<button @click="mode = mode === 'dark' ? 'light' : 'dark'">
Mode {{ mode }}
</button>
</UseColorMode>
</template>
Type Declarations
Show Type Declarations
typescript
export type BasicColorMode = "light" | "dark"
export type BasicColorSchema = BasicColorMode | "auto"
export interface UseColorModeOptions<T extends string = BasicColorMode>
extends UseStorageOptions<T | BasicColorMode> {
/**
* CSS Selector for the target element applying to
*
* @default 'html'
*/
selector?: string | MaybeElementRef
/**
* HTML attribute applying the target element
*
* @default 'class'
*/
attribute?: string
/**
* The initial color mode
*
* @default 'auto'
*/
initialValue?: MaybeRefOrGetter<T | BasicColorSchema>
/**
* Prefix when adding value to the attribute
*/
modes?: Partial<Record<T | BasicColorSchema, string>>
/**
* A custom handler for handle the updates.
* When specified, the default behavior will be overridden.
*
* @default undefined
*/
onChanged?: (
mode: T | BasicColorMode,
defaultHandler: (mode: T | BasicColorMode) => void,
) => void
/**
* Custom storage ref
*
* When provided, `useStorage` will be skipped
*/
storageRef?: Ref<T | BasicColorSchema>
/**
* Key to persist the data into localStorage/sessionStorage.
*
* Pass `null` to disable persistence
*
* @default 'vueuse-color-scheme'
*/
storageKey?: string | null
/**
* Storage object, can be localStorage or sessionStorage
*
* @default localStorage
*/
storage?: StorageLike
/**
* Emit `auto` mode from state
*
* When set to `true`, preferred mode won't be translated into `light` or `dark`.
* This is useful when the fact that `auto` mode was selected needs to be known.
*
* @default undefined
* @deprecated use `store.value` when `auto` mode needs to be known
* @see https://vueuse.org/core/useColorMode/#advanced-usage
*/
emitAuto?: boolean
/**
* Disable transition on switch
*
* @see https://paco.me/writing/disable-theme-transitions
* @default true
*/
disableTransition?: boolean
}
export type UseColorModeReturn<T extends string = BasicColorMode> = Ref<
T | BasicColorSchema
> & {
store: Ref<T | BasicColorSchema>
system: ComputedRef<BasicColorMode>
state: ComputedRef<T | BasicColorMode>
}
/**
* Reactive color mode with auto data persistence.
*
* @see https://vueuse.org/useColorMode
* @param options
*/
export declare function useColorMode<T extends string = BasicColorMode>(
options?: UseColorModeOptions<T>,
): UseColorModeReturn<T>