Skip to content

Dialog

A modal window above every scene, with a title, a description, any content and a row of action buttons. While it is open, keyboard and gamepad focus stay inside it and the back button (B or Backspace) closes it.

Dialog exampleDialog example
local quit = Lucy.Dialog.new({
    id = "quit",
    title = "Leave the club?",
    description = "Your dance combo resets. Your coins are saved.",
    actions = {
        { text = "Keep dancing", variant = "outline" },
        { text = "Leave", variant = "destructive", onClick = function() app.toast("See you next time") end },
    },
})

quit:open()

Props

PropTypeDefaultState keyWhat it does
openbooleanfalseyesWhether the dialog shows
titlestring""yesThe heading
descriptionstring""yesText under the heading
actionstableButtons along the bottom, see Actions
headertableInstances or handles under the title, such as tabs or filters. They stay put while the body scrolls. When all of them are hidden the slot takes no room
cornertableInstances or handles in the top right corner, left of the close button, such as a currency chip. See The corner
childrentableInstances or handles to put in the content area
footertableInstances or handles under the body, above the actions. They stay put while the body scrolls. When all of them are hidden (key hints on touch, say) the footer takes no room
buildfunctionIn place of header, corner, children and footer: makes the content when the dialog is built. See Building on open
mountstring"shown"When the dialog is built: "shown" the first time it opens, "immediate" when it is made
keepbooleantruefalse takes the whole panel away once the dialog has closed and builds it again on the next open. With mount = "shown" only
onBuildfunctionCalled with the dialog once its panel exists, each time it is built, before it shows: the place to style or reach into it. See Reaching into a dialog
preloadlist or functionIts pictures, or a function returning them, read when needed. Warmed by warm(), and first in the queue when it opens. See Warming ahead
triggerbutton, handle or listWhat opens it. Hovered or focused, it warms (it does not open)
aheadoverlays or functionOverlays warmed when this one opens, such as a sheet opened from inside it
footerPlacementstring"above""above" the actions, or "beside": on their left in one row, such as a “Don’t show this again” checkbox next to the main button. On a screen too narrow for both, the actions wrap under it
dismissiblebooleantrueWhether B, Backspace, the overlay and the close button can close it
closeOnOverlaybooleantrueWhether clicking outside closes it (off by default when modal is false)
modalbooleantruefalse lets clicks reach what is behind it and lets focus move out of it
scrimbooleantruefalse drops the dimmed backdrop. The backdrop fades in and out
showClosebooleantrueShows the close button in the corner
defaultFocusobject, handle or functionWhat is selected when it opens on a keyboard or gamepad. A function is called with the dialog. A non modal dialog takes focus only when this is given
returnFocusobject, handle, getter or falseWhere keyboard and gamepad focus goes when it closes, in place of what opened it. false leaves nothing selected, for a menu or panel opened from a HUD whose buttons must not keep the left stick
sizestring"md"How wide it gets: sm (384), md (448), lg (512), xl (640) or full
maxWidthnumberThe widest it gets in pixels, instead of size
maxHeightnumber, ref or getterThe tallest it gets in pixels. The body scrolls past it
titlePlacementstringthe PanelTitleEdge token"inside", or "edge": the title on a plate standing on the top edge, see The frame
onOpenChangefunctionCalled with (open, handle) when it opens or closes

It also takes the props every component takes. Without a Parent, it goes into the app’s modal layer, above every scene. A dialog opened later shows above one opened earlier.

When the content is taller than maxHeight, or than the screen, the body scrolls and the title, the header and footer slots and the actions stay where they are. A slot whose children are all hidden drops out, gap and all, so the body gets that room. So leave out a table’s maxHeight inside a dialog, unless the table should scroll on its own.

On a short screen (a phone held sideways) a dialog keeps less room round itself and pads its inside less (Space4 instead of Space6, tag ui-dialog-short), and the description sits beside the title, so the body gets as much of the height as it can.

While a modal dialog is open, keyboard and gamepad focus stay inside it. When it opens on a keyboard or gamepad, its last action (the main one) is selected, or the close button when it has no actions; pass defaultFocus to pick something else, such as the first item in a shop. When it closes, focus goes back to whatever had it before. If that can’t take focus any more, such as a Buy button that disabled itself, the control nearest it is selected instead. A dialog with modal = false is still on top, but the rest of the screen keeps working, and B or Backspace still closes it. It takes no focus unless you pass defaultFocus: then it selects that when it opens on a keyboard or gamepad and gives focus back when it closes, but the gamepad can still move out of it.

Actions

Each action is a table with text, and optionally variant, disabled and onClick, and the Button props id, size, icon and children (after the text, such as an arrow). The last action defaults to default and the rest to outline. An action closes the dialog after its onClick runs, unless onClick returns false or the action has close = false.

