Layout

Loading Ebi…
fn tiles(): List<Element> {
    return [
        <Box w-26 h-full shrink-0 rounded-5 bg-blue3/>,
        <Box w-26 h-full shrink-0 rounded-5 bg-purple3/>,
        <Box w-26 h-full shrink-0 rounded-5 bg-orange3/>,
        <Box w-26 h-full shrink-0 rounded-5 bg-green3/>,
    ]
}

let row_count = 4
let label_height = 15pt
let section_gap = 5pt

fn caption(label: String): Element {
    return <Text text-sm height={label_height} fill="light1">{label}</Text>
}

mut compression = 0
sig row_width = 100% - 42% * compression
sig row_height = 32pt - 6pt * compression
sig column_width = 100% - 18% * compression
sig column_height = row_count * row_height + (row_count - 1) * (label_height + 2 * section_gap)

<Box w-94% h-244 p-10 gap-12 flex-row items-start rounded-xl bg-dark4
    x_align={Align.Center} y_align={Align.Center}>
    <Box w-70% shrink-0 flex-col gap={section_gap}>
        {caption("Fixed gap")}
        <Box width={row_width} height={row_height} p-6 flex-row gap-10 items-center
            rounded-8 bg-dark3 children={tiles()}/>
        {caption("Space between")}
        <Box width={row_width} height={row_height} p-6 flex-row justify-between items-center
            rounded-8 bg-dark3 children={tiles()}/>
        {caption("Space around")}
        <Box width={row_width} height={row_height} p-6 flex-row justify-around items-center
            rounded-8 bg-dark3 children={tiles()}/>
        {caption("Row growth")}
        <Box width={row_width} height={row_height} p-6 gap-10 flex-row rounded-8 bg-dark3>
            <Box w-26 h-full shrink-0 rounded-5 bg-blue3/>
            <Box basis-48 grow h-full rounded-5 bg-purple3/>
            <Box basis-52 flex_grow={2} h-full rounded-5 bg-orange3/>
            <Box w-26 h-full shrink-0 rounded-5 bg-green3/>
        </Box>
    </Box>
    <Box grow flex-col gap={section_gap}>
        {caption("Column growth")}
        <Box width={column_width} height={column_height} p-6 gap-6 flex-col items-center rounded-8 bg-dark3>
            <Box w-26 h-18 shrink-0 rounded-5 bg-blue3/>
            <Box w-26 basis-60 grow rounded-5 bg-purple3/>
            <Box w-26 basis-60 flex_grow={2} rounded-5 bg-orange3/>
            <Box w-26 h-18 shrink-0 rounded-5 bg-green3/>
        </Box>
    </Box>
</Box>

wait(450ms)
animate(compression, to=1, duration=1100ms, ease="smootherstep")
wait(450ms)
animate(compression, to=0, duration=1100ms, ease="smootherstep")

As the boxes resize, gap-10 keeps the first row's tiles 10 points apart. justify-between puts the available space between tiles. With justify-around, the outside gaps are half as wide as the gaps between tiles.

In the growth examples, the blue and green tiles keep their main-axis size with shrink-0. The purple and orange tiles start from their basis-* sizes and shrink when space runs out. With room to grow, orange takes twice as much extra space as purple. This changes widths in the row and heights in the column.

Use Animation to lay out when actions play. Its gap and padding use durations.

Arrange children

A Box is transparent by default and stacks its children vertically. A plain Box fills the available width and takes its height from its children. Use flex-row for horizontal flow or flex-col for vertical flex flow. A flex box without a width fits its contents within the available width. Add w-full to fill that space.

Useful attributesEffect
gap-8, p-16Add space between children or around the contents.
items-start, items-center, items-endAlign children across the flow.
justify-start, justify-center, justify-end, justify-between, justify-aroundDistribute children along the flow.
grow, shrink, basis-80Control how a child uses available space.
flex-wrapStart a new row or column when the children run out of space.

Size and place elements

fn tile(width: Length, color: Color): Element {
    return <Box width={width} h-26 shrink-0 rounded-5 fill={color}/>
}

mut wrap_width = 100%

