UNPKG

@wordpress/widget-dashboard

Version:

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

32 lines (31 loc) • 1.1 kB
import { useCallback, useEffect, useRef } from "@wordpress/element"; // packages/widget-dashboard/src/components/commands/use-pending-when-edit-mode.ts /** * Runs an action immediately when already in edit mode. Otherwise enables * edit mode and runs the action after edit mode becomes active. * * @param {UsePendingWhenEditModeProps} params Current edit mode and the change handler. * @return {UsePendingWhenEditModeResult} Runs an action, deferring it until edit mode is active. */ function usePendingWhenEditMode({ editMode, onEditChange }) { const pendingRef = useRef(null); const wasEditingRef = useRef(editMode); useEffect(() => { if (!wasEditingRef.current && editMode && pendingRef.current) { const action = pendingRef.current; pendingRef.current = null; action(); } wasEditingRef.current = editMode; }, [editMode]); return useCallback((action) => { if (editMode) { action(); return; } pendingRef.current = action; onEditChange?.(true); }, [editMode, onEditChange]); } export { usePendingWhenEditMode }; //# sourceMappingURL=use-pending-when-edit-mode.mjs.map