From 9437142bc845b6a8c7263f3a00a904deeb0d354c Mon Sep 17 00:00:00 2001 From: Ihor Romaniuk Date: Thu, 30 May 2024 19:15:15 +0200 Subject: [PATCH] fix: optimize scroll position observer after video fullscreen exit (#1371) --- .../course/sequence/Unit/hooks/useIFrameBehavior.js | 12 ++++++------ .../sequence/Unit/hooks/useIFrameBehavior.test.js | 5 ++++- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/courseware/course/sequence/Unit/hooks/useIFrameBehavior.js b/src/courseware/course/sequence/Unit/hooks/useIFrameBehavior.js index 151a3e7592..2dda5c2200 100644 --- a/src/courseware/course/sequence/Unit/hooks/useIFrameBehavior.js +++ b/src/courseware/course/sequence/Unit/hooks/useIFrameBehavior.js @@ -50,12 +50,6 @@ const useIFrameBehavior = ({ if (type === messageTypes.resize) { setIframeHeight(payload.height); - // We observe exit from the video xblock fullscreen mode - // and scroll to the previously saved scroll position - if (windowTopOffset !== null) { - window.scrollTo(0, Number(windowTopOffset)); - } - if (!hasLoaded && iframeHeight === 0 && payload.height > 0) { setHasLoaded(true); if (onLoaded) { @@ -63,6 +57,12 @@ const useIFrameBehavior = ({ } } } else if (type === messageTypes.videoFullScreen) { + // We observe exit from the video xblock fullscreen mode + // and scroll to the previously saved scroll position + if (!payload.open && windowTopOffset !== null) { + window.scrollTo(0, Number(windowTopOffset)); + } + // We listen for this message from LMS to know when we need to // save or reset scroll position on toggle video xblock fullscreen mode setWindowTopOffset(payload.open ? window.scrollY : null); diff --git a/src/courseware/course/sequence/Unit/hooks/useIFrameBehavior.test.js b/src/courseware/course/sequence/Unit/hooks/useIFrameBehavior.test.js index 9daec9c522..4a4142d29f 100644 --- a/src/courseware/course/sequence/Unit/hooks/useIFrameBehavior.test.js +++ b/src/courseware/course/sequence/Unit/hooks/useIFrameBehavior.test.js @@ -154,6 +154,9 @@ describe('useIFrameBehavior hook', () => { const resizeMessage = (height = 23) => ({ data: { type: messageTypes.resize, payload: { height } }, }); + const videoFullScreenMessage = (open = false) => ({ + data: { type: messageTypes.videoFullScreen, payload: { open } }, + }); const testSetIFrameHeight = (height = 23) => { const { cb } = useEventListener.mock.calls[0][1]; cb(resizeMessage(height)); @@ -209,7 +212,7 @@ describe('useIFrameBehavior hook', () => { state.mockVals({ ...defaultStateVals, windowTopOffset }); hook = useIFrameBehavior(props); const { cb } = useEventListener.mock.calls[0][1]; - cb(resizeMessage()); + cb(videoFullScreenMessage()); expect(window.scrollTo).toHaveBeenCalledWith(0, windowTopOffset); }); it('does not scroll if towverticalp offset is not set', () => {