<Box w-94% p-12 gap-7 flex-col items-start rounded-xl bg-dark4
    x_align={Align.Center} y_align={Align.Center}>
    <Text text-sm fill="light1">Fixed, growing, and percentage widths</Text>
    <Box w-full h-42 p-6 gap-6 flex-row rounded-8 bg-dark3>
        <Box w-66 h-full shrink-0 rounded-5 bg-blue4 flex-row items-center justify-center>
            <Text text-sm fill="light1">66pt</Text>
        </Box>
        <Box basis-48 grow h-full rounded-5 bg-purple4 flex-row items-center justify-center>
            <Text text-sm fill="light1">Grow</Text>
        </Box>
        <Box w-28% h-full rounded-5 bg-green5 flex-row items-center justify-center>
            <Text text-sm fill="light1">28%</Text>
        </Box>
    </Box>
    <Text text-sm fill="light1">Wrapping fixed-width tiles</Text>
    <Box width={wrap_width} p-6 gap-6 flex-row flex-wrap rounded-8 bg-dark3>
        {tile(50pt, Color("blue3"))}
        {tile(66pt, Color("purple3"))}
        {tile(42pt, Color("orange3"))}
        {tile(58pt, Color("green3"))}
        {tile(46pt, Color("red3"))}
        {tile(62pt, Color("yellow3"))}
    </Box>
</Box>

wait(600ms)
animate(wrap_width, to=55%, duration=900ms, ease="smootherstep")
wait(600ms)
animate(wrap_width, to=100%, duration=900ms, ease="smootherstep")

The first tile keeps its 66-point width. The second starts from a 48-point basis and grows into spare space. The third starts at 28% of the row's content width. In the second row, flex-wrap moves tiles to new lines as the container narrows.

Use w-240 or h-100 to set a size in points. Like Tailwind, w-full is shorthand for width={100%}. It uses the parent's content width after padding, or the canvas width for a top-level element. In a flex row it can shrink to share space with siblings. Use shrink-0 when the item must keep its full width.

Use width={60%} for other percentages or h-full to fill a parent's set height. Explicit attributes override utilities.

Use x_align={Align.Center} or y_align={Align.End} to align the element itself. position={[x, y]} offsets it. A child with absolute can overlay siblings without taking up space in the flow. For a clipped, scrollable box, see Scrolling.

Surface materials

A Box accepts a material along with its usual layout attributes. corner_style="continuous" softens the transition from each straight edge into the corner.

fn<sig> sheen(ctx: ShaderContext, sig sweep: Number, shade: Color, shine: Color): Color {
    let light = max(0, 1 - 4 * abs(ctx.u - 0.45 * ctx.v - sweep))
    return Rgb(
        lerp(shade.to_rgb().red, shine.to_rgb().red, light),
        lerp(shade.to_rgb().green, shine.to_rgb().green, light),
        lerp(shade.to_rgb().blue, shine.to_rgb().blue, light),
    )
}

mut progress = 0.12
sig sweep = -0.42 + 1.46 * progress
let glass = Material.glass(blur = 4pt, refraction = 14pt, tint = Color("yellow1").alpha(24%))
sig ripple = Material.ripple(
    tint = Color("purple1").alpha(85%), origin = [0.27, 0.58], progress = progress, grain = 0.65,
)
let highlight = sheen(sweep = sweep, shade = Color("dark4"), shine = Color("blue1"))

<Box w-full h-full p-14 gap-10 flex-row items-center fill="dark4">
    <Box flex-1 h-130 gap-6 flex-col>
        <Text text-sm fill="light1">Glass</Text>
        <Box w-full h-108 rounded-14 flex-row items-center justify-center fill="dark3" overflow="hidden">
            <Box absolute w-full h-full p-6 gap-5 flex-row>
                <Box flex-1 h-full rounded-3 fill="orange1"/>
                <Box flex-1 h-full rounded-3 fill="blue2"/>
                <Box flex-1 h-full rounded-3 fill="green1"/>
                <Box flex-1 h-full rounded-3 fill="purple2"/>
                <Box flex-1 h-full rounded-3 fill="yellow1"/>
            </Box>
            <Box w-76% h-66% rounded-18 corner_style="continuous" material={glass}/>
        </Box>
    </Box>
    <Box flex-1 h-130 gap-6 flex-col>
        <Text text-sm fill="light1">Press ripple</Text>
        <Box w-full h-108 rounded-18 corner_style="continuous" fill="purple5" material={ripple}/>
    </Box>
    <Box flex-1 h-130 gap-6 flex-col>
        <Text text-sm fill="light1">Shader</Text>
        <Box w-full h-108 rounded-18 corner_style="continuous" material={Material.shader(shader = highlight)}/>
    </Box>
