Lesson 40 of 56 · Web pages

State, lists and components

A page can hold state. You declare a name with :=, change it in an event handler, and the parts of the page that read it are redrawn. You do not write any update code.

State

count := 0 in a page or a component declares state. Handlers such as click = (): count++ end change it. The compiler sees which elements read count and redraws only those, on the web and in a native window alike.

dsl-state.salam
component Counter:
    count := 0
    box:
        padding = 16
        gap = 8
        heading:
            size = 2
            content = "Count: " + count
        end
        box:
            layout = "row"
            gap = 8
            button:
                id = "down"
                click = (): count-- end
                content = "-1"
            end
            button:
                id = "up"
                click = (): count++ end
                content = "+1"
            end
            button:
                id = "reset"
                click = (): count = 0 end
                content = "Reset"
            end
        end
    end
end

page:
    title = "Counter"
    Counter: end
end
Result

If and each

if shows a part only when its condition holds, and each repeats a part for every item of a list. Add an item and the list follows. Each row must have a key, built from the loop variable or its index, so that it keeps its identity when the list changes; a row without one is error E292.

dsl-each.salam
component Tasks:
    items := Vector {} as Vector<str>
    next := 0
    box:
        padding = 16
        gap = 8
        max width = 360
        button:
            id = "add"
            click = (): items.Push("task " + next) next++ end
            content = "Add a task"
        end
        list:
            each _, it in items:
                item:
                    key = it
                    padding = 8
                    hover background = "#eef4ff"
                    content = it
                end
            end
        end
    end
end

page:
    title = "Tasks"
    Tasks: end
end
Result

Events

An event is an attribute whose value is a function: click, input, change, focus, blur and more. Give a handler the name of a function or write it inline with (): and end.

Text input

A text field passes what the user typed to its input handler. Give the handler one parameter and write it into state: input = (v: str): name = v end. The paragraph below reads name, so it follows every key.

When the field should simply show a variable and write it back, say so once with bind = name. It means value = name plus that handler. On a field with type = "checkbox", bind reads and writes a bool (checked and change). Setting the variable from code, for example clearing it after a button, updates the field too. The variable can be a name, a field such as form.email or an element such as rows[i]. Write the handler by hand when it must do more, such as checking the text before keeping it.

dsl-input.salam
component Greeter:
    name := ""
    box:
        padding = 16
        gap = 8
        input:
            id = "who"
            placeholder = "Your name"
            padding = 8, 12
            radius = 6
            border = 1, "solid", "#d1d5db"
            bind = name
        end
        paragraph:
            id = "hello"
            content = "Hello, " + name
        end
    end
end

page:
    title = "Text input"
    Greeter: end
end
Result

Pointer, selection and scrolling

cursor picks the pointer (pointer, text, move, not allowed), interactive = false makes an element ignore the pointer, selectable controls text selection, and scrollbar is auto, thin or none. accent colours checkboxes, radios and sliders.

Two targets

Build for the web with salam build --platform=web and run as a desktop program with --ui=native. The code above does not change. See native apps.