or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

css-transition.mdindex.mdswitch-transition.mdtransition-group.mdtransition.md
tile.json

switch-transition.mddocs/

Switch Transition Component

SwitchTransition manages transitions between different components, inspired by Vue.js transition modes. It controls the timing of when components enter and exit based on the selected mode.

Capabilities

SwitchTransition Component Class

Component for managing transitions between different components with timing control.

/**
 * A transition component inspired by Vue transition modes.
 * Controls the render timing between state transitions.
 * Makes consistent transitions between components based on the child's key.
 */
declare class SwitchTransition extends Component<SwitchTransitionProps> {}

SwitchTransition Props

Configuration for switch transition behavior and children.

interface SwitchTransitionProps {
  /** 
   * Transition modes controlling timing:
   * - "out-in": Current element exits first, then new element enters
   * - "in-out": New element enters first, then current element exits
   */
  mode?: "out-in" | "in-out";
  
  /** Single Transition or CSSTransition component */
  children: ReactElement;
}

Transition Modes

Available modes for controlling transition timing.

enum modes {
  /** Current element transitions out first, then new element transitions in */
  out = "out-in",
  
  /** New element transitions in first, then current element transitions out */
  in = "in-out"
}

Mode Behavior

"out-in" Mode (Default)

  1. Current component starts exiting
  2. Wait for current component to finish exiting
  3. New component starts entering
  4. New component finishes entering

"in-out" Mode

  1. New component starts entering
  2. Current component starts exiting simultaneously
  3. Wait for new component to finish entering
  4. Wait for current component to finish exiting

Usage Examples:

import React, { useState, useRef } from "react";
import { SwitchTransition, CSSTransition } from "react-transition-group";

// Basic switch transition
const SwitchExample: React.FC = () => {
  const [state, setState] = useState(false);

  return (
    <SwitchTransition>
      <CSSTransition
        key={state ? "goodbye" : "hello"}
        timeout={300}
        classNames="fade"
      >
        <button onClick={() => setState(!state)}>
          {state ? "Goodbye, world!" : "Hello, world!"}
        </button>
      </CSSTransition>
    </SwitchTransition>
  );
};

// With custom mode
const InOutExample: React.FC = () => {
  const [page, setPage] = useState("home");

  return (
    <div>
      <nav>
        <button onClick={() => setPage("home")}>Home</button>
        <button onClick={() => setPage("about")}>About</button>
        <button onClick={() => setPage("contact")}>Contact</button>
      </nav>
      
      <SwitchTransition mode="in-out">
        <CSSTransition
          key={page}
          timeout={500}
          classNames="slide"
        >
          <div className="page">
            {page === "home" && <HomePage />}
            {page === "about" && <AboutPage />}
            {page === "contact" && <ContactPage />}
          </div>
        </CSSTransition>
      </SwitchTransition>
    </div>
  );
};

// With nodeRef and custom end listener
const AdvancedSwitchExample: React.FC = () => {
  const [current, setCurrent] = useState(0);
  const items = ["First", "Second", "Third"];
  const nodeRef = useRef<HTMLDivElement>(null);

  return (
    <div>
      <button onClick={() => setCurrent((current + 1) % items.length)}>
        Next
      </button>
      
      <SwitchTransition mode="out-in">
        <CSSTransition
          key={current}
          nodeRef={nodeRef}
          timeout={400}
          classNames="switch"
          addEndListener={(done) => {
            nodeRef.current?.addEventListener("transitionend", done, false);
          }}
        >
          <div ref={nodeRef} className="switch-item">
            {items[current]}
          </div>
        </CSSTransition>
      </SwitchTransition>
    </div>
  );
};

Key Requirements:

  • Child must be a single Transition or CSSTransition component
  • Child component must have a key prop that changes to trigger transitions
  • The key prop change is what triggers the switch transition
  • Only one child is rendered at a time (unlike TransitionGroup)