123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287 |
- import {Component, Fragment, useCallback, useEffect, useRef} from 'react';
- import {createPortal} from 'react-dom';
- import {browserHistory} from 'react-router';
- import {css} from '@emotion/react';
- import styled from '@emotion/styled';
- import {createFocusTrap, FocusTrap} from 'focus-trap';
- import {AnimatePresence, motion} from 'framer-motion';
- import {closeModal as actionCloseModal} from 'sentry/actionCreators/modal';
- import {ROOT_ELEMENT} from 'sentry/constants';
- import ModalStore from 'sentry/stores/modalStore';
- import space from 'sentry/styles/space';
- import getModalPortal from 'sentry/utils/getModalPortal';
- import testableTransition from 'sentry/utils/testableTransition';
- import {makeClosableHeader, makeCloseButton, ModalBody, ModalFooter} from './components';
- type ModalOptions = {
-
- allowClickClose?: boolean;
-
- backdrop?: 'static' | boolean;
-
- modalCss?: ReturnType<typeof css>;
-
- onClose?: () => void;
- };
- type ModalRenderProps = {
-
- Body: typeof ModalBody;
-
- CloseButton: ReturnType<typeof makeCloseButton>;
-
- Footer: typeof ModalFooter;
-
- Header: ReturnType<typeof makeClosableHeader>;
-
- closeModal: () => void;
- };
- export type ModalTypes = {
- options: ModalOptions;
- renderProps: ModalRenderProps;
- };
- type Props = {
-
- options: ModalOptions;
-
- visible: boolean;
-
- children?: null | ((renderProps: ModalRenderProps) => React.ReactNode);
-
- onClose?: () => void;
- };
- function GlobalModal({visible = false, options = {}, children, onClose}: Props) {
- const closeModal = useCallback(() => {
-
- options.onClose?.();
-
- actionCloseModal();
-
- onClose?.();
- }, [options, onClose]);
- const handleEscapeClose = useCallback(
- (e: KeyboardEvent) => e.key === 'Escape' && closeModal(),
- [closeModal]
- );
- const portal = getModalPortal();
- const focusTrap = useRef<FocusTrap>();
-
- if (window.SentryApp) {
- window.SentryApp.modalFocusTrap = focusTrap;
- }
- useEffect(() => {
- focusTrap.current = createFocusTrap(portal, {
- preventScroll: true,
- escapeDeactivates: false,
- fallbackFocus: portal,
- });
- }, [portal]);
- useEffect(() => {
- const body = document.querySelector('body');
- const root = document.getElementById(ROOT_ELEMENT);
- if (!body || !root) {
- return () => void 0;
- }
- const reset = () => {
- body.style.removeProperty('overflow');
- root.removeAttribute('aria-hidden');
- focusTrap.current?.deactivate();
- document.removeEventListener('keydown', handleEscapeClose);
- };
- if (visible) {
- body.style.overflow = 'hidden';
- root.setAttribute('aria-hidden', 'true');
- focusTrap.current?.activate();
- document.addEventListener('keydown', handleEscapeClose);
- } else {
- reset();
- }
- return reset;
- }, [portal, handleEscapeClose, visible]);
-
- useEffect(() => browserHistory.listen(() => actionCloseModal()), []);
- const renderedChild = children?.({
- CloseButton: makeCloseButton(closeModal),
- Header: makeClosableHeader(closeModal),
- Body: ModalBody,
- Footer: ModalFooter,
- closeModal,
- });
-
- const backdrop = options.backdrop ?? true;
-
- const allowClickClose = options.allowClickClose ?? true;
-
- const containerRef = useRef<HTMLDivElement>(null);
- const clickClose = (e: React.MouseEvent) =>
- containerRef.current === e.target && allowClickClose && closeModal();
- return createPortal(
- <Fragment>
- <Backdrop
- style={backdrop && visible ? {opacity: 0.5, pointerEvents: 'auto'} : {}}
- />
- <Container
- ref={containerRef}
- style={{pointerEvents: visible ? 'auto' : 'none'}}
- onClick={backdrop === true ? clickClose : undefined}
- >
- <AnimatePresence>
- {visible && (
- <Modal role="dialog" css={options.modalCss}>
- <Content role="document">{renderedChild}</Content>
- </Modal>
- )}
- </AnimatePresence>
- </Container>
- </Fragment>,
- portal
- );
- }
- const fullPageCss = css`
- position: fixed;
- top: 0;
- right: 0;
- bottom: 0;
- left: 0;
- `;
- const Backdrop = styled('div')`
- ${fullPageCss};
- z-index: ${p => p.theme.zIndex.modal};
- background: ${p => p.theme.black};
- will-change: opacity;
- transition: opacity 200ms;
- pointer-events: none;
- opacity: 0;
- `;
- const Container = styled('div')`
- ${fullPageCss};
- z-index: ${p => p.theme.zIndex.modal};
- display: flex;
- justify-content: center;
- align-items: flex-start;
- overflow-y: auto;
- `;
- const Modal = styled(motion.div)`
- width: 640px;
- pointer-events: auto;
- padding: 80px ${space(2)} ${space(4)} ${space(2)};
- `;
- Modal.defaultProps = {
- initial: {opacity: 0, y: -10},
- animate: {opacity: 1, y: 0},
- exit: {opacity: 0, y: 15},
- transition: testableTransition({
- opacity: {duration: 0.2},
- y: {duration: 0.25},
- }),
- };
- const Content = styled('div')`
- padding: ${space(4)};
- background: ${p => p.theme.background};
- border-radius: 8px;
- box-shadow: 0 0 0 1px ${p => p.theme.translucentBorder}, ${p => p.theme.dropShadowHeavy};
- position: relative;
- `;
- type State = {
- modalStore: ReturnType<typeof ModalStore.get>;
- };
- class GlobalModalContainer extends Component<Partial<Props>, State> {
- state: State = {
- modalStore: ModalStore.get(),
- };
- componentWillUnmount() {
- this.unlistener?.();
- }
- unlistener = ModalStore.listen(
- (modalStore: State['modalStore']) => this.setState({modalStore}),
- undefined
- );
- render() {
- const {modalStore} = this.state;
- const visible = !!modalStore && typeof modalStore.renderer === 'function';
- return (
- <GlobalModal {...this.props} {...modalStore} visible={visible}>
- {visible ? modalStore.renderer : null}
- </GlobalModal>
- );
- }
- }
- export default GlobalModalContainer;
|