Skip to content

Commit

Permalink
fix(epicmaxco#4346): throw warn and error if observable target is not…
Browse files Browse the repository at this point in the history
… element
  • Loading branch information
m0ksem committed Sep 11, 2024
1 parent 66709a0 commit b11491d
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 additions & 9 deletions packages/ui/src/composables/useResizeObserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,35 @@ type MaybeRef<T> = T | Ref<T>
type MaybeArray<T> = T | T[]

const normalizeElements = <T>(elements: MaybeRef<T>[] | Ref<MaybeArray<T>>) => {
return Array.isArray(elements)
? elements
: Array.isArray(elements.value)
? elements.value
: [unref(elements)] as T[]
if (Array.isArray(elements)) {
return elements.map(unref)
}

const unrefArray = unref(elements)

return Array.isArray(unrefArray) ? unrefArray : [unrefArray]
}

export const useResizeObserver = <T extends HTMLElement | undefined>(elementsList: MaybeRef<T>[] | Ref<MaybeArray<T>>, cb: ResizeObserverCallback) => {
let resizeObserver: ResizeObserver | undefined

const observeAll = (elementsList: MaybeRef<T>[]) => {
elementsList.forEach((element: MaybeRef<T>) => {
const unrefedElement = unref(element)
const observeAll = (elements: MaybeRef<T>[]) => {
elements.forEach((element: MaybeRef<T>) => {
const unrefElement = unref(element)

if (!unrefElement) {
return
}

if (!(unrefElement instanceof Element)) {
console.error('Vuestic: Trying to observe non-HTMLElement', {
target: unrefElement,
array: elementsList,
})
throw new Error('Vuestic: Unable to observe non-HTMLElement')
}

unrefedElement && resizeObserver?.observe(unrefedElement)
unrefElement && resizeObserver?.observe(unrefElement)
})
}

Expand Down

0 comments on commit b11491d

Please sign in to comment.