Skip to content
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
26 changes: 22 additions & 4 deletions src/components/Popover/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import React from 'react';
import React, {useRef} from 'react';
import {createPortal} from 'react-dom';
import {propTypes, defaultProps} from './popoverPropTypes';
import CONST from '../../CONST';
import Modal from '../Modal';
import withWindowDimensions from '../withWindowDimensions';
import PopoverWithoutOverlay from '../PopoverWithoutOverlay';
import {PopoverContext} from '../PopoverProvider';

/*
* This is a convenience wrapper around the Modal component for a responsive Popover.
* On small screen widths, it uses BottomDocked modal type, and a Popover type on wide screen widths.
*/
function Popover(props) {
const {isVisible, onClose, isSmallScreenWidth, fullscreen, animationInTiming, onLayout, animationOutTiming, disableAnimation, withoutOverlay, anchorPosition} = props;
const {isVisible, onClose, isSmallScreenWidth, fullscreen, animationInTiming, onLayout, animationOutTiming, disableAnimation, withoutOverlay, anchorPosition, anchorRef} = props;
const withoutOverlayRef = useRef(null);
const {close, popover} = React.useContext(PopoverContext);

// Not adding this inside the PopoverProvider
// because this is an issue on smaller screens as well.
Expand All @@ -29,11 +32,19 @@ function Popover(props) {
};
}, [onClose, isVisible]);

const onCloseWithPopoverContext = () => {
if (popover) {
close(anchorRef);
}
onClose();
};

if (!fullscreen && !isSmallScreenWidth) {
return createPortal(
<Modal
// eslint-disable-next-line react/jsx-props-no-spreading
{...props}
onClose={onCloseWithPopoverContext}
type={CONST.MODAL.MODAL_TYPE.POPOVER}
popoverAnchorPosition={anchorPosition}
animationInTiming={disableAnimation ? 1 : animationInTiming}
Expand All @@ -46,14 +57,21 @@ function Popover(props) {
}

if (withoutOverlay && !isSmallScreenWidth) {
// eslint-disable-next-line react/jsx-props-no-spreading
return createPortal(<PopoverWithoutOverlay {...props} />, document.body);
return createPortal(
<PopoverWithoutOverlay
// eslint-disable-next-line react/jsx-props-no-spreading
{...props}
withoutOverlayRef={withoutOverlayRef}
/>,
document.body,
);
}

return (
<Modal
// eslint-disable-next-line react/jsx-props-no-spreading
{...props}
onClose={onCloseWithPopoverContext}
type={isSmallScreenWidth ? CONST.MODAL.MODAL_TYPE.BOTTOM_DOCKED : CONST.MODAL.MODAL_TYPE.POPOVER}
popoverAnchorPosition={isSmallScreenWidth ? undefined : anchorPosition}
fullscreen={isSmallScreenWidth ? true : fullscreen}
Expand Down
4 changes: 4 additions & 0 deletions src/components/Popover/popoverPropTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ const propTypes = {

/** Whether disable the animations */
disableAnimation: PropTypes.bool,

/** The ref of the popover */
withoutOverlayRef: refPropTypes,
};

const defaultProps = {
Expand All @@ -36,6 +39,7 @@ const defaultProps = {
anchorPosition: {},
anchorRef: () => {},
disableAnimation: true,
withoutOverlayRef: () => {},
};

export {propTypes, defaultProps};
5 changes: 2 additions & 3 deletions src/components/PopoverWithoutOverlay/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import getModalStyles from '../../styles/getModalStyles';
import withWindowDimensions from '../withWindowDimensions';

function Popover(props) {
const ref = React.useRef(null);
const {onOpen, close} = React.useContext(PopoverContext);
const {modalStyle, modalContainerStyle, shouldAddTopSafeAreaMargin, shouldAddBottomSafeAreaMargin, shouldAddTopSafeAreaPadding, shouldAddBottomSafeAreaPadding} = getModalStyles(
'popover',
Expand All @@ -28,7 +27,7 @@ function Popover(props) {
if (props.isVisible) {
props.onModalShow();
onOpen({
ref,
ref: props.withoutOverlayRef,
close: props.onClose,
anchorRef: props.anchorRef,
});
Expand All @@ -51,7 +50,7 @@ function Popover(props) {
return (
<View
style={[modalStyle, {zIndex: 1}]}
ref={ref}
ref={props.withoutOverlayRef}
>
<SafeAreaInsetsContext.Consumer>
{(insets) => {
Expand Down