Methods

NameWhat it does
dialog:open()Opens it
dialog:close()Closes it
dialog:toggle()Opens it if closed, closes it if open
dialog:build()With build: builds the content now, without opening. Does nothing once it is built
dialog:warm()Warms its pictures and builds it ahead, without opening. See Warming ahead
dialog.bodyThe frame that holds children (hidden when there are none)
dialog.scrollThe ScrollArea around the body
dialog.bodyRoom()The height the body may take before it scrolls, in the dialog’s own units. It follows the screen, the header and the footer, so a body that should fit rather than scroll (a card shrunk to the room) can read it
dialog.headerSlotThe frame that holds header
dialog.cornerSlotThe frame that holds corner
dialog.footerSlotThe frame that holds footer
dialog.contentThe panel itself, where a game’s own tags go
dialog.frameThe clear frame round the panel that the overlay places, with the shadow and the plate
dialog.shadowThe shadow behind the panel (shown when the PanelShadow token is above 0)
dialog.plateThe plate the title stands on, with titlePlacement = "edge" (else nil)
dialog.titleLabelThe title’s label, on the plate or in the header

Building on open

A dialog is built the first time it opens. Until then only the dialog itself exists: its id, ui.<id>.open, its methods and one empty frame in the modal layer. The panel, its title, close button, body, slots and actions, and what goes in them, are made on the first open, before anything treats the dialog as open: it is placed, takes focus and calls onOpenChange after. A game with a dozen dialogs pays for none of them until a player opens one.

Content you made yourself and gave as header, children and the other slots waits until then. To make the content on the first open too, give one build function in place of the slots:

local songs = Lucy.Dialog.new({
    id = "song-picker",
    title = "Pick a song",
    size = "lg",
    build = function(dialog)
        return {
            header = { buildGenreFilter() },
            children = { buildSongList() },
        }
    end,
})
  • build is called with the dialog each time it is built. What it returns goes into the slots in the order header, children, footer, corner, so something made once (tabs: the bar in the header, the panels in the body) is placed in both. build together with a slot is an error. If build throws, the dialog closes again and the error is reported.
  • Once built, the dialog is kept: opening again is instant and finds it as the player left it. keep = false takes the whole panel away after it has closed (after its backdrop has faded) and builds it again next time. Use it for a dialog players see once or seldom, and keep what must last in app state.
  • mount = "immediate" builds it when it is made, for code that must reach its parts at once.
  • dialog:build() builds it ahead without opening, and Lucy.prebuild({ inventory, settings }) builds several, one each frame, so the panels players open first are ready by the time they do.
  • The first time it shows, it waits one frame off the screen while its text is measured, then appears where it belongs. Focus and onOpenChange do not wait.

Reaching into a dialog: onBuild

The fields that point into the panel (frame, content, body, headerSlot, footerSlot, cornerSlot, actionsRow, actions, closeButton, titleLabel, shadow, plate) are nil until it is built, and so are the components inside it. Style them or reach into them in onBuild, which is called with the dialog once they exist, each time it is built, before it shows:

Lucy.Dialog.new({
    id = "shop",
    title = "Shop",
    build = buildShop,
    onBuild = function(dialog)
        Lucy.hydrate(dialog.content)({ Tags = { "my-shop-panel" } })
    end,
})

Code elsewhere that reaches into a dialog that has never opened calls dialog:build() first.

A two line confirm needs no build: give it children as before. It is still built on its first open.

Warming ahead

A panel builds on its first open, and its pictures load then too. Warm it a moment before instead:

local danceSheet = Lucy.Sheet.new({ title = "DANCE", preload = function() return danceIcons end })
local shop = Lucy.Dialog.new({
    title = "THE LAB",
    preload = function() return shopPictures end,
    trigger = shopButton,      -- hovered or focused: warmed
    ahead = { danceSheet },    -- warmed when the shop opens
})
shop:warm()                    -- or by hand: pictures warmed, panel built, still closed

warm() loads the pictures (once each, through the shared queue) and builds the panel ahead. Every overlay warmed this way (by warm(), a trigger or an ahead) waits in one queue and is built one a frame, from the frame after: the frame a menu opens in builds the menu alone, and its ahead follow a frame each. It never opens it. Warmed pictures are not pinned: Roblox may drop them again later, so warm close to when they are needed. On touch, where nothing hovers, ahead and warm() do the job. Sheets, menus and selects take the same props and have the same warm().

Alert dialogs

Lucy.Dialog.alert asks a question with two buttons and opens straight away. Clicking outside it does nothing and it has no close button, so the player has to choose. Back (B or Backspace) picks Cancel; pass backCancels = false to make it do nothing instead.

