Skip to content

Commit a81c5c6

Browse files
committed
fix jitter scrolling by adopting css based overflow
1 parent fd587da commit a81c5c6

File tree

3 files changed

+54
-51
lines changed

3 files changed

+54
-51
lines changed

packages/block-library/src/image/index.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -258,12 +258,8 @@ class="wp-lightbox-overlay zoom"
258258
data-wp-class--show-closing-animation="state.showClosingAnimation"
259259
data-wp-watch="callbacks.setOverlayFocus"
260260
data-wp-on--keydown="actions.handleKeydown"
261-
data-wp-on-async--touchstart="actions.handleTouchStart"
262-
data-wp-on--touchmove="actions.handleTouchMove"
263-
data-wp-on-async--touchend="actions.handleTouchEnd"
264261
data-wp-on-async--click="actions.hideLightbox"
265262
data-wp-on-async-window--resize="callbacks.setOverlayStyles"
266-
data-wp-on-async-window--scroll="actions.handleScroll"
267263
tabindex="-1"
268264
>
269265
<button type="button" aria-label="$close_button_label" style="fill: $close_button_color" class="close-button">

packages/block-library/src/image/style.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@
207207

208208
.lightbox-image-container {
209209
position: absolute;
210-
overflow: hidden;
210+
// overflow: hidden;
211211
top: 50%;
212212
left: 50%;
213213
transform-origin: top left;

packages/block-library/src/image/view.js

Lines changed: 53 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ import { store, getContext, getElement } from '@wordpress/interactivity';
99
*
1010
* @type {boolean}
1111
*/
12-
let isTouching = false;
12+
// let isTouching = false;
1313

1414
/**
1515
* Tracks the last time the screen was touched; used to differentiate behavior
1616
* for touch and mouse input.
1717
*
1818
* @type {number}
1919
*/
20-
let lastTouchTime = 0;
20+
// let lastTouchTime = 0;
2121

2222
/**
2323
* Stores the image reference of the currently opened lightbox.
@@ -77,7 +77,11 @@ const { state, actions, callbacks } = store(
7777
state.scrollTopReset = document.documentElement.scrollTop;
7878
state.scrollLeftReset = document.documentElement.scrollLeft;
7979

80-
// Moves the information of the expaned image to the state.
80+
// store the overflow state of the document to restore it later
81+
state.documentOverflow = document.body.style.overflow;
82+
document.body.style.overflow = 'hidden';
83+
84+
// Moves the information of the expanded image to the state.
8185
ctx.currentSrc = ctx.imageRef.currentSrc;
8286
imageRef = ctx.imageRef;
8387
buttonRef = ctx.buttonRef;
@@ -106,6 +110,9 @@ const { state, actions, callbacks } = store(
106110
state.currentImage = {};
107111
imageRef = null;
108112
buttonRef = null;
113+
114+
// Restore document overflow
115+
document.body.style.overflow = state.documentOverflow;
109116
}, 450 );
110117

111118
// Starts the overlay closing animation. The showClosingAnimation
@@ -128,49 +135,49 @@ const { state, actions, callbacks } = store(
128135
}
129136
}
130137
},
131-
handleTouchMove( event ) {
132-
// On mobile devices, prevents triggering the scroll event because
133-
// otherwise the page jumps around when it resets the scroll position.
134-
// This also means that closing the lightbox requires that a user
135-
// perform a simple tap. This may be changed in the future if there is a
136-
// better alternative to override or reset the scroll position during
137-
// swipe actions.
138-
if ( state.overlayEnabled ) {
139-
event.preventDefault();
140-
}
141-
},
142-
handleTouchStart() {
143-
isTouching = true;
144-
},
145-
handleTouchEnd() {
146-
// Waits a few milliseconds before resetting to ensure that pinch to
147-
// zoom works consistently on mobile devices when the lightbox is open.
148-
lastTouchTime = Date.now();
149-
isTouching = false;
150-
},
151-
handleScroll() {
152-
// Prevents scrolling behaviors that trigger content shift while the
153-
// lightbox is open. It would be better to accomplish through CSS alone,
154-
// but using overflow: hidden is currently the only way to do so and
155-
// that causes a layout to shift and prevents the zoom animation from
156-
// working in some cases because it's not possible to account for the
157-
// layout shift when doing the animation calculations. Instead, it uses
158-
// JavaScript to prevent and reset the scrolling behavior.
159-
if ( state.overlayOpened ) {
160-
// Avoids overriding the scroll behavior on mobile devices because
161-
// doing so breaks the pinch to zoom functionality, and users should
162-
// be able to zoom in further on the high-res image.
163-
if ( ! isTouching && Date.now() - lastTouchTime > 450 ) {
164-
// It doesn't rely on `event.preventDefault()` to prevent scrolling
165-
// because the scroll event can't be canceled, so it resets the
166-
// position instead.
167-
window.scrollTo(
168-
state.scrollLeftReset,
169-
state.scrollTopReset
170-
);
171-
}
172-
}
173-
},
138+
// handleTouchMove( event ) {
139+
// // On mobile devices, prevents triggering the scroll event because
140+
// // otherwise the page jumps around when it resets the scroll position.
141+
// // This also means that closing the lightbox requires that a user
142+
// // perform a simple tap. This may be changed in the future if there is a
143+
// // better alternative to override or reset the scroll position during
144+
// // swipe actions.
145+
// if ( state.overlayEnabled ) {
146+
// event.preventDefault();
147+
// }
148+
// },
149+
// handleTouchStart() {
150+
// isTouching = true;
151+
// },
152+
// handleTouchEnd() {
153+
// // Waits a few milliseconds before resetting to ensure that pinch to
154+
// // zoom works consistently on mobile devices when the lightbox is open.
155+
// lastTouchTime = Date.now();
156+
// isTouching = false;
157+
// },
158+
// handleScroll() {
159+
// // Prevents scrolling behaviors that trigger content shift while the
160+
// // lightbox is open. It would be better to accomplish through CSS alone,
161+
// // but using overflow: hidden is currently the only way to do so and
162+
// // that causes a layout to shift and prevents the zoom animation from
163+
// // working in some cases because it's not possible to account for the
164+
// // layout shift when doing the animation calculations. Instead, it uses
165+
// // JavaScript to prevent and reset the scrolling behavior.
166+
// if ( state.overlayOpened ) {
167+
// // Avoids overriding the scroll behavior on mobile devices because
168+
// // doing so breaks the pinch to zoom functionality, and users should
169+
// // be able to zoom in further on the high-res image.
170+
// if ( ! isTouching && Date.now() - lastTouchTime > 450 ) {
171+
// // It doesn't rely on `event.preventDefault()` to prevent scrolling
172+
// // because the scroll event can't be canceled, so it resets the
173+
// // position instead.
174+
// window.scrollTo(
175+
// state.scrollLeftReset,
176+
// state.scrollTopReset
177+
// );
178+
// }
179+
// }
180+
// },
174181
},
175182
callbacks: {
176183
setOverlayStyles() {

0 commit comments

Comments
 (0)