Skip to content
rizuki edited this page Nov 30, 2026 · 7 revisions

Scroll and clip

Set .clip with a Clay_ClipElementConfig to hide overflow. Set .childOffset in the same struct to scroll the contents.

CC_Scroll updates the offset from raylib wheel and drag input, clamping it to measured content bounds. You can also set .childOffset directly.

Basic clip (overflow hidden)

Set .horizontal / .vertical to clip children that overflow the bounding box:

Column("Card",
       .layout = { .sizing = { Fixed(240), Fixed(120) } },
       .clip   = { .vertical = true }) {
    // children taller than 120px get clipped
}

This is the "CSS overflow: hidden" equivalent. Without .clip, Clay lets children paint outside the parent's bounds.

Clip shorthands

For the three common shapes there are macros that build the .clip struct for you:

Helper Returns Equivalent
ClipX(offset) CC_ClipElementConfig { .horizontal = true, .childOffset = offset }
ClipY(offset) CC_ClipElementConfig { .vertical = true, .childOffset = offset }
ClipXY(offset) CC_ClipElementConfig { .horizontal = true, .vertical = true, .childOffset = offset }

offset is a CC_Vector2, usually the .offset field from a CC_Scroll state.

Column("List", .clip = ClipY(s_list.offset), ...) { ... }
Row("Strip",   .clip = ClipX(s_strip.offset), ...) { ... }
Box("Canvas",  .clip = ClipXY(s_canvas.offset), ...) { ... }

For overflow-hidden with no scrolling, pass a zero offset or use the literal form: .clip = { .vertical = true }.

Scrollable list: CC_Scroll (recommended)

Added in v0.0.1-alpha01

Hold scroll state in a caller-owned CC_Scroll and call CC_ScrollUpdate once per frame before the layout block. ccompose handles wheel + click-drag input and clamps the offset against Clay's measured content size from the previous frame.

static CC_Scroll s_list;   // persists across frames

void DrawList(void) {
    CC_ScrollUpdate(&s_list, "List",
                    .vertical = true, .wheel = true, .drag = true);

    Column("List",
           .layout = { .sizing = { Grow(), Fixed(300) }, .childGap = 4 },
           .clip   = ClipY(s_list.offset)) {
        for (int i = 0; i < 500; ++i) {
            char buf[32];
            snprintf(buf, sizeof buf, "Item %d", i);
            Text(buf, .textColor = Color(230,230,230,255), .fontSize = 14);
        }
    }
}

Use the scroll container's id in CC_ScrollUpdate so ccompose can look up its dimensions for hover detection and clamping.

CC_Scroll

Field Type Meaning
.offset CC_Vector2 Pass this into .clip.childOffset. Negative values scroll toward the end (Clay subtracts the offset from each child's position).
.dragging bool Internal: tracks an active press-drag. Don't touch.

CC_ScrollConfig (designated-init into CC_ScrollUpdate)

Field Default Meaning
.horizontal false Enable horizontal axis.
.vertical false Enable vertical axis.
.drag false Enable click-and-drag scrolling (left mouse button).
.wheel false Enable mouse-wheel scrolling. When only horizontal is enabled, the vertical wheel also scrolls X.
.wheelSpeed 40.0f (when 0) Pixels per wheel unit.
.noClamp false Skip the auto-clamp. Useful for overscroll bounce, animated content where the previous-frame bounds are wrong, or when you want to clamp yourself.

Clay_GetScrollContainerData provides the previous frame's dimensions for clamping. After content resizes, the clamp catches up one frame later.

Horizontal carousel

Enable the horizontal axis in both CC_ScrollUpdate and .clip. Give children an intrinsic width with Fixed or Fit. A Grow() child compresses to the viewport, leaving no overflow to scroll.

static CC_Scroll s_strip;

CC_ScrollUpdate(&s_strip, "Carousel",
                .horizontal = true, .wheel = true, .drag = true);

Row("Carousel",
    .layout = { .sizing = { Grow(), Fit() }, .childGap = 16 },
    .clip   = { .horizontal  = true,
                .childOffset = s_strip.offset }) {
    for (int i = 0; i < count; ++i) {
        Column("Card",
               .layout = { .sizing = { Fixed(180), Fit() },   // NOT Grow()
                           .padding = PadAll(16) },
               .backgroundColor = COLOR_SURFACE_ALT,
               .cornerRadius = RadiusAll(12)) {
            // card contents
        }
    }
}

When only horizontal scrolling is enabled, the vertical wheel also updates offset.x, so users can scroll without holding Shift.

Two-axis scroll

Enable both axes to get a 2D scroll region (think map, image canvas, code editor):

static CC_Scroll s_canvas;

CC_ScrollUpdate(&s_canvas, "Canvas",
                .horizontal = true, .vertical = true,
                .wheel = true, .drag = true);

Box("Canvas",
    .layout = { .sizing = { Grow(), Grow() } },
    .clip   = { .horizontal  = true,
                .vertical    = true,
                .childOffset = s_canvas.offset }) {
    // huge content
}

Manual offset (advanced)

Set .childOffset directly for keyboard, touch, gamepad, inertia, snapping, or animated programmatic scrolling:

typedef struct { float scroll_y; } AppState;
static AppState state = {0};

static void update_scroll(AppState *s, float content_h, float view_h) {
    float wheel = GetMouseWheelMove() * 30.0f;
    s->scroll_y -= wheel;
    float max = content_h - view_h;
    if (max < 0)            max = 0;
    if (s->scroll_y < 0)    s->scroll_y = 0;
    if (s->scroll_y > max)  s->scroll_y = max;
}

Column("List",
       .layout = { .sizing = { Grow(), Fixed(300) } },
       .clip   = { .vertical    = true,
                   .childOffset = { 0, -state.scroll_y } }) {
    // children...
}

Read Clay_GetScrollContainerData(CLAY_ID("List")) to obtain content and viewport dimensions for your own clamping, as CC_ScrollUpdate does internally.

Interaction with Clay_PointerOver

Clay_PointerOver, CC_Hovered, and CC_Clicked return false for elements clipped out of view by a parent's .clip. Scrolled-away buttons stop responding to hover.

CC_ScrollUpdate uses the same hit-test internally, so drag only starts when the press lands on the visible scroll viewport.

Headless mode

In headless builds, CC_ScrollUpdate is a no-op because no raylib input source exists. Set CC_Scroll.offset directly in tests, or use Clay_SetPointerState and handle scrolling yourself.

Gotchas

  • Use Fixed or Fit on the scroll axis so children can overflow. Grow() compresses them to the viewport.
  • Declare offsets as CC_Vector2, an alias for Clay_Vector2. Raylib's Vector2 has the same fields but is a distinct C type.
  • Clamping uses the previous frame's content size. Set .noClamp and provide your own clamp if a resize must take effect immediately.
  • To show a scrollbar, create a floating box with a thumb sized from view_size / content_size.
  • For inertia, store a velocity and reduce it after release. For snapping, animate the offset toward the target each frame.
  • Clip regions nest. A clipped child inside a scroll container, such as a cell with .clip = {.horizontal = true}, is clipped to its own bounds and to the container's. Ending the child restores the container's clip, and Draw callbacks are clipped the same way. Before v0.1.8 the raylib renderer turned clipping off when an inner region ended, so scrolled content could draw over elements above the container.

Clone this wiki locally