@wordpress/components
Version:
UI components for WordPress.
94 lines (93 loc) • 3.24 kB
JavaScript
import clsx from "clsx";
import { __ } from "@wordpress/i18n";
import { RawHTML, renderToString, useEffect } from "@wordpress/element";
import { speak } from "@wordpress/a11y";
import { closeSmall } from "@wordpress/icons";
import Button from "../button/index.mjs";
import { VisuallyHidden } from "../visually-hidden/index.mjs";
import { jsx, jsxs } from "react/jsx-runtime";
// packages/components/src/notice/index.tsx
const noop = () => {};
/**
* Custom hook which announces the message with the given politeness, if a
* valid message is provided.
*/
function useSpokenMessage(message, politeness) {
const spokenMessage = typeof message === "string" ? message : renderToString(message);
useEffect(() => {
if (spokenMessage) speak(spokenMessage, politeness);
}, [spokenMessage, politeness]);
}
function getDefaultPoliteness(status) {
switch (status) {
case "success":
case "warning":
case "info": return "polite";
default: return "assertive";
}
}
function getStatusLabel(status) {
switch (status) {
case "warning": return __("Warning notice");
case "info": return __("Information notice");
case "error": return __("Error notice");
default: return __("Notice");
}
}
/**
* `Notice` is a component used to communicate feedback to the user.
*
*```jsx
* import { Notice } from `@wordpress/components`;
*
* const MyNotice = () => (
* <Notice status="error">An unknown error occurred.</Notice>
* );
* ```
*/
function Notice({ className, status = "info", children, spokenMessage = children, onRemove = noop, isDismissible = true, actions = [], politeness = getDefaultPoliteness(status), __unstableHTML, onDismiss = noop }) {
useSpokenMessage(spokenMessage, politeness);
const classes = clsx(className, "components-notice", "is-" + status);
if (__unstableHTML && typeof children === "string") children = /* @__PURE__ */ jsx(RawHTML, { children });
const onDismissNotice = () => {
onDismiss();
onRemove();
};
return /* @__PURE__ */ jsxs("div", {
className: classes,
children: [
/* @__PURE__ */ jsx(VisuallyHidden, { children: getStatusLabel(status) }),
/* @__PURE__ */ jsx("div", {
className: "components-notice__content",
children
}),
actions.length > 0 && /* @__PURE__ */ jsx("div", {
className: "components-notice__actions",
children: actions.map(({ className: buttonCustomClasses, label, isPrimary, variant, noDefaultClasses = false, onClick, url, disabled }, index) => {
let computedVariant = variant;
if (variant !== "primary" && !noDefaultClasses) computedVariant = !url ? "secondary" : "link";
if (typeof computedVariant === "undefined" && isPrimary) computedVariant = "primary";
return /* @__PURE__ */ jsx(Button, {
size: "compact",
href: url,
variant: computedVariant,
onClick,
disabled,
accessibleWhenDisabled: true,
className: clsx("components-notice__action", buttonCustomClasses),
children: label
}, index);
})
}),
isDismissible && /* @__PURE__ */ jsx(Button, {
size: "small",
className: "components-notice__dismiss",
icon: closeSmall,
label: __("Close"),
onClick: onDismissNotice
})
]
});
}
export { Notice as default };
//# sourceMappingURL=index.mjs.map