@wordpress/widget-dashboard
Version:
Stateless rendering engine for widget dashboards: the WidgetDashboard compound component and its grid-settings kit.
215 lines (214 loc) • 9.41 kB
JavaScript
import { __, sprintf } from "@wordpress/i18n";
import { inertValue } from "@wordpress/react-inert-value";
import { ButtonLink, Icon, Link, Stack, Tooltip } from "@wordpress/ui";
import { HostLink } from "@wordpress/widget-primitives";
import { isCallbackAction } from "../../utils/action-fulfillment.mjs";
import { CallbackAction } from "../widget-actions/callback-action.mjs";
import { useRunActions } from "../widget-actions/use-run-actions.mjs";
import { jsx, jsxs } from "react/jsx-runtime";
// packages/style-runtime/src/index.ts
const STYLE_HASH_ATTRIBUTE = "data-wp-hash";
/**
* Returns the shared style runtime registry.
*
* The registry is stored on `globalThis` so separately bundled copies of this
* package can coordinate through the same document and style maps.
*
* @return The shared runtime registry.
*/
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;
}
/**
* Checks whether a document already contains a style tag for a hash.
*
* @param targetDocument Document to inspect.
* @param hash Stable hash for the transformed CSS.
*
* @return Whether the style hash already exists in the document.
*/
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;
}
/**
* Injects a registered style into a document, unless that document already
* contains a style tag for the same hash.
*
* @param targetDocument Document to inject the style into.
* @param hash Stable hash for the transformed CSS.
* @param css CSS text to inject.
*/
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);
}
/**
* Registers a document as a style injection target.
*
* Existing registered styles are replayed into the document immediately.
* Documents are reference-counted so multiple providers can safely register the
* same document without one cleanup removing it while another registration is
* still active.
*
* @param targetDocument Document to receive registered styles.
* @return Cleanup function that unregisters this document registration.
*/
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);
};
}
/**
* Registers a style and injects it into all registered documents.
*
* The hash is used as the deduplication key, so calling this repeatedly with
* the same hash will not add duplicate style tags to a document.
* Registered styles are retained for the lifetime of the page so they can be
* replayed into documents that are registered later.
*
* @param hash Stable hash for the transformed CSS.
* @param css CSS text to inject.
*/
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("aeb041d5fa", ".fa75498a401a0843__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);box-sizing:border-box;min-block-size:calc(var(--wpds-typography-line-height-sm, 20px) + var(--wpds-dimension-padding-md, 12px)*2);padding-block-end:var(--wpds-dimension-padding-xs,4px);padding-block-start:var(--wpds-dimension-padding-xs,4px);padding-inline:var(--wp-ui-card-padding)}.aaed5cb0b2dfc5a5__compact-actions{margin-inline-start:auto}._46006e230cfe85ca__link-action{align-items:center;display:inline-flex}._8ff36ecb7692d55a__prefixed-action{align-items:center;display:flex;gap:var(--wpds-dimension-gap-xs,4px);text-decoration:inherit}._09247aac5861cd2c__icon-action{--wp-ui-button-aspect-ratio:1;--wp-ui-button-padding-inline:0px;--wp-ui-button-min-width:unset}");
var widget_footer_module_default = {
"widget-footer": "fa75498a401a0843__widget-footer",
"compact-actions": "aaed5cb0b2dfc5a5__compact-actions",
"link-action": "_46006e230cfe85ca__link-action",
"prefixed-action": "_8ff36ecb7692d55a__prefixed-action",
"icon-action": "_09247aac5861cd2c__icon-action"
};
// packages/widget-dashboard/src/components/widget-footer/widget-footer.tsx
/**
* Icon-only link. `openInNewTab` mounts the target on the anchor rather
* than through `Link`, whose external glyph would double the icon; the
* new-tab hint joins the accessible name.
*
* @param {IconActionProps} props Component props.
*/
function IconAction({ action }) {
const label = action.openInNewTab ? sprintf(__("%s (opens in a new tab)"), action.label) : action.label;
return /* @__PURE__ */ jsxs(Tooltip.Root, { children: [/* @__PURE__ */ jsx(Tooltip.Trigger, {
render: /* @__PURE__ */ jsx(ButtonLink, {
variant: "minimal",
tone: "neutral",
size: "compact",
className: widget_footer_module_default["icon-action"],
"aria-label": label,
render: /* @__PURE__ */ jsx(HostLink, {
href: action.href,
download: action.download,
target: action.openInNewTab ? "_blank" : void 0
})
}),
children: /* @__PURE__ */ jsx(ButtonLink.Icon, { icon: action.icon })
}), /* @__PURE__ */ jsx(Tooltip.Popup, { children: action.label })] });
}
/**
* Persistent strip under the widget body. `'high'` actions mount as leading
* text affordances, a declared icon riding as prefix; `'medium'` actions as
* trailing compact affordances, icon-only when they declare an icon. Every
* link is a real anchor; a callback action mounts a button, disabled while
* its promise settles.
*
* A target the host recognizes as one of its own routes mounts the host
* router's link through `HostLink`, so it navigates client-side.
*
* @param {WidgetFooterProps} props Component props.
*/
function WidgetFooter({ uuid, actions, editMode = false }) {
const { run, pendingIds } = useRunActions(uuid);
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_module_default["widget-footer"],
inert: inertValue(editMode),
children: [highActions.length > 0 && /* @__PURE__ */ jsx(Stack, {
direction: "row",
align: "center",
gap: "lg",
wrap: "wrap",
children: highActions.map((action) => isCallbackAction(action) ? /* @__PURE__ */ jsx(CallbackAction, {
action,
isPending: pendingIds.has(action.id),
onRun: run
}, action.id) : /* @__PURE__ */ jsx(Link, {
className: widget_footer_module_default["link-action"],
download: action.download,
openInNewTab: action.openInNewTab,
render: /* @__PURE__ */ jsx(HostLink, { href: action.href }),
children: /* @__PURE__ */ jsxs("span", {
className: widget_footer_module_default["prefixed-action"],
children: [action.icon && /* @__PURE__ */ jsx(Icon, { icon: action.icon }), action.label]
})
}, action.id))
}), mediumActions.length > 0 && /* @__PURE__ */ jsx(Stack, {
direction: "row",
align: "center",
gap: "xs",
className: widget_footer_module_default["compact-actions"],
children: /* @__PURE__ */ jsx(Tooltip.Provider, { children: mediumActions.map((action) => {
if (isCallbackAction(action)) return /* @__PURE__ */ jsx(CallbackAction, {
action,
isPending: pendingIds.has(action.id),
onRun: run,
compact: true
}, action.id);
return action.icon ? /* @__PURE__ */ jsx(IconAction, { action: {
...action,
icon: action.icon
} }, action.id) : /* @__PURE__ */ jsx(Link, {
download: action.download,
openInNewTab: action.openInNewTab,
render: /* @__PURE__ */ jsx(HostLink, { href: action.href }),
children: action.label
}, action.id);
}) })
})]
});
}
export { WidgetFooter };
//# sourceMappingURL=widget-footer.mjs.map