Lesson 46 of 56 · Web pages

Whole pages: six projects

Elements, styles and events are easier to learn one at a time and easier to use together. This lesson builds six whole pages, each the size of something you might really publish. For every project you get the goal, the code running in the preview, the decisions that matter and a few ideas for taking it further.

How to build a page

Every project below was built in the same order, and the order is a good habit. First the content: write the headings and the text in plain elements, with no styling, and check that the page makes sense. Second the structure: put the parts in landmarks such as header, main, section and footer (page structure), because they give the page its outline. Third the look: colours, spacing and layout (styles, layout, responsive pages). Last the behaviour: state, handlers and the dom helpers (state, the DOM and timers). A page that works at each step is easy to repair. A page that was styled before it had content is not.

Two habits keep the code short. Values used in many places, such as a brand colour, go into a constant above the page. A piece that appears more than once, such as a price card or a statistic, becomes a component. All the previews are live: click, type and resize your browser window to see how the pages react.

Project 1: a landing page

Goal. One page that sells one thing: a header with navigation, a hero with a single call to action, three reasons to buy, three plans and a footer. It has to look right on a phone without a separate mobile version.

web-project-landing.salam
const BRAND := "Kettle"
const ACCENT := "#b45309"

component Plan(name, price, note):
    section:
        class = "plan"
        background = "white"
        border = 1px, "solid", "#e7e5e4"
        radius = 16
        padding = 24
        text align = "center"
        transition = 0.2s
        hover translate = 0, -4px
        hover shadow = [0, 12, 28, "#0000001f"]
        heading:
            size = 3
            content = name
            margin = 0, 0, 6px, 0
        end
        paragraph:
            content = price
            font size = 2.2rem
            font weight = "bold"
            color = ACCENT
            margin = 0
        end
        paragraph:
            content = note
            color = "#78716c"
            margin = 4px, 0, 16px, 0
        end
        content: end
        button:
            content = "Choose " + name
            padding = 10px, 22px
            border = 0
            radius = 999
            background = "#292524"
            color = "white"
            cursor = "pointer"
            hover background = ACCENT
        end
    end
end