</Box>

animate(progress, to = 1, duration = 1.8s, ease = "linear")
wait(350ms)
  • Material.glass samples earlier drawing in the same layer. It refracts and blurs the backdrop. The lit rim and darker bevel respond to the brightness of the tinted backdrop. Set blur to the Gaussian standard deviation in points, and refraction to the maximum bend in points. Change the tint's alpha to adjust its strength.
  • Material.ripple draws visual press feedback over a box's fill. The ripple spreads from origin and fades as progress approaches 1. The origin is relative to the box, from [0, 0] at the top left to [1, 1] at the bottom right. Set grain = 0 for a smooth ripple.
  • Material.shader runs a GPU function directly on the box surface. A bound sig argument updates the shader as the signal changes. See GPU shaders for the shader context fields.

Paper

Material.paper shows fine fibers and faint gradient noise on a smooth sheet. Set color for the sheet's base tone, grain from 0 to 1 for texture strength, and scale from 0.01 to 100 to size the texture. The finish moves and rotates with its box.

Set seed to a whole number for a repeatable pattern. Fractional seeds blend neighboring patterns. The blue paper below animates seed from 0 to 2 and back.

let white = Material.paper()
let blue = Color("blue1")
mut seed = 0
sig blue_paper = Material.paper(color = blue, seed = seed)

<Box w-full h-full p-12 gap-8 flex-row items-center fill="dark4">
    <Box flex-1 h-166 gap-6 flex-col>
        <Text text-sm fill="light1">Paper</Text>
        <Box w-full h-144 rounded-4 material={white}/>
    </Box>
    <Box flex-1 h-166 gap-6 flex-col>
        <Text text-sm fill="light1">Blue paper</Text>
        <Box w-full h-144 rounded-4 material={blue_paper}/>
    </Box>
    <Box flex-1 h-166 gap-6 flex-col>
        <Text text-sm fill="light1">Eggshell</Text>
        <Box w-full h-144 rounded-4 material={Material.eggshell(color = blue)}/>
    </Box>
</Box>

animate(seed, to = 2, duration = 3s, ease = "linear")
animate(seed, to = 0, duration = 3s, ease = "linear")

Eggshell

Material.eggshell gives the surface a fine, pebbled finish with soft tonal variation beneath the grains. The right swatch uses the same blue as the paper beside it. Eggshell accepts the same color, grain, and scale controls.

Marble

Material.marble draws softly veined stone. color and vein default to Adwaita light1 and light5. contrast sets vein strength from 0 to 1 and defaults to 0.35. scale sets the pattern size from 0.01 to 100, with larger values giving wider spacing. The veins stay fixed to the box as it moves or rotates.

let cool = Material.marble(color = Color("blue1"), vein = Color("blue5"), contrast = 0.65)

<Box w-full h-full p-12 gap-8 flex-row items-center fill="dark4">
    <Box flex-1 h-150 gap-6 flex-col>
        <Text text-sm fill="light1">Marble</Text>
        <Box w-full h-128 rounded-8 material={Material.marble()}/>
    </Box>
    <Box flex-1 h-150 gap-6 flex-col>
        <Text text-sm fill="light1">Blue marble</Text>
        <Box w-full h-128 rounded-8 material={cool}/>
    </Box>
</Box>

Hatches

Material.hatches draws overlapping bundles of fine ink strokes over the box's fill. The default puts 50 strokes per 100 points, each 0.45 points wide, in varying directions around 45deg. Change density to adjust the spacing, width to adjust the thickness, and length to scale the bundles. roughness varies stroke directions from 0 to 1. Set crosshatch = false for a single set of directions.

let ink = Color("dark4").alpha(72%)
<Box w-full h-full p-12 gap-8 flex-row items-center fill="dark4">
    <Box flex-1 h-150 gap-6 flex-col>
        <Text text-sm fill="light1">Crosshatch</Text>
        <Box w-full h-128 rounded-8 fill="light2" material={Material.hatches(color = ink)}/>
    </Box>
    <Box flex-1 h-150 gap-6 flex-col>
        <Text text-sm fill="light1">One main angle</Text>
        <Box w-full h-128 rounded-8 fill="light2"
            material={Material.hatches(color = ink, crosshatch = false)}/>
    </Box>
</Box>