UNPKG

@amaui/ui-react

Version:
223 lines (222 loc) 11.4 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 Modal_1 = __importDefault(require("../Modal")); const Slide_1 = __importDefault(require("../Slide")); const useSwipe_1 = __importDefault(require("../useSwipe")); const utils_2 = require("../utils"); const useStyle = (0, style_react_1.style)(theme => ({ root: { '& .amaui-Modal-surface': { display: 'flex', flexDirection: 'column', margin: '0', maxHeight: 'unset', maxWidth: 'unset', minWidth: 'unset', flex: '1 0 auto', padding: '0' } }, position_fixed: { position: 'fixed' }, position_absolute: { position: 'absolute' }, version_modal_position_fixed: { '& .amaui-Modal-surface': { position: 'fixed' } }, version_modal_position_absolute: { '& .amaui-Modal-surface': { position: 'absolute' } }, version_standard: { '&.amaui-Modal-root': { position: 'unset', inset: 'unset' }, '& .amaui-Modal-surface': { position: 'relative' } }, direction_top: { '& .amaui-Modal-surface': { top: '0', width: '100%', borderRadius: `0 0 ${theme.methods.shape.radius.value(2, 'px')} ${theme.methods.shape.radius.value(2, 'px')}` } }, direction_left: { '& .amaui-Modal-surface': { left: '0', height: '100%', borderRadius: `0 ${theme.methods.shape.radius.value(2, 'px')} ${theme.methods.shape.radius.value(2, 'px')} 0` } }, direction_right: { '& .amaui-Modal-surface': { right: '0', height: '100%', borderRadius: `${theme.methods.shape.radius.value(2, 'px')} 0 0 ${theme.methods.shape.radius.value(2, 'px')}` } }, direction_bottom: { '& .amaui-Modal-surface': { bottom: '0', width: '100%', borderRadius: `${theme.methods.shape.radius.value(2, 'px')} ${theme.methods.shape.radius.value(2, 'px')} 0 0` } } }), { name: 'amaui-NavigationDrawer' }); const NavigationDrawer = 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.amauiNavigationDrawer) === null || _g === void 0 ? void 0 : _g.props) === null || _h === void 0 ? void 0 : _h.default), props_)); }, [props_]); const Modal = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.Modal) || Modal_1.default; }, [theme]); const Slide = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.Slide) || Slide_1.default; }, [theme]); const { tonal = false, color = [undefined, 'modal'].includes(props.version) ? 'themed' : 'default', version = 'modal', open: open_, openDefault, position = 'fixed', direction: direction_ = 'left', swipe = true, swipeTouchAnywhere = true, swipeBackgroundFollow = true, min, removeOnExited, onClose: onClose_, TransitionComponentProps = {}, Component = 'nav', className, children } = props, other = __rest(props, ["tonal", "color", "version", "open", "openDefault", "position", "direction", "swipe", "swipeTouchAnywhere", "swipeBackgroundFollow", "min", "removeOnExited", "onClose", "TransitionComponentProps", "Component", "className", "children"]); const [entered, setEntered] = react_1.default.useState(false); const [open, setOpen] = react_1.default.useState(false); const refs = { modal: react_1.default.useRef(undefined), background: react_1.default.useRef(undefined) }; const { classes } = useStyle(); let direction = direction_; if (theme.direction === 'rtl') { if (direction === 'left') direction = 'right'; else if (direction === 'right') direction = 'left'; } let swipeValue; if (swipe) { const swipeOptions = { open, min, direction, touchAnywhere: swipeTouchAnywhere }; swipeValue = (0, useSwipe_1.default)(refs.modal.current, swipeOptions); } react_1.default.useEffect(() => { if (open_ !== open) setOpen(open_); }, [open_]); react_1.default.useEffect(() => { if (swipeValue && version === 'modal') { const valueSwipe = swipeValue.value; const valuePercentageSwipe = (0, utils_1.clamp)(swipeValue.valuePercentage, 0, 100); const position_ = swipeValue.position; if (position_ !== undefined) { let value_; if (direction === 'top') value_ = `translateY(${valueSwipe}px)`; if (direction === 'left') value_ = `translateX(${valueSwipe}px)`; if (direction === 'right') value_ = `translateX(${valueSwipe}px)`; if (direction === 'bottom') value_ = `translateY(${valueSwipe}px)`; // Add transitions if (refs.modal.current) { refs.modal.current.style.transition = theme.methods.transitions.make('transform', { duration: 'xs' }); refs.modal.current.style.transform = value_; } if (refs.background.current) { refs.background.current.style.transition = theme.methods.transitions.make('opacity', { duration: 'xs' }); } if (position_ === 'min') { if (refs.background.current) { refs.background.current.style.opacity = '0'; } if (refs.modal.current) setTimeout(() => { onClose(); }, theme.transitions.duration.xs + 14); } if (position_ === 'max') { if (refs.background.current) { refs.background.current.style.opacity = '1'; } } } else { let value_ = ''; if (refs.modal.current) { if (direction === 'top') value_ = `translateY(${valueSwipe}px)`; if (direction === 'left') value_ = `translateX(${valueSwipe}px)`; if (direction === 'right') value_ = `translateX(${valueSwipe}px)`; if (direction === 'bottom') value_ = `translateY(${valueSwipe}px)`; // No transition refs.modal.current.style.transition = 'none'; refs.modal.current.style.transform = value_; } if (swipeBackgroundFollow && refs.background.current) { // No transition refs.background.current.style.transition = 'none'; refs.background.current.style.opacity = `${valuePercentageSwipe / 100}`; } else { // Add transition refs.background.current.style.transition = theme.methods.transitions.make('opacity', { duration: 'xs' }); } } } }, [version, swipeValue === null || swipeValue === void 0 ? void 0 : swipeValue.value, swipeValue === null || swipeValue === void 0 ? void 0 : swipeValue.position]); const onClose = react_1.default.useCallback(() => { if ((0, utils_1.is)('function', onClose_)) onClose_(); }, []); if (version === 'standard') { other.portal = other.portal !== undefined ? other.portal : false; other.freezeScroll = other.freezeScroll !== undefined ? other.freezeScroll : false; other.background = other.background !== undefined ? other.background : false; other.focus = other.focus !== undefined ? other.focus : false; other.disableKeyboardClose = other.disableKeyboardClose !== undefined ? other.disableKeyboardClose : true; } TransitionComponentProps.direction = TransitionComponentProps.direction !== undefined ? TransitionComponentProps.direction : direction; TransitionComponentProps.removeOnExited = TransitionComponentProps.removeOnExited !== undefined ? TransitionComponentProps.removeOnExited : min === undefined; if (min !== undefined) { other.freezeScroll = other.freezeScroll !== undefined ? other.freezeScroll : false; other.openDefault = false; TransitionComponentProps.min = TransitionComponentProps.min !== undefined ? TransitionComponentProps.min : min; } return ((0, jsx_runtime_1.jsx)(Modal, Object.assign({ ref: ref, open: open, mainRef: refs.modal, backgroundRef: refs.background, partialyOpened: min !== undefined, tonal: tonal, color: color, onClose: onClose, TransitionComponentProps: Object.assign({ onAdded: () => setEntered(true), onEntered: () => setEntered(true), onExited: () => setEntered(false), enterOnAdd: false }, TransitionComponentProps), TransitionComponent: Slide, Component: Component, portal: position === 'fixed', className: (0, style_react_1.classNames)([ (0, utils_2.staticClassName)('NavigationDrawer', theme) && [ 'amaui-NavigationDrawer-root' ], className, classes.root, classes[`position_${position}`], classes[`direction_${direction}`], classes[`version_${version}`], classes[`version_${version}_position_${position}`] ]) }, other, { children: children }))); }); NavigationDrawer.displayName = 'amaui-NavigationDrawer'; exports.default = NavigationDrawer;