@atlaskit/modal-dialog
Version:
A modal dialog displays content that requires user interaction, in a layer above the page.
125 lines (123 loc) • 6.27 kB
JavaScript
/* modal-dialog.tsx generated by @compiled/babel-plugin v0.36.1 */
import _extends from "@babel/runtime/helpers/extends";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import _typeof from "@babel/runtime/helpers/typeof";
import "./modal-dialog.compiled.css";
import * as React from 'react';
import { ax, ix } from "@compiled/react/runtime";
import { useEffect, useMemo } from 'react';
import mergeRefs from '@atlaskit/ds-lib/merge-refs';
import useAutoFocus from '@atlaskit/ds-lib/use-auto-focus';
import { useId } from '@atlaskit/ds-lib/use-id';
import { useCloseOnEscapePress, useLayering } from '@atlaskit/layering';
import FadeIn from '@atlaskit/motion/fade-in';
import { combine } from '@atlaskit/pragmatic-drag-and-drop/combine';
import { ModalContext, ScrollContext } from '../context';
import useOnMotionFinish from '../hooks/use-on-motion-finish';
import { disableDraggingToCrossOriginIFramesForElement } from '../pragmatic-drag-and-drop/disable-dragging-to-cross-origin-iframes/element';
import { disableDraggingToCrossOriginIFramesForExternal } from '../pragmatic-drag-and-drop/disable-dragging-to-cross-origin-iframes/external';
import { disableDraggingToCrossOriginIFramesForTextSelection } from '../pragmatic-drag-and-drop/disable-dragging-to-cross-origin-iframes/text-selection';
import { dialogHeight, dialogWidth } from '../utils';
import Positioner from './positioner';
var LOCAL_CURRENT_SURFACE_CSS_VAR = '--ds-elevation-surface-current';
var dialogStyles = {
root: "_16jlidpf _1o9zkb7n _i0dl1wug _1e0c1txw _1bsb1osq _p12fauwl _4t3i1osq _1tkeidpf _c71l1kxc _2lx21bp4 _bfhk1bhr _syaz1fxt _1q1l1bhr _lcxv1wug _1mq81kw7 _m01u1kw7 _1dg11kw7 _mizu1p6i _1ah31bk5 _ra3xnqa1 _128m1bk5 _1cvmnqa1 _4davt94y _we1i1kw7 _zg7p1cd0 _yakv1kw7 _4lht1kw7 _1xlt6m9r",
borderRadius: "_epkx1sit"
};
var viewportScrollStyles = null;
var bodyScrollStyles = null;
var ModalDialog = function ModalDialog(props) {
var _props$width = props.width,
width = _props$width === void 0 ? 'medium' : _props$width,
_props$shouldScrollIn = props.shouldScrollInViewport,
shouldScrollInViewport = _props$shouldScrollIn === void 0 ? false : _props$shouldScrollIn,
shouldCloseOnEscapePress = props.shouldCloseOnEscapePress,
autoFocus = props.autoFocus,
stackIndex = props.stackIndex,
onClose = props.onClose,
onCloseComplete = props.onCloseComplete,
onOpenComplete = props.onOpenComplete,
height = props.height,
hasProvidedOnClose = props.hasProvidedOnClose,
children = props.children,
label = props.label,
testId = props.testId,
_props$isFullScreen = props.isFullScreen,
isFullScreen = _props$isFullScreen === void 0 ? false : _props$isFullScreen;
var id = useId();
var titleId = "modal-dialog-title-".concat(id);
var defaultTestId = testId || 'modal-dialog';
useEffect(function () {
// Modal dialogs can appear on top of iframe elements that are on another domain.
// There is a Chrome bug where drag and drop in an element on top of a cross domain
// iframe is not working. We are applying the workaround for this bug in modal so
// that consumers of our modal don't have to worry about this bug and are free to
// create whatever drag and drop experience they like inside a modal
//
// Chrome bug: https://issues.chromium.org/issues/362301053
return combine(disableDraggingToCrossOriginIFramesForElement(), disableDraggingToCrossOriginIFramesForTextSelection(), disableDraggingToCrossOriginIFramesForExternal());
}, []);
useAutoFocus(_typeof(autoFocus) === 'object' ? autoFocus : undefined,
// When a user supplies a ref to focus we enable this hook
_typeof(autoFocus) === 'object');
var _useOnMotionFinish = useOnMotionFinish({
onOpenComplete: onOpenComplete,
onCloseComplete: onCloseComplete
}),
_useOnMotionFinish2 = _slicedToArray(_useOnMotionFinish, 2),
motionRef = _useOnMotionFinish2[0],
onMotionFinish = _useOnMotionFinish2[1];
var modalDialogContext = useMemo(function () {
return {
testId: defaultTestId,
titleId: titleId,
onClose: onClose,
hasProvidedOnClose: hasProvidedOnClose,
isFullScreen: isFullScreen
};
}, [defaultTestId, titleId, onClose, hasProvidedOnClose, isFullScreen]);
useCloseOnEscapePress({
onClose: onClose,
isDisabled: !shouldCloseOnEscapePress
});
var _useLayering = useLayering(),
currentLevel = _useLayering.currentLevel;
return /*#__PURE__*/React.createElement(Positioner, {
stackIndex: stackIndex,
shouldScrollInViewport: shouldScrollInViewport,
testId: defaultTestId,
isFullScreen: isFullScreen
}, /*#__PURE__*/React.createElement(ModalContext.Provider, {
value: modalDialogContext
}, /*#__PURE__*/React.createElement(ScrollContext.Provider, {
value: shouldScrollInViewport
}, /*#__PURE__*/React.createElement(FadeIn
/**
* We don't want a 'slide in' for the full screen modals.
*/, {
entranceDirection: isFullScreen ? undefined : 'bottom',
onFinish: onMotionFinish
}, function (bottomFadeInProps) {
return /*#__PURE__*/React.createElement("section", _extends({}, bottomFadeInProps, {
"aria-label": label,
ref: mergeRefs([bottomFadeInProps.ref, motionRef]),
style: {
// eslint-disable-next-line @atlaskit/ui-styling-standard/no-imported-style-values -- Ignored via go/DSP-18766
'--modal-dialog-width': dialogWidth(width),
// eslint-disable-next-line @atlaskit/ui-styling-standard/no-imported-style-values -- Ignored via go/DSP-18766
'--modal-dialog-height': dialogHeight(height)
},
// eslint-disable-next-line @atlaskit/ui-styling-standard/no-classname-prop
className: ax([dialogStyles.root, !isFullScreen && dialogStyles.borderRadius, shouldScrollInViewport ? "_1tke1kxc _c71lglyw _8kn617ks" : "_cbiz17ks _bolh1kw7", bottomFadeInProps.className]),
role: "dialog",
"aria-labelledby": label ? undefined : titleId,
"data-testid": defaultTestId,
"data-modal-stack": stackIndex,
tabIndex: -1,
"aria-modal": true,
"data-ds--level": currentLevel
}), children);
}))));
};
// eslint-disable-next-line @repo/internal/react/require-jsdoc
export default ModalDialog;