// from radix // https://github.com/radix-ui/primitives/blob/main/packages/react/context/src/createContext.tsx import * as React from 'react' export type ScopedProps = P & { [Key in `__scope${K}`]?: Scope } export function createContext( rootComponentName: string, defaultContext?: ContextValueType ): readonly [ (props: ContextValueType & { children: React.ReactNode }) => React.JSX.Element, (consumerName: string) => Exclude, ] { const Context = React.createContext(defaultContext) function Provider(props: ContextValueType & { children: React.ReactNode }) { const { children, ...context } = props // Only re-memoize when prop values change const value = React.useMemo(() => context, Object.values(context)) as ContextValueType return {children} } function useContext(consumerName: string): Exclude { const context = React.useContext(Context) if (context) return context as any if (defaultContext !== undefined) return defaultContext as any // if a defaultContext wasn't specified, it's a required context. throw new Error(`\`${consumerName}\` must be used within \`${rootComponentName}\``) } return [Provider, useContext] as const } /* ------------------------------------------------------------------------------------------------- * createContextScope * -----------------------------------------------------------------------------------------------*/ type ScopeHook = (scope: Scope) => { [__scopeProp: string]: Scope } export type Scope = { [scopeName: string]: React.Context[] } | undefined export interface CreateScope { scopeName: string (): ScopeHook } export function createContextScope( scopeName: string, createContextScopeDeps: CreateScope[] = [] ): readonly [ ( rootComponentName: string, defaultContext?: ContextValueType ) => readonly [ ( props: ContextValueType & { scope: Scope children: React.ReactNode } ) => import('react/jsx-runtime').JSX.Element, ( consumerName: string, scope: Scope, options?: { warn?: boolean fallback?: Partial } ) => ContextValueType, ], CreateScope, ] { let defaultContexts: any[] = [] /* ----------------------------------------------------------------------------------------------- * createContext * ---------------------------------------------------------------------------------------------*/ function createContext( rootComponentName: string, defaultContext?: ContextValueType ) { const BaseContext = React.createContext(defaultContext) const index = defaultContexts.length defaultContexts = [...defaultContexts, defaultContext] function Provider( props: ContextValueType & { scope: Scope children: React.ReactNode } ) { const { scope, children, ...context } = props const Context = scope?.[scopeName]?.[index] || BaseContext // Only re-memoize when prop values change const value = React.useMemo( () => context, Object.values(context) ) as ContextValueType return {children} } function useContext( consumerName: string, scope: Scope, options?: { warn?: boolean fallback?: Partial } ) { const Context = scope?.[scopeName]?.[index] || BaseContext const context = React.useContext(Context) if (context) return context // if a defaultContext wasn't specified, it's a required context. if (defaultContext !== undefined) return defaultContext const missingContextMessage = `\`${consumerName}\` must be used within \`${rootComponentName}\`` // fallback can be given per-hook as well if (options?.fallback) { if (options?.warn !== false) { console.warn(missingContextMessage) } return options.fallback as ContextValueType } throw new Error(missingContextMessage) } return [Provider, useContext] as const } /* ----------------------------------------------------------------------------------------------- * createScope * ---------------------------------------------------------------------------------------------*/ const createScope: CreateScope = () => { const scopeContexts = defaultContexts.map((defaultContext) => { return React.createContext(defaultContext) }) return function useScope(scope: Scope) { const contexts = scope?.[scopeName] || scopeContexts return React.useMemo( () => ({ [`__scope${scopeName}`]: { ...scope, [scopeName]: contexts } }), [scope, contexts] ) } } createScope.scopeName = scopeName return [ createContext, composeContextScopes(createScope, ...createContextScopeDeps), ] as const } /* ------------------------------------------------------------------------------------------------- * composeContextScopes * -----------------------------------------------------------------------------------------------*/ function composeContextScopes(...scopes: CreateScope[]) { const baseScope = scopes[0] if (scopes.length === 1) return baseScope const createScope: CreateScope = () => { const scopeHooks = scopes.map((createScope) => ({ useScope: createScope(), scopeName: createScope.scopeName, })) return function useComposedScopes(overrideScopes) { const nextScopes = scopeHooks.reduce((nextScopes, { useScope, scopeName }) => { // We are calling a hook inside a callback which React warns against to avoid inconsistent // renders, however, scoping doesn't have render side effects so we ignore the rule. const scopeProps = useScope(overrideScopes) const currentScope = scopeProps[`__scope${scopeName}`] return { ...nextScopes, ...currentScope } }, {}) return React.useMemo( () => ({ [`__scope${baseScope.scopeName}`]: nextScopes }), [nextScopes] ) } } createScope.scopeName = baseScope.scopeName return createScope } /* -----------------------------------------------------------------------------------------------*/