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 8 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
2 changes: 1 addition & 1 deletion example/html/width.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<h2>Automagically resizing iFrame</h2>
<p>
Back to
<a name="anchorParentTest" href="index.html">horizontal iFrames</a>.
<a name="anchorParentTest" href="index.html">vertical iframes</a>.
</p>
<div id="iframeContainer" style="margin: 20px">
<iframe
Expand Down
32 changes: 27 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,
overflowObserver,
} from './overflow'

const PERF_TIME_LIMIT = 4
const PERF_MIN_ELEMENTS = 99
Expand Down Expand Up @@ -74,6 +79,7 @@
let myID = ''
let offsetHeight
let offsetWidth
let observeOverflow = () => null
let resizeFrom = 'child'
let resizeObserver = null
let sameDomain = false
Expand Down Expand Up @@ -172,6 +178,7 @@
setupCalcElements()
setupPublicMethods()
setupMouseEvents()
setupObserveOverflow()
startEventListeners()
inPageLinks = setupInPageLinks()
addUsedTag(document.documentElement)
Expand All @@ -182,6 +189,13 @@
isInit = false
}

function setupObserveOverflow() {
if (calculateHeight === calculateWidth) return
observeOverflow = overflowObserver({
side: calculateHeight ? 'bottom' : 'right',
})
}

function sendTitle() {
if (document.title && document.title !== '') {
sendMsg(0, 0, 'title', document.title)
Expand Down Expand Up @@ -404,7 +418,7 @@
document.querySelectorAll(`[${attr}]`).forEach((el) => {
found = true
el.removeAttribute(attr)
el.setAttribute(SIZE_ATTR, null)
el.setAttribute(SIZE_ATTR, true)
})

checkAttrs('data-iframe-height')
Expand Down Expand Up @@ -440,6 +454,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 +799,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,20 +889,23 @@
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 targetElements =
!hasTags && isOverflowed() ? getOverflowedElements() : 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
}
Expand Down
44 changes: 44 additions & 0 deletions packages/child/overflow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const OVERFLOW = 'data-iframe-overflow'
let side = 'bottom'

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

let overflowedElements = []
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)
} else {
entry.target.removeAttribute(OVERFLOW)
}
})
overflowedElements = document.querySelectorAll(`[${OVERFLOW}]`)
// console.log('overflowed', overflowedElements)
}

const observer = new IntersectionObserver(callback, options)

export const overflowObserver = (options) => (nodeList) => {
if (options && options.side) {
side = options.side
}

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