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(useElementByPoint): new multiple and interval options #3089

Merged
merged 2 commits into from May 12, 2023
Merged
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
48 changes: 38 additions & 10 deletions packages/core/useElementByPoint/index.ts
@@ -1,13 +1,23 @@
import type { Ref } from 'vue-demi'
import { ref } from 'vue-demi'
import type { MaybeRefOrGetter } from '@vueuse/shared'
import { toValue } from '@vueuse/shared'
import type { MaybeRefOrGetter, Pausable } from '@vueuse/shared'
import { toValue, useIntervalFn } from '@vueuse/shared'
import { useRafFn } from '../useRafFn'
import type { ConfigurableDocument } from '../_configurable'
import { defaultDocument } from '../_configurable'
import { useSupported } from '../useSupported'

export interface UseElementByPointOptions extends ConfigurableDocument {
export interface UseElementByPointOptions<Multiple extends boolean = false> extends ConfigurableDocument {
x: MaybeRefOrGetter<number>
y: MaybeRefOrGetter<number>
multiple?: MaybeRefOrGetter<Multiple>
immediate?: boolean
interval?: 'requestAnimationFrame' | number
}

export interface UseElementByPointReturn<Multiple extends boolean = false> extends Pausable {
isSupported: Ref<boolean>
element: Ref<Multiple extends true ? HTMLElement[] : HTMLElement | null>
}

/**
Expand All @@ -16,19 +26,37 @@ export interface UseElementByPointOptions extends ConfigurableDocument {
* @see https://vueuse.org/useElementByPoint
* @param options - UseElementByPointOptions
*/
export function useElementByPoint(options: UseElementByPointOptions) {
const element = ref<HTMLElement | null>(null)
export function useElementByPoint<M extends boolean = false>(options: UseElementByPointOptions<M>): UseElementByPointReturn<M> {
const {
x, y,
document = defaultDocument,
multiple,
interval = 'requestAnimationFrame',
immediate = true,
} = options

const { x, y, document = defaultDocument } = options
const isSupported = useSupported(() => {
if (toValue(multiple))
return document && 'elementsFromPoint' in document

const controls = useRafFn(() => {
element.value = (document?.elementFromPoint(toValue(x), toValue(y)) || null) as HTMLElement | null
return document && 'elementFromPoint' in document
})

const element = ref<any>(null)

const cb = () => {
element.value = toValue(multiple)
? document?.elementsFromPoint(toValue(x), toValue(y)) ?? []
: document?.elementFromPoint(toValue(x), toValue(y)) ?? null
}

const controls: Pausable = interval === 'requestAnimationFrame'
? useRafFn(cb, { immediate })
: useIntervalFn(cb, interval, { immediate })

return {
isSupported,
element,
...controls,
}
}

export type UseElementByPointReturn = ReturnType<typeof useElementByPoint>