Skip to content

Commit

Permalink
fix(types): MaybeElementRef type, close vitest-dev#685
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Sep 17, 2021
1 parent e7486d3 commit 9728a14
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 16 deletions.
6 changes: 3 additions & 3 deletions packages/core/unrefElement/index.ts
@@ -1,8 +1,8 @@
import { defineComponent, unref } from 'vue-demi'
import { unref, ComponentPublicInstance } from 'vue-demi'
import { MaybeRef } from '@vueuse/shared'

export type VueInstance = InstanceType<ReturnType<typeof defineComponent>>
export type MaybeElementRef = MaybeRef<Element | VueInstance | undefined | null>
export type VueInstance = ComponentPublicInstance
export type MaybeElementRef = MaybeRef<HTMLElement | SVGElement | VueInstance | undefined | null>

/**
* Get the dom element of a ref of element or Vue component instance
Expand Down
13 changes: 6 additions & 7 deletions packages/core/useDraggable/index.ts
@@ -1,7 +1,6 @@
import { Ref, ref, unref, computed } from 'vue-demi'
import { MaybeRef, toRefs, isClient } from '@vueuse/shared'
import { useEventListener } from '../useEventListener'
import { MaybeElementRef } from '../unrefElement'
import { PointerType, Position } from '../types'
import { defaultWindow } from '../_configurable'

Expand All @@ -25,7 +24,7 @@ export interface UseDraggableOptions {
*
* @default window
*/
draggingElement?: MaybeElementRef
draggingElement?: MaybeRef<HTMLElement | SVGElement | Window | Document | null>

/**
* Pointer types that listen to.
Expand Down Expand Up @@ -56,10 +55,10 @@ export interface UseDraggableOptions {
* Make elements draggable.
*
* @see https://vueuse.org/useDraggable
* @param el
* @param target
* @param options
*/
export function useDraggable(el: MaybeElementRef, options: UseDraggableOptions = {}) {
export function useDraggable(target: MaybeRef<HTMLElement | SVGElement | null>, options: UseDraggableOptions = {}) {
const draggingElement = options.draggingElement ?? defaultWindow
const position: Ref<Position> = ref(options.initialValue ?? { x: 0, y: 0 })
const pressedDelta = ref<Position>()
Expand All @@ -76,9 +75,9 @@ export function useDraggable(el: MaybeElementRef, options: UseDraggableOptions =
const start = (e: PointerEvent) => {
if (!filterEvent(e))
return
if (unref(options.exact) && e.target !== el.value)
if (unref(options.exact) && e.target !== unref(target))
return
const react = el.value!.getBoundingClientRect()
const react = unref(target)!.getBoundingClientRect()
const pos = {
x: e.pageX - react.left,
y: e.pageY - react.top,
Expand Down Expand Up @@ -108,7 +107,7 @@ export function useDraggable(el: MaybeElementRef, options: UseDraggableOptions =
}

if (isClient) {
useEventListener(el, 'pointerdown', start, true)
useEventListener(target, 'pointerdown', start, true)
useEventListener(draggingElement, 'pointermove', move, true)
useEventListener(draggingElement, 'pointerup', end, true)
}
Expand Down
9 changes: 5 additions & 4 deletions packages/core/useFullscreen/index.ts
@@ -1,7 +1,7 @@
/* this implementation is original ported from https://github.com/logaretm/vue-use-web by Abdelrahman Awad */

import { ref } from 'vue-demi'
import { MaybeElementRef } from '../unrefElement'
import { MaybeElementRef, unrefElement } from '../unrefElement'
import { useEventListener } from '../useEventListener'
import { ConfigurableDocument, defaultDocument } from '../_configurable'

Expand Down Expand Up @@ -72,7 +72,7 @@ export function useFullscreen(
options: ConfigurableDocument = {},
) {
const { document = defaultDocument } = options
const targetRef = ref(target || document?.querySelector('html'))
const targetRef = target || document?.querySelector('html')
const isFullscreen = ref(false)
let isSupported = false

Expand Down Expand Up @@ -108,8 +108,9 @@ export function useFullscreen(

await exit()

if (targetRef.value) {
await targetRef.value[REQUEST]()
const target = unrefElement(targetRef)
if (target) {
await target[REQUEST]()
isFullscreen.value = true
}
}
Expand Down
3 changes: 1 addition & 2 deletions packages/core/usePointer/index.ts
Expand Up @@ -3,7 +3,6 @@ import { ref, Ref } from 'vue-demi'
import { useEventListener } from '../useEventListener'
import { ConfigurableWindow, defaultWindow } from '../_configurable'
import { PointerType, Position } from '../types'
import { MaybeElementRef } from '../unrefElement'

export interface UsePointerState extends Position {
pressure: number
Expand Down Expand Up @@ -32,7 +31,7 @@ export interface UsePointerOptions extends ConfigurableWindow {
/**
* @default window
*/
target?: MaybeElementRef
target?: MaybeRef<EventTarget | null | undefined> | Document | Window
}

const defaultState: UsePointerState = /* #__PURE__ */ {
Expand Down

0 comments on commit 9728a14

Please sign in to comment.