@wordpress/widget-dashboard
Version:
Stateless rendering engine for widget dashboards: the WidgetDashboard compound component and its grid-settings kit.
45 lines (44 loc) • 1.93 kB
JavaScript
import { useCallback } from "@wordpress/element";
import { __ } from "@wordpress/i18n";
import { Dialog } from "@wordpress/ui";
import { useDashboardInternalContext } from "../../context/dashboard-context.mjs";
import { useDashboardUIContext } from "../../context/ui-context.mjs";
import { createDashboardWidget } from "../../utils/create-dashboard-widget/index.mjs";
import { WidgetPicker } from "../widget-picker/index.mjs";
import { jsx, jsxs } from "react/jsx-runtime";
// packages/widget-dashboard/src/components/widget-inserter/widget-inserter.tsx
/**
* Modal widget inserter, mounted by the engine and shown while `inserterOpen`
* is set in the shared UI context. Selecting widgets appends them to the
* layout and closes the dialog.
*/
function WidgetInserter() {
const { layout, onLayoutChange } = useDashboardInternalContext();
const { inserterOpen, setInserterOpen } = useDashboardUIContext();
const insertWidgets = useCallback((widgetTypes) => {
if (widgetTypes.length > 0) {
const newWidgets = widgetTypes.map((widgetType) => createDashboardWidget(widgetType));
onLayoutChange([...layout, ...newWidgets]);
}
setInserterOpen(false);
}, [
layout,
onLayoutChange,
setInserterOpen
]);
if (!inserterOpen) return null;
return /* @__PURE__ */ jsx(Dialog.Root, {
open: inserterOpen,
onOpenChange: setInserterOpen,
children: /* @__PURE__ */ jsxs(Dialog.Popup, {
size: "full",
portal: /* @__PURE__ */ jsx(Dialog.Portal, {
container: document.body,
style: { "--wp-ui-dialog-z-index": 99999 }
}),
children: [/* @__PURE__ */ jsxs(Dialog.Header, { children: [/* @__PURE__ */ jsx(Dialog.Title, { children: __("Add widget") }), /* @__PURE__ */ jsx(Dialog.CloseIconButton, {})] }), /* @__PURE__ */ jsx(Dialog.Content, { children: /* @__PURE__ */ jsx(WidgetPicker, { onSelect: insertWidgets }) })]
})
});
}
export { WidgetInserter };
//# sourceMappingURL=widget-inserter.mjs.map