@wordpress/widget-dashboard
Version:
Stateless rendering engine for widget dashboards: the WidgetDashboard compound component and its grid-settings kit.
65 lines (64 loc) • 2.38 kB
JavaScript
import { __ } from "@wordpress/i18n";
import { moreVertical, trash } from "@wordpress/icons";
import { IconButton, Menu } from "@wordpress/ui";
import { useDashboardInternalContext } from "../../context/dashboard-context.mjs";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
// packages/widget-dashboard/src/components/widget-layout-controls/widget-layout-controls.tsx
/**
* Customize-mode controls: width menu and removal, each following the
* policy's answer for its operation.
*
* @param {WidgetLayoutControlsProps} props Component props.
*/
function WidgetLayoutControls({ widget, canRemove = true, canResize = true }) {
const { layout, onLayoutChange } = useDashboardInternalContext();
const width = widget.placement?.width;
const updateWidth = (nextWidth) => {
const nextLayout = layout.map((currentWidget) => currentWidget.uuid === widget.uuid ? {
...currentWidget,
placement: {
...currentWidget.placement,
width: nextWidth
}
} : currentWidget);
onLayoutChange(nextLayout);
};
const onNamedWidthChange = (nextWidth) => {
updateWidth(nextWidth);
};
const onRemove = () => {
onLayoutChange(layout.filter((currentWidget) => currentWidget.uuid !== widget.uuid));
};
return /* @__PURE__ */ jsxs(Fragment, { children: [canResize && /* @__PURE__ */ jsxs(Menu.Root, { children: [/* @__PURE__ */ jsx(Menu.Trigger, { render: /* @__PURE__ */ jsx(IconButton, {
icon: moreVertical,
label: __("Widget options"),
size: "compact",
variant: "minimal",
tone: "neutral"
}) }), /* @__PURE__ */ jsx(Menu.Popup, { children: /* @__PURE__ */ jsxs(Menu.RadioGroup, {
value: width ?? null,
onValueChange: onNamedWidthChange,
children: [
/* @__PURE__ */ jsx(Menu.GroupLabel, { children: __("Width") }),
/* @__PURE__ */ jsx(Menu.RadioItem, {
value: "fill",
closeOnClick: true,
children: /* @__PURE__ */ jsx(Menu.ItemLabel, { children: __("Use available width") })
}),
/* @__PURE__ */ jsx(Menu.RadioItem, {
value: "full",
closeOnClick: true,
children: /* @__PURE__ */ jsx(Menu.ItemLabel, { children: __("Make full width") })
})
]
}) })] }), canRemove && /* @__PURE__ */ jsx(IconButton, {
icon: trash,
label: __("Remove"),
size: "compact",
variant: "minimal",
tone: "neutral",
onClick: onRemove
})] });
}
export { WidgetLayoutControls };
//# sourceMappingURL=widget-layout-controls.mjs.map