@wordpress/widget-dashboard
Version:
Stateless rendering engine for widget dashboards: the WidgetDashboard compound component and its grid-settings kit.
1 lines • 2.73 kB
Source Map (JSON)
{"version":3,"file":"use-widget-settings-toggle.cjs","names":["useDashboardUIContext","useDashboardInternalContext","useCallback"],"sources":["../../../src/components/widget-settings/use-widget-settings-toggle.ts"],"sourcesContent":["import { useCallback } from '@wordpress/element';\nimport { useDashboardInternalContext } from '../../context/dashboard-context';\nimport { useDashboardUIContext } from '../../context/ui-context';\nimport type { DashboardWidget } from '../../types';\n\ninterface WidgetSettingsToggle {\n\t/**\n\t * Whether this instance's settings surface is the open one.\n\t */\n\tisOpen: boolean;\n\n\t/**\n\t * Opens the settings surface for this instance.\n\t */\n\topen: () => void;\n\n\t/**\n\t * Closes the surface when this instance already owns it (discarding\n\t * staged edits), opens it otherwise.\n\t */\n\ttoggle: () => void;\n}\n\n/**\n * Shared open/close logic for an instance's settings surface. The surface is\n * mounted once at the dashboard root and reacts to the `uuid` written into\n * the UI context; controls drive it through this hook.\n *\n * @param {DashboardWidget< unknown >} widget The instance whose settings surface the control drives.\n */\nexport function useWidgetSettingsToggle(\n\twidget: DashboardWidget< unknown >\n): WidgetSettingsToggle {\n\tconst { settingsWidgetUuid, setSettingsWidgetUuid } =\n\t\tuseDashboardUIContext();\n\tconst { cancel, flushAutoSave } = useDashboardInternalContext();\n\n\tconst isOpen = settingsWidgetUuid === widget.uuid;\n\n\tconst open = useCallback( () => {\n\t\t// Persist any pending prominent-surface edit before opening, so the\n\t\t// settings surface's edits stay isolated and its Cancel discards only\n\t\t// its own changes.\n\t\tflushAutoSave();\n\t\tsetSettingsWidgetUuid( widget.uuid );\n\t}, [ flushAutoSave, setSettingsWidgetUuid, widget.uuid ] );\n\n\tconst toggle = useCallback( () => {\n\t\t// Re-clicking the open instance's control closes the settings surface,\n\t\t// discarding staged edits like any other non-Save exit.\n\t\tif ( isOpen ) {\n\t\t\tcancel();\n\t\t\tsetSettingsWidgetUuid( null );\n\t\t\treturn;\n\t\t}\n\t\topen();\n\t}, [ isOpen, cancel, setSettingsWidgetUuid, open ] );\n\n\treturn { isOpen, open, toggle };\n}\n"],"mappings":";;;;;;;;;;;;;;;;AA8BA,SAAgB,wBACf,QACuB;CACvB,MAAM,EAAE,oBAAoB,2BAAA,GAC3BA,6BAAAA,sBAAAA,CAAsB;CACvB,MAAM,EAAE,QAAQ,mBAAA,GAAkBC,oCAAAA,4BAAAA,CAA4B;CAE9D,MAAM,SAAS,uBAAuB,OAAO;CAE7C,MAAM,QAAA,GAAOC,mBAAAA,YAAAA,OAAmB;EAI/B,cAAc;EACd,sBAAuB,OAAO,IAAK;CACpC,GAAG;EAAE;EAAe;EAAuB,OAAO;CAAK,CAAE;CAazD,OAAO;EAAE;EAAQ;EAAM,SAAA,GAXRA,mBAAAA,YAAAA,OAAmB;GAGjC,IAAK,QAAS;IACb,OAAO;IACP,sBAAuB,IAAK;IAC5B;GACD;GACA,KAAK;EACN,GAAG;GAAE;GAAQ;GAAQ;GAAuB;EAAK,CAErB;CAAE;AAC/B"}