{"version":3,"file":"createStoreContext.cjs","names":[],"sources":["../src/createStoreContext.tsx"],"sourcesContent":["import { createContext, useContext } from 'react'\nimport type { PropsWithChildren, ReactElement } from 'react'\n\n/**\n * Creates a typed React context for sharing a bundle of atoms and stores with a subtree.\n *\n * The returned `StoreProvider` only transports the provided object through\n * React context. Consumers destructure the contextual atoms and stores, then\n * compose them with the existing hooks like {@link useSelector},\n * {@link useSelector} and {@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={() => totalsStore.setState((state) => ({ ...state, count: state.count + 1 }))}\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  StoreProvider: (\n    props: {\n      value: TValue\n    } & PropsWithChildren,\n  ) => ReactElement\n  useStoreContext: () => TValue\n} {\n  const Context = createContext<TValue | null>(null)\n  Context.displayName = 'StoreContext'\n\n  // eslint-disable-next-line @eslint-react/component-hook-factories\n  function StoreProvider({\n    children,\n    value,\n  }: PropsWithChildren<{\n    value: TValue\n  }>) {\n    // eslint-disable-next-line @eslint-react/no-context-provider\n    return <Context.Provider value={value}>{children}</Context.Provider>\n  }\n\n  // eslint-disable-next-line @eslint-react/component-hook-factories\n  function useStoreContext() {\n    // eslint-disable-next-line @eslint-react/no-use-context\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":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAuCA,SAAgB,qBAOd;CACA,MAAM,mCAAuC,KAAK;AAClD,SAAQ,cAAc;CAGtB,SAAS,cAAc,EACrB,UACA,SAGE;AAEF,SAAO,2CAAC,QAAQ,UAAT;GAAyB;GAAQ;GAA4B;;CAItE,SAAS,kBAAkB;EAEzB,MAAM,8BAAmB,QAAQ;AAEjC,MAAI,UAAU,KACZ,OAAM,IAAI,MAAM,yCAAyC;AAG3D,SAAO;;AAGT,QAAO;EACL;EACA;EACD"}