withContext and withMultiContexts
Using context with jsx-tokenizer can be quite painful. To remedy this I have been using a utility-function withContext in my side-project solid-canvas (see).
I modified this function into withContext and withMultiContexts to be more in-line with other solid-primitives like MultiProvider.
API
withContext
const NumberContext = createContext<number>
const children = withContext(
() => props.children,
NumberContext,
1
)
withMultiContexts
the API mirrors MultiProvider (for now I have kept it simple and only [Context<T>, T] is allowed), but the implementation is a bit different, since with jsx-tokenizer we have to be careful not to resolve the tokens (see) this would cause a warning and prevent the token to be passed on.
const NumberContext = createContext<number>
const StringContext = createContext<string>
const children = withContext(
() => props.children,
[
[NumberContext, 1],
[StringContext, "string"]
]
)
Its usage is not limited to jsx-tokenizer, it could be used with 'regular' solid as well.
Demos:
⚠️ No Changeset found
Latest commit: 3347f0078fc1fb7161eb5ae3c34deefb2037cca3
Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.
This PR includes no changesets
When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types
Click here to learn what changesets are, and how to add one.
Click here if you're a maintainer who wants to add a changeset to this PR
Possibly less essential for jsx-tokenizer if we would change its implementation as proposed here instead, but still could be handy I think.
How would this function handle a custom context provider (in case the context is private)?
const MyContext = createContext</*...*/>();
export const MyContextProvider: ParentComponent = (props) => { /* ... */ }
I think you should add support for generic Component<T> instead of Context<T>