Skip to content

Transitions

rizuki edited this page Nov 30, 2026 · 5 revisions

Transitions

DefineTransition generates enter and exit callbacks and a configuration function from presets or Clay properties. Declare it at file scope, then use the generated function in .transition.

Quick start

// 1. Declare at file scope
DefineTransition(sidebar_anim, 0.4f,
    .enter = { .slideX = -240, .fade = true },
    .exit  = { .slideX = -240, .fade = true },
);

// 2. Use in any container (note the parentheses)
if (showSidebar) {
    Column("Sidebar",
        .transition = sidebar_anim(),
        .layout = { .sizing = { Fixed(240), Grow() } },
        .backgroundColor = Color(20, 20, 20, 255)) {
        Text("Menu", .fontSize = 14, .textColor = Color(200,200,200,255));
    }
}

When showSidebar flips to true, the sidebar slides in from the left and fades in over 0.4 seconds. When it flips to false, it slides back out and fades away.

DefineTransition

DefineTransition(name, duration,
    [.handler    = easing_fn,]
    [.properties = CC_TRANSITION_PROPERTY_*,]
    [.interactionHandling = ...,]
    .enter = { ... },
    .exit  = { ... },
);
Parameter Type Default Description
name identifier (required) Name of the generated config function
duration float (required) Animation duration in seconds
.handler bool (*)(CC_TransitionArgs) CC_EaseOut Easing function
.properties CC_TransitionProperty auto-inferred Bitmask of properties to animate
.interactionHandling CC_TransitionInteraction disable during position How clicks behave mid-transition
.enter CC_TransitionEffect {} Effect applied when element appears
.exit CC_TransitionEffect {} Effect applied when element is removed

The macro must be called at file scope (not inside a function). It generates:

  • name__enter__ / name__exit__: static callback functions
  • name(): a static inline function returning CC_TransitionElementConfig

Reference it as .transition = name() in any container macro.

CC_TransitionEffect

Set .enter and .exit with a CC_TransitionEffect:

High-level presets

Field Type Description
.slideX float Pixel offset on X axis. Negative = left, positive = right.
.slideY float Pixel offset on Y axis. Negative = up, positive = down.
.fade bool true = animate background alpha from/to 0.

Raw Clay properties

For fine-grained control, set these directly. A zero-valued color is treated as "not set" and won't affect the animation.

Field Type Description
.backgroundColor CC_Color Animate from/to this background color
.overlayColor CC_Color Animate from/to this overlay color
.borderColor CC_Color Animate from/to this border color
.borderWidth CC_BorderWidth Animate from/to these border widths

Trigger and ordering

Field Type Default Side
.enterTrigger CC_TransitionEnterTrigger CC_TRANSITION_ENTER_SKIP_ON_FIRST_PARENT_FRAME enter
.exitTrigger CC_TransitionExitTrigger CC_TRANSITION_EXIT_SKIP_WHEN_PARENT_EXITS exit
.siblingOrdering CC_ExitTransitionOrdering CC_EXIT_TRANSITION_ORDERING_UNDERNEATH_SIBLINGS exit

Property bitmask

If you don't set .properties, DefineTransition auto-infers which properties to animate from the effect fields you set:

Effect field Inferred property flag
.slideX CC_TRANSITION_PROPERTY_X
.slideY CC_TRANSITION_PROPERTY_Y
.fade CC_TRANSITION_PROPERTY_BACKGROUND_COLOR
.backgroundColor CC_TRANSITION_PROPERTY_BACKGROUND_COLOR
.overlayColor CC_TRANSITION_PROPERTY_OVERLAY_COLOR
.borderColor CC_TRANSITION_PROPERTY_BORDER_COLOR
.borderWidth CC_TRANSITION_PROPERTY_BORDER_WIDTH

To override, set .properties explicitly:

DefineTransition(expand_anim, 0.25f,
    .properties = CC_TRANSITION_PROPERTY_DIMENSIONS,
);

Available flags: CC_TRANSITION_PROPERTY_NONE, _X, _Y, _POSITION, _WIDTH, _HEIGHT, _DIMENSIONS, _BOUNDING_BOX, _BACKGROUND_COLOR, _OVERLAY_COLOR, _CORNER_RADIUS, _BORDER_COLOR, _BORDER_WIDTH, _BORDER.

Combined flag Includes
_POSITION _X and _Y
_DIMENSIONS _WIDTH and _HEIGHT
_BOUNDING_BOX _POSITION and _DIMENSIONS
_BORDER _BORDER_COLOR and _BORDER_WIDTH

Presets infer individual flags. For example, .slideX sets _X. Set .properties explicitly to animate a combined group.

Easing functions

Choose an easing function with signature bool (*)(CC_TransitionArgs):

