UNPKG

@amaui/ui-react

Version:
145 lines 5.78 kB
import _extends from "@babel/runtime/helpers/extends"; import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; const _excluded = ["open", "invisible", "disableKeyboardClose", "disableBackgroundClose", "BackgroundComponent", "ModalComponent", "BackgroundProps", "ModalProps", "onClose", "Component", "className", "children"]; function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; } import React from 'react'; import { is, isEnvironment } from '@amaui/utils'; import { style as styleMethod, classNames, useAmauiTheme } from '@amaui/style-react'; import LineElement from '../Line'; import FocusElement from '../Focus'; import FadeElement from '../Fade'; import { staticClassName } from '../utils'; const useStyle = styleMethod(theme => ({ root: { position: 'fixed', inset: '0px', zIndex: theme.z_index.modal }, background: { position: 'absolute', inset: '0px', width: '100%', height: '100%', background: 'rgba(0, 0, 0, 0.44)', zIndex: -1 }, invisible: { background: 'transparent' }, backdropRoot: { position: 'relative', height: '100%', display: 'flex', alignItems: 'center', justifyContent: 'center' } }), { name: 'amaui-Backdrop' }); let BACKDROPS_OPEN = 0; const Backdrop = /*#__PURE__*/React.forwardRef((props_, ref) => { const theme = useAmauiTheme(); const props = React.useMemo(() => _objectSpread(_objectSpread(_objectSpread({}, theme?.ui?.elements?.all?.props?.default), theme?.ui?.elements?.amauiBackdrop?.props?.default), props_), [props_]); const Line = React.useMemo(() => theme?.elements?.Line || LineElement, [theme]); const Fade = React.useMemo(() => theme?.elements?.Fade || FadeElement, [theme]); const Focus = React.useMemo(() => theme?.elements?.Focus || FocusElement, [theme]); const { open: open_, invisible, disableKeyboardClose, disableBackgroundClose, BackgroundComponent = Fade, ModalComponent = Fade, BackgroundProps, ModalProps, onClose: onClose_, Component = 'div', className, children } = props, other = _objectWithoutProperties(props, _excluded); const [open, setOpen] = React.useState(open_); const [inProp, setInProp] = React.useState(open_); const { classes } = useStyle(); const refs = { root: React.useRef(undefined), focus: React.useRef(undefined) }; const onClose = () => { if (is('function', onClose_) && open) onClose_(); }; const onKeyDown = event => { if (event.key === 'Escape' && !disableKeyboardClose) { event.stopPropagation(); onClose(); } }; const backdrop = { open: () => { BACKDROPS_OPEN++; const rootDocument = isEnvironment('browser') ? refs.root.current?.ownerDocument || window.document : undefined; rootDocument.body.style.overflow = 'hidden'; }, close: () => { BACKDROPS_OPEN--; const rootDocument = isEnvironment('browser') ? refs.root.current?.ownerDocument || window.document : undefined; if (!BACKDROPS_OPEN) rootDocument.body.style.removeProperty('overflow'); } }; React.useEffect(() => { if (open) backdrop.open(); return () => { if (open) backdrop.close(); }; }, []); React.useEffect(() => { if (open_ && !open) { setOpen(true); setInProp(true); backdrop.open(); } else if (!open_ && open) setInProp(false); }, [open_]); React.useEffect(() => { if (open) refs.focus.current.focus(); }, [open]); const onExited = () => { setOpen(false); backdrop.close(); }; if (!open) return null; return /*#__PURE__*/React.createElement(Line, _extends({ ref: item => { if (ref) { if (is('function', ref)) ref(item);else ref.current = item; } refs.root.current = item; }, gap: 0, direction: "row", align: "center", justify: "center", Component: Component, className: classNames([staticClassName('Backdrop', theme) && ['amaui-Backdrop-root'], className, classes.root]) }, other), /*#__PURE__*/React.createElement(Focus, { ref: refs.focus, onKeyDown: onKeyDown }, /*#__PURE__*/React.createElement(BackgroundComponent, _extends({ in: inProp, add: true }, BackgroundProps), /*#__PURE__*/React.createElement("div", { className: classNames([staticClassName('Backdrop', theme) && ['amaui-Backdrop-background'], classes.background, invisible && classes.invisible]), onClick: () => !disableBackgroundClose && onClose() })), /*#__PURE__*/React.createElement("div", { className: classNames([staticClassName('Backdrop', theme) && ['amaui-Backdrop-backdrop-root'], classes.backdropRoot]) }, /*#__PURE__*/React.createElement(ModalComponent, _extends({ in: inProp, onExited: onExited, add: true }, ModalProps), children)))); }); Backdrop.displayName = 'amaui-Backdrop'; export default Backdrop;