-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a52ad52
commit 7c78ada
Showing
3 changed files
with
51 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,50 @@ | ||
/* global ResizeObserver */ | ||
|
||
function observeNative(node, callback) { | ||
if ((typeof ResizeObserver === 'function') && node) { | ||
const observer = new ResizeObserver(() => callback()); // eslint-disable-line compat/compat | ||
observer.observe(node); | ||
return () => observer.disconnect(); | ||
} | ||
return null; | ||
} | ||
|
||
const items = new Map(); | ||
|
||
function checkItems() { | ||
items.forEach((item, node) => { | ||
const bounds = node.getBoundingClientRect(); | ||
// convert to int (because these numbers needed for comparisons), but preserve 1 decimal point | ||
const width = Math.round(bounds.width * 10); | ||
const height = Math.round(bounds.height * 10); | ||
if (items.size > 0) { | ||
items.forEach((item, node) => { | ||
const bounds = node.getBoundingClientRect(); | ||
// convert to int (because these numbers needed for comparisons), but preserve 1 decimal point | ||
const width = Math.round(bounds.width * 10); | ||
const height = Math.round(bounds.height * 10); | ||
|
||
if ( | ||
(item.width !== width) || | ||
(item.height !== height) | ||
) { | ||
item.width = width; | ||
item.height = height; | ||
item.callback(node); | ||
} | ||
}); | ||
if ( | ||
(item.width !== width) || | ||
(item.height !== height) | ||
) { | ||
item.width = width; | ||
item.height = height; | ||
item.callback(node); | ||
} | ||
}); | ||
|
||
setTimeout(checkItems, 100); | ||
setTimeout(checkItems, 100); | ||
} | ||
} | ||
|
||
checkItems(); // ensure it was called only once! | ||
|
||
export default function observe(node, callback) { | ||
if (!items.has(node)) { | ||
function observeFallback(node, callback) { | ||
if (node && !items.has(node)) { | ||
const shouldTrigger = items.size === 0; | ||
items.set(node, { callback }); | ||
if (shouldTrigger) { | ||
checkItems(); | ||
} | ||
return () => items.delete(node); | ||
} | ||
return () => {}; | ||
return null; | ||
} | ||
|
||
export default function observe(node, callback) { | ||
return observeNative(node, callback) || observeFallback(node, callback) || (() => {}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters