@gechiui/components
Version:
UI components for GeChiUI.
138 lines (121 loc) • 3.65 kB
JavaScript
import { createElement } from "@gechiui/element";
/**
* External dependencies
*/
import { noop } from 'lodash';
import classnames from 'classnames';
/**
* GeChiUI dependencies
*/
import { __ } from '@gechiui/i18n';
import { RawHTML, useEffect, renderToString } from '@gechiui/element';
import { speak } from '@gechiui/a11y';
import { close } from '@gechiui/icons';
/**
* Internal dependencies
*/
import { Button } from '../';
/** @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]);
}
/**
* Given a notice status, returns an assumed default politeness for the status.
* Defaults to 'assertive'.
*
* @param {string} [status] Notice status.
*
* @return {'polite'|'assertive'} Notice politeness.
*/
function getDefaultPoliteness(status) {
switch (status) {
case 'success':
case 'warning':
case 'info':
return 'polite';
case 'error':
default:
return 'assertive';
}
}
function Notice(_ref) {
let {
className,
status = 'info',
children,
spokenMessage = children,
onRemove = noop,
isDismissible = true,
actions = [],
politeness = getDefaultPoliteness(status),
__unstableHTML,
// onDismiss is a callback executed when the notice is dismissed.
// It is distinct from onRemove, which _looks_ like a callback but is
// actually the function to call to remove the notice from the UI.
onDismiss = noop
} = _ref;
useSpokenMessage(spokenMessage, politeness);
const classes = classnames(className, 'components-notice', 'is-' + status, {
'is-dismissible': isDismissible
});
if (__unstableHTML) {
children = createElement(RawHTML, null, children);
}
const onDismissNotice = event => {
var _event$preventDefault;
event === null || event === void 0 ? void 0 : (_event$preventDefault = event.preventDefault) === null || _event$preventDefault === void 0 ? void 0 : _event$preventDefault.call(event);
onDismiss();
onRemove();
};
return createElement("div", {
className: classes
}, createElement("div", {
className: "components-notice__content"
}, children, createElement("div", {
className: "components-notice__actions"
}, actions.map((_ref2, index) => {
let {
className: buttonCustomClasses,
label,
isPrimary,
variant,
noDefaultClasses = false,
onClick,
url
} = _ref2;
let computedVariant = variant;
if (variant !== 'primary' && !noDefaultClasses) {
computedVariant = !url ? 'secondary' : 'link';
}
if (typeof computedVariant === 'undefined' && isPrimary) {
computedVariant = 'primary';
}
return createElement(Button, {
key: index,
href: url,
variant: computedVariant,
onClick: url ? undefined : onClick,
className: classnames('components-notice__action', buttonCustomClasses)
}, label);
}))), isDismissible && createElement(Button, {
className: "components-notice__dismiss",
icon: close,
label: __('忽略此通知'),
onClick: onDismissNotice,
showTooltip: false
}));
}
export default Notice;
//# sourceMappingURL=index.js.map