npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@solid-primitives/transition-group

v1.0.5

Published

Reactive primitives for implementing transition effects in SolidJS

Downloads

55,475

Readme

@solid-primitives/transition-group

turborepo size version stage

Provides reactive primitives for implementing transition effects on a group of elements, or your own <Transition> and <TransitionGroup> components.

Installation

npm install @solid-primitives/transition-group
# or
yarn add @solid-primitives/transition-group
# or
pnpm add @solid-primitives/transition-group

createSwitchTransition

Create an element transition interface for switching between single elements. It can be used to implement own transition effect, or a custom <Transition>-like component.

How to use it

It will observe the source and return a signal with array of elements to be rendered (current one and exiting ones).

createSwitchTransition takes two parameters:

  • source a signal with the current element. Any nullish value will mean there is no element. Any object can used as the source, but most likely you will want to use a HTMLElement or SVGElement.

  • options transition options:

    • onEnter - a function to be called when a new element is entering. It receives the element and a callback to be called when the transition is done.

    • onExit - a function to be called when an exiting element is leaving. It receives the element and a callback to be called when the transition is done.

    • mode - transition mode. Defaults to "parallel". Other options are "out-in" and "in-out".

    • appear - whether to run the transition on the initial element. Defaults to false.

      If enabled, the initial element will still be included in the initial render (for SSR), but the transition fill happen when the first client-side effect is run. So to avoid the initial element to be visible, you can set the initial element's style to display: none and set it to display: block in the onEnter callback.

Returns a signal with an array of the current element and exiting previous elements.

import { createSwitchTransition } from "@solid-primitives/transition-group";

const [el, setEl] = createSignal<HTMLDivElement>();

const rendered = createSwitchTransition(el, {
  onEnter(el, done) {
    // the enter callback is called before the element is inserted into the DOM
    // so run the animation in the next animation frame / microtask
    queueMicrotask(() => {
      /*...*/
    });
  },
  onExit(el, done) {
    // the exitting element is kept in the DOM until the done() callback is called
  },
});

// change the source to trigger the transition
setEl(refToHtmlElement);

Resolving JSX

Usually the source will be a JSX element, and you will want to resolve it to a DOM element before passing it to createSwitchTransition. It leaves the resolving to you, so you can do it in any way you want.

For example, you can children helper from solid-js, to get the first found HTML element.

import { children } from "solid-js";
import { createSwitchTransition } from "@solid-primitives/transition-group";

const resolved = children(() => props.children);
const filtered = createMemo(() => resolved.toArray().find(el => el instanceof HTMLElement));
return createSwitchTransition(filtered, {
  /*...*/
});

Or use a resolveFirst helper from @solid-primitives/refs

import { resolveFirst } from "@solid-primitives/refs";
import { createSwitchTransition } from "@solid-primitives/transition-group";

const resolved = resolveFirst(() => props.children);
return createSwitchTransition(resolved, {
  /*...*/
});

createListTransition

Create an element list transition interface for changes to the list of elements. It can be used to implement own transition effect, or a custom <TransitionGroup>-like component.

How to use it

It will observe the source and return a signal with array of elements to be rendered (current ones and exiting ones).

createListTransition takes two parameters:

  • source a signal with the current list of elements. Any object can used as the element, but most likely you will want to use a HTMLElement or SVGElement.

  • options transition options:

    • onChange - a function to be called when the list changes. It receives the list of added elements, removed elements, and moved elements. It also receives a callback to be called when the removed elements are finished animating (they can be removed from the DOM).

    • appear - whether to run the transition on the initial elements. Defaults to false.

    If enabled, the initial elements will still be included in the initial render (for SSR), but the transition fill happen when the first client-side effect is run. So to avoid the initial elements to be visible, you can set the initial element's style to display: none and set it to display: block in the onChange callback.

    • exitMethod - This controls how the elements exit.

      • "remove" removes the element immediately.
      • "move-to-end" (default) will move elements which have exited to the end of the array.
      • "keep-index" will splice them in at their previous index.

Returns a signal with an array of the current elements and exiting previous elements.

import { createListTransition } from "@solid-primitives/transition-group";

const [els, setEls] = createSignal<HTMLElement[]>([]);

const rendered = createListTransition(els, {
  onChange({ list, added, removed, unchanged, finishRemoved }) {
    // the callback is called before the added elements are inserted into the DOM
    // so run the animation in the next animation frame / microtask
    queueMicrotask(() => {
      /*...*/
    });

    // the removed elements are kept in the DOM until the finishRemoved() callback is called
    finishRemoved(removed);
  },
});

// change the source to trigger the transition
setEls([...refsToHTMLElements]);

Resolving JSX

Usually the source will be a JSX Element, and you will want to resolve it to a list of DOM elements before passing it to createListTransition. It leaves the resolving to you, so you can do it in any way you want.

For example, you can children helper from solid-js, and filter out non-HTML elements:

import { children } from "solid-js";
import { createListTransition } from "@solid-primitives/transition-group";

const resolved = children(() => props.children);
const filtered = createMemo(() => resolved.toArray().filter(el => el instanceof HTMLElement));
return createListTransition(filtered, {
  /*...*/
});

Or use a resolveElements helper from @solid-primitives/refs

import { resolveElements } from "@solid-primitives/refs";
import { createSwitchTransition } from "@solid-primitives/transition-group";

const resolved = resolveElements(() => props.children);
return createListTransition(resolved.toArray, {
  /*...*/
});

Demo

Deployed example | Source code

Usage references

Packages that use @solid-primitives/transition-group:

Changelog

See CHANGELOG.md