Skip to content

Commit 399fa1c

Browse files
committed
fix(sheet): fixing performance regression on modal sheets when expandToScroll is false
1 parent 2018a04 commit 399fa1c

File tree

1 file changed

+15
-15
lines changed
  • core/src/components/modal/gestures

1 file changed

+15
-15
lines changed

core/src/components/modal/gestures/sheet.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { isIonContent, findClosestIonContent } from '@utils/content';
1+
import { findClosestIonContent, isIonContent } from '@utils/content';
22
import { createGesture } from '@utils/gesture';
3-
import { clamp, raf, getElementRoot } from '@utils/helpers';
3+
import { clamp, getElementRoot, raf } from '@utils/helpers';
44
import { FOCUS_TRAP_DISABLE_CLASS } from '@utils/overlays';
55

66
import type { Animation } from '../../../interface';
@@ -83,6 +83,7 @@ export const createSheetGesture = (
8383
let currentBreakpoint = initialBreakpoint;
8484
let offset = 0;
8585
let canDismissBlocksGesture = false;
86+
let cachedScrollEl: HTMLElement | null = null;
8687
const canDismissMaxStep = 0.95;
8788
const maxBreakpoint = breakpoints[breakpoints.length - 1];
8889
const minBreakpoint = breakpoints[0];
@@ -233,6 +234,14 @@ export const createSheetGesture = (
233234
*/
234235
canDismissBlocksGesture = baseEl.canDismiss !== undefined && baseEl.canDismiss !== true && minBreakpoint === 0;
235236

237+
/**
238+
* Cache the scroll element reference when the gesture starts,
239+
* this allows us to avoid querying the DOM for the target in onMove,
240+
* which would impact performance significantly.
241+
*/
242+
const targetEl = findClosestIonContent(detail.event.target! as HTMLElement);
243+
cachedScrollEl = targetEl && isIonContent(targetEl) ? getElementRoot(targetEl).querySelector('.inner-scroll') : targetEl;
244+
236245
/**
237246
* If expandToScroll is disabled, we need to swap
238247
* the footer visibility to the original, so if the modal
@@ -267,13 +276,8 @@ export const createSheetGesture = (
267276
* If `expandToScroll` is disabled, and an upwards swipe gesture is done within
268277
* the scrollable content, we should not allow the swipe gesture to continue.
269278
*/
270-
if (!expandToScroll && detail.deltaY <= 0) {
271-
const contentEl = findClosestIonContent(detail.event.target! as HTMLElement);
272-
const scrollEl =
273-
contentEl && isIonContent(contentEl) ? getElementRoot(contentEl).querySelector('.inner-scroll') : contentEl;
274-
if (scrollEl) {
275-
return;
276-
}
279+
if (!expandToScroll && detail.deltaY <= 0 && cachedScrollEl) {
280+
return;
277281
}
278282

279283
/**
@@ -334,12 +338,8 @@ export const createSheetGesture = (
334338
* function to be called if the user is trying to swipe content upwards and the content
335339
* is not scrolled to the top.
336340
*/
337-
if (!expandToScroll && detail.deltaY <= 0 && findClosestIonContent(detail.event.target! as HTMLElement)) {
338-
const contentEl = findClosestIonContent(detail.event.target! as HTMLElement)!;
339-
const scrollEl = isIonContent(contentEl) ? getElementRoot(contentEl).querySelector('.inner-scroll') : contentEl;
340-
if (scrollEl!.scrollTop > 0) {
341-
return;
342-
}
341+
if (!expandToScroll && detail.deltaY <= 0 && cachedScrollEl && cachedScrollEl.scrollTop > 0) {
342+
return;
343343
}
344344

345345
/**

0 commit comments

Comments
 (0)