page:
    title = BRAND + " coffee club"
    font = "system", "sans"
    margin = 0
    color = "#292524"
    background = "#fafaf9"
    line height = 1.6
    header:
        layout = "row"
        justify = "between"
        align = "center"
        padding = 16px, 6vw
        strong:
            content = BRAND
            font size = 1.4rem
            color = ACCENT
        end
        nav:
            layout = "row"
            gap = 22
            link:
                url = "#features"
                content = "Why us"
                color = "#44403c"
                decoration = "none"
                hover color = ACCENT
            end
            link:
                url = "#plans"
                content = "Plans"
                color = "#44403c"
                decoration = "none"
                hover color = ACCENT
            end
        end
    end
    main:
        section:
            padding = 72px, 6vw
            text align = "center"
            background = linear(135, #fef3c7, #fed7aa)
            heading:
                size = 1
                content = "Fresh coffee, every Monday"
                font size = 3rem
                margin = 0, 0, 12, 0
            end
            paragraph:
                content = "Beans roasted this week, ground the way you like, at your door before breakfast."
                max width = 34rem
                margin = 0, "auto", 24px, "auto"
                font size = 1.15rem
            end
            button:
                content = "Start my first box"
                padding = 14px, 30px
                border = 0
                radius = 999
                background = ACCENT
                color = "white"
                font size = 1.05rem
                cursor = "pointer"
                hover background = "#92400e"
            end
        end
        section:
            id = "features"
            padding = 56px, 6vw
            heading:
                size = 2
                content = "Why people stay"
                text align = "center"
                margin = 0, 0, 28, 0
            end
            box:
                layout = "grid"
                columns = repeat (fit, minmax(200px, 1fr))
                gap = 24
                box:
                    heading:
                        size = 3
                        content = "Roasted to order"
                    end
                    paragraph:
                        content = "Nothing sits on a shelf. Every bag is roasted after you order it."
                    end
                end
                box:
                    heading:
                        size = 3
                        content = "Skip or pause"
                    end
                    paragraph:
                        content = "Going away? Pause in two clicks. No fees, no calls."
                    end
                end
                box:
                    heading:
                        size = 3
                        content = "Fair prices"
                    end
                    paragraph:
                        content = "We buy from farms we know and tell you what each one earned."
                    end
                end
            end
        end
        section:
            id = "plans"
            padding = 56px, 6vw
            background = "#f5f5f4"
            heading:
                size = 2
                content = "Pick a plan"
                text align = "center"
                margin = 0, 0, 28, 0
            end
            box:
                layout = "grid"
                columns = repeat (fit, minmax(220px, 1fr))
                gap = 24
                Plan:
                    name = "Solo"
                    price = "$12"
                    note = "one bag a month"
                    paragraph:
                        content = "250 g, any roast"
                    end
                end
                Plan:
                    name = "Duo"
                    price = "$21"
                    note = "two bags a month"
                    paragraph:
                        content = "2 x 250 g, mixed"
                    end
                end
                Plan:
                    name = "Office"
                    price = "$58"
                    note = "six bags a month"
                    paragraph:
                        content = "6 x 250 g, grinder tips"
                    end
                end
            end
        end
    end
    footer:
        padding = 28, 6vw
        text align = "center"
        color = "#78716c"
        content = "Kettle coffee club. Made with care in a small kitchen."
    end
end
Result

Walkthrough. The page is a stack of full width bands inside main, and each band is a section that sets its own background and its own padding. 6vw of padding left and right keeps the text away from the edges on every screen size, and the heading uses clamp(2rem, 6vw, 3.4rem) so it grows with the window but never gets too small or too big.

The columns need no media query. repeat(auto-fit, minmax(220px, 1fr)) says: make as many columns as fit, each at least 220 pixels wide, and share the rest equally. Three cards sit side by side on a laptop, and the same rule stacks them on a phone. The price cards are one component, Plan, with three parameters. The content: end line inside it is a slot: whatever you write between Plan: and its end lands there, so each plan can list different details. Its look on hover, a small lift and a shadow, is written once with the hover prefix and a transition that makes the change smooth. BRAND and ACCENT are constants, so changing the colour of the whole page is a one line edit.

Change it next. Make the middle plan stand out with a darker border and a small badge. Add a media rule that stacks the header on very narrow screens. Turn the call to action into a link to a sign up page, and add a form such as the one in project 3.

Project 2: a blog post with a sidebar

Goal. A comfortable reading page: a header, one article with a picture, a quotation and a sidebar, which drops below the article when the screen is narrow.

web-project-blog.salam
component Tag(name):
    link:
        url = "#"
        content = name
        padding = 3px, 12px
        margin = 0, 6px, 6px, 0
        radius = 999
        background = "#e0f2f1"
        color = "#115e59"
        decoration = "none"
        font size = 0.9rem
        hover background = "#99f6e4"
    end
end

page:
    title = "Garden Notes: why mulch matters"
    font = "Georgia", "serif"
    margin = 0
    color = "#1c1917"
    background = "#fffdf8"
    line height = 1.75
    header:
        padding = 18px, 5vw
        border bottom = 1px, "solid", "#e7e5e4"
        layout = "row"
        justify = "between"
        align = "baseline"
        wrap = true
        gap = 8
        strong:
            content = "Garden Notes"
            font size = 1.5rem
            color = "#0f766e"
        end
        nav:
            layout = "row"
            gap = 18
            font = "system", "sans"
            font size = 0.95rem
            link:
                url = "#"
                content = "Home"
                decoration = "none"
                color = "#44403c"
            end
            link:
                url = "#"
                content = "Archive"
                decoration = "none"
                color = "#44403c"
            end
            link:
                url = "#"
                content = "About"
                decoration = "none"
                color = "#44403c"
            end
        end
    end
    box:
        layout = "grid"
        columns = minmax(0, 1fr), 17rem
        gap = 40
        padding = 36px, 5vw
        max width = 70rem
        margin = 0, "auto"
        media:
            condition = "(max-width: 760px)"
            columns = 1fr
            gap = 24
        end
        main:
            article:
                header:
                    heading:
                        size = 1
                        content = "Why mulch matters"
                        font size = 2.4rem
                        line height = 1.2
                        margin = 0, 0, 8px, 0
                    end
                    paragraph:
                        color = "#78716c"
                        margin = 0, 0, 24px, 0
                        font = "system", "sans"
                        font size = 0.9rem
                        content = "By Sara Nikoo, "
                        time:
                            datetime = "2026-10-26"
                            content = "26 October 2026"
                        end
                        span:
                            content = ", 4 minute read"
                        end
                    end
                end
                figure:
                    margin = 0, 0, 24px, 0
                    box:
                        height = 220
                        radius = 14
                        background = linear(160, #86efac, #166534)
                    end
                    figure caption:
                        content = "A thick straw layer, one week after planting."
                        color = "#78716c"
                        font size = 0.85rem
                        margin top = 8
                    end
                end
                paragraph:
                    content = "Bare soil loses water fast. A layer of straw or leaves keeps the sun off it, and the beds stay damp for days after rain."
                end
                heading:
                    size = 2
                    content = "Fewer weeds"
                    margin = 28px, 0, 8px, 0
                end
                paragraph:
                    content = "Seeds in the dark do not sprout. Spread the mulch five centimetres deep and most weeds never start."
                end
                block quote:
                    margin = 24px, 0
                    padding = 4px, 20px
                    border start = 4px, "solid", "#14b8a6"
                    color = "#134e4a"
                    italic = true
                    content = "The best tool in my garden is a wheelbarrow of old straw."
                end
                paragraph:
                    content = "Keep it a hand away from the stems, so they can breathe."
                end
            end
        end
        aside:
            font = "system", "sans"
            heading:
                size = 2
                content = "About the author"
                font size = 1.1rem
                margin = 0, 0, 6px, 0
            end
            paragraph:
                content = "Sara grows vegetables on a small balcony and writes about what works."
                font size = 0.95rem
                color = "#57534e"
            end
            heading:
                size = 2
                content = "Topics"
                font size = 1.1rem
                margin = 22px, 0, 8px, 0
            end
            box:
                Tag:
                    name = "Soil"
                end
                Tag:
                    name = "Water"
                end
                Tag:
                    name = "Weeds"
                end
                Tag:
                    name = "Compost"
                end
            end
        end
    end
    footer:
        padding = 24px, 5vw
        border top = 1px, "solid", "#e7e5e4"
        color = "#78716c"
        font = "system", "sans"
        font size = 0.9rem
        content = "Garden Notes. Written slowly."
    end
end
Result

Walkthrough. Reading is the whole point, so the text rules come first: a serif font, a line height of 1.75 and a column that never gets wider than the page allows. The two column layout is a grid with minmax(0, 1fr) 17rem: the sidebar is a fixed 17rem and the article takes the rest. The 0 in minmax matters, because without it a long word or a wide picture can force the column wider than the screen. The box has a max width and margin = 0, "auto", which centres it on a big monitor.

The responsive part is the media: child of the grid. Below 760 pixels it replaces the two columns with one, and since the sidebar comes second in the source, it lands under the article. Order in the source is order on a phone, so write the parts in the order a phone reader should meet them. The meta line uses time with a machine readable datetime, the picture sits in a figure with a figure caption, and the quotation is a block quote whose bar is border inline start. Start and end are logical sides: in a right to left page the bar moves to the right edge by itself. Tag is a small component for the topic pills.

Change it next. Add a table of contents as a nav with links to the headings (give the headings ids). Add a dark version with a style element and prefers-color-scheme. Print it: a media = "print" rule that hides the sidebar.

Project 3: a sign up form with live checks

Goal. A form that tells people what is wrong while they type, not after they press the button: a green or red border on every field, a message under it, a strength bar for the password, and a Join button that stays off until everything is right.

web-project-signup.salam
import dom

func strength(word: str): int:
    mut score := 0
    if word.Len() >= 8:
        score++
    end
    if word.Lower() != word:
        score++
    end
    repeat 10 in d:
        if word.Includes("" + d):
            score++
            break
        end
    end
    ret score
end

func good_name(name: str): bool:
    ret name.Len() >= 2
end

func good_mail(mail: str): bool:
    ret mail.Includes("@") and mail.Includes(".") and mail.Len() >= 6
end

func good_again(word: str, again: str): bool:
    ret word.Len() > 0 and again == word
end

component Signup:
    name := ""
    mail := ""
    word := ""
    again := ""
    shown := false
    greeting := ""
    form:
        id = "signup"
        font = "system", "sans"
        max width = 384
        margin = 28, "auto"
        padding = 28
        background = "white"
        radius = 16
        shadow = 0, 10, 30, "#4f46e526"
        submit = (e: i64):
            dom.PreventDefault(e)
            greeting = "Welcome, " + name + ". Check your inbox."
        end
        heading:
            size = 2
            content = "Create an account"
            margin = 0, 0, 4, 0
        end
        label:
            for = "name"
            font weight = 600
            margin = 14, 0, 4, 0
            content = "Name"
        end
        input:
            id = "name"
            autocomplete = name
            width = "fill"
            padding = 10, 12
            radius = 8
            font size = 16
            border = 2, "solid", "#cbd5e1"
            focus:
                border = 2, "solid", "#6366f1"
            end
            if name.Len() > 0 and good_name(name):
                border = 2, "solid", "#22c55e"
            end
            if name.Len() > 0 and not good_name(name):
                border = 2, "solid", "#ef4444"
            end
            input = (v: str): name = v end
        end
        paragraph:
            id = "name-note"
            color = "#dc2626"
            font size = 14
            min height = 20
            margin = 4, 0, 0, 0
            content = good_name(name) ? "" : "Please tell us your name."
        end
        label:
            for = "mail"
            font weight = 600
            margin = 14, 0, 4, 0
            content = "Email"
        end
        input:
            id = "mail"
            type = "email"
            autocomplete = "email"
            width = "fill"
            padding = 10, 12
            radius = 8
            font size = 16
            border = 2, "solid", "#cbd5e1"
            focus:
                border = 2, "solid", "#6366f1"
            end
            if mail.Len() > 0 and good_mail(mail):
                border = 2, "solid", "#22c55e"
            end
            if mail.Len() > 0 and not good_mail(mail):
                border = 2, "solid", "#ef4444"
            end
            input = (v: str): mail = v end
        end
        paragraph:
            id = "mail-note"
            color = "#dc2626"
            font size = 14
            min height = 20
            margin = 4, 0, 0, 0
            content = good_mail(mail) ? "" : "That does not look like an email address."
        end
        label:
            for = "pw"
            font weight = 600
            margin = 14, 0, 4, 0
            content = "Password"
        end
        input:
            id = "pw"
            type = shown ? "text" : "password"
            autocomplete = "new-password"
            width = "fill"
            padding = 10, 12
            radius = 8
            font size = 16
            border = 2, "solid", "#cbd5e1"
            focus:
                border = 2, "solid", "#6366f1"
            end
            if word.Len() > 0 and strength(word) == 3:
                border = 2, "solid", "#22c55e"
            end
            if word.Len() > 0 and strength(word) != 3:
                border = 2, "solid", "#ef4444"
            end
            input = (v: str): word = v end
        end
        box:
            height = 6
            background = "#e2e8f0"
            radius = 3
            margin = 6, 0
            box:
                id = "meter"
                height = "fill"
                width = strength(word) * 33 + "%"
                background = "#6366f1"
                radius = 3
                transition = 200ms
            end
        end
        paragraph:
            id = "pw-note"
            color = "#dc2626"
            font size = 14
            min height = 20
            margin = 4, 0, 0, 0
            content = strength(word) == 3 ? "" : "Use 8 characters, a capital letter and a digit."
        end
        button:
            id = "peek"
            type = "button"
            background = "transparent"
            border = 0
            color = "#4f46e5"
            cursor = "pointer"
            padding = 0
            click = (): shown = not shown end
            content = "Show or hide password"
        end
        label:
            for = "again"
            font weight = 600
            margin = 14, 0, 4, 0
            content = "Repeat the password"
        end
        input:
            id = "again"
            type = "password"
            width = "fill"
            padding = 10, 12
            radius = 8
            font size = 16
            border = 2, "solid", "#cbd5e1"
            focus:
                border = 2, "solid", "#6366f1"
            end
            if again.Len() > 0 and good_again(word, again):
                border = 2, "solid", "#22c55e"
            end
            if again.Len() > 0 and not good_again(word, again):
                border = 2, "solid", "#ef4444"
            end
            input = (v: str): again = v end
        end
        paragraph:
            id = "again-note"
            color = "#dc2626"
            font size = 14
            min height = 20
            margin = 4, 0, 0, 0
            content = good_again(word, again) ? "" : "The two passwords differ."
        end
        button:
            id = "join"
            type = "submit"
            disabled = not (good_name(name) and good_mail(mail) and strength(word) == 3 and good_again(word, again))
            width = "fill"
            padding = 12
            margin = 10, 0, 0, 0
            border = 0
            radius = 10
            background = "#4f46e5"
            color = "white"
            font size = 16
            cursor = "pointer"
            disabled:
                opacity = 0.5
                cursor = "not allowed"
            end
            content = "Join"
        end
        paragraph:
            id = "done"
            color = "#15803d"
            font weight = 600
            content = greeting
        end
    end
end

page:
    title = "Create an account"
    box:
        background = "#eef2ff"
        min height = 640
        Signup: end
    end
end
Result

Walkthrough. The page remembers four strings, one for each field, and a flag for the Show button. Every field has one job: its input handler writes the new text into its variable. Everything else is worked out from the four strings and drawn again whenever one changes, so the form can never be half updated. That is the pattern from the lesson on state. Small functions say whether a value is good (good_name, good_mail, strength, good_again). Each field draws itself from them: an if inside the input gives it the green border when the field is not empty and good, another gives it the red one when it is not empty and bad (nobody wants red on a form they have not touched), and the note under it is empty or holds the message. The colours sit next to focus: because they are states of an element and not decisions of the script.

The password score counts three things, length, a capital letter and a digit, and the bar shows score * 33 percent. The Show button flips a variable and the field takes its type from it. The email field has type = "email" and autocomplete values, so phones show the right keyboard and password managers know what to fill. The form's submit handler calls dom.PreventDefault(e) and shows a greeting in place of sending anything, because the preview has no server. The Join button is disabled until all four checks pass, and a disabled: block dims it.

Keep the names of your handler functions different from the id of every button and field in the form. A button with id = "join" and a function called join do not get along: inside a form the browser finds the button before the function, and the click does nothing. The functions here are called welcome and reveal. And remember that checks like these are help for the visitor. The server must check the same things again.

Change it next. Add a checkbox "I accept the rules" that must be ticked before the button turns on. Check the name for letters only. Replace the strength bar with a meter element. Send the form to a real address with action and method and validate it on the server (see the last section).

Project 4: a dashboard with a CSS bar chart

Goal. A page for looking at numbers: four statistic cards, a bar chart, and a table of the latest orders. No script at all.

web-project-dashboard.salam
component Stat(label, value, change, tone):
    box:
        background = "white"
        padding = 18
        radius = 14
        shadow = [0, 1, 3, "#0f172a1a"]
        paragraph:
            content = label
            margin = 0
            color = "#64748b"
            font size = 0.85rem
        end
        paragraph:
            content = value
            margin = 4px, 0
            font size = 1.9rem
            font weight = "bold"
        end
        small:
            content = change
            color = tone
            font weight = 600
        end
    end
end

component Bar(day, value):
    box:
        grow = 1
        text align = "center"
        box:
            layout = "row"
            align = "end"
            justify = "center"
            height = 150
            box:
                width = 70%
                height = value * 1.5
                content = value
                padding top = 4
                color = "white"
                font size = 0.8rem
                background = "#6366f1"
                radius = 8px, 8px, 0, 0
                transition = 0.2s
                hover background = "#4338ca"
            end
        end
        small:
            content = day
            color = "#475569"
        end
    end
end

component Pill(text, back, front):
    span:
        content = text
        padding = 2px, 10px
        radius = 999
        font size = 0.8rem
        background = back
        color = front
    end
end

page:
    title = "Shop dashboard"
    font = "system", "sans"
    margin = 0
    background = "#f1f5f9"
    color = "#0f172a"
    header:
        layout = "row"
        justify = "between"
        align = "center"
        padding = 16px, 5vw
        background = "#0f172a"
        color = "white"
        strong:
            content = "Hazel and Co"
        end
        small:
            content = "Week 43"
            color = "#94a3b8"
        end
    end
    main:
        padding = 24px, 5vw
        layout = "grid"
        gap = 20
        box:
            layout = "grid"
            columns = repeat (fit, minmax(180px, 1fr))
            gap = 16
            Stat:
                label = "Revenue"
                value = "$8,420"
                change = "+12% this week"
                tone = "#15803d"
            end
            Stat:
                label = "Orders"
                value = "312"
                change = "+5%"
                tone = "#15803d"
            end
            Stat:
                label = "Returns"
                value = "9"
                change = "-2 since Monday"
                tone = "#15803d"
            end
            Stat:
                label = "Open tickets"
                value = "14"
                change = "+6 since Monday"
                tone = "#b91c1c"
            end
        end
        box:
            layout = "grid"
            columns = repeat (fit, minmax(300px, 1fr))
            gap = 16
            section:
                background = "white"
                padding = 18
                radius = 14
                heading:
                    size = 2
                    content = "Orders per day"
                    font size = 1.1rem
                    margin = 0, 0, 12px, 0
                end
                box:
                    layout = "row"
                    gap = 8
                    Bar:
                        day = "Mon"
                        value = 45
                    end
                    Bar:
                        day = "Tue"
                        value = 62
                    end
                    Bar:
                        day = "Wed"
                        value = 38
                    end
                    Bar:
                        day = "Thu"
                        value = 80
                    end
                    Bar:
                        day = "Fri"
                        value = 95
                    end
                    Bar:
                        day = "Sat"
                        value = 70
                    end
                    Bar:
                        day = "Sun"
                        value = 30
                    end
                end
            end
            section:
                background = "white"
                padding = 18
                radius = 14
                overflow = "auto"
                heading:
                    size = 2
                    content = "Latest orders"
                    font size = 1.1rem
                    margin = 0, 0, 12px, 0
                end
                table:
                    width = 100%
                    header:
                        row:
                            header column:
                                padding = 10px, 8px
                                text align = "left"
                                border bottom = 1, "solid", "#e2e8f0"
                                content = "Order"
                            end
                            header column:
                                padding = 10px, 8px
                                text align = "left"
                                border bottom = 1px, "solid", "#e2e8f0"
                                content = "Customer"
                            end
                            header column:
                                padding = 10px, 8px
                                text align = "left"
                                border bottom = 1px, "solid", "#e2e8f0"
                                content = "Status"
                            end
                        end
                    end
                    main:
                        row:
                            cell:
                                padding = 10px, 8px
                                text align = "left"
                                border bottom = 1, "solid", "#e2e8f0"
                                content = "#2041"
                            end
                            cell:
                                padding = 10px, 8px
                                text align = "left"
                                border bottom = 1px, "solid", "#e2e8f0"
                                content = "Mina Rahimi"
                            end
                            cell:
                                padding = 10px, 8px
                                text align = "left"
                                border bottom = 1px, "solid", "#e2e8f0"
                                Pill:
                                    text = "Shipped"
                                    back = "#dcfce7"
                                    front = "#166534"
                                end
                            end
                        end
                        row:
                            cell:
                                padding = 10px, 8px
                                text align = "left"
                                border bottom = 1, "solid", "#e2e8f0"
                                content = "#2040"
                            end
                            cell:
                                padding = 10px, 8px
                                text align = "left"
                                border bottom = 1px, "solid", "#e2e8f0"
                                content = "Tom Becker"
                            end
                            cell:
                                padding = 10px, 8px
                                text align = "left"
                                border bottom = 1px, "solid", "#e2e8f0"
                                Pill:
                                    text = "Packing"
                                    back = "#fef3c7"
                                    front = "#92400e"
                                end
                            end
                        end
                        row:
                            cell:
                                padding = 10px, 8px
                                text align = "left"
                                border bottom = 1, "solid", "#e2e8f0"
                                content = "#2039"
                            end
                            cell:
                                padding = 10px, 8px
                                text align = "left"
                                border bottom = 1px, "solid", "#e2e8f0"
                                content = "Lena Novak"
                            end
                            cell:
                                padding = 10px, 8px
                                text align = "left"
                                border bottom = 1px, "solid", "#e2e8f0"
                                Pill:
                                    text = "Refunded"
                                    back = "#fee2e2"
                                    front = "#991b1b"
                                end
                            end
                        end
                    end
                end
            end
        end
    end
end
Result

Walkthrough. The chart is made of boxes. Bar is a component with a day and a value. Inside is a track of fixed height (150 pixels, a row whose items sit at the bottom with align = "end"), and in the track a box whose height = value + "%". A value of 80 gives a box 80 percent as high as the track. Putting the height in a track of known size is the trick: a percentage height needs a parent whose height is known. The number is written inside the bar, so the chart can be read with a screen reader, and the bar gets a darker colour on hover. The statistics are the component Stat, and the coloured status labels in the table are Pill, a component that is just one span.

The layout is two nested grids. The cards use auto-fit with a minimum of 180 pixels, and the lower row uses a minimum of 300, so the chart and the table sit side by side when there is room and stack when there is not. The table is a real table with a header and a main section, and its card has overflow = "auto", so on a narrow screen the table scrolls inside its card instead of breaking the page. The global rule for td, th gives all cells the same padding and a thin line below. Colour carries meaning here, green for good and red for bad, so every figure also has a sign or a word: never let colour be the only message.

Change it next. Make the bars grow when the page opens, with a CSS animation (transitions and animations). Add a column of buttons that filter the table, using the pattern of project 5. Fill the numbers from the server instead of the source, as in the last section.

Project 5: a task list that remembers

Goal. A small application: add tasks, tick them, remove them, filter by All, Active and Done, count what is left, and find the list again after a reload.

web-project-todo.salam
import dom

func load(): Vector<str>:
    mut found := Vector {} as Vector<str>
    lines := dom.StoreGetStr("todos").Split("\n")
    repeat lines.Len() - 1 in i:
        found.Push(lines.Get(i))
    end
    ret found
end

func joined(items: Vector<str>): str:
    mut text := ""
    repeat items.Len() in i:
        text += items.Get(i) + "\n"
    end
    ret text
end

func is_done(entry: str): bool:
    ret entry.Substr(0, 1) == "1"
end

func words(entry: str): str:
    ret entry.Substr(1, entry.Len())
end

func shows(mode: int, entry: str): bool:
    ret mode == 0 or (mode == 1 and not is_done(entry)) or (mode == 2 and is_done(entry))
end

func open_count(items: Vector<str>): int:
    mut open := 0
    repeat items.Len() in i:
        if not is_done(items.Get(i)):
            open++
        end
    end
    ret open
end

component Pick(label: str, active: bool, choose):
    button:
        border = 0
        background = "transparent"
        padding = 4, 10
        radius = 6
        cursor = "pointer"
        if active:
            background = "#e0e7ff"
            color = "#3730a3"
        end
        click = choose
        content = label
    end
end

component Row(entry: str):
    held := entry
    box:
        layout = "row"
        align = "center"
        padding = 10, 4
        border bottom = 1, "solid", "#e2e8f0"
        input:
            type = "checkbox"
            checked = is_done(entry)
            change = (): emit("flip", held) end
        end
        span:
            grow = 1
            padding = 0, 10
            color = is_done(entry) ? "#94a3b8" : "#0f172a"
            decoration = is_done(entry) ? "strike" : "none"
            content = words(entry)
        end
        button:
            border = 0
            background = "transparent"
            color = "#94a3b8"
            cursor = "pointer"
            hover:
                color = "#dc2626"
            end
            click = (): emit("remove", held) end
            content = "x"
        end
    end
end

component Tasks:
    items := load() as Vector<str>
    mode := 0
    draft := ""
    main:
        font = "system", "sans"
        max width = 448
        margin = 28, "auto"
        padding = 24
        background = "white"
        radius = 16
        shadow = 0, 8, 24, "#0f172a14"
        heading:
            size = 1
            content = "Tasks"
            font size = 29
            margin = 0, 0, 14, 0
        end
        form:
            layout = "row"
            gap = 8
            submit = (e: i64):
                dom.PreventDefault(e)
                if draft.Len() > 0:
                    items.Push("0" + draft)
                    draft = ""
                    dom.StoreSetStr("todos", joined(items))
                end
            end
            input:
                id = "new"
                grow = 1
                padding = 10
                border = 2, "solid", "#e2e8f0"
                radius = 8
                font size = 16
                placeholder = "Add a task"
                bind = draft
            end
            button:
                type = "submit"
                padding = 0, 18
                border = 0
                radius = 8
                background = "#4f46e5"
                color = "white"
                cursor = "pointer"
                content = "Add"
            end
        end
        list:
            padding = 0
            margin = 12, 0
            each _, entry in items:
                if shows(mode, entry):
                    item:
                        key = entry
                        Row:
                            entry = entry
                            on flip = (who: str):
                                repeat items.Len() in i:
                                    if items.Get(i) == who:
                                        items.Set(i, (is_done(who) ? "0" : "1") + words(who))
                                        break
                                    end
                                end
                                dom.StoreSetStr("todos", joined(items))
                            end
                            on remove = (who: str):
                                repeat items.Len() in i:
                                    if items.Get(i) == who:
                                        items.RemoveAt(i)
                                        break
                                    end
                                end
                                dom.StoreSetStr("todos", joined(items))
                            end
                        end
                    end
                end
            end
        end
        box:
            layout = "row"
            justify = "between"
            align = "center"
            color = "#64748b"
            font size = 14
            span:
                id = "left"
                content = open_count(items) + " left"
            end
            box:
                layout = "row"
                Pick:
                    label = "All"
                    active = mode == 0
                    choose = (): mode = 0 end
                end
                Pick:
                    label = "Active"
                    active = mode == 1
                    choose = (): mode = 1 end
                end
                Pick:
                    label = "Done"
                    active = mode == 2
                    choose = (): mode = 2 end
                end
            end
        end
    end
end

page:
    title = "Tasks"
    box:
        background = "#f8fafc"
        min height = 640
        Tasks: end
    end
end
Result

Walkthrough. This is the state pattern at full size. The state is three variables of one component: items, one string per task with 1 or 0 in front of the text to say whether it is done, mode, the filter, and draft, the text in the box. The page keeps nothing else. The list is an each over items, an if inside it lets a row through only when it passes the filter, and every row is a small component that draws itself from its own string: the tick, the grey colour and the strike-through all come from is_done. A row cannot change the list, so the page passes it two functions, flip and remove, and the row calls them with its text. They find the task in items, change it and save. There is no draw function to call and no handler to keep in step: changing items or mode is all it takes.

A row copies its entry into its own variable (held := entry) because a handler reads the variable of its own component, not the loop variable it was made with. Saving writes one string, one line per task, with 1 or 0 in front of the text. Loading is load, which gives items its first value when the page opens. A ternary expression picks between two values where the code needs a choice.

The preview runs in a frame that does not allow storage, so there the list works but does not survive a reload. On a real page it does. Task text is always put in with SetText, never SetHtml.

Change it next. Let a double click edit a task. Add a "clear done" button. Store the creation time and sort by it. Move the saving to a server, so the list follows the person to another device.

Project 6: a gallery with a lightbox

Goal. A grid of pictures. Clicking one opens it large in a window with Previous, Next and Close buttons, and the arrow keys and Escape work too. The pictures here are gradients, so the page needs no files, and the code is the same for photos.

web-project-gallery.salam
import dom

func title_of(n: int): str:
    switch n:
        0: ret "Sunrise" end
        1: ret "Camp" end
        2: ret "Dunes" end
        3: ret "Oasis" end
        4: ret "Stars" end
        else: ret "Road home" end
    end
    ret ""
end

func light_of(n: int): str:
    switch n:
        0: ret "#f97316" end
        1: ret "#7c3aed" end
        2: ret "#d97706" end
        3: ret "#059669" end
        4: ret "#1e1b4b" end
        else: ret "#475569" end
    end
    ret ""
end

func deep_of(n: int): str:
    switch n:
        0: ret "#facc15" end
        1: ret "#ec4899" end
        2: ret "#fde68a" end
        3: ret "#67e8f9" end
        4: ret "#6366f1" end
        else: ret "#cbd5e1" end
    end
    ret ""
end

func shade(n: int): str:
    ret "linear(135, " + light_of(n) + ", " + deep_of(n) + ")"
end

component Shot(title: str, shade: str, pick):
    button:
        background = shade
        aspect ratio = 1.3333
        border = 0
        radius = 12
        color = "white"
        font size = 16
        font weight = 600
        text align = "start"
        padding = 12
        cursor = "pointer"
        transition = 150ms
        hover:
            scale = 1.04
        end
        click = pick
        content = title
    end
end

component Gallery:
    current := -1
    box:
        font = "system", "sans"
        padding = 24, 40
        background = "#0f172a"
        color = "white"
        min height = 640
        heading:
            size = 1
            content = "Trip to the dunes"
            font size = 29
            margin = 0, 0, 16, 0
        end
        box:
            layout = "grid"
            columns = repeat (fill, minmax(150, 1fr))
            gap = 12
            Shot:
                title = title_of(0)
                shade = shade(0)
                pick = (): current = 0 end
            end
            Shot:
                title = title_of(1)
                shade = shade(1)
                pick = (): current = 1 end
            end
            Shot:
                title = title_of(2)
                shade = shade(2)
                pick = (): current = 2 end
            end
            Shot:
                title = title_of(3)
                shade = shade(3)
                pick = (): current = 3 end
            end
            Shot:
                title = title_of(4)
                shade = shade(4)
                pick = (): current = 4 end
            end
            Shot:
                title = title_of(5)
                shade = shade(5)
                pick = (): current = 5 end
            end
        end
        if current >= 0:
            box:
                id = "lightbox"
                position = "fixed"
                inset = 0
                layout = "stack"
                align = "center"
                justify = "center"
                key down = (e: i64):
                    if dom.EventKey(e) == "ArrowRight":
                        current = (current + 1) % 6
                    else dom.EventKey(e) == "ArrowLeft":
                        current = (current + 5) % 6
                    end
                end
                box:
                    id = "backdrop"
                    width = "fill"
                    height = "fill"
                    background = "#0f172ab3"
                    click = (): current = -1 end
                end
                box:
                    width = 92%
                    max width = 544
                    padding = 16
                    radius = 16
                    background = "white"
                    color = "#0f172a"
                    box:
                        id = "big"
                        height = 260
                        radius = 12
                        background = shade(current)
                    end
                    paragraph:
                        id = "cap"
                        margin = 12, 0
                        font weight = 600
                        content = title_of(current) + " (" + (current + 1) + " of 6)"
                    end
                    box:
                        layout = "row"
                        gap = 8
                        button:
                            id = "back"
                            padding = 8, 16
                            border = 0
                            radius = 8
                            background = "#e2e8f0"
                            cursor = "pointer"
                            click = (): current = (current + 5) % 6 end
                            content = "Previous"
                        end
                        button:
                            id = "forth"
                            padding = 8, 16
                            border = 0
                            radius = 8
                            background = "#e2e8f0"
                            cursor = "pointer"
                            click = (): current = (current + 1) % 6 end
                            content = "Next"
                        end
                        button:
                            id = "shut"
                            autofocus = true
                            padding = 8, 16
                            border = 0
                            radius = 8
                            background = "#e2e8f0"
                            cursor = "pointer"
                            margin start = "auto"
                            click = (): current = -1 end
                            content = "Close"
                        end
                    end
                end
            end
        end
    end
end

page:
    title = "Photo gallery"
    Gallery: end
end
Result

Walkthrough. The window is not a dialog element but a box that exists only while a variable says so. current holds the number of the open picture, and minus one means none. When it is zero or more, an if adds a box with position = "fixed" and inset = 0 that covers the page, laid out as a stack: a dim box that fills it, with a click handler that sets current back to minus one, and the white card on top. The dim layer is a colour with some transparency, so there is no backdrop pseudo element and no style element. Clicking the dim box closes the window and clicking the card does not, because the two are siblings, not parent and child. The Close button has autofocus, so the keyboard focus is inside the window as soon as it opens. The card is width = 92% with max width = 544, which does what min(34rem, 92vw) did.

Each tile is a button from the component Shot, which gets its title and its colours from the page. The page keeps the data in small functions (title_of, light_of, deep_of) and builds the gradient with shade, so the tile and the big picture use the same colours. Previous and Next move current by one, and the % wraps around from the last picture to the first and back. The arrow keys are read by a key down handler with dom.EventKey.

Change it next. Put real images in the tiles (links, images and media) and keep a small and a large version of each. Close the window when the user swipes down. Move the focus back to the tile that opened it.

Serving a page from a Salam program

Everything above is a fixed page: its structure is decided when you compile it, and the preview is the finished result. A real site needs pages that depend on the visitor, such as a greeting with their name or a dashboard with today's numbers. For that, the page gets a name and parameters, and it becomes a function that returns the page as HTML. A web server written in Salam calls it for each request. The first lesson of this part explains these pages in detail; here is the smallest complete program, which would serve the landing page with a name and a plan taken from the address.

web-project-serve.salam
import net.http
import net.router

page Welcome(name: str, plan: str):
    title = "Welcome, " + name
    font = system, sans
    margin = 0
    background = "#fafaf9"
    main:
        max width = 30rem
        margin = 48px, auto
        padding = 28
        background = "white"
        radius = 16
        heading:
            size = 1
            content = "Welcome, " + name
        end
        paragraph:
            content = "Your " + plan + " plan starts on Monday."
        end
    end
end

func home(ctx: i64):
    mut name := http.Query(ctx, "name")
    mut plan := http.Query(ctx, "plan")
    if name == "":
        name = "friend"
    end
    if plan == "":
        plan = "Solo"
    end
    http.Html(ctx, Welcome(name, plan))
end

func main:
    mut r := router.NewRouter()
    router.Get(r, "/", home)
    mut s := http.NewServer(8080)
    http.Use(s, r)
    http.Run(s)
end

The program starts a server and has no output, so it is only checked here. Run it, open http://localhost:8080/?name=Ada&plan=Duo and you get the page. Text that arrives in a parameter is escaped for you, so a visitor who sends ?name=<b> sees the characters and no markup. For the server side, the net.http and net.router packages handle requests, query strings and answers, and net.http.validate checks the form data that project 3 collected in the browser. A static page like projects 1 to 6 is also complete as it is: salam build page.salam --platform=web --inline writes it to a single HTML file that any web host will serve. To serve the site, run several domains, connect PHP and deploy, the salam serve web server lesson covers all of it.

Routes: one page, many screens

A single-page app has one HTML file and many screens: /, /users/3, /settings. The pages/ folder next to main.salam is the route table: one file per screen, and the file name is the path. An Outlet element shows the component of the current path. Changing the route does not reload the page; it is a state change that redraws only the outlet.

main.salam
pages/
  index.salam           /
  login.salam           /login
  admin.salam           /admin
  users/
    [id].salam          /users/:id
  _404.salam            every other path

Each file declares exactly one pub component. page.Guard(condition), when the page has one, is the first line of its body, and the title setting comes after it.

mut signed_in := false

page:
    nav:
        link: to = "/" content = "Home" end
        link: to = "/users/1" content = "User 1" end
    end
    Outlet: end
end
pub component User(id: int):
    title = "User " + id
    heading: content = "User " + id end
    button: click = (): page.route.GoTo("/users/" + (id + 1)) end content = "Next" end
end
pub component Admin:
    page.Guard(signed_in)
    paragraph: content = "Admin area" end
end
pub component Login:
    button:
        click = ():
            signed_in = true
            page.route.Redirect("/admin")
        end
        content = "Sign in"
    end
end
•

A file or folder name is a literal segment, [id] is one segment that fills the parameter id, [[id]] is one that may be missing, and [...rest] takes the rest of the path. The most specific path wins, so users/new.salam beats users/[id].salam. _404.salam catches every other path, and index.salam is its folder itself.

•

A :name segment fills the component parameter with the same name. An int parameter only matches a whole number that fits in an int (digits with an optional minus sign), so /users/abc falls to _404.salam.

•

page.Guard(condition) lets the route match only while the condition is true. It must be the first line of the component, above every setting, style and element, and never inside an if. A _layout.salam in a folder gives the same guard to every page below it. When it is false the route is skipped and the next one that matches is shown, usually _404.salam, while the address stays the same. Guards are checked again after every state change, so signing out leaves a guarded screen at once. To send the visitor somewhere else, call page.route.Redirect(path) in code, as Login does after signing in. title sets the document title.

•

A file name uses ASCII letters, digits, - and _. For a path in another script, set route = "/users/:id" (any text, here a Persian one is the usual case) at the top of the component body: it replaces the path taken from the file name. The Persian names are عنوان, مسیر and پیش ساخت (for prerender, see serving) for the settings and صفحه.نگهبان for page.Guard.

•

A _layout.salam is not a route. It declares one pub component with an Outlet, and it wraps every page in its folder and below, from the outside in. Its page.Guard protects the whole folder once. Put the shared navigation in it:

pub component AdminLayout:
    page.Guard(signed_in)
    nav:
        link: to = "/admin/users" content = "Users" end
    end
    Outlet: end
end
•

A mistake in the folder is an error that names the file: E280 when two files give the same route (a.salam and a/index.salam), E281 when [id] has no parameter id in its component, E282 when a route file has no pub component or more than one, E283 when a layout has no Outlet and E284 for a file name that is not valid.

•

link: to = "/path" is a normal link: middle click, open in a new tab and crawlers work, and a plain click changes the route without a reload. The link of the current route gets aria-current="page". A link with url instead of to always loads the page from the server.

•

In code, page.route.GoTo(path) opens a screen, page.route.Redirect(path) opens one in place of the current screen, so Back skips it (good after a sign in), and page.route.Back() and page.route.Forward() move through the history like the browser buttons. page.route.path, page.route.query and page.route.pattern are state you can read anywhere, and page.route.Param("id") and page.route.Query("q") read single values. A relative path in page.route.GoTo is resolved under the current one, so page.route.GoTo("edit") from /users/3 opens /users/3/edit.

•

The Persian names are نمای مسیر, مقصد, صفحه.مسیر.برو, صفحه.مسیر.هدایت, صفحه.مسیر.عقب, صفحه.مسیر.جلو, صفحه.مسیر, صفحه.مسیر.پارامتر and صفحه.مسیر.مقدار پرسمان.

There are two modes. The default, mode = "history" (a setting of the page: block), uses clean paths such as /users/3. A visitor can open such a path directly, so the server must answer every page path with the app: salam serve does that on its own for a page built in this mode, salam build --platform=web writes an .htaccess file for Apache next to the page, and nginx needs try_files $uri /index.html; (see serving a single-page app). mode = "hash" keeps the route after a #, as in /#/users/3, and works on any static host with no server setting at all. base = "/app" serves the app under a sub path.

A page project with a pages/ folder can also be served as finished HTML. salam build --platform=web --ssr=dist writes dist/server, a native program, and dist/public with the script, the style sheet and a copy of every page that needs no parameter. Start it with PORT=8080 dist/server (HOST defaults to 127.0.0.1, PORT=0 picks a free port). For each request the server finds the route, checks the guards (a page whose guard fails is skipped like an unknown path), builds the layouts and the page into HTML with the same code the browser would run, and answers 404 with your _404 page for an unknown path. Text that comes from the address is escaped in the page and in the state block <script type="application/json" id="__salam">. salam serve --render does the build, runs the server and rebuilds when you save a file. The server rendering lesson has the details and explains hydration.

Several files: the components folder

A page project follows one convention: components/ holds the pub component of the project, pages/ holds the routes, and everything else is included explicitly. When the entry file is main.salam and a folder named components/ sits next to it, the compiler reads every .salam file in it (subfolders too, in sorted order) and each pub component in them can be used from any file with no include. A component without pub stays private to its own file (using it elsewhere is E232). A file in components/ that declares no component is ignored.

main.salam                  page: Card: title = "hi" end end
pages/index.salam           pub component Home: ... end
components/card.salam       pub component Card(title): ... Badge: ... end
components/shop/badge.salam pub component Badge(label): ... end
ui/price_card.salam         reached with include = "ui/price_card.salam"
lib/format.salam            reached with include "lib/format.salam"

Two pub component with the same name among components/, main.salam and the route files in pages/ are error E291, and the message names both files and lines. Because the names are shared, an English name must be written in PascalCase: a capital first letter and no _, as in PriceCard. price_card or priceCard is error E290 and the message shows the spelling it wants. Persian names are exempt from the rule.

•

Without a components/ folder nothing is imported by name. A component in any other file, for example PriceCard in ui/price_card.salam, is used only after the line include = "ui/price_card.salam" inside the page: block or a component body (the path is relative to that file). If you use it without that line, the unknown element error says that a pub component with that name exists outside components/: move it there or include it. A name that the standard kit also has, such as Card, means the kit element instead.

•

Functions are loaded differently: write include "lib/format.salam", without =, at the top of main.salam. That file starts with package format (the file name), only its pub names are visible, and pages and components call them as format.Money(n). The = form does not load functions, and this form does not import components.

•

Not read inside components/: hidden folders, node_modules, build, dist, out, target, vendor, tests, test and __tests__, any folder that has its own main.salam (another project), and files ending in _test.salam or .test.salam. The pages/ folder is route files only.

•

Building one file that is not named main.salam (salam build card.salam --platform=web), the playground and layouts used from a Salam program work as before: nothing is imported unless you write include, and a repeated pub name only gives a warning.

Try it yourself

•

Combine two projects. Give the landing page a sign up form from project 3 under the plans, and make the Choose buttons fill in the plan name.

•

Make the blog post into a reusable page: a component Post with a title and a date, and two posts that use it. Which parts stay fixed, and which become parameters?

•

Put the dashboard on a phone: open the preview in a narrow window and fix whatever looks cramped, using only auto-fit and one media rule.

•

Translate one project into Persian, or into English if you started in Persian. Which style properties need to change for a right to left page, and which ones change by themselves?