Skip to content

Floating

rizuki edited this page Nov 30, 2026 · 5 revisions

Floating

Use .floating to position an element relative to its parent, another element, or the window root. Floating elements sit outside normal flow, which makes them useful for tooltips, modals, menus, and overlays.

Set .floating = { ... } with fields from CC_FloatingElementConfig, an alias for Clay_FloatingElementConfig.

Basic tooltip

Box("Anchor",
    .layout = { .sizing = { Fixed(120), Fixed(40) } }) {

    Text("Hover me", .textColor = Color(236,236,236,255), .fontSize = 14);

    // Tooltip overlay anchored to this Box
    if (CC_Hovered("Anchor")) {
        Box("Tooltip",
            .layout          = { .padding = PadAll(6) },
            .backgroundColor = Color(40, 40, 40, 240),
            .cornerRadius    = RadiusAll(4),
            .floating        = { .attachTo = CC_ATTACH_TO_PARENT,
                                 .offset   = { 0, 24 },
                                 .zIndex   = 100 }) {
            Text("Tooltip text",
                 .textColor = Color(255,255,255,255),
                 .fontSize  = 12);
        }
    }
}

The tooltip participates in layout only while CC_Hovered("Anchor") is true. You can conditionally declare it each frame.

Shorthands

Use these macros to attach to the parent or root:

Macro Equivalent
FloatOnParent(z) { .attachTo = CC_ATTACH_TO_PARENT, .zIndex = (z) }
FloatOnRoot(z) { .attachTo = CC_ATTACH_TO_ROOT, .zIndex = (z) }
Box("Badge",
    .floating = FloatOnParent(10),
    .backgroundColor = Color(255,0,0,255),
    .layout = { .sizing = { Fixed(16), Fixed(16) } }) {}

Use the literal CC_FloatingElementConfig whenever you need attach points, offsets, or attach-to-by-id.

Fields

Field Meaning
.attachTo How the element's position is anchored. See below.
.parentId Used with CC_ATTACH_TO_ELEMENT_WITH_ID to name a specific anchor.
.attachPoints Two anchor points: .element (which corner of the floating element) and .parent (which corner of the anchor).
.offset CC_Vector2 pixel offset applied after anchoring.
.expand CC_Dimensions that grows the element's bounding box outward from its anchored position.
.zIndex Integer draw order. Higher = on top.
.pointerCaptureMode Whether this element captures pointer events or passes them through.
.clipTo Whether the floating element is clipped by its parent's clip region.

Attach modes

The attach modes have CC_* aliases:

CC_* mode (alias for) Effect
CC_ATTACH_TO_NONE CLAY_ATTACH_TO_NONE Not floating: treated as a normal child. (Default.)
CC_ATTACH_TO_PARENT CLAY_ATTACH_TO_PARENT Anchor to the element this block is nested inside.
CC_ATTACH_TO_ELEMENT_WITH_ID CLAY_ATTACH_TO_ELEMENT_WITH_ID Anchor to the element whose id matches .parentId. Use CLAY_ID("name").id to produce the id.
CC_ATTACH_TO_ROOT CLAY_ATTACH_TO_ROOT Anchor to the root element (the whole window).

Floating is only active when .attachTo != CC_ATTACH_TO_NONE. Omitting .floating entirely or leaving it zero-initialized keeps the element in normal flow.

Attach points

.attachPoints.element is glued to .attachPoints.parent. Both fields take one of nine values:

Left Center Right
Top CC_ATTACH_POINT_LEFT_TOP CC_ATTACH_POINT_CENTER_TOP CC_ATTACH_POINT_RIGHT_TOP
Center CC_ATTACH_POINT_LEFT_CENTER CC_ATTACH_POINT_CENTER_CENTER CC_ATTACH_POINT_RIGHT_CENTER
Bottom CC_ATTACH_POINT_LEFT_BOTTOM CC_ATTACH_POINT_CENTER_BOTTOM CC_ATTACH_POINT_RIGHT_BOTTOM

Both attach points default to LEFT_TOP. The floating element starts at the parent's top-left corner, then .offset shifts it.

Custom positioning inside a Column / Row

Use .floating to place a badge, label, or other child at a specific position inside a Column or Row. Siblings ignore the floating child when calculating layout.

Column("Card",
       .layout          = { .sizing  = { Fixed(300), Fixed(200) },
                            .padding = PadAll(16),
                            .childGap = 8 },
       .backgroundColor = Color(28,28,34,255),
       .cornerRadius    = RadiusAll(8)) {

    Text("Title",  .fontSize = 18, .textColor = Color(236,236,236,255));
    Text("Body...", .fontSize = 14, .textColor = Color(180,180,190,255));

    // Pinned to the top-right corner of "Card", inset by 8px
    Box("Badge",
        .floating = {
            .attachTo     = CC_ATTACH_TO_PARENT,
            .attachPoints = { .parent  = CC_ATTACH_POINT_RIGHT_TOP,
                              .element = CC_ATTACH_POINT_RIGHT_TOP },
            .offset       = { -8, 8 },
            .zIndex       = 1,
        },
        .backgroundColor = Color(255,80,80,255),
        .cornerRadius    = RadiusAll(8),
        .layout = { .sizing = { Fixed(16), Fixed(16) } }) {}
}

