Skip to content

Containers

rizukirr edited this page Apr 9, 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 the same underlying 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: a string literal 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.

The string-literal constraint is enforced at compile time. Passing a const char *, NULL, or an integer gives you a compile error right at the call site.

Column

Column("id", ...) lays out children top-to-bottom (CLAY_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 = AlignCenter(),
                                       .y = AlignMiddle() } }) { ... }

// 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 (CLAY_LEFT_TO_RIGHT). Use .layout.childAlignment.y = AlignMiddle() to vertically center them within the row's height.

Row("Toolbar",
    .layout = { .sizing         = { Grow(), Fixed(40) },
                .padding        = PadAll(8),
                .childGap       = 12,
                .childAlignment = { .y = AlignMiddle() } }) {
    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 direction-neutral container (internally CLAY_TOP_TO_BOTTOM). The name signals "direction doesn't matter here" and is idiomatic for two patterns:

1. Single-child decoration / frame

The child fills the box and the box carries the styling:

Box("Avatar",
    .layout       = { .sizing = { Fixed(64), Fixed(64) } },
    .image        = { .imageData = my_texture },    // Texture2D*
    .cornerRadius = RadiusAll(999)) {
}

2. Stacked / overlay children

Each child opts into .floating so it's taken out of normal flow and layered on top:

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

    // background image layer
    Box("CardBg",
        .layout   = { .sizing = { Grow(), Grow() } },
        .image    = { .imageData = bg_texture },
        .floating = { .attachTo = CLAY_ATTACH_TO_PARENT, .zIndex = 0 }) { }

    // foreground text layer
    Column("CardText",
           .layout   = { .padding = PadAll(16) },
           .floating = { .attachTo = CLAY_ATTACH_TO_PARENT, .zIndex = 1 }) {
        Text("Title",
             .textColor = Color(255,255,255,255),
             .fontSize  = 24);
    }
}

See Floating for attach modes.

Element — the generic form

Element(direction, "id", ...) is what Column, Row, and Box are built on. Reach for it when you need to pick the layout direction at runtime:

Element(orientation == VERTICAL ? CLAY_TOP_TO_BOTTOM : CLAY_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, .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), use Clay's CLAY_IDI("row", i) directly and pair with raw CLAY()/Clay_PointerOver calls — the ccompose macros only accept string literals.

Clone this wiki locally