-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
a utility function to wrap children in context without having to use JSX. see solidjs-community/solid-primitives#464
- Loading branch information
1 parent
c034bd7
commit ba231d3
Showing
1 changed file
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { Accessor, Context, JSX } from 'solid-js' | ||
|
||
export type ContextProviderProps = { | ||
children?: JSX.Element | ||
} & Record<string, unknown> | ||
export type ContextProvider<T extends ContextProviderProps> = (props: { children: JSX.Element } & T) => JSX.Element | ||
/** | ||
* A utility-function to provide context to components. | ||
* | ||
* @param children Accessor of Children | ||
* @param context Context<T> | ||
* @param value T | ||
* | ||
* @example | ||
* ```tsx | ||
* const NumberContext = createContext<number> | ||
* | ||
* const children = withContext( | ||
* () => props.children, | ||
* NumberContext, | ||
* 1 | ||
* ) | ||
* ``` | ||
*/ | ||
|
||
export function withContext<T>(children: Accessor<JSX.Element | JSX.Element[]>, context: Context<T>, value: T) { | ||
let result: JSX.Element | JSX.Element[] | ||
|
||
context.Provider({ | ||
value, | ||
children: (() => { | ||
result = children() | ||
return '' | ||
}) as any as JSX.Element, | ||
}) | ||
|
||
return () => result | ||
} | ||
|
||
/* | ||
Type validation of the `values` array thanks to the amazing @otonashixav (https://github.com/otonashixav) | ||
*/ | ||
|
||
/** | ||
* A utility-function to provide multiple context to components. | ||
* | ||
* @param children Accessor of Children | ||
* @param values Array of tuples of `[Context<T>, value T]`. | ||
* | ||
* @example | ||
* ```tsx | ||
* const NumberContext = createContext<number> | ||
* const StringContext = createContext<string> | ||
* const children = withContext( | ||
* () => props.children, | ||
* [ | ||
* [NumberContext, 1], | ||
* [StringContext, "string"] | ||
* ] | ||
* ) | ||
* ``` | ||
*/ | ||
|
||
export function withMultiContexts<T extends readonly [unknown?, ...unknown[]]>( | ||
children: Accessor<JSX.Element | JSX.Element[]>, | ||
values: { | ||
[K in keyof T]: readonly [Context<T[K]>, [T[K]][T extends unknown ? 0 : never]] | ||
}, | ||
) { | ||
let result: JSX.Element | JSX.Element[] | ||
|
||
const fn = (index: number) => { | ||
const [context, value] = values[index]! | ||
context.Provider({ | ||
value, | ||
children: (() => { | ||
if (index < values.length - 1) { | ||
fn(index + 1) | ||
} else { | ||
result = children() | ||
} | ||
return '' | ||
}) as any as JSX.Element, | ||
}) | ||
} | ||
|
||
fn(0) | ||
|
||
return () => result | ||
} |