-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38755 from suneox/35380-virtual-viewport-on-repor…
…t-screen 35380 virtual viewport on report screen
- Loading branch information
Showing
6 changed files
with
178 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/** | ||
* Native doesn't support DOM so default value is 0 | ||
*/ | ||
|
||
export default function useViewportOffsetTop(): number { | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import {useEffect, useRef, useState} from 'react'; | ||
import addViewportResizeListener from '@libs/VisualViewport'; | ||
|
||
/** | ||
* A hook that returns the offset of the top edge of the visual viewport | ||
*/ | ||
export default function useViewportOffsetTop(shouldAdjustScrollView = false): number { | ||
const [viewportOffsetTop, setViewportOffsetTop] = useState(0); | ||
const initialHeight = useRef(window.visualViewport?.height ?? window.innerHeight).current; | ||
const cachedDefaultOffsetTop = useRef<number>(0); | ||
useEffect(() => { | ||
const updateOffsetTop = (event?: Event) => { | ||
let targetOffsetTop = window.visualViewport?.offsetTop ?? 0; | ||
if (event?.target instanceof VisualViewport) { | ||
targetOffsetTop = event.target.offsetTop; | ||
} | ||
|
||
if (shouldAdjustScrollView && window.visualViewport) { | ||
const adjustScrollY = Math.round(initialHeight - window.visualViewport.height); | ||
if (cachedDefaultOffsetTop.current === 0) { | ||
cachedDefaultOffsetTop.current = targetOffsetTop; | ||
} | ||
|
||
if (adjustScrollY > targetOffsetTop) { | ||
setViewportOffsetTop(adjustScrollY); | ||
} else if (targetOffsetTop !== 0 && adjustScrollY === targetOffsetTop) { | ||
setViewportOffsetTop(cachedDefaultOffsetTop.current); | ||
} else { | ||
setViewportOffsetTop(targetOffsetTop); | ||
} | ||
} else { | ||
setViewportOffsetTop(targetOffsetTop); | ||
} | ||
}; | ||
updateOffsetTop(); | ||
return addViewportResizeListener(updateOffsetTop); | ||
}, [initialHeight, shouldAdjustScrollView]); | ||
|
||
useEffect(() => { | ||
if (!shouldAdjustScrollView) { | ||
return; | ||
} | ||
window.scrollTo({top: viewportOffsetTop}); | ||
}, [shouldAdjustScrollView, viewportOffsetTop]); | ||
|
||
return viewportOffsetTop; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters