ilusm.dev

mdl

Modal lifecycle manager - create named modal state objects with open/close callbacks and ESC-dismiss flags, open, close, and toggle them, attach a data payload, manage a modal stack (push/pop/peek/clear), and generate a hook object for reactive binding.

Load with: use mdl

What this module does

mdl is a pure-state modal management system. It tracks whether a modal is open and calls lifecycle callbacks on state changes. The modal stack (mdlst) supports layered UIs where multiple modals can be active simultaneously - mdlpp closes and pops the topmost one.

mdlhk generates a small hook object with open/close/toggle/is methods bound to the modal, convenient for passing to UI components.

Quick example

use mdl

# Create a modal
m = mdlnw("confirm", {
    onop: \(m) prn("opened"),
    oncl: \(m) prn("closed"),
    esc: tru
})

# Open/close/toggle
m = mdlop(m)   # open
m = mdlcl(m)   # close
m = mdltg(m)   # toggle
prn(mdlis(m))  # is open?

# Attach data (e.g. item being confirmed)
m = mdldt(m, {item: "delete account"})
prn(mdlgd(m))

# Stack of modals
s = mdlst()
s = mdlps(s, m)       # push and open
top = mdltp(s)        # peek
popped = mdlpp(s)     # pop and close
s = mdlca(s)          # clear all

# Hook
h = mdlhk(m)
h.open()
h.tgl()

Functions

Modal lifecycle

mdlnw(id, opts) / mdl.new(id, opts)

Creates a modal state object. opts may include: data (initial payload), cls (close-on-backdrop, default tru), esc (close-on-ESC, default tru), onop (open callback), oncl (close callback).

mdlop(m) / mdl.open(m)

Opens the modal and calls m.onop(m) if set. Returns the updated modal.

mdlcl(m) / mdl.cls(m)

Closes the modal and calls m.oncl(m) if set. Returns the updated modal.

mdltg(m) / mdl.tgl(m)

Toggles the modal open/closed.

mdlis(m) / mdl.is(m)

Returns tru if the modal is currently open.

Data payload

mdldt(m, data) / mdl.data(m, data)

Attaches a data payload to the modal. Useful for passing context (e.g. which item is being deleted).

mdlgd(m)

Returns the current data payload.

Modal stack

mdlst() / mdl.stk()

Creates a new empty modal stack {stk:[]}.

mdlps(stack, m) / mdl.psh(stack, m)

Opens m and pushes it onto the stack.

mdlpp(stack) / mdl.pop(stack)

Closes and pops the top modal. Returns the closed modal.

mdltp(stack) / mdl.top(stack)

Returns the top modal without removing it, or nil if the stack is empty.

mdlsz(stack)

Returns the number of modals in the stack.

mdlca(stack) / mdl.clr(stack)

Closes and pops all modals in the stack.

Hook

mdlhk(m) / mdl.hook(m)

Returns a {open, cls, tgl, is} object with methods bound to modal m. Pass this to a UI component to let it control the modal without holding a direct reference.

Notes

  • Pure state management - no DOM interaction. For DOM-based dialog rendering see the dlg module.
  • Requires trl.