UNPKG

@wordpress/widget-dashboard

Version:

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

255 lines (250 loc) • 9.86 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; 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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // packages/widget-dashboard/src/components/widget-footer/widget-footer.tsx var widget_footer_exports = {}; __export(widget_footer_exports, { WidgetFooter: () => WidgetFooter }); module.exports = __toCommonJS(widget_footer_exports); var import_i18n = require("@wordpress/i18n"); var import_ui = require("@wordpress/ui"); var import_widget_primitives = require("@wordpress/widget-primitives"); var import_get_action_route = require("../widget-actions/get-action-route.cjs"); // 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 var import_jsx_runtime = require("react/jsx-runtime"); function IconAction({ action, routeRender }) { const label = action.openInNewTab ? (0, import_i18n.sprintf)( /* translators: %s: action label. */ (0, import_i18n.__)("%s (opens in a new tab)"), action.label ) : action.label; return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ui.Tooltip.Root, { children: [ /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_ui.Tooltip.Trigger, { render: /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_ui.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__ */ (0, import_jsx_runtime.jsx)("a", { target: "_blank", rel: "noopener noreferrer" }) ) : void 0) } ), children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ui.LinkButton.Icon, { icon: action.icon }) } ), /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ui.Tooltip.Popup, { children: action.label }) ] }); } function WidgetFooter({ actions, editMode = false }) { const { links } = (0, import_widget_primitives.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__ */ (0, import_jsx_runtime.jsxs)( import_ui.Stack, { direction: "row", align: "center", gap: "lg", className: widget_footer_default["widget-footer"], ...editMode ? { inert: "true" } : {}, children: [ highActions.length > 0 && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ui.Stack, { direction: "row", align: "center", gap: "lg", wrap: "wrap", children: highActions.map((action) => { const path = (0, import_get_action_route.getActionRoute)(links, action); const className = action.icon ? widget_footer_default["prefixed-action"] : void 0; const children = /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [ action.icon && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ui.Icon, { icon: action.icon }), action.label ] }); return path !== null && HostLink ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_ui.Link, { className, render: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(HostLink, { path }), children }, action.id ) : /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_ui.Link, { href: action.href, download: action.download, openInNewTab: action.openInNewTab, className, children }, action.id ); }) }), mediumActions.length > 0 && /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_ui.Stack, { direction: "row", align: "center", gap: "xs", className: widget_footer_default["compact-actions"], children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ui.Tooltip.Provider, { children: mediumActions.map((action) => { const path = (0, import_get_action_route.getActionRoute)(links, action); const routeRender = path !== null && HostLink ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(HostLink, { path }) : void 0; if (action.icon) { return /* @__PURE__ */ (0, import_jsx_runtime.jsx)( IconAction, { action: { ...action, icon: action.icon }, routeRender }, action.id ); } return routeRender ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ui.Link, { render: routeRender, children: action.label }, action.id) : /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_ui.Link, { href: action.href, download: action.download, openInNewTab: action.openInNewTab, children: action.label }, action.id ); }) }) } ) ] } ); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { WidgetFooter }); //# sourceMappingURL=widget-footer.cjs.map