Skip to content

Containers

rizuki edited this page Nov 30, 2026 · 8 revisions

Containers

Containers are the scoped-block building primitive of ccompose. This page covers Column, Row, Box, and the generic Element — all four expand to a single-iteration for loop that opens a Clay element, runs your block as its children, and closes the element on exit.

The shape

Column("id", .field = value, ...) { children }
Row(   "id", .field = value, ...) { children }
Box(   "id", .field = value, ...) { children }
Element(direction, "id", .field = value, ...) { children }
  • First argument: an ID — any const char * (string literal, stack buffer, snprintf'd string). The pointer is interned via CC_StrIntern and hashed into a Clay element id. Pass "" for anonymous elements — Clay still generates an internal ID for layout bookkeeping, but you can't target "" from Clay_OnHover, Clay_PointerOver, scroll-offset persistence, or floating .parentId anchoring.
  • Remaining arguments: designated initializers on Clay's own Clay_ElementDeclaration struct. Every Clay field is available — ccompose doesn't wrap or rename anything.

Ids hash by content, not by pointer identity — two source sites passing the same const char * contents address the same element. Inside loops, format unique ids per iteration with snprintf (or fall back to CC_IDI("row", i) + raw CLAY({ ... }) if you want index-offset hashing for free).

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 dedicated Image leaf macro instead of stuffing .image into a Box.

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 let the pointer through. A Button under a later layer still gets its hover and click.
  • A child that sets its own .floating is left alone. Use that for a custom attach point, offset or zIndex. See Floating.
  • .layout.childGap has no effect, since the children do not flow.

Element — the generic form

Element(direction, "id", ...) is what Column and Row are built on. Reach for it when you need to pick 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 & 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 */ }

This has two consequences you need to be aware of:

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.

If you need early exit:

  • Use continue — it runs the loop's step clause, closing the element cleanly.
  • Wrap the block in a helper function and return from there — the scope variable is destroyed when the function returns, which closes the element via the for-loop step.
// 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 any of:

  • 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 throwaway containers where none of those apply. Clay still hashes an internal ID for layout bookkeeping — the visual result is identical either way.

For runtime-generated IDs (e.g. list rows with per-item indices), format the id with snprintf and pass it like any other string — the ccompose macros accept any const char *:

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

If you'd rather hash by (label, index) without formatting, fall back to CC_IDI("row", i) + raw CLAY({ ... }) blocks.

For pure layout filler (gaps, separators) that doesn't need hover, click, or scroll identity, prefer the anonymous leaf macros — see Spacer & Divider. They avoid the duplicate-ID error you'll hit if you reuse a literal like "Spacer" across components.

Clone this wiki locally