@wordpress/widget-dashboard
Version:
Stateless rendering engine for widget dashboards: the WidgetDashboard compound component and its grid-settings kit.
93 lines (92 loc) • 2.85 kB
JavaScript
// packages/widget-dashboard/src/components/widget-layout-controls/widget-layout-controls.tsx
import { privateApis as componentsPrivateApis } from "@wordpress/components";
import { __ } from "@wordpress/i18n";
import { moreVertical, trash } from "@wordpress/icons";
import { IconButton } from "@wordpress/ui";
import { unlock } from "../../lock-unlock.mjs";
import { useDashboardInternalContext } from "../../context/dashboard-context.mjs";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
var { Menu } = unlock(componentsPrivateApis);
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, { children: [
/* @__PURE__ */ jsx(
Menu.TriggerButton,
{
render: /* @__PURE__ */ jsx(
IconButton,
{
icon: moreVertical,
label: __("Widget options"),
size: "compact",
variant: "minimal",
tone: "neutral"
}
)
}
),
/* @__PURE__ */ jsx(Menu.Popover, { children: /* @__PURE__ */ jsxs(Menu.Group, { children: [
/* @__PURE__ */ jsx(Menu.GroupLabel, { children: __("Width") }),
/* @__PURE__ */ jsx(
Menu.Item,
{
disabled: width === "fill",
onClick: () => onNamedWidthChange("fill"),
children: /* @__PURE__ */ jsx(Menu.ItemLabel, { children: __("Use available width") })
}
),
/* @__PURE__ */ jsx(
Menu.Item,
{
disabled: width === "full",
onClick: () => onNamedWidthChange("full"),
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