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

Added: Only check overflowed elements #1278

Merged
merged 21 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions example/child/frame.animate.html
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@
<h4>Data returned by parentIFrame.getParentProps()</h4>
<table id="data"></table>

<span id="insert"></span>

<!--
The data-iframe-size attribute tells iframe-resizer to use this element
to calculate the height of the iframe, when the content overflows the
Expand All @@ -99,10 +101,11 @@ <h4>Data returned by parentIFrame.getParentProps()</h4>

<script>
// Add some content to to show the effect of using data-iframe-size above
for (let i = 0; i < 10_000; i++) {
document.write(
'<div style="position: absolute">This is a test to see if the page will grow to accommodate the content.</div>',
);
for (let i = 0; i < 500; i++) {
const span = document.createElement('span')
span.style.setProperty('position', 'relative')
span.textContent = `. `
document.getElementById('insert').append(span);
}

</script>
Expand Down
1 change: 0 additions & 1 deletion example/child/frame.content.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
<script
type="text/javascript"
src="../../js/iframe-resizer.child.js"
defer
></script>
<script>
function toggle() {
Expand Down
34 changes: 29 additions & 5 deletions packages/child/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
import formatAdvise from '../common/format-advise'
import { addEventListener, removeEventListener } from '../common/listeners'
import { getModeData } from '../common/mode'
import {
getOverflowedElements,
isOverflowed,
observeOverflow,
} from './overflow'

const PERF_TIME_LIMIT = 4
const PERF_MIN_ELEMENTS = 99
Expand Down Expand Up @@ -440,6 +445,7 @@
const taggedElements = document.querySelectorAll(`[${SIZE_ATTR}]`)
hasTags = taggedElements.length > 0
calcElements = hasTags ? taggedElements : getAllElements(document)()
if (!hasTags) observeOverflow(calcElements)
Fixed Show fixed Hide fixed
}

function checkCalcMode(calcMode, calcModeDefault, modes, type) {
Expand Down Expand Up @@ -784,9 +790,13 @@
const getAllNonStaticElements = () =>
[...getAllElements(document)()].filter(checkPositionType)

const resizeSet = new WeakSet()

function setupResizeObservers(el) {
if (!el) return
if (resizeSet.has(el)) return
resizeObserver.observe(el)
resizeSet.add(el)
log(`Attached resizeObserver: ${getElementName(el)}`)
}

Expand Down Expand Up @@ -870,24 +880,38 @@
const Side = capitalizeFirstLetter(side)

let elVal = 0
let len = calcElements.length
let maxEl = document.documentElement
let maxVal = hasTags
? 0
: document.documentElement.getBoundingClientRect().bottom
let timer = performance.now()

calcElements.forEach((element) => {
const elements = [] // TODO: remove

const targetElements =
!hasTags && isOverflowed() ? getOverflowedElements() : calcElements

// console.log('!hasTags', !hasTags)
console.log('isOverflowed', isOverflowed())
Fixed Show fixed Hide fixed
console.log('test', !hasTags && isOverflowed())
Fixed Show fixed Hide fixed
console.log('targetElements', targetElements)
Fixed Show fixed Hide fixed
// console.log('calcElements', calcElements)

let len = targetElements.length

targetElements.forEach((element) => {
if (
!hasTags &&
hasCheckVisibility &&
hasCheckVisibility && // Safari missing checkVisibility
!element.checkVisibility(checkVisibilityOptions)
) {
log(`Skipping non-visible element: ${getElementName(element)}`)
len -= 1
return
}

// console.log('element', element)
elements.push(element) // TODO: remove

elVal =
element.getBoundingClientRect()[side] +
parseFloat(getComputedStyle(element).getPropertyValue(`margin-${side}`))
Expand All @@ -908,7 +932,7 @@
Position calculated from HTML element: ${getElementName(maxEl)} (${elementSnippet(maxEl, 100)})`

if (timer < PERF_TIME_LIMIT || len < PERF_MIN_ELEMENTS || hasTags || isInit) {
log(logMsg)
console.log(logMsg, elements) // log(logMsg)
Fixed Show fixed Hide fixed
} else if (perfWarned < timer && perfWarned < lastTimer) {
perfWarned = timer * 1.2
advise(
Expand Down
45 changes: 45 additions & 0 deletions packages/child/overflow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const OVERFLOW = 'data-iframe-overflow'
const side = 'bottom'

const options = {
root: document.documentElement,
rootMargin: '0px',
threshold: 1,
}

let overflowedElements = document.querySelectorAll(`[${OVERFLOW}]`)
const observedElements = new WeakSet()

const callback = (entries) => {
entries.forEach((entry) => {
if (
entry.boundingClientRect[side] === 0 ||
entry.boundingClientRect[side] >= entry.rootBounds[side]
) {
entry.target.setAttribute(OVERFLOW, true)
// console.log(
// entry.target,
// entry.boundingClientRect[side],
// entry.rootBounds[side],
// )
} else {
entry.target.removeAttribute(OVERFLOW)
}
})
overflowedElements = document.querySelectorAll(`[${OVERFLOW}]`)
console.log('overflowed', overflowedElements)
Fixed Show fixed Hide fixed
}

const observer = new IntersectionObserver(callback, options)

export const observeOverflow = (nodeList) => {
nodeList.forEach((el) => {
if (observedElements.has(el)) return
observer.observe(el)
observedElements.add(el)
})
}

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

export const getOverflowedElements = () => overflowedElements
Loading