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

[TS migration] Migrate 'Popover.js' component to TypeScript #30624

Merged
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
1 change: 1 addition & 0 deletions src/components/Modal/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,4 @@ type BaseModalProps = WindowDimensionsProps &
};

export default BaseModalProps;
export type {PopoverAnchorPosition};
35 changes: 0 additions & 35 deletions src/components/Popover/index.native.js

This file was deleted.

27 changes: 27 additions & 0 deletions src/components/Popover/index.native.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import React from 'react';
import Modal from '@components/Modal';
import CONST from '@src/CONST';
import {PopoverProps} from './types';

/*
* 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({animationIn, animationOut, popoverAnchorPosition, disableAnimation, anchorPosition = {}, fromSidebarMediumScreen, ...propsWithoutAnimation}: PopoverProps) {
return (
kubabutkiewicz marked this conversation as resolved.
Show resolved Hide resolved
<Modal
type={fromSidebarMediumScreen ? CONST.MODAL.MODAL_TYPE.POPOVER : CONST.MODAL.MODAL_TYPE.BOTTOM_DOCKED}
popoverAnchorPosition={fromSidebarMediumScreen ? anchorPosition : undefined}
// eslint-disable-next-line react/jsx-props-no-spreading
{...propsWithoutAnimation}
// Mobile will always has fullscreen menu
fullscreen
animationIn="slideInUp"
animationOut="slideOutDown"
Copy link
Contributor

@sobitneupane sobitneupane Nov 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NAB: I am not sure why this change is required. animationIn and animationOut were not previously included.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question, im unsure too. Can you let us know @kubabutkiewicz.
Its also a NAB for me. Since the rest LGTM, I'll proceed ahead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some reason when moved this file to TS this two props was needed by TS and without them was complaining. I just added two values which are default base on documentation

/>
);
}

Popover.displayName = 'Popover';

export default Popover;
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,30 @@ import {PopoverContext} from '@components/PopoverProvider';
import PopoverWithoutOverlay from '@components/PopoverWithoutOverlay';
import withWindowDimensions from '@components/withWindowDimensions';
import CONST from '@src/CONST';
import {defaultProps, propTypes} from './popoverPropTypes';
import {PopoverWithWindowDimensionsProps} from './types';

/*
* 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, anchorRef} = props;

function Popover(props: PopoverWithWindowDimensionsProps) {
const {
isVisible,
onClose,
isSmallScreenWidth,
fullscreen,
animationInTiming = CONST.ANIMATED_TRANSITION,
onLayout,
animationOutTiming,
disableAnimation = true,
withoutOverlay,
anchorPosition = {},
anchorRef = () => {},
animationIn = 'fadeIn',
animationOut = 'fadeOut',
} = props;

const withoutOverlayRef = useRef(null);
const {close, popover} = React.useContext(PopoverContext);

Expand All @@ -33,7 +49,7 @@ function Popover(props) {
}, [onClose, isVisible]);

const onCloseWithPopoverContext = () => {
if (popover) {
if (popover && 'current' in anchorRef) {
close(anchorRef);
}
onClose();
Expand All @@ -51,6 +67,8 @@ function Popover(props) {
animationOutTiming={disableAnimation ? 1 : animationOutTiming}
shouldCloseOnOutsideClick
onLayout={onLayout}
animationIn={animationIn}
animationOut={animationOut}
/>,
document.body,
);
Expand All @@ -62,6 +80,8 @@ function Popover(props) {
// eslint-disable-next-line react/jsx-props-no-spreading
{...props}
withoutOverlayRef={withoutOverlayRef}
animationIn={animationIn}
animationOut={animationOut}
/>,
document.body,
);
Expand All @@ -78,12 +98,12 @@ function Popover(props) {
animationInTiming={disableAnimation && !isSmallScreenWidth ? 1 : animationInTiming}
animationOutTiming={disableAnimation && !isSmallScreenWidth ? 1 : animationOutTiming}
onLayout={onLayout}
animationIn={animationIn}
animationOut={animationOut}
/>
);
}

Popover.propTypes = propTypes;
Popover.defaultProps = defaultProps;
Popover.displayName = 'Popover';

export default withWindowDimensions(Popover);
42 changes: 42 additions & 0 deletions src/components/Popover/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import BaseModalProps, {PopoverAnchorPosition} from '@components/Modal/types';
import {WindowDimensionsProps} from '@components/withWindowDimensions/types';

type AnchorAlignment = {horizontal: string; vertical: string};

type PopoverDimensions = {
width: number;
height: number;
};

type PopoverProps = BaseModalProps & {
/** The anchor position of the popover */
anchorPosition?: PopoverAnchorPosition;

/** The anchor alignment of the popover */
anchorAlignment: AnchorAlignment;

/** The anchor ref of the popover */
anchorRef: React.RefObject<HTMLElement>;

/** Whether disable the animations */
disableAnimation: boolean;

/** Whether we don't want to show overlay */
withoutOverlay: boolean;
kubabutkiewicz marked this conversation as resolved.
Show resolved Hide resolved

/** The dimensions of the popover */
popoverDimensions?: PopoverDimensions;

/** The ref of the popover */
withoutOverlayRef?: React.RefObject<HTMLElement>;

/** Whether we want to show the popover on the right side of the screen */
fromSidebarMediumScreen?: boolean;

/** The popover children */
children: React.ReactNode;
};

type PopoverWithWindowDimensionsProps = PopoverProps & WindowDimensionsProps;

export type {PopoverProps, PopoverWithWindowDimensionsProps};
Loading