fewkz / froact

Wrapper around Roact & Roact Hooks to simplify UI development and add full, correct Luau typing
MIT License
3 stars 0 forks source link

Make our own context #14

Open fewkz opened 1 year ago

fewkz commented 1 year ago

We should implement our own wrapper around context that would be fully typed.

api ideas:

froact.provide, makes the type for Context smaller, by not including Provider:

local context = froact.createContext({ hello = "world" }) -- infered type: Context<{ hello = "world" }>
local bah = froact.c({}, function(props, hooks)
    local value = hooks.useContext(context)
    return TextLabel({ text = `hello is {value.hello}` })
end
local element = froact.provide(context, { hello = "foo" }, {
    bah({})
})

similar to react

local context = froact.createContext({ hello = "world" } -- infered type: Context<{ hello = "world" }>
local bah = froact.c({}, function(props, hooks)
    local value = hooks.useContext(context)
    return TextLabel({ text = `hello is {value.hello}` })
end
local element = context.Provider({
    value = { hello = "foo" }
}, {
    bah({})
})

Context<T> should be { default: T, Provider: ({ value: T }) -> Element }, Consumer: any (too lazy to define consumer, you shouldn't use consumer, use the hook instead)