Transition

This example showcases Transition animating appearing and disappearing elements with standard CSS classes (fade-* for opacity and slide-* for transforms), delaying DOM removal until leave animations complete cleanly.

Use Transition whenever banners, drawers, modals, or status notices should animate cleanly out of the DOM rather than snapping away immediately on state changes.

Transition

Demo
Code
from webcompy.app import WebComPyApp
from webcompy.components import ComponentContext, define_component
from webcompy.elements import Transition, html
from webcompy.signal import use_state


@define_component("transition-demo-app")
def TransitionDemo(_: ComponentContext[None]):
    fade_visible = use_state(lambda: False)
    slide_visible = use_state(lambda: False)

    def _toggle_fade(_ev):
        fade_visible.value = not fade_visible.value

    def _toggle_slide(_ev):
        slide_visible.value = not slide_visible.value

    return html.DIV(
        {"class": "transition-demo"},
        html.H1({}, "Transition Demo"),
        html.P({}, "Show and hide content; the CSS classes are driven by WebComPy."),
        html.DIV(
            {"class": "demo-controls"},
            html.BUTTON({"id": "toggle-fade", "@click": _toggle_fade}, "Toggle Fade"),
            html.BUTTON({"id": "toggle-slide", "@click": _toggle_slide}, "Toggle Slide"),
        ),
        Transition(
            {"name": "fade"},
            lambda: html.DIV({"class": "demo-fade-box"}, "Fade content") if fade_visible.value else None,
        ),
        Transition(
            {"name": "slide"},
            lambda: html.DIV({"class": "demo-slide-box"}, "Slide content") if slide_visible.value else None,
        ),
    )


TransitionDemo.scoped_style = {
    ".transition-demo": {
        "font-family": "var(--font-sans)",
        "color": "var(--color-fg)",
        "padding": "var(--space-4)",
    },
    ".demo-controls": {
        "display": "flex",
        "gap": "0.5rem",
        "margin-top": "1rem",
    },
    ".demo-controls button": {
        "padding": "0.5rem 1rem",
        "border": "1px solid var(--color-border)",
        "border-radius": "var(--radius-sm)",
        "background": "var(--color-bg-elevated)",
        "color": "var(--color-fg)",
        "cursor": "pointer",
    },
    ".demo-fade-box": {
        "margin-top": "1rem",
        "padding": "1rem",
        "background": "color-mix(in srgb, var(--color-danger) 15%, var(--color-bg-card))",
        "border": "1px solid var(--color-danger)",
        "border-radius": "var(--radius-md)",
        "color": "var(--color-fg)",
        "transition": "opacity 400ms ease",
    },
    ".fade-enter-from": {
        "opacity": "0",
    },
    ".fade-enter-active": {
        "transition": "opacity 400ms ease",
    },
    ".fade-enter-to": {
        "opacity": "1",
    },
    ".fade-leave-from": {
        "opacity": "1",
    },
    ".fade-leave-active": {
        "transition": "opacity 400ms ease",
    },
    ".fade-leave-to": {
        "opacity": "0",
    },
    ".demo-slide-box": {
        "margin-top": "1rem",
        "padding": "1rem",
        "background": "color-mix(in srgb, var(--color-accent) 15%, var(--color-bg-card))",
        "border": "1px solid var(--color-accent)",
        "border-radius": "var(--radius-md)",
        "color": "var(--color-fg)",
        "transform": "translateX(0)",
    },
    ".slide-enter-from": {
        "transform": "translateX(100%)",
    },
    ".slide-enter-active": {
        "transition": "transform 400ms ease",
    },
    ".slide-enter-to": {
        "transform": "translateX(0)",
    },
    ".slide-leave-from": {
        "transform": "translateX(0)",
    },
    ".slide-leave-active": {
        "transition": "transform 400ms ease",
    },
    ".slide-leave-to": {
        "transform": "translateX(-100%)",
    },
}

app = WebComPyApp(root_component=TransitionDemo)
app.run()

What this demonstrates#

  • Six-class Vue-compatible lifecycle: Transition({"name": "fade"}, ...) applies {name}-enter-from on mount, swaps to {name}-enter-active and {name}-enter-to on the next animation frame, and reverses the sequence (-leave-from -> -leave-active / -leave-to) on hide before detaching the DOM node.
  • Automatic CSS duration detection: Because no explicit "duration" prop is passed, WebComPy inspects computed transition-duration and waits for transitionend (or its computed fallback timeout) automatically.
  • Independent named transitions: Notice the fade box (name="fade") and slide box (name="slide") animate independently through dedicated .fade-* and .slide-* selectors in TransitionDemo.scoped_style.

Key mechanisms and how to adapt#

  1. Wrap a single concrete element: The generator callback inside Transition(...) must return either None or one real element (html.DIV, etc.) — do not pass multi-root Fragments or bare strings directly.
  2. Use box layout modes on component children: If transitioning a component wrapper directly, pass @define_component(display="block") so the host custom element generates a real CSS box instead of defaulting to display: contents.
  3. Avoid reactive "class" bindings on the animated node: Keep visual state classes on a wrapper or child element so signal re-renders do not overwrite active -enter-* or -leave-* classes mid-animation.

Next steps#