@wordpress/widget-dashboard
Version:
Stateless rendering engine for widget dashboards: the WidgetDashboard compound component and its grid-settings kit.
8 lines (7 loc) • 2.9 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../../src/context/ui-context.tsx"],
"sourcesContent": ["import type { ReactNode } from 'react';\nimport {\n\tcreateContext,\n\tuseContext,\n\tuseMemo,\n\tuseState,\n} from '@wordpress/element';\n\ninterface DashboardUIContextValue {\n\tinserterOpen: boolean;\n\tsetInserterOpen: ( next: boolean ) => void;\n\n\tresetDialogOpen: boolean;\n\tsetResetDialogOpen: ( next: boolean ) => void;\n\n\t/**\n\t * `uuid` of the instance whose settings drawer is open, or `null`\n\t * when no settings drawer is showing. The per-instance gear in the\n\t * chrome sets it; the single `WidgetSettings` at the root reads\n\t * it.\n\t */\n\tsettingsWidgetUuid: string | null;\n\tsetSettingsWidgetUuid: ( next: string | null ) => void;\n}\n\nconst Context = createContext< DashboardUIContextValue | null >( null );\n\n/**\n * Accesses the shared UI state: overlay open flags and the active settings\n * instance with its placement. Throws when called outside `WidgetDashboard`.\n */\nexport function useDashboardUIContext(): DashboardUIContextValue {\n\tconst ctx = useContext( Context );\n\tif ( ! ctx ) {\n\t\tthrow new Error(\n\t\t\t'Dashboard compound used outside a WidgetDashboard subtree.'\n\t\t);\n\t}\n\treturn ctx;\n}\n\ninterface ProviderProps {\n\tchildren: ReactNode;\n}\n\n/**\n * Holds transient UI state shared across compounds (the inserter modal and\n * the per-instance settings drawer). Kept separate from the data context so\n * that data mutations don't churn the UI state and vice-versa.\n *\n * @param {ProviderProps} props Component props.\n */\nexport function WidgetDashboardUIProvider( { children }: ProviderProps ) {\n\tconst [ inserterOpen, setInserterOpen ] = useState( false );\n\tconst [ resetDialogOpen, setResetDialogOpen ] = useState( false );\n\tconst [ settingsWidgetUuid, setSettingsWidgetUuid ] = useState<\n\t\tstring | null\n\t>( null );\n\n\tconst value = useMemo< DashboardUIContextValue >(\n\t\t() => ( {\n\t\t\tinserterOpen,\n\t\t\tsetInserterOpen,\n\t\t\tresetDialogOpen,\n\t\t\tsetResetDialogOpen,\n\t\t\tsettingsWidgetUuid,\n\t\t\tsetSettingsWidgetUuid,\n\t\t} ),\n\t\t[ inserterOpen, resetDialogOpen, settingsWidgetUuid ]\n\t);\n\n\treturn <Context.Provider value={ value }>{ children }</Context.Provider>;\n}\n"],
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,qBAKO;AAiEC;AA9CR,IAAM,cAAU,8BAAiD,IAAK;AAM/D,SAAS,wBAAiD;AAChE,QAAM,UAAM,2BAAY,OAAQ;AAChC,MAAK,CAAE,KAAM;AACZ,UAAM,IAAI;AAAA,MACT;AAAA,IACD;AAAA,EACD;AACA,SAAO;AACR;AAaO,SAAS,0BAA2B,EAAE,SAAS,GAAmB;AACxE,QAAM,CAAE,cAAc,eAAgB,QAAI,yBAAU,KAAM;AAC1D,QAAM,CAAE,iBAAiB,kBAAmB,QAAI,yBAAU,KAAM;AAChE,QAAM,CAAE,oBAAoB,qBAAsB,QAAI,yBAEnD,IAAK;AAER,QAAM,YAAQ;AAAA,IACb,OAAQ;AAAA,MACP;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACD;AAAA,IACA,CAAE,cAAc,iBAAiB,kBAAmB;AAAA,EACrD;AAEA,SAAO,4CAAC,QAAQ,UAAR,EAAiB,OAAkB,UAAU;AACtD;",
"names": []
}