@gechiui/components
Version:
UI components for GeChiUI.
140 lines (123 loc) • 4.08 kB
JavaScript
import { createElement } from "@gechiui/element";
/**
* External dependencies
*/
import { noop } from 'lodash';
import classnames from 'classnames';
/**
* GeChiUI dependencies
*/
import { speak } from '@gechiui/a11y';
import { useEffect, forwardRef, renderToString } from '@gechiui/element';
import { __ } from '@gechiui/i18n';
import warning from '@gechiui/warning';
/**
* Internal dependencies
*/
import { Button } from '../';
const NOTICE_TIMEOUT = 10000;
/** @typedef {import('@gechiui/element').GCElement} GCElement */
/**
* Custom hook which announces the message with the given politeness, if a
* valid message is provided.
*
* @param {string|GCElement} [message] Message to announce.
* @param {'polite'|'assertive'} politeness Politeness to announce.
*/
function useSpokenMessage(message, politeness) {
const spokenMessage = typeof message === 'string' ? message : renderToString(message);
useEffect(() => {
if (spokenMessage) {
speak(spokenMessage, politeness);
}
}, [spokenMessage, politeness]);
}
function Snackbar(_ref, ref) {
let {
className,
children,
spokenMessage = children,
politeness = 'polite',
actions = [],
onRemove = noop,
icon = null,
explicitDismiss = false,
// onDismiss is a callback executed when the snackbar is dismissed.
// It is distinct from onRemove, which _looks_ like a callback but is
// actually the function to call to remove the snackbar from the UI.
onDismiss = noop,
listRef
} = _ref;
onDismiss = onDismiss || noop;
function dismissMe(event) {
if (event && event.preventDefault) {
event.preventDefault();
} // Prevent focus loss by moving it to the list element.
listRef.current.focus();
onDismiss();
onRemove();
}
function onActionClick(event, onClick) {
event.stopPropagation();
onRemove();
if (onClick) {
onClick(event);
}
}
useSpokenMessage(spokenMessage, politeness); // Only set up the timeout dismiss if we're not explicitly dismissing.
useEffect(() => {
const timeoutHandle = setTimeout(() => {
if (!explicitDismiss) {
onDismiss();
onRemove();
}
}, NOTICE_TIMEOUT);
return () => clearTimeout(timeoutHandle);
}, [onDismiss, onRemove]);
const classes = classnames(className, 'components-snackbar', {
'components-snackbar-explicit-dismiss': !!explicitDismiss
});
if (actions && actions.length > 1) {
// we need to inform developers that snackbar only accepts 1 action
typeof process !== "undefined" && process.env && process.env.NODE_ENV !== "production" ? warning('Snackbar can only have 1 action, use Notice if your message require many messages') : void 0; // return first element only while keeping it inside an array
actions = [actions[0]];
}
const snackbarContentClassnames = classnames('components-snackbar__content', {
'components-snackbar__content-with-icon': !!icon
});
return createElement("div", {
ref: ref,
className: classes,
onClick: !explicitDismiss ? dismissMe : noop,
tabIndex: "0",
role: !explicitDismiss ? 'button' : '',
onKeyPress: !explicitDismiss ? dismissMe : noop,
"aria-label": !explicitDismiss ? __('忽略此通知') : ''
}, createElement("div", {
className: snackbarContentClassnames
}, icon && createElement("div", {
className: "components-snackbar__icon"
}, icon), children, actions.map((_ref2, index) => {
let {
label,
onClick,
url
} = _ref2;
return createElement(Button, {
key: index,
href: url,
variant: "tertiary",
onClick: event => onActionClick(event, onClick),
className: "components-snackbar__action"
}, label);
}), explicitDismiss && createElement("span", {
role: "button",
"aria-label": "Dismiss this notice",
tabIndex: "0",
className: "components-snackbar__dismiss-button",
onClick: dismissMe,
onKeyPress: dismissMe
}, "\u2715")));
}
export default forwardRef(Snackbar);
//# sourceMappingURL=index.js.map