UNPKG

@wordpress/widget-dashboard

Version:

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

44 lines (43 loc) • 1.32 kB
import { useCallback } from "@wordpress/element"; import { useDashboardInternalContext } from "../../context/dashboard-context.mjs"; import { useDashboardUIContext } from "../../context/ui-context.mjs"; // packages/widget-dashboard/src/components/widget-settings/use-widget-settings-toggle.ts /** * Shared open/close logic for an instance's settings surface. The surface is * mounted once at the dashboard root and reacts to the `uuid` written into * the UI context; controls drive it through this hook. * * @param {DashboardWidget< unknown >} widget The instance whose settings surface the control drives. */ function useWidgetSettingsToggle(widget) { const { settingsWidgetUuid, setSettingsWidgetUuid } = useDashboardUIContext(); const { cancel, flushAutoSave } = useDashboardInternalContext(); const isOpen = settingsWidgetUuid === widget.uuid; const open = useCallback(() => { flushAutoSave(); setSettingsWidgetUuid(widget.uuid); }, [ flushAutoSave, setSettingsWidgetUuid, widget.uuid ]); return { isOpen, open, toggle: useCallback(() => { if (isOpen) { cancel(); setSettingsWidgetUuid(null); return; } open(); }, [ isOpen, cancel, setSettingsWidgetUuid, open ]) }; } export { useWidgetSettingsToggle }; //# sourceMappingURL=use-widget-settings-toggle.mjs.map