Skip to content

Commit 7bb75dd

Browse files
fix: optimize scroll position observer after video fullscreen exit (openedx#1405)
1 parent d76c0cc commit 7bb75dd

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

src/courseware/course/sequence/Unit/hooks/useIFrameBehavior.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,19 @@ const useIFrameBehavior = ({
5050
if (type === messageTypes.resize) {
5151
setIframeHeight(payload.height);
5252

53-
// We observe exit from the video xblock fullscreen mode
54-
// and scroll to the previously saved scroll position
55-
if (windowTopOffset !== null) {
56-
window.scrollTo(0, Number(windowTopOffset));
57-
}
58-
5953
if (!hasLoaded && iframeHeight === 0 && payload.height > 0) {
6054
setHasLoaded(true);
6155
if (onLoaded) {
6256
onLoaded();
6357
}
6458
}
6559
} else if (type === messageTypes.videoFullScreen) {
60+
// We observe exit from the video xblock fullscreen mode
61+
// and scroll to the previously saved scroll position
62+
if (!payload.open && windowTopOffset !== null) {
63+
window.scrollTo(0, Number(windowTopOffset));
64+
}
65+
6666
// We listen for this message from LMS to know when we need to
6767
// save or reset scroll position on toggle video xblock fullscreen mode
6868
setWindowTopOffset(payload.open ? window.scrollY : null);

src/courseware/course/sequence/Unit/hooks/useIFrameBehavior.test.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,9 @@ describe('useIFrameBehavior hook', () => {
154154
const resizeMessage = (height = 23) => ({
155155
data: { type: messageTypes.resize, payload: { height } },
156156
});
157+
const videoFullScreenMessage = (open = false) => ({
158+
data: { type: messageTypes.videoFullScreen, payload: { open } },
159+
});
157160
const testSetIFrameHeight = (height = 23) => {
158161
const { cb } = useEventListener.mock.calls[0][1];
159162
cb(resizeMessage(height));
@@ -209,7 +212,7 @@ describe('useIFrameBehavior hook', () => {
209212
state.mockVals({ ...defaultStateVals, windowTopOffset });
210213
hook = useIFrameBehavior(props);
211214
const { cb } = useEventListener.mock.calls[0][1];
212-
cb(resizeMessage());
215+
cb(videoFullScreenMessage());
213216
expect(window.scrollTo).toHaveBeenCalledWith(0, windowTopOffset);
214217
});
215218
it('does not scroll if towverticalp offset is not set', () => {

0 commit comments

Comments
 (0)