@loke/ui
Version:
27 lines (26 loc) • 1.19 kB
TypeScript
import { type Context, type FC, type ReactNode } from "react";
/**
* Creates a context with a custom provider and a hook to use the context.
*/
declare function createContext<ContextValueType extends object | null>(rootComponentName: string, defaultContext?: ContextValueType): readonly [FC<ContextValueType & {
children: ReactNode;
}>, (consumerName: string) => ContextValueType];
type Scope<C = any> = {
[scopeName: string]: Context<C>[];
} | undefined;
type ScopeHook = (_scope: Scope) => {
[__scopeProp: string]: Scope;
};
interface CreateScope {
scopeName: string;
(): ScopeHook;
}
/**
* Creates a scoped context that can be composed with other scoped contexts.
*/
declare function createContextScope(scopeName: string, createContextScopeDeps?: CreateScope[]): readonly [<ContextValueType extends object | null>(rootComponentName: string, defaultContext?: ContextValueType) => readonly [FC<ContextValueType & {
scope: Scope<ContextValueType>;
children: ReactNode;
}>, (consumerName: string, scope: Scope<ContextValueType | undefined>) => ContextValueType], CreateScope];
export { createContext, createContextScope };
export type { CreateScope, Scope };