Transition Group

Use the transition-group patch to customize this element.

Customization

Must see the source of patch at the bottom of each patch page to understand the structure then code it still code as html native element.

There are four levels of customization, in increasing order of effort:

  1. Patch props. Each patch exposes a small, stable set of props—typically fewer than five. Lowest friction.
  2. Context attributes. Use dataTone, dataSize, and dataDensity on a container to shift tone, size, or density for an entire subtree without touching individual elements.
  3. Inline override. Native-wins merge strategy: any property set directly on the element overrides the patch value.
  4. Create a variant. Clone a similar patch and edit it. Use this only when you need a reusable custom version.
Formulas

Unit - U = fontSize / 4 - convert final values with themeSpacing(n).

Size - n = intrinsic text lines, w = wrapping level, d = density factor:

height        = (n * 6 + 2 * d * w) * U
paddingBlock  = d * w * U
paddingInline = ceil(3 / w) * d * w * U
radius        = d * w * U

Base density d = 1.5:

Uw=0w=1w=2w=3
height (n = 1)691215
paddingBlock01.534.5
paddingInline34.564.5
radius01.534.5

Tone - K = N / 2 where N is the palette length. For N = 18, K = 9.

RoleShiftn=0
Backgroundparent +/- n0
Textbg + K6
Borderbg + K/23
Hoverbg + 2K/34
Selected / Focusabove +/- K/32-4

State shift range: K/3 <= delta <= 2K/3.

<div class="blocks">
<div class="block active" data-tab="0">
import { ElementNode, type PartialElement } from "@domphy/core";

type RectMap = Map<string, DOMRect>;

function getItemId(node: ElementNode, index: number): string {
  if (node.key !== undefined && node.key !== null) {
    return String(node.key);
  }
  return `index-${index}`;
}

/**
 * Animates child reordering using the FLIP technique: records each child's
 * position before an update and smoothly transitions it from its old to new
 * position afterward. No host tag check; applied to the list container.
 *
 * @param props.duration - Transition duration in milliseconds. Optional. Defaults to `300`.
 * @param props.delay - Transition delay in milliseconds. Optional. Defaults to `0`.
 * @example { ul: null, $: [transitionGroup({ duration: 300 })] }
 */
function transitionGroup(
  props: { duration?: number; delay?: number } = {},
): PartialElement {
  const { duration = 300, delay = 0 } = props;

  let previousRects: RectMap = new Map();

  return {
    _onBeforeUpdate: (node) => {
      previousRects = new Map();
      node.children.items.forEach((item, index) => {
        if (!(item instanceof ElementNode)) return;
        const dom = item.domElement as HTMLElement | undefined;
        if (!dom) return;
        previousRects.set(getItemId(item, index), dom.getBoundingClientRect());
      });
    },
    _onUpdate: (node) => {
      node.children.items.forEach((item, index) => {
        if (!(item instanceof ElementNode)) return;
        const dom = item.domElement as HTMLElement | undefined;
        if (!dom) return;

        const key = getItemId(item, index);
        const prev = previousRects.get(key);
        if (!prev) return;

        const next = dom.getBoundingClientRect();
        const deltaX = prev.left - next.left;
        const deltaY = prev.top - next.top;
        if (Math.abs(deltaX) < 0.5 && Math.abs(deltaY) < 0.5) return;

        const previousTransition = dom.style.transition;
        const previousTransform = dom.style.transform;

        dom.style.transition = "none";
        dom.style.transform = `translate(${deltaX}px, ${deltaY}px)`;
        dom.getBoundingClientRect();

        requestAnimationFrame(() => {
          dom.style.transition = `transform ${duration}ms ease ${delay}ms`;
          dom.style.transform = "translate(0px, 0px)";
        });

        const cleanup = () => {
          dom.style.transition = previousTransition;
          dom.style.transform = previousTransform;
          dom.removeEventListener("transitionend", onEnd);
        };

        const onEnd = (event: Event) => {
          const transitionEvent = event as TransitionEvent;
          if (transitionEvent.propertyName === "transform") {
            cleanup();
          }
        };

        dom.addEventListener("transitionend", onEnd);
        setTimeout(cleanup, duration + delay + 34);
      });
      previousRects.clear();
    },
  };
}

export { transitionGroup };
</div>
</div>