Share logic with composables
A composable is a plain function, named useX, that creates refs, computeds and
effects and returns them. Call it in a component's render and everything it
creates belongs to that component: it stops when the component is destroyed.
Use one when the same behaviour shows up in more than one component, or when a render gets long and part of it is about one thing.
The built in ones
Lucy comes with useDensity, useMetric, useResponsive, useBackHandler,
useFocusScope, useFocusZone, useHoldArrows, useSize, useFit, useHasVisibleChild, useUiSound, useTable, useSort, useWhile, useCountdown, useDrag, useReveal, useCount, useRequest, useIntersect, useDelayGroup, useDismiss, useFocus, useHover, usePress and useProximity. The composables reference
says what each takes and returns.
render = function(h)
local height = Lucy.useMetric(h.app, "ControlMd")
local gap = Lucy.useResponsive(h.app, { touch = 16, default = 8 })
return Lucy.new "Frame" {
Size = function()
return UDim2.new(1, 0, 0, height())
end,
Lucy.new "UIListLayout" {
SortOrder = Enum.SortOrder.LayoutOrder,
Padding = function()
return UDim.new(0, gap())
end,
},
}
end,
Lucy.Component.list is for a layout that never changes: it sets its
properties once. A layout with a bound property is made with Lucy.new.
active can be a ref, a computed or a getter, so a dialog passes its open key
and the back handler comes and goes with it:
local open = h:ref("open")
Lucy.useBackHandler(h.app, open, function()
open(false)
return true -- handled; stop here
end)
Write your own: a cooldown
A hype button shows its name when ready, counts down after it is used, and cannot be pressed until the count reaches zero. The timing is the part worth sharing, so it goes in a composable:
local function useCooldown(seconds: number)
local remaining = Lucy.ref(0)
local thread: thread? = nil
local function start()
if thread then
task.cancel(thread)
end
local endsAt = os.clock() + seconds
thread = task.spawn(function()
while os.clock() < endsAt do
remaining(endsAt - os.clock())
task.wait(0.1)
end
remaining(0)
thread = nil
end)
end
-- Runs when the owner (the component) is destroyed.
Lucy.onCleanup(function()
if thread then
task.cancel(thread)
end
end)
local ready = Lucy.computed(function()
return remaining() <= 0
end)
return { remaining = remaining, ready = ready, start = start }
end
Use it in render like any other value:
render = function(h)
local hype = useCooldown(3)
return Lucy.new "Frame" {
BackgroundTransparency = 1,
AutomaticSize = Enum.AutomaticSize.XY,
Lucy.Button.new({
text = function()
return if hype.ready() then "Hype the crowd" else string.format("%.1f s", hype.remaining())
end,
disabled = function()
return not hype.ready()
end,
onClick = hype.start,
}),
}
end,
While the cooldown runs, the button's text updates ten times a second and
nothing else in the component runs. If the component is destroyed mid
cooldown, onCleanup cancels the thread.
The rules
- Call it where there is an owner: a component's render, an effect, or
scope:runof aLucy.effectScope(). What it creates stops with that owner. The built in composables raise an error if there is none. - Take refs or getters, return refs, computeds or functions. Taking a ref
(rather than a plain value) lets the caller pass a component key, an
app.state:refor a computed and have the composable follow it.Lucy.toValue(x)reads any of them, and a plain value too. - Clean up with
Lucy.onCleanup: connections, threads, anything the owner stopping would not otherwise end. Effects and computeds made inside the composable stop on their own. - Keep watchers for side effects. If the composable's result is a value,
return a computed. Use
Lucy.watchonly to do something, such as play a sound when a value crosses a line.
Next
- Reactivity: composables in the reference, next to effects and lifetime.
- Build a component