@cssints/reveal

Open in playground

import { reset } from "@cssints/reset" with { type: "cssints" };
import { bg, borderRadius, cn, color, flex, hover, p } from "cssints" with { type: "cssints" };
import { reveal } from "@cssints/reveal" with { type: "cssints" };

reset();

// reveal() animates an element as it scrolls into view (scroll-driven animations, with a small runtime where a target lacks
// them). A preset names the effect; range says which part of the scroll it plays over.
const fade = cn(reveal(), p(6), bg("#e0e7ff"), color("#0f172a"), borderRadius("12px"));
const card = cn(
  reveal().preset("slide-in-up").range("entry 0% entry 100%").duration("500ms").easing("linear"),
  p(6),
  bg("#dbeafe"),
  color("#0f172a"),
  borderRadius("12px"),
);
const tile = cn(
  reveal().preset("blur-in"),
  hover(reveal().duration("1s")),
  p(6),
  bg("#fce7f3"),
  color("#0f172a"),
  borderRadius("12px"),
);

export default function App() {
  return (
    <div className={cn(flex().direction("column").gap(10), p(4))}>
      <div style={{ height: "40vh" }}>Scroll down</div>
      <div className={fade}>fade</div>
      <div className={card}>slide-in-up</div>
      <div className={tile}>blur-in</div>
      <div style={{ height: "30vh" }} />
    </div>
  );
}

Entrance animations for cssints, on @cssints/motion's presets, plus a scroll-progress bar, as three chain members of cssints/plugin scope():

Where a target lacks the feature a member runs on, a 0.4 kB script plays the same animation once when the element enters.

import { cn, hover } from "cssints" with { type: "cssints" };
import { blockSize, background } from "cssints" with { type: "cssints" };
import { appear, pointer, progress, reveal } from "@cssints/reveal" with { type: "cssints" };

// scroll-driven: the scroll scrubs it
export const fade = reveal();
export const card = reveal()
  .preset("slide-in-up")
  .range("entry 0% entry 100%")
  .duration("500ms")
  .easing("linear");
export const tile = cn(reveal().preset("blur-in"), hover(reveal().duration("1s")));

// scroll-triggered: it plays once on entry, with its own time
export const lede = appear().preset("slide-in-up").duration("600ms").easing("linear");
export const band = appear().preset("fade-in").inOut();

// a reading bar in a pane: the pane is the nearest scrollport, the bar grows from its left edge
export const bar = cn(
  progress().timeline("scroll(nearest)"),
  blockSize("3px"),
  background("tomato"),
);
export const side = progress().axis("y").range("entry 0% cover 100%"); // a section's own progress, from the top

The CSS of reveal() (fade and card above):

@layer _.k {
  @keyframes kf-7f473efd {
    from {
      opacity: var(--motion-o);
      translate: var(--motion-x) var(--motion-y);
      scale: var(--motion-s);
      filter: blur(var(--motion-b));
    }
  }
  @keyframes kf-4f766ef7 {
    from {
      opacity: var(--motion-o);
    }
  }
  ._reveal-core {
    animation: var(--reveal-k) var(--reveal-d) var(--reveal-e) both;
    animation-range: var(--reveal-g);
    --reveal-k: kf-7f473efd;
    --reveal-rk: kf-4f766ef7;
    --reveal-d: 300ms;
    --reveal-e: ease-out;
    --reveal-g: entry 0% cover 40%;
    --motion-o: 0;
    --motion-x: 0;
    --motion-y: 0;
    --motion-s: 1;
    --motion-b: 0;
    animation-timeline: view();
  }
  @media (prefers-reduced-motion: reduce) {
    ._reveal-core {
      animation-name: var(--reveal-rk);
    }
  }
  /* only when a target lacks scroll-driven animations */
  @supports not (animation-timeline: view()) {
    [data-reveal] ._reveal-core:not([data-reveal-in]) {
      animation-play-state: paused;
    }
  }
}
@layer _.a {
  ._1a9p5a9 {
    --motion-y: 25%;
  }
  ._yorqor {
    --reveal-g: entry 0% entry 100%;
  }
  ._k4abtq {
    --reveal-d: 500ms;
  }
  /* ... */
}

The CSS of appear() (lede and band above):

@layer _.k {
  /* motion's keyframes, the two rules reveal() names too (one pair for the page) */
  ._reveal-enter {
    animation: var(--reveal-enter-k) var(--reveal-enter-d) var(--reveal-enter-e) both;
    timeline-trigger: --reveal-t view() var(--reveal-enter-g);
    --reveal-enter-k: kf-7f473efd;
    --reveal-enter-rk: kf-4f766ef7;
    --reveal-enter-d: 300ms;
    --reveal-enter-e: ease-out;
    --reveal-enter-g: entry 100% exit 0%;
    --reveal-enter: play-once;
    --reveal-leave: none;
    --motion-o: 0;
    --motion-x: 0;
    --motion-y: 0;
    --motion-s: 1;
    --motion-b: 0;
    animation-trigger: --reveal-t var(--reveal-enter) var(--reveal-leave);
    trigger-scope: --reveal-t;
  }
  @media (prefers-reduced-motion: reduce) {
    ._reveal-enter {
      animation-name: var(--reveal-enter-rk);
    }
  }
  /* only when a target lacks animation triggers */
  @supports not (animation-trigger: none) {
    [data-reveal] ._reveal-enter:not([data-reveal-in]) {
      animation-play-state: paused;
    }
  }
}
@layer _.a {
  ._5mnsmm {
    --reveal-enter: play-forwards;
  }
  ._67iv7r {
    --reveal-leave: play-backwards;
  }
  ._1a9p5a9 {
    --motion-y: 25%;
  }
  ._knj5bv {
    --motion-o: 0;
  }
  ._oflqj4 {
    --reveal-enter-e: linear;
  }
  ._rsl9nu {
    --reveal-enter-d: 600ms;
  }
}

Rules

progress()

progress()
  .axis("x" | "y")
  .timeline("<animation-timeline>")
  .range("<animation-range>");

pointer()

pointer().axis("x" | "y");

Browsers

Limits

cd packages/reveal && bun run test            # fixtures through the engine (output by targets, runtime modules, conflicts, errors)
cd packages/reveal && bun run test:browsers   # Chrome, Firefox and WebKit through playwright-core

Credits

In the spirit of the supersonic plugin (MIT, Alex Illarionov) and lax.js (MIT, Alex Fox). The presets are @cssints/motion's. No code is copied.