@wordpress/widget-dashboard
Version:
Stateless rendering engine for widget dashboards: the WidgetDashboard compound component and its grid-settings kit.
57 lines (56 loc) • 1.95 kB
JavaScript
import { useCallback, useId, useMemo } from "@wordpress/element";
import { WidgetHostProvider, WidgetRender as WidgetRender$1 } from "@wordpress/widget-primitives";
import { useDashboardInternalContext } from "../../context/dashboard-context.mjs";
import { declareRuntimeActions } from "../../utils/runtime-actions-map.mjs";
import { jsx } from "react/jsx-runtime";
// packages/widget-dashboard/src/components/widget-render/widget-render.tsx
/**
* Adapter around the host-agnostic `WidgetRender` primitive. Bridges the
* dashboard context (`resolveWidgetModule`, layout state) and turns
* layout-level attribute updates into the per-instance `setAttributes`
* callback the render contract expects. When the policy denies `edit`,
* the widget renders read-only: it receives no `setAttributes`.
*
* Lends the widget the `actions` capability, bound to this render of the
* instance.
*
* @param {WidgetRenderProps} props Component props.
*/
function WidgetRender({ widget, widgetType }) {
const { layout, onLayoutChange, resolveWidgetModule, canPerform, runtimeActions } = useDashboardInternalContext();
const canEdit = canPerform({
operation: "edit",
widget,
widgetType
});
const setAttributes = useCallback((next) => {
onLayoutChange(layout.map((w) => w.uuid === widget.uuid ? {
...w,
attributes: {
...w.attributes,
...next
}
} : w));
}, [
widget.uuid,
layout,
onLayoutChange
]);
const renderId = useId();
const host = useMemo(() => ({ actions: { declare: (actions) => declareRuntimeActions(runtimeActions, widget.uuid, renderId, actions) } }), [
runtimeActions,
widget.uuid,
renderId
]);
return /* @__PURE__ */ jsx(WidgetHostProvider, {
value: host,
children: /* @__PURE__ */ jsx(WidgetRender$1, {
widgetType,
attributes: widget.attributes,
setAttributes: canEdit ? setAttributes : void 0,
resolveWidgetModule
})
});
}
export { WidgetRender };
//# sourceMappingURL=widget-render.mjs.map