Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cross platform VisualViewport lib for visual viewport resize #10208

Merged
merged 6 commits into from
Aug 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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