UNPKG

@momentum-ui/react-collaboration

Version:

Cisco Momentum UI Framework for React Collaboration Applications

111 lines 5.56 kB
var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; 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; }; import { toast } from 'react-toastify'; import { DEFAULTS, ATTENTION } from './NotificationSystem.constants'; import ScreenReaderAnnouncer from '../ScreenReaderAnnouncer'; /** * Generates the container id for the notifications to be placed in * @param id identifier for container * @param attention attention type of container * @returns the generated id */ export var getContainerID = function (id, attention) { return [id, attention, DEFAULTS.CONTAINER_ID_SUFFIX].join('_').replace(/^_+/, ''); }; /** * Calculates the autoClose value, based on the passed in NotifyOptions * @param options notify options, which will decide about what value the autoClose will have * @returns a number or false, depended on the passed in options */ export var calculateAutoClose = function (options) { var defaultAutoClose = options.attention === ATTENTION.MEDIUM ? false : DEFAULTS.AUTOCLOSE_TIMEOUT; return options.autoClose === false || options.autoClose > 0 ? options.autoClose : defaultAutoClose; }; /** * **notify** utility function, to trigger a notification with the provided `content` * in the defined notification system (`notificationSystemId`) * Other options are `attention`, `autoClose`, `toastId` (if u want to define a fixed toastId) * and more * @param content content to render in the Notification (React.ReactNode) * @param options several options to pass in (for details check type) * @returns the toastId of the triggered notification */ export var notify = function (content, options) { var notificationSystemId = options.notificationSystemId, screenReaderAnnouncement = options.screenReaderAnnouncement, toastId = options.toastId, attention = options.attention, onClose = options.onClose, role = options.role, announcerIdentityFromOptions = options.announcerIdentity; if (screenReaderAnnouncement) { var announcerIdentity = notificationSystemId; if (announcerIdentityFromOptions) { if (ScreenReaderAnnouncer.isRegistered(announcerIdentityFromOptions)) { announcerIdentity = announcerIdentityFromOptions; } else { console.warn("ScreenReaderAnnouncer with identity ".concat(announcerIdentityFromOptions, " is not registered, falling back to ").concat(notificationSystemId)); } } ScreenReaderAnnouncer.announce({ body: screenReaderAnnouncement }, announcerIdentity); } return toast(content, { toastId: toastId, autoClose: calculateAutoClose(options), containerId: getContainerID(notificationSystemId, attention || ATTENTION.LOW), onClose: onClose, role: role, }); }; /** * **update** utility function, to update an existing notification in the defined notification system (`notificationSystemId`) * Other update options are `render` (to update the content), `autoClose`, `attention` and more * @param toastId id of the notification to update * @param options several options to pass in (for details check type) */ export var update = function (toastId, options) { var notificationSystemId = options.notificationSystemId, attention = options.attention, screenReaderAnnouncement = options.screenReaderAnnouncement, announcerIdentityFromOptions = options.announcerIdentity, updateOptions = __rest(options, ["notificationSystemId", "attention", "screenReaderAnnouncement", "announcerIdentity"]); if (screenReaderAnnouncement) { var announcerIdentity = notificationSystemId; if (announcerIdentityFromOptions) { if (ScreenReaderAnnouncer.isRegistered(announcerIdentityFromOptions)) { announcerIdentity = announcerIdentityFromOptions; } else { console.warn("ScreenReaderAnnouncer with identity ".concat(announcerIdentityFromOptions, " is not registered, falling back to ").concat(notificationSystemId)); } } ScreenReaderAnnouncer.announce({ body: screenReaderAnnouncement }, announcerIdentity); } toast.update(toastId, __assign(__assign({}, updateOptions), { containerId: getContainerID(notificationSystemId, attention || ATTENTION.LOW) })); }; /** * **dismiss** utility function, to dismiss an existing notification * @param toastId id of the notification to dismiss */ export var dismiss = function (toastId) { toast.dismiss(toastId); }; /** * **isActive** utility function, which returns true if notification is currently active (=shown) * @param toastId id of the notification to check for */ export var isActive = function (toastId) { return toast.isActive(toastId); }; //# sourceMappingURL=NotificationSystem.utils.js.map