@onesy/ui-react
Version:
UI for React
216 lines (212 loc) • 8.33 kB
JavaScript
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
const _excluded = ["ref", "max", "position", "alignment", "SnackbarProps", "className", "children"];
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
import React from 'react';
import { is, unique } from '@onesy/utils';
import { classNames, style as styleMethod, useOnesyTheme } from '@onesy/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';
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
const useStyle = styleMethod(theme => ({
root: {
position: 'fixed',
zIndex: theme.z_index.modal + 114,
'& > *': {
width: 'auto !important'
}
},
position_top: {
top: '24px',
'& .onesy-Snackbar-root': {
marginBottom: '16px'
}
},
position_bottom: {
bottom: '24px',
'& .onesy-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: 'onesy-Snackbars'
});
const Snackbars = props_ => {
const theme = useOnesyTheme();
const props = _objectSpread(_objectSpread(_objectSpread({}, theme?.ui?.elements?.all?.props?.default), theme?.ui?.elements?.onesySnackbars?.props?.default), props_);
const Line = theme?.elements?.Line || LineElement;
const Snackbar = theme?.elements?.Snackbar || SnackbarElement;
const Slide = theme?.elements?.Slide || SlideElement;
const Expand = theme?.elements?.Expand || ExpandElement;
const {
ref,
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_0 => {
const itemsNew_0 = [...previous_0];
if (position === 'top') itemsNew_0.push(value_);else if (position === 'bottom') itemsNew_0.unshift(value_);
return unique(itemsNew_0, '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_0 => {
setOpen(items => {
const itemsNew_1 = unique([...items], 'id');
const item = itemsNew_1.find(item_ => item_.id === id_0);
if (item) item.in = false;
return unique(itemsNew_1, 'id');
});
};
const onSnackbarExited = id_1 => {
setOpen(items_0 => {
const itemsNew_2 = unique([...items_0], 'id');
const item_0 = itemsNew_2.find(item__0 => item__0.id === id_1);
if (item_0) item_0.expand = false;
return unique(itemsNew_2, 'id');
});
};
const onExpandExited = id_2 => {
setOpen(items_1 => {
const itemsNew_3 = unique([...items_1], 'id');
const index = itemsNew_3.findIndex(item__1 => item__1.id === id_2);
if (index > -1) itemsNew_3.splice(index, 1);
return unique(itemsNew_3, '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__*/_jsxs(SnackbarsContext.Provider, {
value: refs.value.current,
children: [children, /*#__PURE__*/_jsx(Line, _objectSpread(_objectSpread({
ref: item_1 => {
if (ref) {
if (is('function', ref)) ref(item_1);else if (ref?.current) ref.current = item_1;
}
refs.root.current = item_1;
},
align: align,
gap: 0,
className: classNames([staticClassName('Snackbars', theme) && [`onesy-Snackbars-root`], className, classes.root, classes[`position_${position}`], classes[`alignment_${alignment}`]])
}, other), {}, {
children: open.map(item_2 => /*#__PURE__*/_jsx(Expand, {
in: item_2.expand,
parent: refs.root.current,
onExited: () => onExpandExited(item_2.id),
children: /*#__PURE__*/_jsx(Snackbar, _objectSpread(_objectSpread(_objectSpread({
id: item_2.id,
open: item_2.in
}, snackbarProps), item_2.Snackbar), {}, {
TransitionComponentProps: _objectSpread(_objectSpread(_objectSpread({}, snackbarProps?.TransitionComponentProps), item_2.Snackbar?.TransitionComponentProps), {}, {
removeOnExited: false,
onExited: (...args) => {
onSnackbarExited(item_2.id);
if (is('function', snackbarProps?.TransitionComponentProps?.onExited)) snackbarProps.TransitionComponentProps.onExited(...args);
if (is('function', item_2.Snackbar?.onExited)) item_2.Snackbar.onExited(...args);
}
}),
onClose: () => onClose(item_2.id),
fixed: false
}), item_2.id)
}, item_2.id))
}))]
});
};
Snackbars.displayName = 'onesy-Snackbars';
export default Snackbars;