跳到主要內容

useColorMode

類別
導出大小
2.69 kB
上次變更
5 天前
相關

響應式顏色模式(深色 / 淺色 / 自訂)具有自動資料持久性。

演示

← 點擊以更改顏色模式

基本用法

js
import { useColorMode } from '@vueuse/core'

const mode = useColorMode() // Ref<'dark' | 'light'>

預設情況下,它將使用 usePreferredDark (又名 auto 模式) 匹配用戶的瀏覽器偏好設定。當讀取 ref 時,預設情況下它將返回目前的顏色模式 (darklight 或您的自訂模式)。透過啟用 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.dev.org.tw/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.dev.org.tw/useColorMode
 * @param options
 */
export declare function useColorMode<T extends string = BasicColorMode>(
  options?: UseColorModeOptions<T>,
): UseColorModeReturn<T>

原始碼

原始碼演示文件

貢獻者

Anthony Fu
Anthony Fu
Waleed Khaled
Dominik Freier
wheat
IlyaL
Alex
Jean-Philippe Leclerc
reslear
Jason Liang
Yang
丶远方
ntnyq
vaakian X
sun0day
vaakian X
Jelf
Andreas Weber
Andrej Hýll

更新日誌

v12.8.0 於 2025/3/5
7432f - feat(types): 棄用 MaybeRefMaybeRefOrGetter,改用 Vue 原生型別 (#4636)
v12.0.0-beta.1 於 2024/11/21
0a9ed - feat!: 移除 Vue 2 支援,優化捆綁包並清理程式碼 (#4349)
v11.0.0-beta.2 於 2024/7/17
905b9 - fix(useColorMode, useDark): 修正在呼叫 useColorMode 和 useDark 時的整頁重新排版問題 (#4001)
v10.2.0 於 2023/6/16
78a3a - feat: disableTransition 支援偽元素 (#3129)
v10.1.0 於 2023/4/22
a1bef - feat: 將 state 暴露給 ref,棄用 emitAuto (#2980)
adbbb - fix: 元素 ref 支援,關閉 #3003
v10.0.0-beta.4 於 2023/4/13
5c82c - fix!: 預設啟用 disableTransition
d150c - feat: 暴露 systemstore ref,關閉 #2023
02ccc - feat: 支援將元素作為 selector 傳遞 (#2760)
v10.0.0-beta.0 於 2023/3/14
320ab - feat(useDark, useColorMode): 引入 disableTransition 選項

在 MIT 許可證下發布。