@loke/ui
Version:
To install dependencies:
57 lines (56 loc) • 2.57 kB
TypeScript
import { type Context, type FC, type ReactNode } from "react";
type ContextProvider<ContextValueType extends object | null> = FC<ContextValueType & {
children: ReactNode;
}>;
type UseAssertedContext<ContextValueType extends object | null> = (consumerName: string) => ContextValueType;
interface UseContext<ContextValueType extends object | null> {
(consumerName: string): ContextValueType;
(consumerName: string, options: {
optional: true;
}): ContextValueType | undefined;
}
/**
* Creates a context with a custom provider and a hook to use the context.
*/
declare function createContext<ContextValueType extends object | null>(rootComponentName: string): readonly [ContextProvider<ContextValueType>, UseContext<ContextValueType>];
declare function createContext<ContextValueType extends object | null>(rootComponentName: string, defaultContext: ContextValueType): readonly [
ContextProvider<ContextValueType>,
UseAssertedContext<ContextValueType>
];
type Scope = {
[scopeName: string]: Context<unknown>[];
} | undefined;
type ScopeHook = (_scope: Scope) => {
[__scopeProp: string]: Scope;
};
interface CreateScope {
scopeName: string;
(): ScopeHook;
}
type ScopedContextProvider<ContextValueType extends object | null> = FC<ContextValueType & {
children: ReactNode;
scope: Scope;
}>;
interface UseScopedContext<ContextValueType extends object | null> {
(consumerName: string, scope: Scope): ContextValueType;
(consumerName: string, scope: Scope, options: {
optional?: true;
}): ContextValueType | undefined;
}
type UseAssertedScopedContext<ContextValueType extends object | null> = (consumerName: string, scope: Scope) => ContextValueType;
interface CreateScopedContext {
<ContextValueType extends object | null>(rootComponentName: string): readonly [
ScopedContextProvider<ContextValueType>,
UseScopedContext<ContextValueType>
];
<ContextValueType extends object | null>(rootComponentName: string, defaultContext: ContextValueType): readonly [
ScopedContextProvider<ContextValueType>,
UseAssertedScopedContext<ContextValueType>
];
}
/**
* Creates a scoped context that can be composed with other scoped contexts.
*/
declare function createContextScope(scopeName: string, createContextScopeDeps?: CreateScope[]): readonly [CreateScopedContext, CreateScope];
export type { CreateScope, CreateScopedContext, Scope, UseAssertedContext, UseAssertedScopedContext, UseContext, UseScopedContext, };
export { createContext, createContextScope };