Skip to content

Containers

rizuki edited this page Nov 30, 2026 · 8 revisions

Containers

Column, Row, Box, and Element open a Clay element and declare children inside a C block. Each macro uses a single-iteration for loop, with the close call in the loop step.

The shape

Column("id", .field = value, ...) { children }
Row(   "id", .field = value, ...) { children }
Box(   "id", .field = value, ...) { children }
Element(direction, "id", .field = value, ...) { children }

The first argument is an id of type const char *. CC_StrIntern copies it into stable storage before Clay hashes it. Pass "" for an anonymous element. Clay generates an internal id for layout bookkeeping, but "" cannot target hover, click, scroll persistence, or floating .parentId anchoring.

The remaining arguments are designated initializers on Clay_ElementDeclaration. Every Clay field is available.

Ids hash by content. Two calls with the same string refer to the same element. Inside loops, generate a unique id per iteration with snprintf, or use CC_IDI("row", i) with raw CLAY({ ... }) blocks for indexed hashing.

Column

Column("id", ...) lays out children top-to-bottom (CC_TOP_TO_BOTTOM). Children are separated by .layout.childGap pixels.

Column("Sidebar",
       .layout = { .sizing   = { Fixed(220), Grow() },
                   .padding  = PadAll(16),
                   .childGap = 8 },
       .backgroundColor = Color(28, 28, 34, 255),
       .cornerRadius    = RadiusAll(8)) {
    Text("Home",     .textColor = Color(236,236,236,255), .fontSize = 16);
    Text("Settings", .textColor = Color(236,236,236,255), .fontSize = 16);
    Text("About",    .textColor = Color(236,236,236,255), .fontSize = 16);
}

Common patterns:

// Fill the screen and center children both axes
Column("Root",
       .layout = { .sizing = { Grow(), Grow() },
                   .childAlignment = { .x = AlignXCenter(),
                                       .y = AlignYCenter() } }) { ... }

// Scrollable list
Column("List",
       .layout = { .sizing = { Grow(), Fixed(280) }, .childGap = 4 },
       .clip   = { .vertical = true, .childOffset = { 0, scroll_y } }) { ... }

Row

Row("id", ...) lays out children left-to-right (CC_LEFT_TO_RIGHT). Use .layout.childAlignment.y = AlignYCenter() to vertically center them within the row's height.

Row("Toolbar",
    .layout = { .sizing         = { Grow(), Fixed(40) },
                .padding        = PadAll(8),
                .childGap       = 12,
                .childAlignment = { .y = AlignYCenter() } }) {
    Text("File", .textColor = Color(236,236,236,255), .fontSize = 14);
    Text("Edit", .textColor = Color(236,236,236,255), .fontSize = 14);
    Text("View", .textColor = Color(236,236,236,255), .fontSize = 14);
}

Two-pane layout:

Row("Main", .layout = { .sizing = { Grow(), Grow() } }) {
    Column("Nav",
           .layout = { .sizing = { Fixed(240), Grow() } }) { ... }
    Column("Content",
           .layout = { .sizing = { Grow(), Grow() } }) { ... }
}

Box

Box("id", ...) is a z-stack. Its children are layered on top of each other in declaration order, with the last one on top. .layout.padding and .layout.childAlignment apply to every layer.

Box("Timer",
    .layout = { .sizing         = { Fixed(200), Fixed(200) },
                .childAlignment = { .x = AlignXCenter(),
                                    .y = AlignYCenter() } }) {
    CircularProgress(0.75f, .sizing = { Grow(), Grow() });
    Text("24:59", .textColor = Color(236,236,236,255), .fontSize = 40);
}

The time is drawn centered over the ring. In a Column it would sit below it.

Stacking arrived in v0.1.4. Up to v0.1.3 a Box laid its children out top to bottom like a Column, and only children that set .floating were layered. If you relied on that flow, change the Box to a Column.

Decoration / frame

With one child, the box carries the styling and the child sits inside it:

Box("Badge",
    .layout          = { .sizing = { Fixed(24), Fixed(24) } },
    .backgroundColor = Color(230, 80, 80, 255),
    .cornerRadius    = RadiusAll(999)) {
    Text("!", .textColor = Color(255,255,255,255), .fontSize = 14);
}

For image content, use the Image leaf macro.

Overlays

