@tamagui/create-context 1.0.1-beta.59 → 1.0.1-beta.60

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tamagui/create-context",
3
- "version": "1.0.1-beta.59",
3
+ "version": "1.0.1-beta.60",
4
4
  "sideEffects": false,
5
5
  "source": "src/index.ts",
6
6
  "types": "./types/index.d.ts",
@@ -8,6 +8,7 @@
8
8
  "module": "dist/esm",
9
9
  "module:jsx": "dist/jsx",
10
10
  "files": [
11
+ "src",
11
12
  "types",
12
13
  "dist"
13
14
  ],
@@ -19,7 +20,7 @@
19
20
  "react": "*"
20
21
  },
21
22
  "devDependencies": {
22
- "@tamagui/build": "^1.0.1-beta.59",
23
+ "@tamagui/build": "^1.0.1-beta.60",
23
24
  "react": "*"
24
25
  },
25
26
  "publishConfig": {
@@ -0,0 +1,139 @@
1
+ // from radix
2
+ // https://github.com/radix-ui/primitives/blob/main/packages/react/context/src/createContext.tsx
3
+
4
+ import * as React from 'react'
5
+
6
+ export type ScopedProps<P, K extends string> = P & { [Key in `__scope${K}`]?: Scope }
7
+
8
+ export function createContext<ContextValueType extends object | null>(
9
+ rootComponentName: string,
10
+ defaultContext?: ContextValueType
11
+ ) {
12
+ const Context = React.createContext<ContextValueType | undefined>(defaultContext)
13
+
14
+ function Provider(props: ContextValueType & { children: React.ReactNode }) {
15
+ const { children, ...context } = props
16
+ // Only re-memoize when prop values change
17
+ // eslint-disable-next-line react-hooks/exhaustive-deps
18
+ const value = React.useMemo(() => context, Object.values(context)) as ContextValueType
19
+ return <Context.Provider value={value}>{children}</Context.Provider>
20
+ }
21
+
22
+ function useContext(consumerName: string) {
23
+ const context = React.useContext(Context)
24
+ if (context) return context
25
+ if (defaultContext !== undefined) return defaultContext
26
+ // if a defaultContext wasn't specified, it's a required context.
27
+ throw new Error(`\`${consumerName}\` must be used within \`${rootComponentName}\``)
28
+ }
29
+
30
+ Provider.displayName = rootComponentName + 'Provider'
31
+ return [Provider, useContext] as const
32
+ }
33
+
34
+ /* -------------------------------------------------------------------------------------------------
35
+ * createContextScope
36
+ * -----------------------------------------------------------------------------------------------*/
37
+
38
+ type ScopeHook = (scope: Scope) => { [__scopeProp: string]: Scope }
39
+
40
+ export type Scope<C = any> = { [scopeName: string]: React.Context<C>[] } | undefined
41
+
42
+ export interface CreateScope {
43
+ scopeName: string
44
+ (): ScopeHook
45
+ }
46
+
47
+ export function createContextScope(scopeName: string, createContextScopeDeps: CreateScope[] = []) {
48
+ let defaultContexts: any[] = []
49
+
50
+ /* -----------------------------------------------------------------------------------------------
51
+ * createContext
52
+ * ---------------------------------------------------------------------------------------------*/
53
+
54
+ function createContext<ContextValueType extends object | null>(
55
+ rootComponentName: string,
56
+ defaultContext?: ContextValueType
57
+ ) {
58
+ const BaseContext = React.createContext<ContextValueType | undefined>(defaultContext)
59
+ const index = defaultContexts.length
60
+ defaultContexts = [...defaultContexts, defaultContext]
61
+
62
+ function Provider(
63
+ props: ContextValueType & { scope: Scope<ContextValueType>; children: React.ReactNode }
64
+ ) {
65
+ const { scope, children, ...context } = props
66
+ const Context = scope?.[scopeName][index] || BaseContext
67
+ // Only re-memoize when prop values change
68
+ // eslint-disable-next-line react-hooks/exhaustive-deps
69
+ const value = React.useMemo(() => context, Object.values(context)) as ContextValueType
70
+ return <Context.Provider value={value}>{children}</Context.Provider>
71
+ }
72
+
73
+ function useContext(consumerName: string, scope: Scope<ContextValueType | undefined>) {
74
+ const Context = scope?.[scopeName][index] || BaseContext
75
+ const context = React.useContext(Context)
76
+ if (context) return context
77
+ if (defaultContext !== undefined) return defaultContext
78
+ // if a defaultContext wasn't specified, it's a required context.
79
+ throw new Error(`\`${consumerName}\` must be used within \`${rootComponentName}\``)
80
+ }
81
+
82
+ Provider.displayName = rootComponentName + 'Provider'
83
+ return [Provider, useContext] as const
84
+ }
85
+
86
+ /* -----------------------------------------------------------------------------------------------
87
+ * createScope
88
+ * ---------------------------------------------------------------------------------------------*/
89
+
90
+ const createScope: CreateScope = () => {
91
+ const scopeContexts = defaultContexts.map((defaultContext) => {
92
+ return React.createContext(defaultContext)
93
+ })
94
+ return function useScope(scope: Scope) {
95
+ const contexts = scope?.[scopeName] || scopeContexts
96
+ return React.useMemo(
97
+ () => ({ [`__scope${scopeName}`]: { ...scope, [scopeName]: contexts } }),
98
+ [scope, contexts]
99
+ )
100
+ }
101
+ }
102
+
103
+ createScope.scopeName = scopeName
104
+ return [createContext, composeContextScopes(createScope, ...createContextScopeDeps)] as const
105
+ }
106
+
107
+ /* -------------------------------------------------------------------------------------------------
108
+ * composeContextScopes
109
+ * -----------------------------------------------------------------------------------------------*/
110
+
111
+ function composeContextScopes(...scopes: CreateScope[]) {
112
+ const baseScope = scopes[0]
113
+ if (scopes.length === 1) return baseScope
114
+
115
+ const createScope: CreateScope = () => {
116
+ const scopeHooks = scopes.map((createScope) => ({
117
+ useScope: createScope(),
118
+ scopeName: createScope.scopeName,
119
+ }))
120
+
121
+ return function useComposedScopes(overrideScopes) {
122
+ const nextScopes = scopeHooks.reduce((nextScopes, { useScope, scopeName }) => {
123
+ // We are calling a hook inside a callback which React warns against to avoid inconsistent
124
+ // renders, however, scoping doesn't have render side effects so we ignore the rule.
125
+ // eslint-disable-next-line react-hooks/rules-of-hooks
126
+ const scopeProps = useScope(overrideScopes)
127
+ const currentScope = scopeProps[`__scope${scopeName}`]
128
+ return { ...nextScopes, ...currentScope }
129
+ }, {})
130
+
131
+ return React.useMemo(() => ({ [`__scope${baseScope.scopeName}`]: nextScopes }), [nextScopes])
132
+ }
133
+ }
134
+
135
+ createScope.scopeName = baseScope.scopeName
136
+ return createScope
137
+ }
138
+
139
+ /* -----------------------------------------------------------------------------------------------*/
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './create-context'