pesde goatsoft / lucy

useFocusScope

Lucy.useFocusScope keeps keyboard Tab navigation and gamepad selection inside a container frame while active is true. When activated, it moves focus to defaultFocus (or the first selectable element inside root). When deactivated, it restores focus to the element that had focus before the scope opened.

Usage

Lucy.useFocusScope(app?, active, options)
ArgumentTypeDefaultWhat it is
appLucy.App?Lucy.App.current()The app instance managing input and focus
activeMaybeRef<boolean>requiredWhile true, focus stays inside root
optionstablerequiredOptions table

Options

OptionTypeDefaultWhat it does
rootGuiObjectrequiredBoundary frame holding the focusable items
defaultFocusGuiObject | (() -> GuiObject?)nilTarget to focus when the scope activates

Example: Club Lucy VIP lounge modal

Keep controller focus inside a confirmation modal so pressing the D-pad or stick cannot move to elements on the dance floor behind it:

local function VipConfirmDialog(h)
    local open = h:ref("open")
    local confirmButton = Lucy.ref(nil)

    local dialogRoot = Lucy.new "Frame" {
        Visible = open,
        Size = UDim2.fromOffset(360, 200),

        Lucy.new "TextButton" {
            [Lucy.instance] = confirmButton,
            Text = "Enter VIP",
            Size = UDim2.fromOffset(140, 40),
        },
    }

    Lucy.useFocusScope(h.app, open, {
        root = dialogRoot,
        defaultFocus = function()
            return confirmButton()
        end,
    })

    return dialogRoot
end