Box("Card",
    .layout          = { .sizing = { Grow(), Fixed(220) }, .padding = PadAll(16) },
    .backgroundColor = Color(28, 28, 34, 255),
    .cornerRadius    = RadiusAll(12)) {

    // background layer
    Image("CardBg", ImgCrop(&bg_texture),
          .layout = { .sizing = { Grow(), Grow() } });

    // foreground layer, drawn over the image
    Text("Title", .textColor = Color(255,255,255,255), .fontSize = 24);
}

How the stack is built

Clay has no z-stack layout mode, so Box builds one. The first child stays in normal flow. Every later child is wrapped in a floating layer the size of the box that repeats the box's padding and alignment.

  • A Fit() box takes its size from its first child. Put the largest layer first, or give the box an explicit size.
  • Layers pass pointer input through, so a Button under a later layer still receives hover and click input.
  • A child with its own .floating configuration keeps its attach point, offset, and zIndex. See Floating.
  • .layout.childGap has no effect because the children do not flow.

Element: the generic form

Element(direction, "id", ...) underlies Column and Row. Use it to choose the layout direction at runtime:

Element(orientation == VERTICAL ? CC_TOP_TO_BOTTOM : CC_LEFT_TO_RIGHT,
        "Toolbar",
        .layout = { .childGap = 8 }) {
    /* children */
}

Available declaration fields

Every field on Clay_ElementDeclaration is available via designated initializers. See Layout, Styling, Scroll and clip, and Floating for the details of each sub-struct.

  • .layout: sizing, padding, gap, alignment, direction (direction is overwritten by Column/Row/Box to guarantee the shape)
  • .backgroundColor, .overlayColor, .cornerRadius
  • .image (use the Image leaf macro instead of writing .image on a container), .custom
  • .floating, .clip, .aspectRatio, .border
  • .userData: arbitrary void* forwarded to every render command generated by this element

Nesting and shadowing

Container macros use __COUNTER__ to generate a unique scope variable per call site, so nested blocks don't trigger -Wshadow at arbitrary depth:

Column("A") {
    Row("B") {
        Box("C") {
            Column("", .layout = { .childGap = 4 }) {
                Text("deep",
                     .textColor = Color(255,255,255,255),
                     .fontSize  = 12);
            }
        }
    }
}

Control-flow caveats

The container macros expand to a for loop with a single iteration:

for (CC_Scope s = CC_OpenElement(...); s.active; CC_CloseScope(&s)) { /* body */ }

Follow these control-flow rules:

1. break leaves a dangling open element

A plain break exits the loop without running the close step, which leaves an unbalanced open element in Clay's internal stack. Clay will report CLAY_ERROR_TYPE_UNBALANCED_OPEN_CLOSE at end-of-layout.

To leave the block early:

Use continue to run the loop's close step. break, return, and goto out of the block skip that step. In a helper function, let the container block finish before returning.

// Bad: leaves Column open
Column("X") {
    if (fail) break;   // ← Clay error
    Text("ok", ...);
}

// Good: continue skips remaining children but closes cleanly
Column("X") {
    if (fail) continue;
    Text("ok", ...);
}

2. Don't put a container as the unbraced body of an if / while / else

The macro expands to a statement that wants its own scope. Wrap in { ... } if you need that shape:

// Bad:
if (show_sidebar) Column("Sidebar") { Text("Menu", ...); }

// Good:
if (show_sidebar) {
    Column("Sidebar") { Text("Menu", ...); }
}

ID guidance

Use stable, non-empty ids for elements that need:

  • hover / click detection via CC_Hovered / CC_Clicked / Clay_PointerOver / Clay_OnHover
  • scroll-offset persistence across frames
  • floating .parentId anchoring
  • sticky layout identity across frames (so layout transitions animate correctly, if you do that)

Use "" for containers that need only layout. Clay generates an internal id and produces the same visual result.

For runtime ids, format a string with snprintf and pass it to the macro:

for (int i = 0; i < count; ++i) {
    char id[24];
    snprintf(id, sizeof id, "row-%d", i);
    Column(id, .layout = { ... }) { /* ... */ }
}

To hash a (label, index) pair, use CC_IDI("row", i) with raw CLAY({ ... }) blocks.

For gaps and separators, use the anonymous leaf macros in Spacer and divider. Reusing a named id such as "Spacer" across components causes a duplicate-id error.

Clone this wiki locally