Skip to content

Commit

Permalink
feat(useResizeObserver): support element list (#2841)
Browse files Browse the repository at this point in the history
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
  • Loading branch information
acyza and antfu committed Mar 28, 2023
1 parent 16461db commit 3e18793
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions packages/core/useResizeObserver/index.ts
@@ -1,5 +1,5 @@
import { tryOnScopeDispose } from '@vueuse/shared'
import { watch } from 'vue-demi'
import { computed, watch } from 'vue-demi'
import type { MaybeComputedElementRef } from '../unrefElement'
import { unrefElement } from '../unrefElement'
import { useSupported } from '../useSupported'
Expand Down Expand Up @@ -47,7 +47,7 @@ declare class ResizeObserver {
* @param options
*/
export function useResizeObserver(
target: MaybeComputedElementRef,
target: MaybeComputedElementRef | MaybeComputedElementRef[],
callback: ResizeObserverCallback,
options: UseResizeObserverOptions = {},
) {
Expand All @@ -62,17 +62,23 @@ export function useResizeObserver(
}
}

const targets = computed(() =>
Array.isArray(target)
? target.map(el => unrefElement(el))
: [unrefElement(target)],
)

const stopWatch = watch(
() => unrefElement(target),
(el) => {
targets,
(els) => {
cleanup()

if (isSupported.value && window && el) {
if (isSupported.value && window) {
observer = new ResizeObserver(callback)
observer!.observe(el, observerOptions)
for (const _el of els)
_el && observer!.observe(_el, observerOptions)
}
},
{ immediate: true, flush: 'post' },
{ immediate: true, flush: 'post', deep: true },
)

const stop = () => {
Expand Down

0 comments on commit 3e18793

Please sign in to comment.