Absolute coordinates in the window

For "raw" screen coordinates, attach to root with LEFT_TOP on both sides and put the absolute x,y into .offset:

Box("CursorHint",
    .floating = {
        .attachTo     = CC_ATTACH_TO_ROOT,
        .attachPoints = { .parent  = CC_ATTACH_POINT_LEFT_TOP,
                          .element = CC_ATTACH_POINT_LEFT_TOP },
        .offset       = { 120, 80 },
    },
    .backgroundColor = Color(0,0,0,200)) { ... }

Modal overlay

Attach a modal to the root with a full-size background and a z-index above the rest of the UI:

if (modal_open) {
    // Background scrim
    Box("ModalScrim",
        .layout          = { .sizing = { Grow(), Grow() } },
        .backgroundColor = Color(0, 0, 0, 160),
        .floating        = { .attachTo = CC_ATTACH_TO_ROOT,
                             .zIndex   = 1000 }) { }

    // Centered modal panel
    Box("ModalPanel",
        .layout          = { .sizing = { Fixed(420), Fit() },
                             .padding = PadAll(24),
                             .childGap = 12 },
        .backgroundColor = Color(28, 28, 34, 255),
        .cornerRadius    = RadiusAll(12),
        .floating        = { .attachTo     = CC_ATTACH_TO_ROOT,
                             .attachPoints = { .element = CC_ATTACH_POINT_CENTER_CENTER,
                                               .parent  = CC_ATTACH_POINT_CENTER_CENTER },
                             .zIndex       = 1001 }) {
        Text("Confirm", .textColor = Color(255,255,255,255), .fontSize = 20);
        Text("Are you sure?", .textColor = Color(200,200,200,255), .fontSize = 14);
        Row("ModalActions", .layout = { .childGap = 8 }) {
            if (CC_Clicked("ModalCancel")) modal_open = false;
            Button("ModalCancel", .layout = { .padding = PadAll(8) }) {
                Text("Cancel", .textColor = Color(255,255,255,255), .fontSize = 14);
            }
            if (CC_Clicked("ModalOK")) { confirm(); modal_open = false; }
            Button("ModalOK", .layout = { .padding = PadAll(8) }) {
                Text("OK", .textColor = Color(255,255,255,255), .fontSize = 14);
            }
        }
    }
}

Dropdown attached by id

If the floating element isn't a child of its anchor in the tree, use CC_ATTACH_TO_ELEMENT_WITH_ID:

// Anchor lives deep in the toolbar
Button("UserMenuBtn", ...) { Text("User ▾", ...); }

// Dropdown rendered at the top level but visually attached to the button
if (user_menu_open) {
    Column("UserMenu",
           .layout          = { .padding = PadAll(4), .childGap = 2 },
           .backgroundColor = Color(30, 30, 36, 255),
           .cornerRadius    = RadiusAll(6),
           .floating        = { .attachTo = CC_ATTACH_TO_ELEMENT_WITH_ID,
                                .parentId = CLAY_ID("UserMenuBtn").id,
                                .attachPoints = {
                                    .element = CC_ATTACH_POINT_LEFT_TOP,
                                    .parent  = CC_ATTACH_POINT_LEFT_BOTTOM,
                                },
                                .zIndex = 500 }) {
        if (CC_Clicked("MenuProfile"))  open_profile();
        if (CC_Clicked("MenuLogout"))   log_out();
        Button("MenuProfile", ...) { Text("Profile", ...); }
        Button("MenuLogout",  ...) { Text("Log out", ...); }
    }
}

Set .parentId to a hashed integer id from CLAY_ID("name").id or CC_IDI("name", i).id.

Z-index

Higher z-index values draw on top. Clay has no automatic stacking groups, so reserve ranges for your app:

Range Suggested use
0 to 99 Normal in-flow content
100 to 499 Tooltips, small popovers
500 to 999 Dropdowns, context menus
1000 to 1999 Modals and their scrim
9999 Debug overlays, dev HUD

Gotchas

  • Floating children do not affect parent sizing. A Fit() anchor will not grow to contain them.
  • .zIndex orders floating siblings. Tree position determines the order between a floating element and a non-floating element.
  • .parentId takes a hashed id from CLAY_ID("name").id.
  • Pointer input hits the topmost floating element by default. Set .pointerCaptureMode = CLAY_POINTER_CAPTURE_MODE_PASSTHROUGH for visual overlays that should let clicks through.

Clone this wiki locally