Lesson 39 of 50 · Web pages

States and transitions

An element can look different when the pointer is over it, when it has focus, when it is pressed or when it is turned off. In the page language you put the name of the state in front of the property.

State prefixes

hover background = #1d4ed8 applies only while the pointer is over the element. The states are hover, focus, focus within, active, disabled, checked, invalid, placeholder and selection. Any style property can carry one.

dsl-hover.salam
page:
    title = "Hover"
    box:
        layout = row
        gap = 12
        padding = 16
        button:
            padding = 8, 16
            radius = 6
            border = 1, solid, #1d4ed8
            background = #2563eb
            color = white
            cursor = pointer
            hover background = #1d4ed8
            active background = #1e3a8a
            focus outline = 2, solid, #f59e0b
            disabled opacity = 0.5
            content = "Press me"
        end
        button: disabled = true padding = 8, 16 content = "Off" end
    end
end

State blocks

When one state changes several properties, write the state name with a colon and put the properties inside. hover: followed by background = navy and padding = 12 means exactly hover background = navy and hover padding = 12. Every state works as a block name, also focus within:. A state block holds style properties only, no child elements, and state blocks do not nest.

dsl-state-blocks.salam
page:
    title = "State blocks"
    box:
        layout = row
        gap = 12
        padding = 16
        button:
            padding = 8, 16
            radius = 6
            background = #2563eb
            color = white
            hover:
                background = #1d4ed8
                padding = 10, 20
            end
            active:
                background = #1e3a8a
            end
            content = "Press me"
        end
        box:
            padding = 8
            border = 1, solid, #cbd5e1
            focus within:
                border = 2, solid, #f59e0b
            end
            input: end
        end
    end
end

Smooth changes

transition = 300ms, ease out makes every animatable property take 300 milliseconds to change. transition properties = background, scale limits it to some. Easing is linear, ease, ease in, ease out or ease in out. The reference marks which properties animate.

dsl-transition.salam
page:
    title = "Transitions"
    box:
        padding = 24
        box:
            width = 120
            height = 80
            background = #3366aa
            radius = 8
            transition = 300ms, ease
            hover background = #f59e0b
            hover scale = 1.2
            hover rotate = 8
        end
    end
end

State blocks

When several properties change in the same state, write the state once as a block: hover:, focus:, active:, focus within: and the other states of the list above open a block that holds style properties only and closes with end. A block is the same as putting the state in front of each property.

dsl-hover-block.salam
page:
    title = "State blocks"
    box:
        layout = row
        gap = 12
        padding = 16
        button:
            padding = 8, 16
            radius = 6
            background = #2563eb
            color = white
            transition = 200ms, ease out
            hover:
                background = #1d4ed8
                scale = 1.05
            end
            active:
                background = #1e3a8a
            end
            focus:
                outline = 2, solid, #f59e0b
            end
            content = "Press me"
        end
    end
end

Style that depends on state

An if inside an element, holding only style properties, applies them while its condition is true. The page reads the state, so the style follows it. else if and else work too, and a state block may sit inside. A number property needs a base value before the if (for example opacity = 1).

dsl-if-style.salam
component Toggle:
    picked := false
    box:
        padding = 16
        button:
            id = "pick"
            padding = 8, 16
            radius = 6
            background = #2563eb
            color = white
            if picked:
                background = #f59e0b
                color = #111827
            end
            click = (): picked = not picked end
            content = "Pick me"
        end
    end
end

page:
    title = "Conditional style"
    Toggle: end
end