UNPKG

@wordpress/widget-dashboard

Version:

Stateless rendering engine for widget dashboards: the WidgetDashboard compound component and its grid-settings kit.

230 lines (226 loc) • 8.1 kB
// packages/widget-dashboard/src/components/widget-footer/widget-footer.tsx import { __, sprintf } from "@wordpress/i18n"; import { Icon, Link, LinkButton, Stack, Tooltip } from "@wordpress/ui"; import { useWidgetHost } from "@wordpress/widget-primitives"; import { getActionRoute } from "../widget-actions/get-action-route.mjs"; // packages/style-runtime/src/index.ts var STYLE_HASH_ATTRIBUTE = "data-wp-hash"; function getRuntime() { const globalScope = globalThis; if (globalScope.__wpStyleRuntime) { return globalScope.__wpStyleRuntime; } globalScope.__wpStyleRuntime = { documents: /* @__PURE__ */ new Map(), styles: /* @__PURE__ */ new Map(), injectedStyles: /* @__PURE__ */ new WeakMap() }; if (typeof document !== "undefined") { registerDocument(document); } return globalScope.__wpStyleRuntime; } function documentContainsStyleHash(targetDocument, hash) { if (!targetDocument.head) { return false; } for (const style of targetDocument.head.querySelectorAll( `style[${STYLE_HASH_ATTRIBUTE}]` )) { if (style.getAttribute(STYLE_HASH_ATTRIBUTE) === hash) { return true; } } return false; } function injectStyle(targetDocument, hash, css) { if (!targetDocument.head) { return; } const runtime = getRuntime(); let injectedStyles = runtime.injectedStyles.get(targetDocument); if (!injectedStyles) { injectedStyles = /* @__PURE__ */ new Set(); runtime.injectedStyles.set(targetDocument, injectedStyles); } if (injectedStyles.has(hash)) { return; } if (documentContainsStyleHash(targetDocument, hash)) { injectedStyles.add(hash); return; } const style = targetDocument.createElement("style"); style.setAttribute(STYLE_HASH_ATTRIBUTE, hash); style.appendChild(targetDocument.createTextNode(css)); targetDocument.head.appendChild(style); injectedStyles.add(hash); } function registerDocument(targetDocument) { const runtime = getRuntime(); runtime.documents.set( targetDocument, (runtime.documents.get(targetDocument) ?? 0) + 1 ); for (const [hash, css] of runtime.styles) { injectStyle(targetDocument, hash, css); } return () => { const count = runtime.documents.get(targetDocument); if (count === void 0) { return; } if (count <= 1) { runtime.documents.delete(targetDocument); return; } runtime.documents.set(targetDocument, count - 1); }; } function registerStyle(hash, css) { const runtime = getRuntime(); runtime.styles.set(hash, css); for (const targetDocument of runtime.documents.keys()) { injectStyle(targetDocument, hash, css); } } // packages/widget-dashboard/src/components/widget-footer/widget-footer.module.css if (typeof process === "undefined" || process.env.NODE_ENV !== "test") { registerStyle("57b3a79a04", ".a3e067538ad701bf__widget-footer{border-block-start-color:var(--wpds-color-stroke-surface-neutral-weak,#f0f0f0);border-block-start-style:solid;border-block-start-width:var(--wpds-border-width-xs,1px);padding-block:var(--wpds-dimension-padding-md,12px);padding-inline:var(--wp-ui-card-padding)}.b7d8b6ddfcf30003__compact-actions{margin-inline-start:auto}._767e14b4bc49e9b5__prefixed-action{align-items:center;display:inline-flex;gap:var(--wpds-dimension-gap-xs,4px)}._031b4fd8e77c13ce__icon-action{--wp-ui-button-aspect-ratio:1;--wp-ui-button-padding-inline:0px;--wp-ui-button-min-width:unset}"); } var widget_footer_default = { "widget-footer": "a3e067538ad701bf__widget-footer", "compact-actions": "b7d8b6ddfcf30003__compact-actions", "prefixed-action": "_767e14b4bc49e9b5__prefixed-action", "icon-action": "_031b4fd8e77c13ce__icon-action" }; // packages/widget-dashboard/src/components/widget-footer/widget-footer.tsx import { Fragment, jsx, jsxs } from "react/jsx-runtime"; function IconAction({ action, routeRender }) { const label = action.openInNewTab ? sprintf( /* translators: %s: action label. */ __("%s (opens in a new tab)"), action.label ) : action.label; return /* @__PURE__ */ jsxs(Tooltip.Root, { children: [ /* @__PURE__ */ jsx( Tooltip.Trigger, { render: /* @__PURE__ */ jsx( LinkButton, { variant: "minimal", tone: "neutral", size: "compact", className: widget_footer_default["icon-action"], "aria-label": label, ...routeRender ? {} : { href: action.href, download: action.download }, render: routeRender ?? (action.openInNewTab ? ( /* href and content merge in at runtime. */ // eslint-disable-next-line jsx-a11y/anchor-has-content, jsx-a11y/anchor-is-valid /* @__PURE__ */ jsx("a", { target: "_blank", rel: "noopener noreferrer" }) ) : void 0) } ), children: /* @__PURE__ */ jsx(LinkButton.Icon, { icon: action.icon }) } ), /* @__PURE__ */ jsx(Tooltip.Popup, { children: action.label }) ] }); } function WidgetFooter({ actions, editMode = false }) { const { links } = useWidgetHost(); const HostLink = links?.Link; if (actions.length === 0) { return null; } const highActions = actions.filter( (action) => action.relevance === "high" ); const mediumActions = actions.filter( (action) => action.relevance === "medium" ); return /* @__PURE__ */ jsxs( Stack, { direction: "row", align: "center", gap: "lg", className: widget_footer_default["widget-footer"], ...editMode ? { inert: "true" } : {}, children: [ highActions.length > 0 && /* @__PURE__ */ jsx(Stack, { direction: "row", align: "center", gap: "lg", wrap: "wrap", children: highActions.map((action) => { const path = getActionRoute(links, action); const className = action.icon ? widget_footer_default["prefixed-action"] : void 0; const children = /* @__PURE__ */ jsxs(Fragment, { children: [ action.icon && /* @__PURE__ */ jsx(Icon, { icon: action.icon }), action.label ] }); return path !== null && HostLink ? /* @__PURE__ */ jsx( Link, { className, render: /* @__PURE__ */ jsx(HostLink, { path }), children }, action.id ) : /* @__PURE__ */ jsx( Link, { href: action.href, download: action.download, openInNewTab: action.openInNewTab, className, children }, action.id ); }) }), mediumActions.length > 0 && /* @__PURE__ */ jsx( Stack, { direction: "row", align: "center", gap: "xs", className: widget_footer_default["compact-actions"], children: /* @__PURE__ */ jsx(Tooltip.Provider, { children: mediumActions.map((action) => { const path = getActionRoute(links, action); const routeRender = path !== null && HostLink ? /* @__PURE__ */ jsx(HostLink, { path }) : void 0; if (action.icon) { return /* @__PURE__ */ jsx( IconAction, { action: { ...action, icon: action.icon }, routeRender }, action.id ); } return routeRender ? /* @__PURE__ */ jsx(Link, { render: routeRender, children: action.label }, action.id) : /* @__PURE__ */ jsx( Link, { href: action.href, download: action.download, openInNewTab: action.openInNewTab, children: action.label }, action.id ); }) }) } ) ] } ); } export { WidgetFooter }; //# sourceMappingURL=widget-footer.mjs.map