Skip to content

Commit

Permalink
Fixed overflowObserver for SSR
Browse files Browse the repository at this point in the history
  • Loading branch information
davidjbradshaw committed Jul 11, 2024
1 parent 21d19fd commit 044e947
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 26 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "iframe-resizer",
"version": "5.1.5",
"version": "5.2.0-beta.5",
"private": true,
"type": "module",
"license": "GPL-3.0",
Expand Down
46 changes: 21 additions & 25 deletions packages/child/overflow.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,34 @@
import { HEIGHT_EDGE, OVERFLOW_ATTR } from '../common/consts'
import { id } from '../common/utils'

let side = HEIGHT_EDGE

let onChange = id
let overflowedElements = []

const options = {
root: document.documentElement,
rootMargin: '0px',
threshold: 1,
}
export const overflowObserver = (options) => {
const side = options.side || HEIGHT_EDGE
const onChange = options.onChange || id

let overflowedElements = []
const observedElements = new WeakSet()
const observerOptions = {
root: document.documentElement,
rootMargin: '0px',
threshold: 1,
}

const isTarget = (entry) =>
entry.boundingClientRect[side] === 0 ||
entry.boundingClientRect[side] > entry.rootBounds[side]
const observedElements = new WeakSet()

const callback = (entries) => {
entries.forEach((entry) => {
entry.target.toggleAttribute(OVERFLOW_ATTR, isTarget(entry))
})
const isTarget = (entry) =>
entry.boundingClientRect[side] === 0 ||
entry.boundingClientRect[side] > entry.rootBounds[side]

overflowedElements = document.querySelectorAll(`[${OVERFLOW_ATTR}]`)
onChange()
}
const callback = (entries) => {
entries.forEach((entry) => {
entry.target.toggleAttribute(OVERFLOW_ATTR, isTarget(entry))
})

const observer = new IntersectionObserver(callback, options)
overflowedElements = document.querySelectorAll(`[${OVERFLOW_ATTR}]`)
onChange()
}

export const overflowObserver = (options) => {
side = options.side
onChange = options.onChange
const observer = new IntersectionObserver(callback, observerOptions)

return (nodeList) =>
nodeList.forEach((el) => {
Expand All @@ -42,5 +39,4 @@ export const overflowObserver = (options) => {
}

export const isOverflowed = () => overflowedElements.length > 0

export const getOverflowedElements = () => overflowedElements

0 comments on commit 044e947

Please sign in to comment.