UNPKG

@wordpress/components

Version:
143 lines (142 loc) • 5.03 kB
import clsx from "clsx"; import { useIsPresent } from "framer-motion"; import { speak } from "@wordpress/a11y"; import { useMergeRefs } from "@wordpress/compose"; import { forwardRef, renderToString, useEffect, useLayoutEffect, useRef } from "@wordpress/element"; import { __ } from "@wordpress/i18n"; import warning from "@wordpress/warning"; import Button from "../button/index.mjs"; import ExternalLink from "../external-link/index.mjs"; import { jsx, jsxs } from "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 : renderToString(message); useEffect(() => { if (spokenMessage) speak(spokenMessage, politeness); }, [spokenMessage, politeness]); } function UnforwardedSnackbar({ className, children, spokenMessage = children, politeness = "polite", actions = [], onRemove, icon = null, explicitDismiss = false, onDismiss, listRef }, ref) { const isPresent = useIsPresent(); const snackbarRef = useRef(null); const mergedRef = useMergeRefs([snackbarRef, ref]); const focusReturnRef = 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 = useRef({ onDismiss, onRemove }); useLayoutEffect(() => { callbacksRef.current = { onDismiss, onRemove }; }); const timeout = actions.length > 0 ? NOTICE_TIMEOUT_WITH_ACTIONS : NOTICE_TIMEOUT; useEffect(() => { if (explicitDismiss || !isPresent) return; const timeoutHandle = setTimeout(() => { callbacksRef.current.onDismiss?.(); callbacksRef.current.onRemove?.(); }, timeout); return () => clearTimeout(timeoutHandle); }, [ explicitDismiss, isPresent, timeout ]); const classes = clsx(className, "components-snackbar", { "components-snackbar-explicit-dismiss": !!explicitDismiss }); if (actions && actions.length > 1) { warning("Snackbar can only have one action. Use Notice if your message requires many actions."); actions = [actions[0]]; } const snackbarContentClassnames = clsx("components-snackbar__content", { "components-snackbar__content-with-icon": !!icon }); return /* @__PURE__ */ 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 ? __("Dismiss this notice") : void 0, "data-testid": "snackbar", children: /* @__PURE__ */ jsxs("div", { className: snackbarContentClassnames, children: [ icon && /* @__PURE__ */ jsx("div", { className: "components-snackbar__icon", children: icon }), children, actions.map(({ label, onClick, url, openInNewTab = false }, index) => url !== void 0 && openInNewTab ? /* @__PURE__ */ jsx(ExternalLink, { href: url, onClick: (event) => onActionClick(event, onClick), className: "components-snackbar__action", children: label }, index) : /* @__PURE__ */ jsx(Button, { __next40pxDefaultSize: true, href: url, variant: "link", onClick: (event) => onActionClick(event, onClick), className: "components-snackbar__action", children: label }, index)), explicitDismiss && /* @__PURE__ */ jsx("span", { role: "button", "aria-label": __("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 = forwardRef(UnforwardedSnackbar); Snackbar.displayName = "Snackbar"; export { Snackbar, Snackbar as default }; //# sourceMappingURL=index.mjs.map