Skip to content

Interaction

rizuki edited this page Nov 30, 2026 · 6 revisions

Interaction: Button, CC_Hovered, CC_Clicked

CC_Begin() forwards raylib mouse state to Clay each frame. Query an element by id with CC_Hovered or CC_Clicked, or use Button for background feedback:

Thing What it is
CC_Hovered("id") Macro: true while the mouse is over the element.
CC_Clicked("id") Macro: true on the frame the left mouse button was pressed while hovering the element.
Button("id", ...) Scoped-block macro, same shape as Row/Column/Box. Opens a CC_LEFT_TO_RIGHT element and lowers its background alpha while hovered or pressed. Pairs with CC_Hovered / CC_Clicked.

Declare the UI each frame and query its state by id, as in Dear ImGui. Clay tests the pointer against the previous frame's layout, so a newly visible element can first register hover on the next frame.

Typical pattern

if (CC_Clicked("Save")) save_file();

Button("Save",
       .layout = { .padding        = PadAll(12),
                   .childAlignment = { .x = AlignXCenter(),
                                       .y = AlignYCenter() } },
       .backgroundColor = Color( 40, 100, 200, 255),
       .cornerRadius = RadiusAll(8)) {
    Text("Save",
         .textColor = Color(255, 255, 255, 255),
         .fontSize  = 16);
}

CC_Clicked can appear before or after the Button block because it queries the previous frame's layout. Put the query near the element it targets.

Query and callback handling

Use CC_Clicked in normal C control flow to update app state. The element declaration is Clay's struct, so it has no ccompose .onClick field. Queries let handlers access app state directly.

For callback handling, use Clay_OnHover(). It accepts a function pointer and void *userData, and passes Clay_PointerData to distinguish hover from press:

static void on_save(Clay_ElementId id, Clay_PointerData data, void *ud) {
    if (data.state == CLAY_POINTER_DATA_PRESSED_THIS_FRAME) save_file();
}

Button("Save", .backgroundColor = ...) {
    Clay_OnHover(on_save, NULL);
    Text("Save", ...);
}

CC_Hovered

#define CC_Hovered(id) CC_PointerOver(CC_SID(CC__Str(id)))

CC_Hovered wraps Clay_PointerOver. Pass any const char *, including a literal, stack buffer, or pointer into another string. CC__Str interns it with CC_StrIntern, then CC_SID hashes it into a Clay_ElementId.

Box("Ping",
    .layout          = { .sizing = { Fixed(40), Fixed(40) } },
    .backgroundColor = CC_Hovered("Ping")
                           ? Color(80, 200, 80, 255)
                           : Color(40, 120, 40, 255),
    .cornerRadius    = RadiusAll(6)) { }

Use the same string content for the element and its hover or click query. For loop-generated ids, format a buffer with snprintf. To use indexed hashing directly, combine CC_IDI("row", i) with Clay_PointerOver.

CC_Clicked

#define CC_Clicked(id_literal) \
    (CC__MousePressedThisFrame() && CC_Hovered(id_literal))

Returns true on the frame the left mouse button changes from up to down while the pointer is over the element. It checks the mouse state first and computes the Clay hash only on a press frame.

if (CC_Clicked("Delete")) confirm_delete();

CC__MousePressedThisFrame() calls IsMouseButtonPressed(MOUSE_BUTTON_LEFT) with raylib and returns false in headless mode. For simulated input, see Pointer input in tests.

Button

#define Button(id_literal, ...) \
    CC_BUTTON_IMPL_(CC_SCOPE_NAME_(__COUNTER__), CC__Str(id_literal), __VA_ARGS__)

Button opens a CC_LEFT_TO_RIGHT element through CC_OpenButton. It accepts the same Clay_ElementDeclaration fields as Row and lowers background alpha in response to pointer input.

Pointer state Background alpha
Not over the button As declared
Over the button Declared alpha x CC_BUTTON_HOVER_ALPHA (default 0.85f)
Over the button, left button held Declared alpha x CC_BUTTON_PRESS_ALPHA (default 0.70f)

Only the alpha channel changes, so the button blends toward whatever is drawn behind it. Text, border and layout are untouched.

To change the alpha factors for all buttons, define CC_BUTTON_HOVER_ALPHA and CC_BUTTON_PRESS_ALPHA before including ccompose.h, or pass a compiler definition such as -DCC_BUTTON_HOVER_ALPHA=0.9f.

To handle colors yourself, use Row("id", ...). CC_Hovered and CC_Clicked work with any element id.

