@radix-ui/react-context 1.0.0 → 1.0.1-rc.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
export function createContext<ContextValueType extends object | null>(rootComponentName: string, defaultContext?: ContextValueType): readonly [{
|
|
3
|
+
(props: ContextValueType & {
|
|
4
|
+
children: React.ReactNode;
|
|
5
|
+
}): JSX.Element;
|
|
6
|
+
displayName: string;
|
|
7
|
+
}, (consumerName: string) => ContextValueType];
|
|
8
|
+
export type Scope<C = any> = {
|
|
9
|
+
[scopeName: string]: React.Context<C>[];
|
|
10
|
+
} | undefined;
|
|
11
|
+
type ScopeHook = (scope: Scope) => {
|
|
12
|
+
[__scopeProp: string]: Scope;
|
|
13
|
+
};
|
|
14
|
+
export interface CreateScope {
|
|
15
|
+
scopeName: string;
|
|
16
|
+
(): ScopeHook;
|
|
17
|
+
}
|
|
18
|
+
export function createContextScope(scopeName: string, createContextScopeDeps?: CreateScope[]): readonly [<ContextValueType extends object | null>(rootComponentName: string, defaultContext?: ContextValueType | undefined) => readonly [{
|
|
19
|
+
(props: ContextValueType & {
|
|
20
|
+
scope: Scope<ContextValueType>;
|
|
21
|
+
children: React.ReactNode;
|
|
22
|
+
}): JSX.Element;
|
|
23
|
+
displayName: string;
|
|
24
|
+
}, (consumerName: string, scope: Scope<ContextValueType | undefined>) => ContextValueType], CreateScope];
|
|
25
|
+
|
|
26
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -125,4 +125,4 @@ function $c512c27ab02ef895$export$fd42f52fd3ae1109(rootComponentName, defaultCon
|
|
|
125
125
|
|
|
126
126
|
|
|
127
127
|
export {$c512c27ab02ef895$export$fd42f52fd3ae1109 as createContext, $c512c27ab02ef895$export$50c7b4e9d9f19c1 as createContextScope};
|
|
128
|
-
//# sourceMappingURL=index.
|
|
128
|
+
//# s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,CAAtB,EAAyB,OAAOS,SAAP,CAAzB;IAEA,MAAMP,YAAwB,GAAG,IAAM;QACrC,MAAMQ,UAAU,GAAGF,MAAM,CAACJ,GAAP,CAAYF,CAAAA,WAAD,GAAkB,CAAA;gBAC9CG,QAAQ,EAAEH,WAAW,EADyB;gBAE9CP,SAAS,EAAEO,WAAW,CAACP,SAAvBA;aAF4B,CAAA;QAAX,CAAnB,AAAgD;QAKhD,OAAO,SAASgB,iBAAT,CAA2BC,cAA3B,EAA2C;YAChD,MAAMC,WAAU,GAAGH,UAAU,CAACI,MAAX,CAAkB,CAACD,UAAD,EAAa,EAlHxD,UAkH0DR,QAAF,CAAA,EAlHxD,WAkHoEV,SAAAA,CAAAA,EAAzB,GAAyC;gBAC5E,0FAAA;gBACA,oFAAA;gBACA,sDAAA;gBACA,MAAMoB,UAAU,GAAGV,QAAQ,CAACO,cAAD,CAA3B,AAAA;gBACA,MAAMI,YAAY,GAAGD,UAAU,CAAE,CAAA,OAAA,EAASpB,SAAU,CAAA,CAArB,CAA/B,AAAA;gBACA,OAAO;oBAAE,GAAGkB,UAAL;oBAAiB,GAAGG,YAAH;iBAAxB,CAAO;aANU,EAOhB,EAPgB,CAAnB,AAOC;YAED,OAAOtC,cAAA,CAAc,IAAO,CAAA;oBAAE,CAAE,CAAA,OAAA,EAAS+B,SAAS,CAACd,SAAU,CAAA,CAA/B,CAAA,EAAmCkB,WAAnC;iBAAT,CAAA;YAAd,EAAyE;gBAACA,WAAD;aAAzE,CAAP,CAA4B;SAV9B,CAWC;KAjBH,AAkBC;IAEDX,YAAW,CAACP,SAAZ,GAAwBc,SAAS,CAACd,SAAlC,CAAAO;IACA,OAAOA,YAAP,CAAA;CACD;;ADjID","sources":["packages/react/context/src/index.ts","packages/react/context/src/createContext.tsx"],"sourcesContent":["export { createContext, createContextScope } from './createContext';\nexport type { CreateScope, Scope } from './createContext';\n","import * as React from 'react';\n\nfunction createContext<ContextValueType extends object | null>(\n rootComponentName: string,\n defaultContext?: ContextValueType\n) {\n const Context = React.createContext<ContextValueType | undefined>(defaultContext);\n\n function Provider(props: ContextValueType & { children: React.ReactNode }) {\n const { children, ...context } = props;\n // Only re-memoize when prop values change\n // eslint-disable-next-line react-hooks/exhaustive-deps\n const value = React.useMemo(() => context, Object.values(context)) as ContextValueType;\n return <Context.Provider value={value}>{children}</Context.Provider>;\n }\n\n function useContext(consumerName: string) {\n const context = React.useContext(Context);\n if (context) return context;\n if (defaultContext !== undefined) return defaultContext;\n // if a defaultContext wasn't specified, it's a required context.\n throw new Error(`\\`${consumerName}\\` must be used within \\`${rootComponentName}\\``);\n }\n\n Provider.displayName = rootComponentName + 'Provider';\n return [Provider, useContext] as const;\n}\n\n/* -------------------------------------------------------------------------------------------------\n * createContextScope\n * -----------------------------------------------------------------------------------------------*/\n\ntype Scope<C = any> = { [scopeName: string]: React.Context<C>[] } | undefined;\ntype ScopeHook = (scope: Scope) => { [__scopeProp: string]: Scope };\ninterface CreateScope {\n scopeName: string;\n (): ScopeHook;\n}\n\nfunction createContextScope(scopeName: string, createContextScopeDeps: CreateScope[] = []) {\n let defaultContexts: any[] = [];\n\n /* -----------------------------------------------------------------------------------------------\n * createContext\n * ---------------------------------------------------------------------------------------------*/\n\n function createContext<ContextValueType extends object | null>(\n rootComponentName: string,\n defaultContext?: ContextValueType\n ) {\n const BaseContext = React.createContext<ContextValueType | undefined>(defaultContext);\n const index = defaultContexts.length;\n defaultContexts = [...defaultContexts, defaultContext];\n\n function Provider(\n props: ContextValueType & { scope: Scope<ContextValueType>; children: React.ReactNode }\n ) {\n const { scope, children, ...context } = props;\n const Context = scope?.[scopeName][index] || BaseContext;\n // Only re-memoize when prop values change\n // eslint-disable-next-line react-hooks/exhaustive-deps\n const value = React.useMemo(() => context, Object.values(context)) as ContextValueType;\n return <Context.Provider value={value}>{children}</Context.Provider>;\n }\n\n function useContext(consumerName: string, scope: Scope<ContextValueType | undefined>) {\n const Context = scope?.[scopeName][index] || BaseContext;\n const context = React.useContext(Context);\n if (context) return context;\n if (defaultContext !== undefined) return defaultContext;\n // if a defaultContext wasn't specified, it's a required context.\n throw new Error(`\\`${consumerName}\\` must be used within \\`${rootComponentName}\\``);\n }\n\n Provider.displayName = rootComponentName + 'Provider';\n return [Provider, useContext] as const;\n }\n\n /* -----------------------------------------------------------------------------------------------\n * createScope\n * ---------------------------------------------------------------------------------------------*/\n\n const createScope: CreateScope = () => {\n const scopeContexts = defaultContexts.map((defaultContext) => {\n return React.createContext(defaultContext);\n });\n return function useScope(scope: Scope) {\n const contexts = scope?.[scopeName] || scopeContexts;\n return React.useMemo(\n () => ({ [`__scope${scopeName}`]: { ...scope, [scopeName]: contexts } }),\n [scope, contexts]\n );\n };\n };\n\n createScope.scopeName = scopeName;\n return [createContext, composeContextScopes(createScope, ...createContextScopeDeps)] as const;\n}\n\n/* -------------------------------------------------------------------------------------------------\n * composeContextScopes\n * -----------------------------------------------------------------------------------------------*/\n\nfunction composeContextScopes(...scopes: CreateScope[]) {\n const baseScope = scopes[0];\n if (scopes.length === 1) return baseScope;\n\n const createScope: CreateScope = () => {\n const scopeHooks = scopes.map((createScope) => ({\n useScope: createScope(),\n scopeName: createScope.scopeName,\n }));\n\n return function useComposedScopes(overrideScopes) {\n const nextScopes = scopeHooks.reduce((nextScopes, { useScope, scopeName }) => {\n // We are calling a hook inside a callback which React warns against to avoid inconsistent\n // renders, however, scoping doesn't have render side effects so we ignore the rule.\n // eslint-disable-next-line react-hooks/rules-of-hooks\n const scopeProps = useScope(overrideScopes);\n const currentScope = scopeProps[`__scope${scopeName}`];\n return { ...nextScopes, ...currentScope };\n }, {});\n\n return React.useMemo(() => ({ [`__scope${baseScope.scopeName}`]: nextScopes }), [nextScopes]);\n };\n };\n\n createScope.scopeName = baseScope.scopeName;\n return createScope;\n}\n\n/* -----------------------------------------------------------------------------------------------*/\n\nexport { createContext, createContextScope };\nexport type { CreateScope, Scope };\n"],"names":["createContext","createContextScope","React","rootComponentName","defaultContext","Context","Provider","props","children","context","value","useMemo","Object","values","useContext","consumerName","undefined","Error","displayName","scopeName","createContextScopeDeps","defaultContexts","BaseContext","index","length","scope","createScope","scopeContexts","map","useScope","contexts","composeContextScopes","scopes","baseScope","scopeHooks","useComposedScopes","overrideScopes","nextScopes","reduce","scopeProps","currentScope"],"version":3,"file":"index.module.js.map"}
|
|
1
|
+
{"mappings":";;ACAA;AAEA,SAASA,yCAAT,CACEG,iBADF,EAEEC,cAFF,EAGE;IACA,MAAMC,OAAO,GAAA,aAAGH,CAAAA,oBAAA,CAAkDE,cAAlD,CAAhB,AAAA;IAEA,SAASE,QAAT,CAAkBC,KAAlB,EAA2E;QACzE,MAAM,EATV,UASYC,QAAF,CAAA,EAAY,GAAGC,OAAH,EAAZ,GAA2BF,KAAjC,AADyE,EAEzE,0CADM;QAEN,uDAAA;QACA,MAAMG,KAAK,GAAGR,cAAA,CAAc,IAAMO,OAApB;QAAA,EAA6BG,MAAM,CAACC,MAAP,CAAcJ,OAAd,CAA7B,CAAd,AAAA;QACA,OAAA,aAAO,CAAA,oBAAA,CAAC,OAAD,CAAS,QAAT,EAAP;YAAyB,KAAK,EAAEC,KAAP;SAAlB,EAAiCF,QAAjC,CAAP,CAAO;KACR;IAED,SAASM,UAAT,CAAoBC,YAApB,EAA0C;QACxC,MAAMN,OAAO,GAAGP,iBAAA,CAAiBG,OAAjB,CAAhB,AAAA;QACA,IAAII,OAAJ,EAAa,OAAOA,OAAP,CAAb;QACA,IAAIL,cAAc,KAAKY,SAAvB,EAAkC,OAAOZ,cAAP,CAHM,CAIxC,iEADA;QAEA,MAAM,IAAIa,KAAJ,CAAW,CAAA,EAAA,EAAIF,YAAa,CAAA,yBAAA,EAA2BZ,iBAAkB,CAAA,EAAA,CAAzE,CAAN,CAAA;KACD;IAEDG,QAAQ,CAACY,WAAT,GAAuBf,iBAAiB,GAAG,UAA3C,CAAAG;IACA,OAAO;QAACA,QAAD;QAAWQ,UAAX;KAAP,CAAA;CACD;AAED;;oGAEA,CASA,SAASb,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react/context/src/index.ts","packages/react/context/src/createContext.tsx"],"sourcesContent":["export { createContext, createContextScope } from './createContext';\nexport type { CreateScope, Scope } from './createContext';\n","import * as React from 'react';\n\nfunction createContext<ContextValueType extends object | null>(\n rootComponentName: string,\n defaultContext?: ContextValueType\n) {\n const Context = React.createContext<ContextValueType | undefined>(defaultContext);\n\n function Provider(props: ContextValueType & { children: React.ReactNode }) {\n const { children, ...context } = props;\n // Only re-memoize when prop values change\n // eslint-disable-next-line react-hooks/exhaustive-deps\n const value = React.useMemo(() => context, Object.values(context)) as ContextValueType;\n return <Context.Provider value={value}>{children}</Context.Provider>;\n }\n\n function useContext(consumerName: string) {\n const context = React.useContext(Context);\n if (context) return context;\n if (defaultContext !== undefined) return defaultContext;\n // if a defaultContext wasn't specified, it's a required context.\n throw new Error(`\\`${consumerName}\\` must be used within \\`${rootComponentName}\\``);\n }\n\n Provider.displayName = rootComponentName + 'Provider';\n return [Provider, useContext] as const;\n}\n\n/* -------------------------------------------------------------------------------------------------\n * createContextScope\n * -----------------------------------------------------------------------------------------------*/\n\ntype Scope<C = any> = { [scopeName: string]: React.Context<C>[] } | undefined;\ntype ScopeHook = (scope: Scope) => { [__scopeProp: string]: Scope };\ninterface CreateScope {\n scopeName: string;\n (): ScopeHook;\n}\n\nfunction createContextScope(scopeName: string, createContextScopeDeps: CreateScope[] = []) {\n let defaultContexts: any[] = [];\n\n /* -----------------------------------------------------------------------------------------------\n * createContext\n * ---------------------------------------------------------------------------------------------*/\n\n function createContext<ContextValueType extends object | null>(\n rootComponentName: string,\n defaultContext?: ContextValueType\n ) {\n const BaseContext = React.createContext<ContextValueType | undefined>(defaultContext);\n const index = defaultContexts.length;\n defaultContexts = [...defaultContexts, defaultContext];\n\n function Provider(\n props: ContextValueType & { scope: Scope<ContextValueType>; children: React.ReactNode }\n ) {\n const { scope, children, ...context } = props;\n const Context = scope?.[scopeName][index] || BaseContext;\n // Only re-memoize when prop values change\n // eslint-disable-next-line react-hooks/exhaustive-deps\n const value = React.useMemo(() => context, Object.values(context)) as ContextValueType;\n return <Context.Provider value={value}>{children}</Context.Provider>;\n }\n\n function useContext(consumerName: string, scope: Scope<ContextValueType | undefined>) {\n const Context = scope?.[scopeName][index] || BaseContext;\n const context = React.useContext(Context);\n if (context) return context;\n if (defaultContext !== undefined) return defaultContext;\n // if a defaultContext wasn't specified, it's a required context.\n throw new Error(`\\`${consumerName}\\` must be used within \\`${rootComponentName}\\``);\n }\n\n Provider.displayName = rootComponentName + 'Provider';\n return [Provider, useContext] as const;\n }\n\n /* -----------------------------------------------------------------------------------------------\n * createScope\n * ---------------------------------------------------------------------------------------------*/\n\n const createScope: CreateScope = () => {\n const scopeContexts = defaultContexts.map((defaultContext) => {\n return React.createContext(defaultContext);\n });\n return function useScope(scope: Scope) {\n const contexts = scope?.[scopeName] || scopeContexts;\n return React.useMemo(\n () => ({ [`__scope${scopeName}`]: { ...scope, [scopeName]: contexts } }),\n [scope, contexts]\n );\n };\n };\n\n createScope.scopeName = scopeName;\n return [createContext, composeContextScopes(createScope, ...createContextScopeDeps)] as const;\n}\n\n/* -------------------------------------------------------------------------------------------------\n * composeContextScopes\n * -----------------------------------------------------------------------------------------------*/\n\nfunction composeContextScopes(...scopes: CreateScope[]) {\n const baseScope = scopes[0];\n if (scopes.length === 1) return baseScope;\n\n const createScope: CreateScope = () => {\n const scopeHooks = scopes.map((createScope) => ({\n useScope: createScope(),\n scopeName: createScope.scopeName,\n }));\n\n return function useComposedScopes(overrideScopes) {\n const nextScopes = scopeHooks.reduce((nextScopes, { useScope, scopeName }) => {\n // We are calling a hook inside a callback which React warns against to avoid inconsistent\n // renders, however, scoping doesn't have render side effects so we ignore the rule.\n // eslint-disable-next-line react-hooks/rules-of-hooks\n const scopeProps = useScope(overrideScopes);\n const currentScope = scopeProps[`__scope${scopeName}`];\n return { ...nextScopes, ...currentScope };\n }, {});\n\n return React.useMemo(() => ({ [`__scope${baseScope.scopeName}`]: nextScopes }), [nextScopes]);\n };\n };\n\n createScope.scopeName = baseScope.scopeName;\n return createScope;\n}\n\n/* -----------------------------------------------------------------------------------------------*/\n\nexport { createContext, createContextScope };\nexport type { CreateScope, Scope };\n"],"names":["createContext","createContextScope","React","rootComponentName","defaultContext","Context","Provider","props","children","context","value","useMemo","Object","values","useContext","consumerName","undefined","Error","displayName","scopeName","createContextScopeDeps","defaultContexts","BaseContext","index","length","scope","createScope","scopeContexts","map","useScope","contexts","composeContextScopes","scopes","baseScope","scopeHooks","useComposedScopes","overrideScopes","nextScopes","reduce","scopeProps","currentScope"],"version":3,"file":"index.mjs.map"}
|
package/package.json
CHANGED
|
@@ -1,11 +1,23 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@radix-ui/react-context",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1-rc.1",
|
|
4
4
|
"license": "MIT",
|
|
5
|
-
"
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
5
|
+
"exports": {
|
|
6
|
+
".": {
|
|
7
|
+
"import": {
|
|
8
|
+
"types": "./dist/index.d.mts",
|
|
9
|
+
"default": "./dist/index.mjs"
|
|
10
|
+
},
|
|
11
|
+
"require": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"default": "./dist/index.js"
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"source": "./src/index.ts",
|
|
18
|
+
"main": "./dist/index.js",
|
|
19
|
+
"module": "./dist/index.mjs",
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
9
21
|
"files": [
|
|
10
22
|
"dist",
|
|
11
23
|
"README.md"
|
|
@@ -28,5 +40,6 @@
|
|
|
28
40
|
},
|
|
29
41
|
"dependencies": {
|
|
30
42
|
"@babel/runtime": "^7.13.10"
|
|
31
|
-
}
|
|
43
|
+
},
|
|
44
|
+
"stableVersion": "1.0.0"
|
|
32
45
|
}
|