Skip to content

Commit

Permalink
fix(useColorMode): element ref support, close #3003
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Apr 21, 2023
1 parent a2147fd commit adbbb6e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
5 changes: 3 additions & 2 deletions packages/core/ssr-handlers.ts
@@ -1,4 +1,5 @@
import type { Awaitable, MaybeRefOrGetter } from '@vueuse/shared'
import type { Awaitable } from '@vueuse/shared'
import type { MaybeElementRef } from './unrefElement'

export interface StorageLikeAsync {
getItem(key: string): Awaitable<string | null>
Expand All @@ -18,7 +19,7 @@ export interface StorageLike {
export interface SSRHandlersMap {
getDefaultStorage: () => StorageLike | undefined
getDefaultStorageAsync: () => StorageLikeAsync | undefined
updateHTMLAttrs: (selector: string | MaybeRefOrGetter<HTMLElement | null | undefined>, attribute: string, value: string) => void
updateHTMLAttrs: (selector: string | MaybeElementRef, attribute: string, value: string) => void
}

const _global
Expand Down
9 changes: 5 additions & 4 deletions packages/core/useColorMode/index.ts
@@ -1,13 +1,14 @@
import type { Ref } from 'vue-demi'
import { computed, ref, watch } from 'vue-demi'
import type { MaybeRefOrGetter } from '@vueuse/shared'
import { toValue, tryOnMounted } from '@vueuse/shared'
import { tryOnMounted } from '@vueuse/shared'
import type { StorageLike } from '../ssr-handlers'
import { getSSRHandler } from '../ssr-handlers'
import type { UseStorageOptions } from '../useStorage'
import { useStorage } from '../useStorage'
import { defaultWindow } from '../_configurable'
import { usePreferredDark } from '../usePreferredDark'
import type { MaybeElementRef } from '../unrefElement'
import { unrefElement } from '../unrefElement'

export type BasicColorSchema = 'light' | 'dark' | 'auto'

Expand All @@ -17,7 +18,7 @@ export interface UseColorModeOptions<T extends string = BasicColorSchema> extend
*
* @default 'html'
*/
selector?: string | MaybeRefOrGetter<HTMLElement | null | undefined>
selector?: string | MaybeElementRef

/**
* HTML attribute applying the target element
Expand Down Expand Up @@ -146,7 +147,7 @@ export function useColorMode<T extends string = BasicColorSchema>(
(selector, attribute, value) => {
const el = typeof selector === 'string'
? window?.document.querySelector(selector)
: toValue(selector)
: unrefElement(selector)
if (!el)
return

Expand Down

0 comments on commit adbbb6e

Please sign in to comment.