UNPKG

@amaui/ui-react

Version:
213 lines (209 loc) 8.39 kB
import _extends from "@babel/runtime/helpers/extends"; import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; const _excluded = ["max", "position", "alignment", "SnackbarProps", "className", "children"]; function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; } import React from 'react'; import { is, unique } from '@amaui/utils'; import { classNames, style as styleMethod, useAmauiTheme } from '@amaui/style-react'; import SnackbarElement from '../Snackbar'; import SlideElement from '../Slide'; import ExpandElement from '../Expand'; import LineElement from '../Line'; import SnackbarsContext from './Context'; import { staticClassName } from '../utils'; const useStyle = styleMethod(theme => ({ root: { position: 'fixed', zIndex: theme.z_index.modal + 114, '& > *': { width: 'auto !important' } }, position_top: { top: '24px', '& .amaui-Snackbar-root': { marginBottom: '16px' } }, position_bottom: { bottom: '24px', '& .amaui-Snackbar-root': { marginTop: '16px' } }, 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-Snackbars' }); const Snackbars = /*#__PURE__*/React.forwardRef((props_, ref) => { const theme = useAmauiTheme(); const props = React.useMemo(() => _objectSpread(_objectSpread(_objectSpread({}, theme?.ui?.elements?.all?.props?.default), theme?.ui?.elements?.amauiSnackbars?.props?.default), props_), [props_]); const Line = React.useMemo(() => theme?.elements?.Line || LineElement, [theme]); const Snackbar = React.useMemo(() => theme?.elements?.Snackbar || SnackbarElement, [theme]); const Slide = React.useMemo(() => theme?.elements?.Slide || SlideElement, [theme]); const Expand = React.useMemo(() => theme?.elements?.Expand || ExpandElement, [theme]); const { max = 4, position = 'bottom', alignment = 'start', SnackbarProps, className, children } = props, other = _objectWithoutProperties(props, _excluded); const { classes } = useStyle(); const [preOpen, setPreOpen] = React.useState([]); const [open, setOpen] = React.useState([]); const refs = { root: React.useRef(undefined), value: React.useRef({}), open: React.useRef(undefined), preOpen: React.useRef(undefined) }; refs.open.current = open; refs.preOpen.current = preOpen; const snackbarProps = _objectSpread({ TransitionComponent: Slide, TransitionComponentProps: { add: true, direction: props.position === 'top' && props.alignment === 'center' ? 'top' : props.position === 'bottom' && props.alignment === 'center' ? 'bottom' : props.alignment === 'left' ? 'left' : 'right' } }, SnackbarProps); // Start and ltr change direction to left or right if (alignment === 'start' && theme.direction === 'ltr') snackbarProps.TransitionComponentProps.direction = 'left'; if (alignment === 'start' && theme.direction === 'rtl') snackbarProps.TransitionComponentProps.direction = 'right'; // End and ltr change direction to right or left if (alignment === 'end' && theme.direction === 'ltr') snackbarProps.TransitionComponentProps.direction = 'right'; if (alignment === 'end' && theme.direction === 'rtl') snackbarProps.TransitionComponentProps.direction = 'left'; // Add preOpen React.useEffect(() => { // Add from preOpen const toAdd = max - open.length; if (toAdd > 0 && !!refs.preOpen.current.length) { const itemsToAdd = refs.preOpen.current.slice(0, toAdd); setPreOpen(itemsPreOpen => unique([...itemsPreOpen], 'id').slice(toAdd)); if (itemsToAdd.length) setOpen(() => { const itemsNew = [...refs.open.current]; if (position === 'top') itemsNew.push(...itemsToAdd);else if (position === 'bottom') itemsNew.unshift(...itemsToAdd); return unique(itemsNew, 'id'); }); } }, [open.length]); const add = value => { const value_ = { id: new Date().getTime(), in: true, expand: true, Snackbar: _objectSpread({ color: 'default' }, value) }; // Pre open if (is('number', max) && open.length >= max) { setPreOpen(previous => unique([...previous, value_], 'id')); } // Open else { setOpen(previous => { const itemsNew = [...previous]; if (position === 'top') itemsNew.push(value_);else if (position === 'bottom') itemsNew.unshift(value_); return unique(itemsNew, 'id'); }); } return value_; }; const remove = id_ => { let id = id_; if (id_ === 'first') id = open[open.length - 1]?.id; if (id_ === 'last') id = open[0]?.id; onClose(id); }; const onClose = id => { setOpen(items => { const itemsNew = unique([...items], 'id'); const item = itemsNew.find(item_ => item_.id === id); if (item) item.in = false; return unique(itemsNew, 'id'); }); }; const onSnackbarExited = id => { setOpen(items => { const itemsNew = unique([...items], 'id'); const item = itemsNew.find(item_ => item_.id === id); if (item) item.expand = false; return unique(itemsNew, 'id'); }); }; const onExpandExited = id => { setOpen(items => { const itemsNew = unique([...items], 'id'); const index = itemsNew.findIndex(item_ => item_.id === id); if (index > -1) itemsNew.splice(index, 1); return unique(itemsNew, 'id'); }); }; refs.value.current.add = add; refs.value.current.remove = remove; let align = 'center'; if (['start', 'left'].includes(alignment)) align = 'flex-start'; if (['right', 'end'].includes(alignment)) align = 'flex-end'; return /*#__PURE__*/React.createElement(SnackbarsContext.Provider, { value: refs.value.current }, children, /*#__PURE__*/React.createElement(Line, _extends({ ref: item => { if (ref) { if (is('function', ref)) ref(item);else if (ref?.current) ref.current = item; } refs.root.current = item; }, align: align, gap: 0, className: classNames([staticClassName('Snackbars', theme) && [`amaui-Snackbars-root`], className, classes.root, classes[`position_${position}`], classes[`alignment_${alignment}`]]) }, other), open.map(item => /*#__PURE__*/React.createElement(Expand, { key: item.id, in: item.expand, parent: refs.root.current, onExited: () => onExpandExited(item.id) }, /*#__PURE__*/React.createElement(Snackbar, _extends({ key: item.id, id: item.id, open: item.in }, snackbarProps, item.Snackbar, { TransitionComponentProps: _objectSpread(_objectSpread(_objectSpread({}, snackbarProps?.TransitionComponentProps), item.Snackbar?.TransitionComponentProps), {}, { removeOnExited: false, onExited: function () { onSnackbarExited(item.id); if (is('function', snackbarProps?.TransitionComponentProps?.onExited)) snackbarProps.TransitionComponentProps.onExited(...arguments); if (is('function', item.Snackbar?.onExited)) item.Snackbar.onExited(...arguments); } }), onClose: () => onClose(item.id), fixed: false })))))); }); Snackbars.displayName = 'amaui-Snackbars'; export default Snackbars;