UNPKG

@wordpress/components

Version:
164 lines (162 loc) 6.17 kB
"use strict"; 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/snackbar/index.tsx var snackbar_exports = {}; __export(snackbar_exports, { Snackbar: () => Snackbar, default: () => snackbar_default }); module.exports = __toCommonJS(snackbar_exports); var import_clsx = __toESM(require("clsx")); var import_a11y = require("@wordpress/a11y"); var import_element = require("@wordpress/element"); var import_i18n = require("@wordpress/i18n"); var import_warning = __toESM(require("@wordpress/warning")); var import_button = __toESM(require("../button")); var import_external_link = __toESM(require("../external-link")); var import_jsx_runtime = require("react/jsx-runtime"); var NOTICE_TIMEOUT = 1e4; 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 UnforwardedSnackbar({ className, children, spokenMessage = children, politeness = "polite", actions = [], onRemove, 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, listRef }, ref) { function dismissMe(event) { if (event && event.preventDefault) { event.preventDefault(); } listRef?.current?.focus(); onDismiss?.(); onRemove?.(); } function onActionClick(event, onClick) { event.stopPropagation(); onRemove?.(); if (onClick) { onClick(event); } } useSpokenMessage(spokenMessage, politeness); const callbacksRef = (0, import_element.useRef)({ onDismiss, onRemove }); (0, import_element.useLayoutEffect)(() => { callbacksRef.current = { onDismiss, onRemove }; }); (0, import_element.useEffect)(() => { const timeoutHandle = setTimeout(() => { if (!explicitDismiss) { callbacksRef.current.onDismiss?.(); callbacksRef.current.onRemove?.(); } }, NOTICE_TIMEOUT); return () => clearTimeout(timeoutHandle); }, [explicitDismiss]); const classes = (0, import_clsx.default)(className, "components-snackbar", { "components-snackbar-explicit-dismiss": !!explicitDismiss }); if (actions && actions.length > 1) { globalThis.SCRIPT_DEBUG === true ? (0, import_warning.default)("Snackbar can only have one action. Use Notice if your message requires many actions.") : void 0; actions = [actions[0]]; } const snackbarContentClassnames = (0, import_clsx.default)("components-snackbar__content", { "components-snackbar__content-with-icon": !!icon }); return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { ref, className: classes, onClick: !explicitDismiss ? dismissMe : void 0, tabIndex: 0, role: !explicitDismiss ? "button" : void 0, onKeyPress: !explicitDismiss ? dismissMe : void 0, "aria-label": !explicitDismiss ? (0, import_i18n.__)("Dismiss this notice") : void 0, "data-testid": "snackbar", children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: snackbarContentClassnames, children: [icon && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "components-snackbar__icon", children: icon }), children, actions.map(({ label, onClick, url, openInNewTab = false }, index) => url !== void 0 && openInNewTab ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_external_link.default, { href: url, onClick: (event) => onActionClick(event, onClick), className: "components-snackbar__action", children: label }, index) : /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_button.default, { __next40pxDefaultSize: true, href: url, variant: "link", onClick: (event) => onActionClick(event, onClick), className: "components-snackbar__action", children: label }, index)), explicitDismiss && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { role: "button", "aria-label": (0, import_i18n.__)("Dismiss this notice"), tabIndex: 0, className: "components-snackbar__dismiss-button", onClick: dismissMe, onKeyPress: dismissMe, children: "\u2715" })] }) }); } var Snackbar = (0, import_element.forwardRef)(UnforwardedSnackbar); var snackbar_default = Snackbar; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { Snackbar }); //# sourceMappingURL=index.js.map