{ "$schema": "https://ui.shadcn.com/schema/registry-item.json", "name": "native-internal-utils-create-context", "type": "registry:lib", "title": "Create Context", "description": "Create Context from @pitsi-ui/native for native projects.", "dependencies": [], "registryDependencies": [], "files": [ { "path": "registry/native-ui/src/helpers/internal/utils/create-context.ts", "content": "import * as React from \"react\";\n\nexport interface CreateContextOptions {\n /**\n * If `true`, React will throw if context is `null` or `undefined`\n * In some cases, you might want to support nested context, so you can set it to `false`\n */\n strict?: boolean;\n /**\n * Error message to throw if the context is `undefined`\n */\n errorMessage?: string;\n /**\n * The display name of the context\n */\n name?: string;\n}\n\nexport type CreateContextReturn = [React.Provider, () => T, React.Context];\n\n/**\n * Creates a named context, provider, and hook.\n *\n * @param options create context options\n */\nexport function createContext(options: CreateContextOptions = {}) {\n const {\n strict = true,\n errorMessage = \"useContext: `context` is undefined. Seems you forgot to wrap component within the Provider\",\n name,\n } = options;\n\n const Context = React.createContext(undefined);\n\n Context.displayName = name;\n\n function useContext() {\n const context = React.useContext(Context);\n\n if (!context && strict) {\n const error = new Error(errorMessage);\n\n error.name = \"ContextError\";\n (\n Error as ErrorConstructor & {\n captureStackTrace?: (\n targetObject: object,\n constructorOpt?: (...args: unknown[]) => unknown,\n ) => void;\n }\n ).captureStackTrace?.(error, useContext);\n throw error;\n }\n\n return context;\n }\n\n return [Context.Provider, useContext, Context] as CreateContextReturn;\n}\n", "type": "registry:lib", "target": "@components/pitsi-ui/native-ui/src/helpers/internal/utils/create-context.ts" } ], "categories": [ "native", "react-native", "internal-lib" ], "meta": { "package": "@pitsi-ui/native", "packageSlug": "native-ui", "platform": "native" } }