Skip to main content

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 = { ... },
);
ParameterTypeDefaultDescription
nameidentifier(required)Name of the generated config function
durationfloat(required)Animation duration in seconds
.handlerbool (*)(CC_TransitionArgs)CC_EaseOutEasing function
.propertiesCC_TransitionPropertyauto-inferredBitmask of properties to animate
.interactionHandlingCC_TransitionInteractionCC_TRANSITION_DISABLE_INTERACTIONS_WHILE_TRANSITIONING_POSITIONWhether the element takes pointer input while it animates. The default ignores hover and clicks while it enters, exits, or moves. CC_TRANSITION_ALLOW_INTERACTIONS_WHILE_TRANSITIONING_POSITION keeps it clickable while entering and moving, and only ignores input while it exits.
.enterCC_TransitionEffect{}Effect applied when element appears
.exitCC_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​

FieldTypeDescription
.slideXfloatPixel offset on X axis. Negative = left, positive = right.
.slideYfloatPixel offset on Y axis. Negative = up, positive = down.
.fadebooltrue = 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.

FieldTypeDescription
.backgroundColorCC_ColorAnimate from/to this background color
.overlayColorCC_ColorAnimate from/to this overlay color
.borderColorCC_ColorAnimate from/to this border color
.borderWidthCC_BorderWidthAnimate from/to these border widths

Trigger and ordering​

FieldTypeDefaultOther valuesSide
.enterTriggerCC_TransitionEnterTriggerCC_TRANSITION_ENTER_SKIP_ON_FIRST_PARENT_FRAME: no enter animation when the parent itself just appearedCC_TRANSITION_ENTER_TRIGGER_ON_FIRST_PARENT_FRAME: animate in together with a parent that just appearedenter
.exitTriggerCC_TransitionExitTriggerCC_TRANSITION_EXIT_SKIP_WHEN_PARENT_EXITS: no exit animation when the parent is removed tooCC_TRANSITION_EXIT_TRIGGER_WHEN_PARENT_EXITS: animate out even when the parent is removedexit
.siblingOrderingCC_ExitTransitionOrderingCC_EXIT_TRANSITION_ORDERING_UNDERNEATH_SIBLINGS: the exiting element draws below its siblingsCC_EXIT_TRANSITION_ORDERING_NATURAL_ORDER: keeps its old position among siblings. CC_EXIT_TRANSITION_ORDERING_ABOVE_SIBLINGS: draws above them.exit

Property bitmask​

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

Effect fieldInferred property flag
.slideXCC_TRANSITION_PROPERTY_X
.slideYCC_TRANSITION_PROPERTY_Y
.fadeCC_TRANSITION_PROPERTY_BACKGROUND_COLOR
.backgroundColorCC_TRANSITION_PROPERTY_BACKGROUND_COLOR
.overlayColorCC_TRANSITION_PROPERTY_OVERLAY_COLOR
.borderColorCC_TRANSITION_PROPERTY_BORDER_COLOR
.borderWidthCC_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 flagIncludes
_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):

FunctionCurveDescription
CC_EaseOutcubic ease-outFast start, slow finish. Default.
CC_LinearlinearConstant speed.
CC_EaseIncubic ease-inSlow start, fast finish.
CC_EaseInOutcubic ease-in-outSlow 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.

ConstantMeaning
CC_TRANSITION_STATE_IDLEElement is at rest. No transition is running.
CC_TRANSITION_STATE_ENTERINGElement just appeared. Enter callback runs the lerp.
CC_TRANSITION_STATE_TRANSITIONINGElement is mid-transition (e.g. animating between two positions on subsequent frames).
CC_TRANSITION_STATE_EXITINGElement 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_ aliasClay original
CC_TransitionElementConfigClay_TransitionElementConfig
CC_TransitionDataClay_TransitionData
CC_TransitionArgsClay_TransitionCallbackArguments
CC_TransitionPropertyClay_TransitionProperty
CC_TransitionStateClay_TransitionState
CC_TransitionEnterTriggerClay_TransitionEnterTriggerType
CC_TransitionExitTriggerClay_TransitionExitTriggerType
CC_TransitionInteractionClay_TransitionInteractionHandlingType
CC_ExitTransitionOrderingClay_ExitTransitionSiblingOrdering