Skip to content

Commit

Permalink
Merge pull request #10208 from Expensify/neil-visual-viewport-lib
Browse files Browse the repository at this point in the history
Add cross platform VisualViewport lib for visual viewport resize
  • Loading branch information
marcaaron authored Aug 3, 2022
2 parents b0f80a4 + 04f48f7 commit 69b7357
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
16 changes: 16 additions & 0 deletions src/libs/VisualViewport/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Add a visual viewport resize listener if available. Return a function to remove the listener.
*
* @param {Function} onViewportResize
* @returns {Function}
*/
function addViewportResizeListener(onViewportResize) {
if (!window.visualViewport) {
return () => {};
}

window.visualViewport.addEventListener('resize', onViewportResize);
return () => window.visualViewport.removeEventListener('resize', onViewportResize);
}

export default addViewportResizeListener;
11 changes: 11 additions & 0 deletions src/libs/VisualViewport/index.native.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

/**
* Visual Viewport is not available on native, so return an empty function.
*
* @returns {Function}
*/
function addViewportResizeListener() {
return () => {};
}

export default addViewportResizeListener;
12 changes: 5 additions & 7 deletions src/pages/home/ReportScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import ReportActionsSkeletonView from '../../components/ReportActionsSkeletonVie
import reportActionPropTypes from './report/reportActionPropTypes';
import ArchivedReportFooter from '../../components/ArchivedReportFooter';
import toggleReportActionComposeView from '../../libs/toggleReportActionComposeView';
import addViewportResizeListener from '../../libs/VisualViewport';
import {withNetwork} from '../../components/OnyxProvider';
import compose from '../../libs/compose';
import networkPropTypes from '../../components/networkPropTypes';
Expand Down Expand Up @@ -118,7 +119,8 @@ class ReportScreen extends React.Component {
super(props);

this.onSubmitComment = this.onSubmitComment.bind(this);
this.viewportOffsetTop = this.updateViewportOffsetTop.bind(this);
this.updateViewportOffsetTop = this.updateViewportOffsetTop.bind(this);
this.removeViewportResizeListener = () => {};

this.state = {
skeletonViewContainerHeight: 0,
Expand All @@ -128,9 +130,7 @@ class ReportScreen extends React.Component {

componentDidMount() {
this.storeCurrentlyViewedReport();
if (window.visualViewport) {
window.visualViewport.addEventListener('resize', this.viewportOffsetTop);
}
this.removeViewportResizeListener = addViewportResizeListener(this.updateViewportOffsetTop);
}

componentDidUpdate(prevProps) {
Expand All @@ -142,9 +142,7 @@ class ReportScreen extends React.Component {

componentWillUnmount() {
clearTimeout(this.loadingTimerId);
if (window.visualViewport) {
window.visualViewport.removeEventListener('resize', this.viewportOffsetTop);
}
this.removeViewportResizeListener();
}

/**
Expand Down

0 comments on commit 69b7357

Please sign in to comment.