Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(useColorMode): support passing element as selector #2760

Merged
merged 3 commits into from Apr 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/core/ssr-handlers.ts
@@ -1,4 +1,4 @@
import type { Awaitable } from '@vueuse/shared'
import type { Awaitable, MaybeRefOrGetter } from '@vueuse/shared'

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

const _global
Expand Down
10 changes: 6 additions & 4 deletions packages/core/useColorMode/index.ts
@@ -1,6 +1,7 @@
import type { Ref } from 'vue-demi'
import { computed, ref, watch } from 'vue-demi'
import { tryOnMounted } from '@vueuse/shared'
import type { MaybeRefOrGetter } from '@vueuse/shared'
import { toValue, tryOnMounted } from '@vueuse/shared'
import type { StorageLike } from '../ssr-handlers'
import { getSSRHandler } from '../ssr-handlers'
import type { UseStorageOptions } from '../useStorage'
Expand All @@ -16,7 +17,7 @@ export interface UseColorModeOptions<T extends string = BasicColorSchema> extend
*
* @default 'html'
*/
selector?: string
selector?: string | MaybeRefOrGetter<HTMLElement | null | undefined>

/**
* HTML attribute applying the target element
Expand Down Expand Up @@ -136,14 +137,15 @@ export function useColorMode<T extends string = BasicColorSchema>(options: UseCo
const updateHTMLAttrs = getSSRHandler(
'updateHTMLAttrs',
(selector, attribute, value) => {
const el = window?.document.querySelector(selector)
const el = typeof selector === 'string'
? window?.document.querySelector(selector)
: toValue(selector)
if (!el)
return

let style: HTMLStyleElement | undefined
if (disableTransition) {
style = window!.document.createElement('style')
style.type = 'text/css'
style.appendChild(document.createTextNode('*{-webkit-transition:none!important;-moz-transition:none!important;-o-transition:none!important;-ms-transition:none!important;transition:none!important}'))
window!.document.head.appendChild(style)
}
Expand Down