UNPKG

@amaui/ui-react

Version:
147 lines (146 loc) 9.32 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 __1 = require(".."); const Slide = 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.amauiSlide) === null || _g === void 0 ? void 0 : _g.props) === null || _h === void 0 ? void 0 : _h.default), props_)); }, [props_]); const { in: inProp, prefix, run, append, add, enter, exit, enterOnAdd, exitOnAdd, noAbruption, removeOnExited, duration: duration_, onTransition, onAppended, onAdd, onAdding, onAdded, onEnter, onEntering, onEntered, onExit, onExiting, onExited, onRemoved, root, min, direction = 'bottom', timing_function, addTransition, delay, className, style, children } = props, other = __rest(props, ["in", "prefix", "run", "append", "add", "enter", "exit", "enterOnAdd", "exitOnAdd", "noAbruption", "removeOnExited", "duration", "onTransition", "onAppended", "onAdd", "onAdding", "onAdded", "onEnter", "onEntering", "onEntered", "onExit", "onExiting", "onExited", "onRemoved", "root", "min", "direction", "timing_function", "addTransition", "delay", "className", "style", "children"]); const refs = { root: react_1.default.useRef(undefined), rect: react_1.default.useRef(undefined) }; const translate = (useMin = false) => { var _a, _b, _c, _d, _e, _f, _g, _h; const rect = !removeOnExited ? refs.rect.current : (_b = (_a = refs.root) === null || _a === void 0 ? void 0 : _a.current) === null || _b === void 0 ? void 0 : _b.getBoundingClientRect(); const { width = 0, height = 0 } = rect || {}; const h = root ? root.offsetHeight : window.innerHeight; const w = root ? root.offsetWidth : window.innerWidth; const top = root ? (_c = refs.root.current) === null || _c === void 0 ? void 0 : _c.offsetTop : rect === null || rect === void 0 ? void 0 : rect.top; const left = root ? (_d = refs.root.current) === null || _d === void 0 ? void 0 : _d.offsetLeft : rect === null || rect === void 0 ? void 0 : rect.left; const right = root ? ((_e = refs.root.current) === null || _e === void 0 ? void 0 : _e.offsetLeft) + ((_f = refs.root.current) === null || _f === void 0 ? void 0 : _f.offsetWidth) : rect === null || rect === void 0 ? void 0 : rect.right; const bottom = root ? ((_g = refs.root.current) === null || _g === void 0 ? void 0 : _g.offsetTop) + ((_h = refs.root.current) === null || _h === void 0 ? void 0 : _h.offsetHeight) : rect === null || rect === void 0 ? void 0 : rect.bottom; let toAdd = 0; if (min !== undefined && useMin) toAdd = min; if (direction === 'top') return `translate(0, ${bottom !== undefined ? ((!inProp && bottom < height) ? -height : -bottom) + toAdd + 'px' : '-100vh'})`; if (direction === 'left') return `translate(${right !== undefined ? ((!inProp && right < width) ? -width : -right) + toAdd + 'px' : '-100vw'}, 0)`; if (direction === 'right') return `translate(${left !== undefined ? ((!inProp && Math.abs(w - left) < width) ? width : Math.abs(w - left)) - toAdd + 'px' : '100vw'}, 0)`; if (direction === 'bottom') return `translate(0, ${top !== undefined ? ((!inProp && Math.abs(h - top) < height) ? height : Math.abs(h - top)) - toAdd + 'px' : '100vh'})`; }; const styles = (status) => { var _a; if (!status) return { visibility: 'hidden' }; const { transform = 'none' } = (refs.root.current && window.getComputedStyle((_a = refs.root) === null || _a === void 0 ? void 0 : _a.current)) || {}; const translateValueMin = translate(true); const translateValue = translate(); const other_ = {}; if (min === undefined) { other_.visibility = 'hidden'; } const allStyles = { appended: { visibility: 'hidden' }, add: Object.assign({ transition: 'none', transform: translateValue }, other_), adding: { transform: 'translate(0, 0)', visibility: 'visible' }, added: { transform: 'none', visibility: 'visible' }, enter: Object.assign({ transition: 'none', transform: min !== undefined ? transform : translateValue }, other_), entering: { transform: 'translate(0, 0)', visibility: 'visible' }, entered: { transform: 'none', visibility: 'visible' }, exit: { transform, visibility: 'visible' }, exiting: { transform: translateValueMin, visibility: 'visible' }, exited: Object.assign({ transform: min !== undefined ? translateValueMin : transform }, other_) }; return allStyles[status]; }; const duration = (status, property = 'transform') => { const properties = { transform: theme.transitions.duration.xs }; return `${((0, utils_1.is)('simple', duration) ? duration : duration[status]) || properties[property]}ms`; }; const timingFunction = status => ((0, utils_1.is)('simple', timing_function) ? timing_function : timing_function[status]) || theme.transitions.timing_function.standard; return ((0, jsx_runtime_1.jsx)(__1.Transition, Object.assign({ append: true, removeOnExited: true, onAppended: element => { refs.rect.current = element === null || element === void 0 ? void 0 : element.getBoundingClientRect(); }, onEnter: element => { if (min === undefined) { element.style.visibility = 'hidden'; element.style.transition = 'none'; element.style.transform = 'none'; refs.rect.current = element === null || element === void 0 ? void 0 : element.getBoundingClientRect(); // Clean up element.style.removeProperty('transition'); } }, onExit: element => { refs.rect.current = element === null || element === void 0 ? void 0 : element.getBoundingClientRect(); } }, props, { children: (status, ref_) => { var _a, _b; return react_1.default.cloneElement(children, Object.assign(Object.assign({}, other), { ref: (item) => { refs.root.current = item; if (ref) { if ((0, utils_1.is)('function', ref)) ref(item); else ref.current = item; } if (ref_) { if ((0, utils_1.is)('function', ref_)) ref_(item); else ref_.current = item; } if (children.ref) { if ((0, utils_1.is)('function', children.ref)) children.ref(item); else children.ref.current = item; } }, className: (0, style_react_1.classNames)([ className, (_a = children === null || children === void 0 ? void 0 : children.props) === null || _a === void 0 ? void 0 : _a.className ]), style: Object.assign(Object.assign(Object.assign({ transition: `transform ${duration(status)} ${timingFunction(status)} ${addTransition ? `, ${addTransition}` : ''}` }, styles(status)), (_b = children === null || children === void 0 ? void 0 : children.props) === null || _b === void 0 ? void 0 : _b.style), style) })); } }))); }); Slide.displayName = 'amaui-Slide'; exports.default = Slide;