Function Curve Description
CC_EaseOut cubic ease-out Fast start, slow finish. Default.
CC_Linear linear Constant speed.
CC_EaseIn cubic ease-in Slow start, fast finish.
CC_EaseInOut cubic ease-in-out Slow start, fast middle, slow finish.

Override with .handler:

DefineTransition(bounce_in, 0.6f,
    .handler = CC_EaseIn,
    .enter = { .slideY = -60, .fade = true },
);

Write a custom easing by implementing the same signature:

bool MyBounce(CC_TransitionArgs args) {
    float ratio = args.duration > 0
        ? fminf(args.elapsedTime / args.duration, 1.0f) : 1.0f;
    // ... compute curve, lerp args.current from args.initial to args.target ...
    return ratio >= 1.0f;
}

DefineTransition(custom_anim, 0.5f,
    .handler = MyBounce,
    .enter = { .slideX = -100 },
);

Examples

Toast notification

DefineTransition(toast_anim, 0.3f,
    .enter = { .slideY = -40, .fade = true },
    .exit  = {
        .slideY = -40,
        .fade = true,
        .siblingOrdering = CC_EXIT_TRANSITION_ORDERING_ABOVE_SIBLINGS,
    },
);

if (showToast) {
    Box("Toast",
        .transition = toast_anim(),
        .layout = { .sizing = { Fixed(300), Fixed(60) },
                    .padding = PadAll(12),
                    .childAlignment = { .y = AlignYCenter() } },
        .backgroundColor = Color(50, 180, 80, 255),
        .cornerRadius = RadiusAll(8)) {
        Text("Saved!", .fontSize = 14, .textColor = Color(255,255,255,255));
    }
}

Smooth expand/collapse

Clay interpolates between old and new sizes automatically when you select the dimension properties:

DefineTransition(expand_anim, 0.25f,
    .properties = CC_TRANSITION_PROPERTY_DIMENSIONS,
);

Column("Details",
    .transition = expand_anim(),
    .layout = { .sizing = { Grow(), Fixed(expanded ? 300 : 0) } },
    .backgroundColor = Color(30, 30, 30, 255)) {
    Text("Details...", .fontSize = 12, .textColor = Color(180,180,180,255));
}

Color animation

DefineTransition(highlight, 0.5f,
    .properties = CC_TRANSITION_PROPERTY_BACKGROUND_COLOR
                | CC_TRANSITION_PROPERTY_BORDER,
    .enter = {
        .backgroundColor = Color(255, 0, 0, 255),
        .borderColor     = Color(255, 0, 0, 255),
        .borderWidth     = { 2, 2, 2, 2 },
    },
);

Mixed presets and raw

DefineTransition(fancy_panel, 0.4f,
    .enter = {
        .slideX = -200,
        .fade   = true,
        .backgroundColor = Color(100, 100, 255, 0),
    },
    .exit = { .slideX = -200, .fade = true },
);

Performance notes

  • Clay skips its transition pass when no elements are transitioning.
  • Transition state uses Clay's pre-allocated arena, with 200 slots by default.
  • setInitialState runs when an element appears, and setFinalState runs when it starts exiting. The easing handler runs each frame during an active transition.
  • Cost scales with active transitions. Keep simultaneous animations under roughly 50 for smooth frame times.

Transition states

The CC_TransitionState enum (alias for Clay_TransitionState) describes where an element is in its transition lifecycle. Read it from a CC_TransitionData callback argument or via Clay's introspection APIs when you need to branch behavior on transition phase.

Constant Meaning
CC_TRANSITION_STATE_IDLE Element is at rest. No transition is running.
CC_TRANSITION_STATE_ENTERING Element just appeared. Enter callback runs the lerp.
CC_TRANSITION_STATE_TRANSITIONING Element is mid-transition (e.g. animating between two positions on subsequent frames).
CC_TRANSITION_STATE_EXITING Element was removed. Exit callback runs the lerp until it returns true.

Read args.transitionState in the DefineTransition handler to select behavior for each phase.

CC_* type aliases

Use the CC_* aliases or their equivalent Clay types:

CC_ alias Clay original
CC_TransitionElementConfig Clay_TransitionElementConfig
CC_TransitionData Clay_TransitionData
CC_TransitionArgs Clay_TransitionCallbackArguments
CC_TransitionProperty Clay_TransitionProperty
CC_TransitionState Clay_TransitionState
CC_TransitionEnterTrigger Clay_TransitionEnterTriggerType
CC_TransitionExitTrigger Clay_TransitionExitTriggerType
CC_TransitionInteraction Clay_TransitionInteractionHandlingType
CC_ExitTransitionOrdering Clay_ExitTransitionSiblingOrdering

Clone this wiki locally