UNPKG

@wordpress/widget-dashboard

Version:

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

62 lines (61 loc) • 2.35 kB
"use strict"; Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } }); let _wordpress_element = require("@wordpress/element"); let _wordpress_widget_primitives = require("@wordpress/widget-primitives"); let ______context_dashboard_context_tsx = require("../../context/dashboard-context.cjs"); let ______utils_runtime_actions_map_ts = require("../../utils/runtime-actions-map.cjs"); let react_jsx_runtime = require("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 } = (0, ______context_dashboard_context_tsx.useDashboardInternalContext)(); const canEdit = canPerform({ operation: "edit", widget, widgetType }); const setAttributes = (0, _wordpress_element.useCallback)((next) => { onLayoutChange(layout.map((w) => w.uuid === widget.uuid ? { ...w, attributes: { ...w.attributes, ...next } } : w)); }, [ widget.uuid, layout, onLayoutChange ]); const renderId = (0, _wordpress_element.useId)(); const host = (0, _wordpress_element.useMemo)(() => ({ actions: { declare: (actions) => (0, ______utils_runtime_actions_map_ts.declareRuntimeActions)(runtimeActions, widget.uuid, renderId, actions) } }), [ runtimeActions, widget.uuid, renderId ]); return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_widget_primitives.WidgetHostProvider, { value: host, children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_widget_primitives.WidgetRender, { widgetType, attributes: widget.attributes, setAttributes: canEdit ? setAttributes : void 0, resolveWidgetModule }) }); } exports.WidgetRender = WidgetRender; //# sourceMappingURL=widget-render.cjs.map