Skip to content

Docs menu // site/ui

[ SDK ] /docs/sdk/ui

site/ui

Talking to the visitor. Prompts use the command palette, like rex.ui.input and rex.ui.pick, and answer through a callback: null means the visitor dismissed it.

import
import ui from "site/ui"

Functions#

SignatureDescription
fn toast(text: string, tone: Tone) -> voidA short message in the corner of the screen, up to 160 characters.
fn banner(text: string) -> voida ticker under the top bar, "" removes it
fn pick(spec: Pick, done: ?fn(?string) -> void) -> voidThe answer callback is optional only because a host function cannot take a plain function value with an optional parameter; a lambda converts implicitly, and null means nobody needs the answer.
fn input(spec: Input, done: ?fn(?string) -> void) -> voidAsks the visitor for a line of text in the palette. The callback receives the text, or null if it was dismissed.
fn palette() -> voidopen the command palette

Types#

enum Tone#

.Info.Ok.Warn.Error

struct Item#

FieldTypeNotes
idstring
titlestring
subtitlestring
detailstring

struct Pick#

FieldTypeNotes
titlestring
placeholderstring
items[]Item

struct Input#

FieldTypeNotes
titlestring
placeholderstring
valuestring
confirmstring

Examples#

Toast, banner, pick and input
import site from "site"
import ui from "site/ui"

ui.banner("docs example is running")

site.action(site.Action {
    name: "jump",
    title: "Jump to a section",
    description: "Pick a section and scroll to it",
    category: "Docs",
    keywords: ["goto"],
    run: () => {
        ui.pick(ui.Pick {
            title: "Jump to",
            placeholder: "a section",
            items: [
                ui.Item { id: "work", title: "Work", subtitle: "projects", detail: "" },
                ui.Item { id: "log", title: "Log", subtitle: "experience", detail: "" },
                ui.Item { id: "contact", title: "Contact", subtitle: "uplink", detail: "" },
            ],
        }, choice => {
            if let id = choice {
                site.goto(id)
            }
        })
    },
})

site.action(site.Action {
    name: "rename",
    title: "Rename the hero",
    description: "Ask for a title",
    category: "Docs",
    keywords: [],
    run: () => {
        ui.input(ui.Input {
            title: "New title",
            placeholder: "up to 24 characters",
            value: "",
            confirm: "Apply",
        }, answer => {
            if let text = answer {
                site.title(text)
                ui.toast("renamed", .Ok)
            }
        })
    },
})

Limits#

  • Toasts are cut to 160 characters, the banner to 200.
  • A pick shows up to 200 items; titles, placeholders and item fields are cut to 60 or 80 characters, and the confirm label to 16.
  • Only one prompt is open at a time: opening another answers the first with null.
  • Prompts and the palette opened by a timer or event (not by a key, the palette or Run) are refused while one is open and for 5 seconds after the last one. The visitor can press Escape twice, or use Reset site in the prompt, to clear the script.

Declaration file#

This page is generated from the file below, served as /eitri/sdk/site/ui.eith. Types and signatures are exactly what the compiler checks scripts against. Back to all modules.

site/ui.eith
// site/ui: talking to the visitor. Prompts use the command palette, like
// rex.ui.input and rex.ui.pick, and answer through a callback: null means the
// visitor dismissed it.

pub enum Tone {
    Info
    Ok
    Warn
    Error
}

pub struct Item {
    pub id: string
    pub title: string
    pub subtitle: string
    pub detail: string
}

pub struct Pick {
    pub title: string
    pub placeholder: string
    pub items: []Item
}

pub struct Input {
    pub title: string
    pub placeholder: string
    pub value: string
    pub confirm: string
}

pub fn toast(text: string, tone: Tone) -> void
pub fn banner(text: string) -> void         // a ticker under the top bar, "" removes it
// The answer callback is optional only because a host function cannot take a
// plain function value with an optional parameter; a lambda converts
// implicitly, and null means nobody needs the answer.
pub fn pick(spec: Pick, done: ?fn(?string) -> void) -> void
pub fn input(spec: Input, done: ?fn(?string) -> void) -> void
pub fn palette() -> void                    // open the command palette