UNPKG

@amaui/ui-react

Version:
146 lines (145 loc) 7.47 kB
"use strict"; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const jsx_runtime_1 = require("react/jsx-runtime"); const react_1 = __importDefault(require("react")); const utils_1 = require("@amaui/utils"); const style_react_1 = require("@amaui/style-react"); const Line_1 = __importDefault(require("../Line")); const Focus_1 = __importDefault(require("../Focus")); const Fade_1 = __importDefault(require("../Fade")); const utils_2 = require("../utils"); const useStyle = (0, style_react_1.style)(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 = react_1.default.forwardRef((props_, ref) => { const theme = (0, style_react_1.useAmauiTheme)(); const props = react_1.default.useMemo(() => { var _a, _b, _c, _d, _e, _f, _g, _h; return (Object.assign(Object.assign(Object.assign({}, (_d = (_c = (_b = (_a = theme === null || theme === void 0 ? void 0 : theme.ui) === null || _a === void 0 ? void 0 : _a.elements) === null || _b === void 0 ? void 0 : _b.all) === null || _c === void 0 ? void 0 : _c.props) === null || _d === void 0 ? void 0 : _d.default), (_h = (_g = (_f = (_e = theme === null || theme === void 0 ? void 0 : theme.ui) === null || _e === void 0 ? void 0 : _e.elements) === null || _f === void 0 ? void 0 : _f.amauiBackdrop) === null || _g === void 0 ? void 0 : _g.props) === null || _h === void 0 ? void 0 : _h.default), props_)); }, [props_]); const Line = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.Line) || Line_1.default; }, [theme]); const Fade = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.Fade) || Fade_1.default; }, [theme]); const Focus = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.Focus) || Focus_1.default; }, [theme]); const { open: open_, invisible, disableKeyboardClose, disableBackgroundClose, BackgroundComponent = Fade, ModalComponent = Fade, BackgroundProps, ModalProps, onClose: onClose_, Component = 'div', className, children } = props, other = __rest(props, ["open", "invisible", "disableKeyboardClose", "disableBackgroundClose", "BackgroundComponent", "ModalComponent", "BackgroundProps", "ModalProps", "onClose", "Component", "className", "children"]); const [open, setOpen] = react_1.default.useState(open_); const [inProp, setInProp] = react_1.default.useState(open_); const { classes } = useStyle(); const refs = { root: react_1.default.useRef(undefined), focus: react_1.default.useRef(undefined) }; const onClose = () => { if ((0, utils_1.is)('function', onClose_) && open) onClose_(); }; const onKeyDown = (event) => { if (event.key === 'Escape' && !disableKeyboardClose) { event.stopPropagation(); onClose(); } }; const backdrop = { open: () => { var _a; BACKDROPS_OPEN++; const rootDocument = (0, utils_1.isEnvironment)('browser') ? (((_a = refs.root.current) === null || _a === void 0 ? void 0 : _a.ownerDocument) || window.document) : undefined; rootDocument.body.style.overflow = 'hidden'; }, close: () => { var _a; BACKDROPS_OPEN--; const rootDocument = (0, utils_1.isEnvironment)('browser') ? (((_a = refs.root.current) === null || _a === void 0 ? void 0 : _a.ownerDocument) || window.document) : undefined; if (!BACKDROPS_OPEN) rootDocument.body.style.removeProperty('overflow'); } }; react_1.default.useEffect(() => { if (open) backdrop.open(); return () => { if (open) backdrop.close(); }; }, []); react_1.default.useEffect(() => { if (open_ && !open) { setOpen(true); setInProp(true); backdrop.open(); } else if (!open_ && open) setInProp(false); }, [open_]); react_1.default.useEffect(() => { if (open) refs.focus.current.focus(); }, [open]); const onExited = () => { setOpen(false); backdrop.close(); }; if (!open) return null; return ((0, jsx_runtime_1.jsx)(Line, Object.assign({ ref: item => { if (ref) { if ((0, utils_1.is)('function', ref)) ref(item); else ref.current = item; } refs.root.current = item; }, gap: 0, direction: 'row', align: 'center', justify: 'center', Component: Component, className: (0, style_react_1.classNames)([ (0, utils_2.staticClassName)('Backdrop', theme) && [ 'amaui-Backdrop-root' ], className, classes.root ]) }, other, { children: (0, jsx_runtime_1.jsxs)(Focus, Object.assign({ ref: refs.focus, onKeyDown: onKeyDown }, { children: [(0, jsx_runtime_1.jsx)(BackgroundComponent, Object.assign({ in: inProp, add: true }, BackgroundProps, { children: (0, jsx_runtime_1.jsx)("div", { className: (0, style_react_1.classNames)([ (0, utils_2.staticClassName)('Backdrop', theme) && [ 'amaui-Backdrop-background' ], classes.background, invisible && classes.invisible ]), onClick: () => !disableBackgroundClose && onClose() }) })), (0, jsx_runtime_1.jsx)("div", Object.assign({ className: (0, style_react_1.classNames)([ (0, utils_2.staticClassName)('Backdrop', theme) && [ 'amaui-Backdrop-backdrop-root' ], classes.backdropRoot ]) }, { children: (0, jsx_runtime_1.jsx)(ModalComponent, Object.assign({ in: inProp, onExited: onExited, add: true }, ModalProps, { children: children })) }))] })) }))); }); Backdrop.displayName = 'amaui-Backdrop'; exports.default = Backdrop;