@braineet/ui
Version:
Braineet design system
194 lines • 8.32 kB
JavaScript
var _excluded = ["animation", "blockScrollOnMount", "children", "footer", "finalFocusRef", "initialFocusRef", "isCentered", "size", "overlayStyles", "overlayEvents", "modalWrapperChildren", "modalInnerStyles", "modalInnerProps"],
_excluded2 = ["animation", "blockScrollOnMount", "children", "footer", "initialFocusRef", "finalFocusRef", "isCentered", "size", "overlayStyles", "overlayEvents", "modalWrapperChildren", "modalInnerStyles", "modalInnerProps", "scrollBehavior"];
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
import React, { useRef } from 'react';
import FocusLock from 'react-focus-lock';
import Portal from '../portal';
import { Fade, None, ScaleFade, Slide } from '../transition';
import { ModalCloseButton, ModalHeader, ModalBody, ModalFooter } from './components';
import { ModalProvider, useModalContext } from './context';
import { ModalWrapper, ModalInner, ModalContentInner, ModalOverlay } from './styles';
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
export var destroyFns = [];
export var OriginalModal = /*#__PURE__*/React.memo(function (_ref) {
var animation = _ref.animation,
blockScrollOnMount = _ref.blockScrollOnMount,
_children = _ref.children,
footer = _ref.footer,
finalFocusRef = _ref.finalFocusRef,
initialFocusRef = _ref.initialFocusRef,
isCentered = _ref.isCentered,
size = _ref.size,
overlayStyles = _ref.overlayStyles,
overlayEvents = _ref.overlayEvents,
modalWrapperChildren = _ref.modalWrapperChildren,
modalInnerStyles = _ref.modalInnerStyles,
modalInnerProps = _ref.modalInnerProps,
restProps = _objectWithoutPropertiesLoose(_ref, _excluded);
var modalContext = useModalContext();
var _modalContext$restPro = _extends({}, modalContext, restProps),
modalRef = _modalContext$restPro.modalRef,
modalInnerRef = _modalContext$restPro.modalInnerRef,
autoFocus = _modalContext$restPro.autoFocus,
componentsProps = _modalContext$restPro.componentsProps,
isOpen = _modalContext$restPro.isOpen,
noClose = _modalContext$restPro.noClose,
okLoading = _modalContext$restPro.okLoading,
standalone = _modalContext$restPro.standalone,
variant = _modalContext$restPro.variant,
scrollBehavior = _modalContext$restPro.scrollBehavior,
onClose = _modalContext$restPro.onClose,
closeOnOverlayClick = _modalContext$restPro.closeOnOverlayClick,
width = _modalContext$restPro.width,
modalID = _modalContext$restPro.modalID,
labelID = _modalContext$restPro.labelID,
title = _modalContext$restPro.title;
var onActivation = React.useCallback(function () {
var animationDiration = !animation || animation && animation.name === 'none' ? 0 : 250;
setTimeout(function () {
if (initialFocusRef && initialFocusRef.current) {
initialFocusRef.current.focus();
}
}, animationDiration);
}, [initialFocusRef]);
var onDeactivation = React.useCallback(function () {
if (finalFocusRef && finalFocusRef.current) {
finalFocusRef.current.focus();
}
}, [finalFocusRef]);
var onBackdrop = function onBackdrop(e) {
e.stopPropagation();
if ((modalRef == null ? void 0 : modalRef.current) !== e.target) return;
if (closeOnOverlayClick) {
onClose == null || onClose();
}
};
var animationComponents = {
none: None,
fade: Fade,
scaleFade: ScaleFade,
slide: Slide
};
var AnimationWrapper = animation && animation.name ? animationComponents[animation.name] : animationComponents.none;
return /*#__PURE__*/_jsxs(_Fragment, {
children: [/*#__PURE__*/_jsx(ModalOverlay, {
overlayStyles: overlayStyles,
"aria-hidden": "true"
}), /*#__PURE__*/_jsx(AnimationWrapper, _extends({
show: isOpen
}, animation, {
children: function children(styles) {
return /*#__PURE__*/_jsxs(ModalWrapper, _extends({
ref: modalRef,
variant: variant,
isCentered: isCentered,
scrollBehavior: scrollBehavior,
onClick: onBackdrop,
"aria-labelledby": labelID,
"aria-modal": "true",
role: "dialog"
}, overlayEvents, {
children: [modalWrapperChildren || null, /*#__PURE__*/_jsx(ModalInner, _extends({
id: modalID,
size: size,
$width: width,
style: styles,
$modalInnerStyles: modalInnerStyles,
$scrollBehavior: scrollBehavior,
role: "document",
$standalone: standalone
}, modalInnerProps, {
children: /*#__PURE__*/_jsx(FocusLock, {
ref: modalInnerRef,
autoFocus: variant !== 'confirmation' && autoFocus && !okLoading,
disabled: !autoFocus,
onActivation: onActivation,
onDeactivation: onDeactivation,
className: "focusLockModal",
children: standalone ? /*#__PURE__*/_jsxs(_Fragment, {
children: [/*#__PURE__*/_jsx("p", {
id: labelID,
className: "sr-only",
children: title
}), _children]
}) : /*#__PURE__*/_jsxs(_Fragment, {
children: [variant !== 'confirmation' && !noClose && /*#__PURE__*/_jsx(ModalCloseButton, {}), /*#__PURE__*/_jsxs(ModalContentInner, _extends({
variant: variant
}, componentsProps.ModalContentInner, {
children: [/*#__PURE__*/_jsx(ModalHeader, {}), /*#__PURE__*/_jsx(ModalBody, {
children: _children
}), footer !== undefined ? footer : /*#__PURE__*/_jsx(ModalFooter, {})]
}))]
})
})
}))]
}));
}
}))]
});
});
export { ModalCloseButton, ModalHeader, ModalBody, ModalFooter };
var Modal = /*#__PURE__*/React.forwardRef(function (_ref2, ref) {
var animation = _ref2.animation,
blockScrollOnMount = _ref2.blockScrollOnMount,
children = _ref2.children,
footer = _ref2.footer,
initialFocusRef = _ref2.initialFocusRef,
finalFocusRef = _ref2.finalFocusRef,
isCentered = _ref2.isCentered,
size = _ref2.size,
overlayStyles = _ref2.overlayStyles,
overlayEvents = _ref2.overlayEvents,
modalWrapperChildren = _ref2.modalWrapperChildren,
modalInnerStyles = _ref2.modalInnerStyles,
modalInnerProps = _ref2.modalInnerProps,
scrollBehavior = _ref2.scrollBehavior,
providerProps = _objectWithoutPropertiesLoose(_ref2, _excluded2);
var modalRef = useRef(null);
var modalInnerRef = useRef(null);
React.useImperativeHandle(ref, function () {
return modalRef.current;
});
var modalProps = {
animation: animation,
blockScrollOnMount: blockScrollOnMount,
children: children,
footer: footer,
initialFocusRef: initialFocusRef,
finalFocusRef: finalFocusRef,
isCentered: isCentered,
size: size,
overlayStyles: overlayStyles,
overlayEvents: overlayEvents,
modalWrapperChildren: modalWrapperChildren,
modalInnerStyles: modalInnerStyles,
modalInnerProps: modalInnerProps,
scrollBehavior: scrollBehavior
};
return /*#__PURE__*/_jsx(Portal, {
children: /*#__PURE__*/_jsx(ModalProvider, _extends({
blockScrollOnMount: blockScrollOnMount,
scrollBehavior: scrollBehavior,
modalRef: modalRef,
modalInnerRef: modalInnerRef
}, providerProps, {
children: /*#__PURE__*/_jsx(OriginalModal, _extends({}, modalProps))
}))
});
});
var defaultProps = {
blockScrollOnMount: true,
size: 'xl',
animation: {
name: 'scaleFade',
from: 'top'
},
modalWrapperChildren: null,
modalInnerStyles: {},
overlayEvents: {},
scrollBehavior: 'outside'
};
OriginalModal.defaultProps = defaultProps;
Modal.defaultProps = defaultProps;
export default /*#__PURE__*/React.memo(Modal);