Skip to content

Spacer and Divider

rizuki edited this page Nov 30, 2026 · 6 revisions

Spacer and divider

Use spacers for flexible gaps and dividers for separator lines. These five leaf macros take no id or child block and are safe to call in loops.

HSpacer();                                    // grows horizontal
VSpacer();                                    // grows vertical
Spacer(.width = Grow(), .height = Fixed(12)); // explicit per-axis sizing
HDivider();                                   // 1px horizontal line, default grey
HDivider(.thickness = 2, .color = Color(...)); // styled horizontal line
VDivider();                                   // 1px vertical line
VDivider(.thickness = 2, .color = Color(...)); // styled vertical line

Anonymous element ids

Spacers and dividers use anonymous elements because they need only layout. Named container ids must be unique per frame. See Container id guidance.

If you reused the id "Spacer" across multiple components in the same frame, Clay would emit:

ccompose/clay error: An element with this ID was already previously declared during this layout.

Each call opens an anonymous element with Clay__OpenElement(), and Clay generates a unique internal id. You can repeat calls inside loops:

for (int i = 0; i < rows; i++) {
    Text(rows[i].label, ...);
    HDivider();
}

HSpacer / VSpacer

HSpacer() uses .sizing = { Grow(), Fit() }. VSpacer() uses .sizing = { Fit(), Grow() }. Put HSpacer in a Row or VSpacer in a Column to fill the remaining main-axis space.

Row("Toolbar",
    .layout = { .sizing = { Grow(), Fixed(40) },
                .childAlignment = { .y = AlignYCenter() } }) {
    Text("File", ...);
    Text("Edit", ...);
    HSpacer();                // pushes the next child to the far right
    Text("Settings", ...);
}

Column("Card",
       .layout = { .sizing = { Grow(), Fixed(400) },
                   .padding = PadAll(16) }) {
    Text("Title", ...);
    Text("Subtitle", ...);
    VSpacer();                // pushes Actions to the bottom
    Row("Actions") { Button(...) { ... } }
}

Growth applies on the parent's main axis, so HSpacer in a Column, or VSpacer in a Row, has no visible effect.

Spacer (per-axis sizing)

For a fixed gap, percentage gap, or growth on both axes, use Spacer(.width, .height) with Fit(), Grow(), Fixed(n), or Percent(p):

Spacer();                                       // fit / fit: zero-sized stub
Spacer(.width  = Grow());                       // like HSpacer
Spacer(.height = Grow());                       // like VSpacer
Spacer(.width  = Grow(), .height = Grow());     // grow on both axes (fills parent)
Spacer(.height = Fixed(12));                    // 12px vertical gap in a Column
Spacer(.width  = Percent(0.25f));               // 25% of parent's width

Both fields default to Fit() because Clay_SizingAxis zero-initializes to CLAY__SIZING_TYPE_FIT == 0. The options struct is:

typedef struct {
    Clay_SizingAxis width;   // default Fit()
    Clay_SizingAxis height;  // default Fit()
} CC_SpacerOpts;

Spacer is the same anonymous leaf as HSpacer / VSpacer, so it's loop-safe and has no ID, hover, or click hook.

HDivider / VDivider

Both take an optional designated-initializer CC_DividerOpts struct:

typedef struct {
    float    length;      // Size along the flow axis in px. <= 0 grows.
    float    thickness;   // Size across the flow axis. 0 uses 1.0 px.
    CC_Color color;       // alpha == 0 => built-in mid-grey default
} CC_DividerOpts;
  • HDivider runs along the X axis: .length controls width (defaults to Grow()), .thickness is the height.
  • VDivider runs along the Y axis: .length controls height (defaults to Grow()), .thickness is the width.
Column("Menu", .layout = { .childGap = 8 }) {
    Text("Item A", ...);
    HDivider();                                            // 1px, default grey
    Text("Item B", ...);
    HDivider(.thickness = 2,
             .color = Color(0x38, 0x3C, 0x44, 0xFF));      // thicker, custom
    Text("Item C", ...);
}

Row("Inline", .layout = { .childGap = 12 }) {
    Text("Home", ...);
    VDivider();
    Text("Docs", ...);
    VDivider();
    Text("Blog", ...);
}

Defaults

  • length: <= 0 falls back to Grow() on the divider's flow axis.
  • thickness: 0 falls back to 1.0 pixel.
  • color: {0} (alpha zero) falls back to the built-in mid-grey (#50545C). Works regardless of whether CC_Color is byte or float.

Fixed-length dividers

Set .length for a divider shorter than its parent:

Row("ButtonRow", .layout = { .childAlignment = { .y = AlignYCenter() } }) {
    Button("Save", ...)   { Text("Save",   ...); }
    VDivider(.length = 48, .color = Color(0x38,0x3C,0x44,0xFF));
    Button("Cancel", ...) { Text("Cancel", ...); }
}

Limitations

  • Pass .color explicitly for a themed divider. CC_DividerDefaultColor is fixed.

  • An anonymous divider cannot be targeted by CC_Hovered or CC_Clicked. Use a named Box or Button for an interactive separator.

See also

  • Containers: named, child-bearing elements.
  • Layout: .childGap, .childAlignment, and the full sizing model.
  • Styling: borders: .border.width.betweenChildren for Clay-native dividers drawn as part of the container, instead of as separate elements.
  • Troubleshooting: duplicate-ID errors and how leaf macros avoid them.

Clone this wiki locally