@wordpress/components
Version:
UI components for WordPress.
151 lines (149 loc) • 5.55 kB
JavaScript
;
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 __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// packages/components/src/notice/index.tsx
var notice_exports = {};
__export(notice_exports, {
default: () => notice_default
});
module.exports = __toCommonJS(notice_exports);
var import_clsx = __toESM(require("clsx"));
var import_i18n = require("@wordpress/i18n");
var import_element = require("@wordpress/element");
var import_a11y = require("@wordpress/a11y");
var import_icons = require("@wordpress/icons");
var import_button = __toESM(require("../button"));
var import_visually_hidden = require("../visually-hidden");
var import_jsx_runtime = require("react/jsx-runtime");
var noop = () => {
};
function useSpokenMessage(message, politeness) {
const spokenMessage = typeof message === "string" ? message : (0, import_element.renderToString)(message);
(0, import_element.useEffect)(() => {
if (spokenMessage) {
(0, import_a11y.speak)(spokenMessage, politeness);
}
}, [spokenMessage, politeness]);
}
function getDefaultPoliteness(status) {
switch (status) {
case "success":
case "warning":
case "info":
return "polite";
// The default will also catch the 'error' status.
default:
return "assertive";
}
}
function getStatusLabel(status) {
switch (status) {
case "warning":
return (0, import_i18n.__)("Warning notice");
case "info":
return (0, import_i18n.__)("Information notice");
case "error":
return (0, import_i18n.__)("Error notice");
// The default will also catch the 'success' status.
default:
return (0, import_i18n.__)("Notice");
}
}
function Notice({
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
}) {
useSpokenMessage(spokenMessage, politeness);
const classes = (0, import_clsx.default)(className, "components-notice", "is-" + status, {
"is-dismissible": isDismissible
});
if (__unstableHTML && typeof children === "string") {
children = /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_element.RawHTML, {
children
});
}
const onDismissNotice = () => {
onDismiss();
onRemove();
};
return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
className: classes,
children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_visually_hidden.VisuallyHidden, {
children: getStatusLabel(status)
}), /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
className: "components-notice__content",
children: [children, /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", {
className: "components-notice__actions",
children: actions.map(({
className: buttonCustomClasses,
label,
isPrimary,
variant,
noDefaultClasses = false,
onClick,
url
}, index) => {
let computedVariant = variant;
if (variant !== "primary" && !noDefaultClasses) {
computedVariant = !url ? "secondary" : "link";
}
if (typeof computedVariant === "undefined" && isPrimary) {
computedVariant = "primary";
}
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_button.default, {
__next40pxDefaultSize: true,
href: url,
variant: computedVariant,
onClick: url ? void 0 : onClick,
className: (0, import_clsx.default)("components-notice__action", buttonCustomClasses),
children: label
}, index);
})
})]
}), isDismissible && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_button.default, {
size: "small",
className: "components-notice__dismiss",
icon: import_icons.close,
label: (0, import_i18n.__)("Close"),
onClick: onDismissNotice
})]
});
}
var notice_default = Notice;
//# sourceMappingURL=index.js.map