(input: {
name: string
init: ((input: Props) => T) | (() => T)
})
| 1 | import { createContext, Show, useContext, type ParentProps } from "solid-js" |
| 2 | |
| 3 | export function createSimpleContext<T, Props extends Record<string, any>>(input: { |
| 4 | name: string |
| 5 | init: ((input: Props) => T) | (() => T) |
| 6 | }) { |
| 7 | const ctx = createContext<T>() |
| 8 | |
| 9 | return { |
| 10 | context: ctx, |
| 11 | provider: (props: ParentProps<Props>) => { |
| 12 | const init = input.init(props) |
| 13 | return ( |
| 14 | // @ts-expect-error |
| 15 | <Show when={init.ready === undefined || init.ready === true}> |
| 16 | <ctx.Provider value={init}>{props.children}</ctx.Provider> |
| 17 | </Show> |
| 18 | ) |
| 19 | }, |
| 20 | use() { |
| 21 | const value = useContext(ctx) |
| 22 | if (!value) throw new Error(`${input.name} context must be used within a context provider`) |
| 23 | return value |
| 24 | }, |
| 25 | } |
| 26 | } |
no test coverage detected