useColorMode
自動データ永続化を備えたリアクティブカラーモード(ダーク/ライト/カスタム)。
デモ
← クリックしてカラーモードを変更
基本使用法
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' // change to dark mode and persist
mode.value = 'auto' // change to auto mode
設定
js
import { useColorMode } from '@vueuse/core'
const mode = useColorMode({
attribute: 'theme',
modes: {
// custom colors
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)
コンポーネントの使用法
この関数は、
@vueuse/components
パッケージを介して、レンダーレスコンポーネントバージョンも提供します。使用法について詳しくは、こちらをご覧ください。
vue
<template>
<UseColorMode v-slot="{ mode }">
<button @click="mode = mode === 'dark' ? 'light' : 'dark'">
Mode {{ mode }}
</button>
</UseColorMode>
</template>
型宣言
型宣言を表示
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.dokyumento.jp/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.dokyumento.jp/useColorMode
* @param options
*/
export declare function useColorMode<T extends string = BasicColorMode>(
options?: UseColorModeOptions<T>,
): UseColorModeReturn<T>
ソース
貢献者
変更履歴
v12.0.0-beta.1
2024/11/21v11.0.0-beta.2
2024/7/17v10.2.0
2023/06/16v10.1.0
2023/04/22v10.0.0-beta.4
2023/04/135c82c
- fix!: disableTransition
をデフォルトで有効化v10.0.0-beta.0
2023/03/14320ab
- feat(useDark, useColorMode): disableTransition
オプションを導入v9.11.0
2023/01/17