An alpha-zero background stays transparent when multiplied. For hover feedback, choose a color explicitly: .backgroundColor = CC_Hovered("Ghost") ? COLOR_HOVER : COLOR_TRANSPARENT.

Give buttons a non-empty id to query them. Button("", ...) still shows background feedback, but CC_Hovered("") and CC_Clicked("") cannot target it.

Anatomy

Button("SaveBtn",
       //            ─── Clay_ElementDeclaration fields ───
       .layout          = { .padding        = PadAll(12),
                            .childAlignment = { .x = AlignXCenter(),
                                                .y = AlignYCenter() } },
       .backgroundColor = Color( 40, 100, 200, 255),  // alpha drops on hover and press
       .cornerRadius    = RadiusAll(8),
       .border          = { .color = Color(20, 60, 140, 255),
                            .width = { .left = 1, .right = 1,
                                       .top  = 1, .bottom = 1 } }) {
    //  ─── children (just like a Row) ───
    Text("Save",
         .textColor = Color(255, 255, 255, 255),
         .fontSize  = 16);
}

Recipes

Sidebar navigation

Each entry uses a transparent Button that gains a darker background on hover. A click selects the current page.

static const char *current_page = "Home";

static const CC_Color COLOR_TEXT        = {236, 236, 236, 255};
static const CC_Color COLOR_HOVER       = { 48,  48,  58, 255};
static const CC_Color COLOR_TRANSPARENT = {  0,   0,   0,   0};

Column("Sidebar",
       .layout = { .sizing = { Fixed(220), Grow() },
                   .padding = PadAll(16),
                   .childGap = 8 }) {

    if (CC_Clicked("NavHome"))    current_page = "Home";
    Button("NavHome",
           .layout = { .sizing = { Grow(), Fit() },
                       .padding = PadSymmetric(12, 8),
                       .childAlignment = { .y = AlignYCenter() } },
           .backgroundColor = CC_Hovered("NavHome")
                                  ? COLOR_HOVER : COLOR_TRANSPARENT,
           .cornerRadius = RadiusAll(6)) {
        Text("Home", .textColor = COLOR_TEXT, .fontSize = 16);
    }

    if (CC_Clicked("NavSettings")) current_page = "Settings";
    Button("NavSettings",
           .layout = { .sizing = { Grow(), Fit() },
                       .padding = PadSymmetric(12, 8),
                       .childAlignment = { .y = AlignYCenter() } },
           .backgroundColor = CC_Hovered("NavSettings")
                                  ? COLOR_HOVER : COLOR_TRANSPARENT,
           .cornerRadius = RadiusAll(6)) {
        Text("Settings", .textColor = COLOR_TEXT, .fontSize = 16);
    }
}

Custom hover and pressed colors

The default feedback covers the common case. For distinct colors per state, compute the color yourself. The default alpha change still applies on top, so use Row if you want only your own colors:

bool held = CC_Hovered("Save") && IsMouseButtonDown(MOUSE_BUTTON_LEFT);

CC_Color bg;
if      (held)               bg = Color(20,  60, 140, 255);
else if (CC_Hovered("Save")) bg = Color(80, 140, 220, 255);
else                         bg = Color(40, 100, 200, 255);

Row("Save", .backgroundColor = bg, ...) { Text("Save", ...); }

CC_Clicked is true for a single frame (the press transition), so it suits actions and not a held-down look.

Toggle / checkbox

static bool enabled = false;

if (CC_Clicked("Toggle")) enabled = !enabled;

Button("Toggle",
       .layout = { .padding = PadAll(10), .childGap = 8,
                   .childAlignment = { .y = AlignYCenter() } },
       .backgroundColor = enabled
                              ? Color(40, 140, 80, 255)
                              : Color(60, 60, 70, 255),
       .cornerRadius = RadiusAll(6)) {
    Text(enabled ? "ON" : "OFF",
         .textColor = Color(255,255,255,255), .fontSize = 14);
}

Gotchas

  • Clay queries the previous frame's layout. On a new element's first frame, hover and click queries return false.

  • Ids hash by content. Calls in different source locations with the same id string target the same element. Give each element a unique id.

  • CC_Clicked returns true for one frame per press. Store the result to use it in several places:

    bool save_clicked = CC_Clicked("Save");
    if (save_clicked) save_file();
    if (save_clicked) show_toast();
  • Holding the mouse button outside an element and dragging over it shows pressed alpha. Clay does not record where the press began.

  • For double-click or right-click handling, use raylib input functions and Clay pointer queries directly.

Clone this wiki locally