@wordpress/components
Version:
UI components for WordPress.
174 lines (173 loc) • 6.88 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 framer_motion = require("framer-motion");
let _wordpress_a11y = require("@wordpress/a11y");
let _wordpress_compose = require("@wordpress/compose");
let _wordpress_element = require("@wordpress/element");
let _wordpress_i18n = require("@wordpress/i18n");
let _wordpress_warning = require("@wordpress/warning");
_wordpress_warning = __toESM(_wordpress_warning);
let ___button_index_tsx = require("../button/index.cjs");
___button_index_tsx = __toESM(___button_index_tsx);
let ___external_link_index_tsx = require("../external-link/index.cjs");
___external_link_index_tsx = __toESM(___external_link_index_tsx);
let react_jsx_runtime = require("react/jsx-runtime");
// packages/components/src/snackbar/index.tsx
const NOTICE_TIMEOUT = 6e3;
const NOTICE_TIMEOUT_WITH_ACTIONS = 1e4;
/**
* Custom hook which announces the message with the given politeness, if a
* valid message is provided.
*
* @param message Message to announce.
* @param politeness Politeness to announce.
*/
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 UnforwardedSnackbar({ className, children, spokenMessage = children, politeness = "polite", actions = [], onRemove, icon = null, explicitDismiss = false, onDismiss, listRef }, ref) {
const isPresent = (0, framer_motion.useIsPresent)();
const snackbarRef = (0, _wordpress_element.useRef)(null);
const mergedRef = (0, _wordpress_compose.useMergeRefs)([snackbarRef, ref]);
const focusReturnRef = (0, _wordpress_element.useRef)(null);
function onFocus(event) {
if (!event.currentTarget.contains(event.relatedTarget)) focusReturnRef.current = event.relatedTarget;
}
function dismissMe(event) {
if (event && event.preventDefault) event.preventDefault();
if (listRef?.current) listRef.current.focus();
else if (snackbarRef.current?.contains(snackbarRef.current.ownerDocument.activeElement)) {
if (focusReturnRef.current?.isConnected) focusReturnRef.current.focus({ preventScroll: true });
}
onDismiss?.();
onRemove?.();
}
function onKeyPress(event) {
if (event.target !== event.currentTarget) return;
dismissMe(event);
}
function onActionClick(event, onClick) {
event.stopPropagation();
onRemove?.();
if (onClick) onClick(event);
}
useSpokenMessage(spokenMessage, politeness);
const callbacksRef = (0, _wordpress_element.useRef)({
onDismiss,
onRemove
});
(0, _wordpress_element.useLayoutEffect)(() => {
callbacksRef.current = {
onDismiss,
onRemove
};
});
const timeout = actions.length > 0 ? NOTICE_TIMEOUT_WITH_ACTIONS : NOTICE_TIMEOUT;
(0, _wordpress_element.useEffect)(() => {
if (explicitDismiss || !isPresent) return;
const timeoutHandle = setTimeout(() => {
callbacksRef.current.onDismiss?.();
callbacksRef.current.onRemove?.();
}, timeout);
return () => clearTimeout(timeoutHandle);
}, [
explicitDismiss,
isPresent,
timeout
]);
const classes = (0, clsx.default)(className, "components-snackbar", { "components-snackbar-explicit-dismiss": !!explicitDismiss });
if (actions && actions.length > 1) {
(0, _wordpress_warning.default)("Snackbar can only have one action. Use Notice if your message requires many actions.");
actions = [actions[0]];
}
const snackbarContentClassnames = (0, clsx.default)("components-snackbar__content", { "components-snackbar__content-with-icon": !!icon });
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
ref: mergedRef,
onFocus,
className: classes,
onClick: !explicitDismiss ? dismissMe : void 0,
tabIndex: 0,
role: !explicitDismiss ? "button" : void 0,
onKeyPress: !explicitDismiss ? onKeyPress : void 0,
"aria-label": !explicitDismiss ? (0, _wordpress_i18n.__)("Dismiss this notice") : void 0,
"data-testid": "snackbar",
children: /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
className: snackbarContentClassnames,
children: [
icon && /* @__PURE__ */ (0, react_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, react_jsx_runtime.jsx)(___external_link_index_tsx.default, {
href: url,
onClick: (event) => onActionClick(event, onClick),
className: "components-snackbar__action",
children: label
}, index) : /* @__PURE__ */ (0, react_jsx_runtime.jsx)(___button_index_tsx.default, {
__next40pxDefaultSize: true,
href: url,
variant: "link",
onClick: (event) => onActionClick(event, onClick),
className: "components-snackbar__action",
children: label
}, index)),
explicitDismiss && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
role: "button",
"aria-label": (0, _wordpress_i18n.__)("Dismiss this notice"),
tabIndex: 0,
className: "components-snackbar__dismiss-button",
onClick: dismissMe,
onKeyPress: dismissMe,
children: "✕"
})
]
})
});
}
/**
* A Snackbar displays a succinct message that is cleared out after a small delay.
*
* It can also offer the user options, like viewing a published post.
* But these options should also be available elsewhere in the UI.
*
* ```jsx
* const MySnackbarNotice = () => (
* <Snackbar>Post published successfully.</Snackbar>
* );
* ```
*/
const Snackbar = (0, _wordpress_element.forwardRef)(UnforwardedSnackbar);
Snackbar.displayName = "Snackbar";
exports.Snackbar = Snackbar;
exports.default = Snackbar;
//# sourceMappingURL=index.cjs.map