@wordpress/components
Version:
UI components for WordPress.
122 lines (121 loc) • 4.91 kB
JavaScript
;
Object.defineProperties(exports, {
__esModule: { value: true },
[Symbol.toStringTag]: { value: "Module" }
});
// \0rolldown/runtime.js
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
key = keys[i];
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
get: ((k) => from[k]).bind(null, key),
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
});
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
value: mod,
enumerable: true
}) : target, mod));
let clsx = require("clsx");
clsx = __toESM(clsx);
let _wordpress_i18n = require("@wordpress/i18n");
let _wordpress_element = require("@wordpress/element");
let _wordpress_a11y = require("@wordpress/a11y");
let _wordpress_icons = require("@wordpress/icons");
let ___button_index_tsx = require("../button/index.cjs");
___button_index_tsx = __toESM(___button_index_tsx);
let ___visually_hidden_index_ts = require("../visually-hidden/index.cjs");
let react_jsx_runtime = require("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 : (0, _wordpress_element.renderToString)(message);
(0, _wordpress_element.useEffect)(() => {
if (spokenMessage) (0, _wordpress_a11y.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 (0, _wordpress_i18n.__)("Warning notice");
case "info": return (0, _wordpress_i18n.__)("Information notice");
case "error": return (0, _wordpress_i18n.__)("Error notice");
default: return (0, _wordpress_i18n.__)("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 = (0, clsx.default)(className, "components-notice", "is-" + status);
if (__unstableHTML && typeof children === "string") children = /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_element.RawHTML, { children });
const onDismissNotice = () => {
onDismiss();
onRemove();
};
return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
className: classes,
children: [
/* @__PURE__ */ (0, react_jsx_runtime.jsx)(___visually_hidden_index_ts.VisuallyHidden, { children: getStatusLabel(status) }),
/* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
className: "components-notice__content",
children
}),
actions.length > 0 && /* @__PURE__ */ (0, react_jsx_runtime.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__ */ (0, react_jsx_runtime.jsx)(___button_index_tsx.default, {
size: "compact",
href: url,
variant: computedVariant,
onClick,
disabled,
accessibleWhenDisabled: true,
className: (0, clsx.default)("components-notice__action", buttonCustomClasses),
children: label
}, index);
})
}),
isDismissible && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(___button_index_tsx.default, {
size: "small",
className: "components-notice__dismiss",
icon: _wordpress_icons.closeSmall,
label: (0, _wordpress_i18n.__)("Close"),
onClick: onDismissNotice
})
]
});
}
exports.default = Notice;
//# sourceMappingURL=index.cjs.map