Dialog exampleDialog example
Lucy.Dialog.alert({
    title = "Delete this outfit?",
    description = "It leaves your Wardrobe for good.",
    confirmText = "Delete",  -- default "Continue"
    cancelText = "Keep",     -- default "Cancel"
    destructive = true,      -- a red confirm button
    onConfirm = deleteOutfit,
    onCancel = function() end,
})

Either button closes it after its function runs. An alert is used once: it is destroyed when it closes, so call Lucy.Dialog.alert again each time you ask.

Examples

Open a dialog from anywhere by its id:

app.state:set("ui.quit.open", true)

Keep the dialog open when a check fails:

Dialog exampleDialog example
Lucy.Dialog.new({
    title = "Name your crew",
    children = { Lucy.Input.new({ id = "crewName", placeholder = "Crew name" }) },
    actions = {
        { text = "Cancel", variant = "outline" },
        {
            text = "Create",
            onClick = function()
                local name = app.state:get("ui.crewName.value")
                if name == "" then
                    app.toast.error("Pick a name first")
                    return false -- stay open
                end
                createCrew(name)
            end,
        },
    },
    open = true,
})

Preview

In Studio, from the command bar: Lucy.Preview.show("Dialog").

The frame

A dialog’s panel sits in a frame that can add two things, both off until asked:

  • A shadow: a hard shadow behind the panel, its size, offset right and down by the PanelShadow token. Lucy’s themes set it to 0; set it in your theme’s primitives and colour it with a rule on ui-dialog-shadow.
  • The title on the edge: with titlePlacement = "edge" the title sits on a plate (ui-dialog-plate) standing on the panel’s top edge, which leaves the header row for tabs or controls. The description stays in the header. The PanelTitleEdge token set to 1 makes it the default for every dialog and sheet.
Dialog exampleDialog example
local pass = Lucy.Dialog.new({
    title = "Season pass",
    description = "Season one ends in 89 days.",
    titlePlacement = "edge",
    actions = { { text = "Close" } },
})
pass:open()

A bold look for every panel, from the theme (a thick pink border and a hard deep pink shadow; the tokens go in the primitives you give Lucy.App.new):

local primitives = table.clone(Lucy.Tokens.primitives)
primitives.PanelShadow = 6
primitives.PanelTitleEdge = 1

app.theme:addRules("myPanels", {
    { selector = ".ui-dialog-content::UIStroke", priority = 10, properties = { Color = "$Pink500", Thickness = 4 } },
    { selector = ".ui-dialog-plate::UIStroke", priority = 10, properties = { Color = "$Pink500", Thickness = 4 } },
    { selector = ".ui-dialog-shadow", priority = 10, properties = { BackgroundColor3 = "$Pink900" } },
})

The corner

The close button sits in the panel’s top right corner, in the same place and at the same size on every dialog and sheet, whether the title is inside or on the edge. The corner slot puts more beside it, on its left: a currency chip, a help button. The header’s top row stops short of the corner, so tabs never run under it, and with the title on the edge the tabs move down to clear the plate while the corner stays at the top.

Dialog exampleDialog example
local coins = Lucy.Badge.new({ text = "1,250", icon = "lucide-extended:coins-filled" })
local shop = Lucy.Dialog.new({
    title = "Shop",
    titlePlacement = "edge",
    size = "lg",
    corner = { coins },
    header = {
        Lucy.Tabs.new({
            variant = "line",
            items = {
                { value = "featured", text = "Featured" },
                { value = "emotes", text = "Emotes" },
                { value = "boosts", text = "Boosts" },
            },
        }).bar,
    },
    children = { Lucy.Text.new({ text = "Today's picks.", variant = "muted" }) },
})
shop:open()

A panel you build by hand can have the same corner with Lucy.PanelCorner, so its close button sits where every dialog’s does. Give it the frame that holds the panel, one with no list layout of its own (a layout would place the corner with the rest). Call it while the panel is made (in a component’s render or an effect scope). clear is how far the frame’s content is padded down, if it is, so the corner stays at the top, and room() is how much of the top row it takes, for that row to stop short of it.

Dialog exampleDialog example
local holder = Lucy.new("Frame")({
    Size = UDim2.fromOffset(360, 120),
    BackgroundTransparency = 1,
    Parent = parent,
})
Lucy.Panel.new({
    title = "Crew",
    description = "Four of you tonight.",
    size = UDim2.fromScale(1, 1),
    Parent = holder,
})
Lucy.effectScope():run(function()
    Lucy.PanelCorner.new(app, holder, {
        close = true,
        onClose = function()
            holder.Visible = false
        end,
        children = { Lucy.Badge.new({ text = "LIVE", variant = "destructive" }) },
    })
end)