solid-transition-group

repository·main·Indexed 43 days ago

https://github.com/solidjs-community/solid-transition-group

Components for managing entry, exit, and move animations in SolidJS. Version 0.3.0 provides the <Transition> component for single elements and <TransitionGroup> for animating lists using the FLIP technique. Supports CSS-based animations via class prefixes and JavaScript-based animations through lifecycle hooks like onEnter and onExit.

Tokens
2.4K
Snippets
7
Records
11
Agent score
5%

What's inside solid-transition-group

  1. Use the Transition component for single elements

    main

    The <Transition> component applies animation effects to a single element or component as it enters or leaves the DOM. It does not render an extra DOM element or appear in the component hierarchy.

    By default, transitions are not applied on the initial render. To apply them on mount, set the appear prop to true.

    Warning: Using appear with SSR may cause a flash of unstyled content as the initial transition is applied on the client-side. You must handle initial transitions on the server-side manually if using SSR.

    import { Transition } from "solid-transition-group"
    
    const [isVisible, setVisible] = createSignal(true)
    
    <Transition name="slide-fade">
      <Show when={isVisible()}>
        <div>Hello</div>
      </Show>
    </Transition>
    
    setVisible(false) // triggers exit transition
  2. Animate with CSS using Transition

    main

    To use CSS transitions, provide a name prop to <Transition>. This name acts as a prefix for automatically generated CSS classes. For example, name="fade" generates .fade-enter, .fade-enter-active, etc.

    CSS Class Mapping

    PropDescription
    namePrefix for generated classes. Defaults to "s".
    enterClassClass applied at the start of enter transition. Defaults to "s-enter".
    enterToClassClass applied after enter transition starts. Defaults to "s-enter-to".
    enterActiveClassClass applied for the entire duration of enter transition. Defaults to "s-enter-active".
    exitClassClass applied at the start of exit transition. Defaults to "s-exit".
    exitToClassClass applied after exit transition starts. Defaults to "s-exit-to".
    exitActiveClassClass applied for the entire duration of exit transition. Defaults to "s-exit-active".
    /* Example for name="slide-fade" */
    .slide-fade-enter-active, 
    .slide-fade-exit-active {
      transition: opacity 0.3s, transform 0.3s;
    }
    .slide-fade-enter, 
    .slide-fade-exit-to {
      transform: translateX(10px);
      opacity: 0;
    }
    .slide-fade-enter {
      transform: translateX(-10px);
    }
  3. Define Transition CSS class names

    main

    By default, the library uses the prefix s- to generate CSS classes. If you provide a name prop, the classes will be prefixed with that name.

    For a name="fade", the following classes are used:

    Enter Phase:

    • fade-enter: Initial state of the entering element.
    • fade-enter-active: State applied during the entire duration of the enter transition.
    • fade-enter-to: Final state of the entering element.

    Exit Phase:

    • fade-exit: Initial state of the exiting element.
    • fade-exit-active: State applied during the entire duration of the exit transition.
    • fade-exit-to: Final state of the exiting element.

    Move Phase (TransitionGroup only):

    • fade-move: Applied to elements that are repositioning within the group.
  4. Animate with JavaScript using Transition

    main

    You can hook into the transition lifecycle using JavaScript events. When using these events, you must call the done callback to signal the end of the transition. If done is not provided, the component will automatically call it on transitionend or animationend.

    Lifecycle Events

    EventParametersDescription
    onBeforeEnterelement: ElementCalled before enter starts. Element is not yet rendered.
    onEnterelement: Element, done: () => voidCalled when enter starts. Element is in DOM. Call done() to finish.
    onAfterEnterelement: ElementCalled after enter ends.
    onBeforeExitelement: ElementCalled before exit starts. Element is still rendered.
    onExitelement: Element, done: () => voidCalled when exit starts. Call done() to remove element from DOM.
    onAfterExitelement: ElementCalled after exit ends.
    <Transition
      onEnter={(el, done) => {
        const a = el.animate([{ opacity: 0 }, { opacity: 1 }], {
          duration: 600
        });
        a.finished.then(done);
      }}
      onExit={(el, done) => {
        const a = el.animate([{ opacity: 1 }, { opacity: 0 }], {
          duration: 600
        });
        a.finished.then(done);
      }}
    >
      {show() && <div>Hello</div>}
    </Transition>
  5. Use TransitionGroup for multiple elements

    main

    The <TransitionGroup> component is used for animating multiple elements (e.g., items in a list). It supports moving transitions via the FLIP technique using CSS transforms.

    When a child's position changes, it receives a moving CSS class. If the transform property is transition-able, the element will animate smoothly to its new position.

    Props

    • moveClass: CSS class applied to moving elements for the duration of the move. Defaults to "s-move".
    • Inherits all <Transition> props except mode.
    <ul>
      <TransitionGroup name="slide">
        <For each={state.items}>{item => <li>{item.text}</li>}</For>
      </TransitionGroup>
    </ul>
  6. Use the Transition component

    main

    The <Transition> component allows you to apply enter and leave animations to a single element passed as props.children. It is ideal for toggling visibility of a single component.

    Key Props

    • name: (string) Prefix for auto-generated CSS classes (e.g., name="fade" generates .fade-enter, .fade-enter-active, etc.). Defaults to "s".
    • appear: (boolean) Whether to apply the transition on the initial render. Defaults to false.
    • mode: ("inout" | "outin") Controls the timing sequence. "outin" waits for the exiting element to finish before starting the entering one. "inout" starts them simultaneously.
    • enterActiveClass, enterClass, enterToClass: Custom CSS classes for the entering phase.
    • exitActiveClass, exitClass, exitToClass: Custom CSS classes for the exiting phase.
    • onBeforeEnter, onEnter, onAfterEnter, onBeforeExit, onExit, onAfterExit: Lifecycle event hooks.
    <Transition name="fade" appear mode="outin">
      {show() && <div class="content">Hello World</div>}
    </Transition>
  7. Use the TransitionGroup component

    main

    The <TransitionGroup> component is used for animating lists of elements. It supports entering, exiting, and moving elements when the list order changes.

    Key Props

    • name: (string) Prefix for auto-generated CSS classes. Defaults to "s".
    • appear: (boolean) Whether to apply the transition on the initial render.
    • moveClass: (string) CSS class applied to elements during the "move" transition (e.g., when an item's position in a list changes). Defaults to "s-move".
    • onBeforeEnter, onEnter, onAfterEnter, onBeforeExit, onExit, onAfterExit: Lifecycle event hooks.

    Note: Unlike <Transition>, <TransitionGroup> does not support the mode prop as it manages multiple elements simultaneously.

    <TransitionGroup name="list">
      <For each={items()}>
        {(item) => <div>{item}</div>}
      </For>
    </TransitionGroup>
  8. Configure Transition lifecycle events

    main

    Both <Transition> and <TransitionGroup> expose a TransitionEvents object via props to hook into the animation lifecycle.

    EventTimingDescription
    onBeforeEnterBefore enter startsElement is not yet rendered.
    onEnterEnter startsElement is in the DOM. Call done() to end the transition.
    onAfterEnterAfter enter endsElement is rendered and transition is complete.
    onBeforeExitBefore exit startsElement is still rendered; exit classes are not yet applied.
    onExitExit startsExit classes are applied. Call done() to remove the element from the DOM.
    onAfterExitAfter exit endsElement is removed from the DOM.
  9. Exported exitTransition function

    main

    The exitTransition function is an exported utility that runs an exit transition on a specific DOM element. It is typically used internally by TransitionGroup but can be used to manually trigger exit logic.

    Signature: exitTransition(classes: ReturnType<ReturnType<typeof createClassnames>>, events: TransitionEvents, el: Element, done?: VoidFunction)