@atlaskit/modal-dialog
Version:
A modal dialog displays content that requires user interaction, in a layer above the page.
174 lines (172 loc) • 8.77 kB
JavaScript
/* modal-dialog.tsx generated by @compiled/babel-plugin v0.39.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 { cx } from '@atlaskit/css';
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 { Motion } from '@atlaskit/motion';
import FadeIn from '@atlaskit/motion/fade-in';
import { fg } from '@atlaskit/platform-feature-flags';
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 { width } from '../width';
import Positioner from './positioner';
var dialogWidth = function dialogWidth(input) {
if (!input) {
return 'auto';
}
var isWidthName = width.values.indexOf(input.toString()) !== -1;
var widthName = isWidthName && input;
if (widthName) {
return "".concat(width.widths[widthName], "px");
}
return typeof input === 'number' ? "".concat(input, "px") : input;
};
var dialogHeight = function dialogHeight(input) {
if (!input) {
return 'auto';
}
return typeof input === 'number' ? "".concat(input, "px") : input;
};
var LOCAL_CURRENT_SURFACE_CSS_VAR = '--ds-elevation-surface-current';
var dialogStyles = {
root: "_16jlidpf _1o9zkb7n _i0dl1wug _1e0c1txw _1bsb1osq _p12fauwl _4t3i1osq _1tkeidpf _c71l1kxc _2lx21bp4 _bfhk1bhr _syazi7uo _1q1l1bhr _lcxv1wug _1mq81kw7 _m01u1kw7 _1dg11kw7 _mizu1v1w _1ah3dkaa _ra3xnqa1 _128mdkaa _1cvmnqa1 _4davt94y _we1i1kw7 _zg7p130s _yakv1kw7 _4lht1kw7 _1xlt6m9r",
borderRadius: "_epkxfajl",
borderRadiusT26Shape: "_epkxpb1k",
motion: "_4t3i1osq _c71l1kw7 _p12f1kw7 _1jyk1wug _yakv1kw7 _4lht1kw7",
fullscreen: "_1jyk1osq"
};
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,
providedAutoFocus = 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';
// https://product-fabric.atlassian.net/browse/DSP-24307
// If flag and falsy, use true instead.
// When we remove boolean `autoFocus`, we won't have to worry about this
var autoFocus = !providedAutoFocus ? true : providedAutoFocus;
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
}, fg('platform-dst-motion-uplift') ? /*#__PURE__*/React.createElement(Motion, {
enteringAnimation: "var(--ds-modal-enter, 200ms cubic-bezier(0.4, 0, 0, 1) ScaleIn95, 200ms cubic-bezier(0.4, 0, 0, 1) FadeIn)",
exitingAnimation: "var(--ds-modal-exit, 200ms cubic-bezier(0.4, 1, 0.6, 1) ScaleOut95, 200ms cubic-bezier(0.4, 1, 0.6, 1) FadeOut)",
onFinish: onMotionFinish,
xcss: cx(dialogStyles.motion, isFullScreen && dialogStyles.fullscreen)
}, /*#__PURE__*/React.createElement("section", {
"aria-label": label,
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)
},
role: "dialog",
"aria-labelledby": label ? undefined : titleId,
"data-testid": defaultTestId,
"data-modal-stack": stackIndex,
tabIndex: -1,
"aria-modal": true,
"data-ds--level": currentLevel,
className: ax([dialogStyles.root, !isFullScreen && dialogStyles.borderRadius, !isFullScreen && fg('platform-dst-shape-theme-default') && dialogStyles.borderRadiusT26Shape, shouldScrollInViewport ? "_1tke1kxc _c71lglyw _8kn617ks" : "_cbiz17ks _bolh1kw7"])
}, children)) : /*#__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, !isFullScreen && fg('platform-dst-shape-theme-default') && dialogStyles.borderRadiusT26Shape, 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;