{"version":3,"file":"createStoreContext.cjs","names":[],"sources":["../src/createStoreContext.tsx"],"sourcesContent":["// eslint-disable-next-line import/consistent-type-specifier-style\nimport { type ComponentChildren, createContext } from 'preact'\nimport { useContext } from 'preact/hooks'\n\n/**\n * Creates a typed Preact context for sharing a bundle of atoms and stores with\n * a subtree.\n *\n * The returned `StoreProvider` only transports the provided object through\n * Preact context. Consumers destructure the contextual atoms and stores, then\n * compose them with the existing hooks like {@link useSelector} and\n * {@link useAtom}.\n *\n * The object shape is preserved exactly, so keyed atoms and stores remain fully\n * typed when read back with `useStoreContext()`.\n *\n * @example\n * ```tsx\n * const { StoreProvider, useStoreContext } = createStoreContext<{\n *   countAtom: Atom<number>\n *   totalsStore: Store<{ count: number }>\n * }>()\n *\n * function CountButton() {\n *   const { countAtom, totalsStore } = useStoreContext()\n *   const count = useSelector(countAtom)\n *   const total = useSelector(totalsStore, (state) => state.count)\n *\n *   return (\n *     <button\n *       type=\"button\"\n *       onClick={() =>\n *         totalsStore.setState((state) => ({ ...state, count: state.count + 1 }))\n *       }\n *     >\n *       {count} / {total}\n *     </button>\n *   )\n * }\n * ```\n *\n * @throws When `useStoreContext()` is called outside the matching `StoreProvider`.\n */\nexport function createStoreContext<TValue extends object>() {\n  const Context = createContext<TValue | null>(null)\n  Context.displayName = 'StoreContext'\n\n  function StoreProvider({\n    children,\n    value,\n  }: {\n    children?: ComponentChildren\n    value: TValue\n  }) {\n    return <Context.Provider value={value}>{children}</Context.Provider>\n  }\n\n  function useStoreContext() {\n    const value = useContext(Context)\n\n    if (value === null) {\n      throw new Error('Missing StoreProvider for StoreContext')\n    }\n\n    return value\n  }\n\n  return {\n    StoreProvider,\n    useStoreContext,\n  }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2CA,SAAgB,qBAA4C;CAC1D,MAAM,oCAAuC,KAAK;AAClD,SAAQ,cAAc;CAEtB,SAAS,cAAc,EACrB,UACA,SAIC;AACD,SAAO,4CAAC,QAAQ,UAAT;GAAyB;GAAQ;GAA4B;;CAGtE,SAAS,kBAAkB;EACzB,MAAM,qCAAmB,QAAQ;AAEjC,MAAI,UAAU,KACZ,OAAM,IAAI,MAAM,yCAAyC;AAG3D,SAAO;;AAGT,QAAO;EACL;EACA;EACD"}