@cssints/gradient

Open in playground

import { reset } from "@cssints/reset" with { type: "cssints" };
import {
  backgroundImage,
  bg,
  cn,
  color,
  flex,
  fontSize,
  minHeight,
  p,
} from "cssints" with { type: "cssints" };
import { gradient } from "@cssints/gradient" with { type: "cssints" };

reset();

// gradient(kind, direction, from, to, easing, stops) writes the stops of an eased gradient: no band where a plain two-colour
// gradient ends in a hard edge.
const hero = cn(
  flex().direction("column").justifyContent("flex-end"),
  minHeight("18rem"),
  p(6),
  color("white"),
  bg("#1e3a8a"),
  backgroundImage(gradient("linear", "to bottom", "transparent", "#020617", "ease-in-out", 6)),
);
const glow = cn(
  minHeight("8rem"),
  backgroundImage(
    gradient("radial", "circle at top", "#f472b6", "#1e1b4b", "cubic-bezier(0.5, 0, 0.5, 1)", 5),
  ),
);

export default function App() {
  return (
    <div className={cn(flex().direction("column").gap(3), p(4))}>
      <section className={hero}>
        <h1 className={cn(fontSize("2rem"))}>Eased overlay</h1>
      </section>
      <section className={glow} />
    </div>
  );
}

Eased gradients for cssints: a gradient from one colour to another along an easing curve, expanded into colour stops at build time, so the fade has no hard edge where a two-stop gradient shows one. It is one value member of cssints/plugin scope() and returns an <image>, so it goes where an image goes (backgroundImage, maskImage, borderImageSource). No runtime.

import { backgroundImage, createTokens } from "cssints" with { type: "cssints" };
import { gradient } from "@cssints/gradient" with { type: "cssints" };

const t = createTokens({ color: { $type: "color", ink: { $value: "#111111" } } });

// gradient(kind, direction, from, to, easing?, stops?)
export const fade = backgroundImage(
  gradient("linear", "to bottom", "#000", "transparent", "ease-in-out", 5),
);
export const glow = backgroundImage(
  gradient("radial", "circle at top", "red", "blue", "cubic-bezier(0.5, 0, 0.5, 1)", 4),
);
export const ink = backgroundImage(
  gradient("linear", "to right", t.color.ink, "white", "ease-in", 4),
);
._4we9ve {
  background-image: linear-gradient(
    to bottom,
    #000,
    #000000d7 27.44%,
    #00000080 50%,
    #00000028 72.56%,
    transparent
  );
}
._ba8wye {
  background-image: radial-gradient(circle at top, red, #c44a6f 37.04%, #5348d0 62.96%, blue);
}
._1l9gl59 {
  background-image: linear-gradient(
    to right,
    var(--color-ink),
    color-mix(in oklab, var(--color-ink), white 25.93%) 44.59%,
    color-mix(in oklab, var(--color-ink), white 74.07%) 83.41%,
    white
  );
}

Rules

Limits

cd packages/gradient && bun run test        # the stop math, fixtures through the engine (output, errors)
cd packages/gradient && bun run typecheck   # and the type test, test/types/gradient.ts

Credits

The stops follow postcss-easing-gradients and easing-coordinates (MIT, Andreas Larsen): the easing curve sampled into colour stops. The code is written here; no code is copied. Colours are mixed with culori (MIT, Dan Burzo), a dependency.