Skip to content
This repository has been archived by the owner on May 24, 2024. It is now read-only.

[terra-pills] Added debouncing for resize observer #2143

Merged
merged 2 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions packages/terra-pills/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Unreleased

* Fixed
* Fixed `terra-pills` Infinite loop in ResizeObserver callback with debounce.

## 1.24.0 - (April 4, 2024)

* Changed
Expand Down
16 changes: 10 additions & 6 deletions packages/terra-pills/src/FilterPills.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ const FilterPills = (props) => {
const isPillDeleted = useRef(false);
const isRollUpRemoved = useRef(false);
const removedLabel = useRef();
const resizingDelayTimer = useRef(null);
const resizeTimer = 100;

// Identifies the number of pills that needs to be hidden/rolled up
const generateRollUp = useCallback(() => {
Expand Down Expand Up @@ -214,16 +216,18 @@ const FilterPills = (props) => {
}, [children, isCollapsible, resetTabIndex, handleFocusOnRollUpTrigger, handlePillDeletion, generateRollUp]);

useLayoutEffect(() => {
let observer = new ResizeObserver((entries) => {
handleResize(entries);
});
const debounceResize = (entries) => {
clearTimeout(resizingDelayTimer.current);
resizingDelayTimer.current = setTimeout(() => {
if (resizingDelayTimer.current) handleResize(entries);
}, resizeTimer);
};
const observer = new ResizeObserver(debounceResize);
observer.observe(filterPillsRef.current.parentNode);

return () => {
observer.disconnect();
observer = null;
};
}, [children, handleResize]);
}, [children, filterPillsRef, handleResize]);

const focusNextNode = (pills, rollUpPill) => {
const rollUpPillId = rollUpPill ? rollUpPill.getAttribute('id') : null;
Expand Down
Loading