Skip to content

Commit 075ab98

Browse files
authored
fix(perf): pause animations when not displayed (#1323)
* fix(perf): pause animations when not displayed Safari still calculates rendering for animations that are set with animateTransform, even if they aren't being displayed (svg or parent is `hidden`). This causes performance issues on older devices. We work around this by manually pausing or unpausing the svg depending on whether it's displayed. https://bugs.webkit.org/show_bug.cgi?id=298217 closes #1322 * condition * safer
1 parent de8a195 commit 075ab98

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

packages/autocomplete-js/src/render.tsx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ export function renderSearchBox<TItem extends BaseItem>({
5757
);
5858
setProperties(dom.label, { hidden: state.status === 'stalled' });
5959
setProperties(dom.loadingIndicator, { hidden: state.status !== 'stalled' });
60+
toggleAnimation(dom.loadingIndicator, state.status === 'stalled');
61+
6062
setProperties(dom.clearButton, { hidden: !state.query });
6163
setProperties(dom.detachedSearchButtonQuery, {
6264
textContent: state.query,
@@ -66,6 +68,30 @@ export function renderSearchBox<TItem extends BaseItem>({
6668
});
6769
}
6870

71+
// Safari will animate the SVG even when it's hidden. We need to pause the
72+
// animation manually. See:
73+
// - https://github.com/algolia/autocomplete/issues/1322
74+
// - https://bugs.webkit.org/show_bug.cgi?id=298217
75+
function toggleAnimation(element: HTMLElement, isActive: boolean) {
76+
const svgElement =
77+
element.firstChild?.nodeName === 'svg'
78+
? (element.firstChild as SVGSVGElement)
79+
: null;
80+
if (
81+
!svgElement ||
82+
!svgElement.pauseAnimations ||
83+
!svgElement.unpauseAnimations
84+
) {
85+
return;
86+
}
87+
88+
if (isActive) {
89+
svgElement.unpauseAnimations();
90+
} else {
91+
svgElement.pauseAnimations();
92+
}
93+
}
94+
6995
export function renderPanel<TItem extends BaseItem>(
7096
render: AutocompleteRender<TItem>,
7197
{

0 commit comments

Comments
 (0)