UNPKG

@onesy/ui-react

Version:
145 lines 5.63 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(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } import React from 'react'; import { is, isEnvironment } from '@onesy/utils'; import { style as styleMethod, classNames, useOnesyTheme } from '@onesy/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: 'onesy-Backdrop' }); let BACKDROPS_OPEN = 0; const Backdrop = /*#__PURE__*/React.forwardRef((props_, ref) => { const theme = useOnesyTheme(); const props = React.useMemo(() => _objectSpread(_objectSpread(_objectSpread({}, theme?.ui?.elements?.all?.props?.default), theme?.ui?.elements?.onesyBackdrop?.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) && ['onesy-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) && ['onesy-Backdrop-background'], classes.background, invisible && classes.invisible]), onClick: () => !disableBackgroundClose && onClose() })), /*#__PURE__*/React.createElement("div", { className: classNames([staticClassName('Backdrop', theme) && ['onesy-Backdrop-backdrop-root'], classes.backdropRoot]) }, /*#__PURE__*/React.createElement(ModalComponent, _extends({ in: inProp, onExited: onExited, add: true }, ModalProps), children)))); }); Backdrop.displayName = 'onesy-Backdrop'; export default Backdrop;