UNPKG

@amaui/ui-react

Version:
192 lines (191 loc) 11.1 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 IconMaterialCloseW100_1 = __importDefault(require("@amaui/icons-material-rounded-react/IconMaterialCloseW100")); const Grow_1 = __importDefault(require("../Grow")); const Type_1 = __importDefault(require("../Type")); const Surface_1 = __importDefault(require("../Surface")); const IconButton_1 = __importDefault(require("../IconButton")); const Line_1 = __importDefault(require("../Line")); const utils_2 = require("../utils"); const useStyle = (0, style_react_1.style)(theme => ({ root: { minWidth: '340px', minHeight: '50px', borderRadius: `${theme.shape.radius.unit}px`, boxShadow: 'rgb(0 0 0 / 1%) 0px 4px 5px 0px, rgb(0 0 0 / 1%) 0px 1px 10px 0px, rgb(0 0 0 / 1%) 0px 2px 4px -1px' }, end: { marginInlineStart: 'auto' }, size_small: { padding: `${theme.methods.space.value(0.5, 'px')} ${theme.methods.space.value(2, 'px')}` }, size_regular: { padding: `${theme.methods.space.value(1, 'px')} ${theme.methods.space.value(2.5, 'px')}` }, size_large: { padding: `${theme.methods.space.value(1.5, 'px')} ${theme.methods.space.value(3, 'px')}` }, fixed: { position: 'fixed', zIndex: theme.z_index.modal }, position_top: { top: '24px' }, position_bottom: { bottom: '24px' }, alignment_left: { left: '24px', right: 'auto' }, alignment_start: { insetInlineStart: '24px', insetInlineEnd: 'auto' }, alignment_center: { left: '50%', right: 'auto', transform: 'translateX(-50%)' }, alignment_right: { right: '24px', left: 'auto' }, alignment_end: { insetInlineEnd: '24px', insetInlineStart: 'auto' } }), { name: 'amaui-Snackbar' }); const timeouts = {}; const Snackbar = 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.amauiSnackbar) === 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 Grow = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.Grow) || Grow_1.default; }, [theme]); const Type = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.Type) || Type_1.default; }, [theme]); const Surface = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.Surface) || Surface_1.default; }, [theme]); const IconButton = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.IconButton) || IconButton_1.default; }, [theme]); const { id = (0, utils_1.getID)(), tonal = true, color = 'primary', size = 'regular', elevation = 0, open = true, primary, end: end_, position = 'bottom', alignment = 'start', autoHide = true, autoHideDuration = 4000, fixed = props.open !== undefined, resumeFromStart = true, closeButton = true, onMouseEnter: onMouseEnter_, onMouseLeave: onMouseLeave_, onClose: onClose_, IconClose = IconMaterialCloseW100_1.default, TransitionComponent = Grow, IconProps, IconButtonProps, TransitionComponentProps, Component = 'div', className, children } = props, other = __rest(props, ["id", "tonal", "color", "size", "elevation", "open", "primary", "end", "position", "alignment", "autoHide", "autoHideDuration", "fixed", "resumeFromStart", "closeButton", "onMouseEnter", "onMouseLeave", "onClose", "IconClose", "TransitionComponent", "IconProps", "IconButtonProps", "TransitionComponentProps", "Component", "className", "children"]); const { classes } = useStyle(); const refs = { root: react_1.default.useRef(undefined), timeoutStart: react_1.default.useRef(undefined), timeoutLeftOver: react_1.default.useRef(undefined), autoHideDuration: react_1.default.useRef(undefined) }; refs.autoHideDuration.current = autoHideDuration; const end = react_1.default.Children.toArray(end_); const onClose = react_1.default.useCallback(() => { if ((0, utils_1.is)('function', onClose_)) onClose_(); }, [onClose_]); const addTimeout = react_1.default.useCallback((value = autoHideDuration) => { clearTimeout(timeouts[id]); timeouts[id] = setTimeout(() => onClose(), value); refs.timeoutStart.current = new Date().getTime(); }, [autoHideDuration, timeouts, onClose]); const removeTimeout = react_1.default.useCallback(() => { clearTimeout(timeouts[id]); const start = resumeFromStart ? refs.autoHideDuration.current : (refs.timeoutLeftOver.current !== undefined ? refs.timeoutLeftOver.current : refs.autoHideDuration.current); refs.timeoutLeftOver.current = start - (new Date().getTime() - refs.timeoutStart.current); }, [resumeFromStart]); const onMouseEnter = react_1.default.useCallback((event) => { if (timeouts[id] !== undefined) removeTimeout(); if ((0, utils_1.is)('function', onMouseEnter_)) onMouseEnter_(event); }, [onMouseEnter_]); const onMouseLeave = react_1.default.useCallback((event) => { if (refs.timeoutLeftOver.current !== undefined) addTimeout(refs.timeoutLeftOver.current); if ((0, utils_1.is)('function', onMouseLeave_)) onMouseLeave_(event); }, [onMouseLeave_]); react_1.default.useEffect(() => { var _a; const onTabFocus = () => { if (refs.timeoutLeftOver.current !== undefined) addTimeout(refs.timeoutLeftOver.current); }; const onTabBlur = () => { if (timeouts[id] !== undefined) removeTimeout(); }; const rootDocument = (0, utils_1.isEnvironment)('browser') ? (((_a = refs.root.current) === null || _a === void 0 ? void 0 : _a.ownerDocument) || window.document) : undefined; rootDocument.addEventListener('focus', onTabFocus); rootDocument.addEventListener('blur', onTabBlur); return () => { rootDocument.removeEventListener('focus', onTabFocus); rootDocument.removeEventListener('blur', onTabBlur); }; }, []); react_1.default.useEffect(() => { if (open) { if (autoHide && props.open !== undefined) addTimeout(); } else { clearTimeout(timeouts[id]); timeouts[id] = refs.timeoutStart.current = refs.timeoutLeftOver.current = undefined; } }, [open, autoHide]); if (props.open !== undefined) { if (closeButton) { end.push((0, jsx_runtime_1.jsx)(IconButton, Object.assign({ version: 'text', color: 'inherit', onClick: onClose }, IconButtonProps, { children: (0, jsx_runtime_1.jsx)(IconClose, Object.assign({}, IconProps)) }))); } } return ((0, jsx_runtime_1.jsx)(TransitionComponent, Object.assign({ in: open }, TransitionComponentProps, { children: (0, jsx_runtime_1.jsxs)(Surface, Object.assign({ ref: item => { if (ref) { if ((0, utils_1.is)('function', ref)) ref(item); else ref.current = item; } refs.root.current = item; }, tonal: tonal, color: color, elevation: elevation, gap: 2, wrap: 'wrap', direction: 'row', align: 'center', justify: 'space-between', onMouseEnter: onMouseEnter, onMouseLeave: onMouseLeave, Component: Line, className: (0, style_react_1.classNames)([ (0, utils_2.staticClassName)('Snackbar', theme) && [ `amaui-Snackbar-root`, `amaui-Snackbar-size-${size}` ], className, classes.root, classes[`size_${size}`], fixed && [ classes.fixed, classes[`position_${position}`], classes[`alignment_${alignment}`] ] ]) }, other, { children: [(children !== undefined || primary !== undefined) && ((0, jsx_runtime_1.jsxs)("div", Object.assign({ className: (0, style_react_1.classNames)([ (0, utils_2.staticClassName)('Snackbar', theme) && [ `amaui-Snackbar-primary` ], classes.primary ]) }, { children: [(0, utils_1.is)('string', primary) ? ((0, jsx_runtime_1.jsx)(Type, Object.assign({ version: 'b2' }, { children: primary }))) : primary, children] }))), !!end.length && ((0, jsx_runtime_1.jsx)(Line, Object.assign({ gap: 0, direction: 'row', wrap: 'wrap', align: 'center', justify: 'flex-end', className: (0, style_react_1.classNames)([ (0, utils_2.staticClassName)('Snackbar', theme) && [ `amaui-Snackbar-end` ], classes.end ]) }, { children: end })))] })) }))); }); Snackbar.displayName = 'amaui-Snackbar'; exports.default = Snackbar;