@cssints/gradient

中文文档
0.35.1发布于 2026/11/07MITTypeScriptES Module构建来源已验证近 30 天下载 7488

Eased gradients for cssints: an easing curve expanded into colour stops (mixed in oklab) at build time

原始使用文档
文档目录3 节
  1. Rules
  2. Limits
  3. Credits

@cssints/gradient

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

  • Arguments, each its own string: kind is linear, radial or conic; direction is that function's first argument, required ("to bottom", "45deg", "circle at top", "at center", "from 90deg"); from and to are colours or colour tokens; easing is linear, ease, ease-in, ease-out, ease-in-out (the default) or cubic-bezier(x1, y1, x2, y2); stops is 2 to 64 (13 by default). They are checked in the types and again at build time, against one grammar.
  • Stops are placed as postcss-easing-gradients places them: the curve's parameter t is sampled evenly, x(t) is the position of a stop and y(t) the share of to in it. A curve that overshoots (y outside 0 to 1) is clamped to the two colours.
  • Colours are mixed in oklab with premultiplied alpha, as CSS mixes them, so transparent fades the other colour's hue and does not grey it. The endpoints are printed as written; the stops between them are sRGB hex (#rrggbbaa when not opaque). Positions and shares have two decimals at most, so the output is the same on every build.
  • Tokens and currentColor. A token arrives as var(--name) and currentColor is the element's: neither can be read at build time. When an endpoint cannot be, every stop is color-mix(in oklab, from, to N%) at the same positions, and the browser mixes them. color-mix() needs Chrome 111, Safari 16.2, Firefox 113: for older targets the build warns, as it does for any value.
  • Errors, at the call: a part outside the grammar (gradient: unexpected "steps(4" in "linear to bottom red blue steps(4)", expected linear | <cubic-bezier-easing-function> | <integer> | end of value), arguments in the wrong order (cssints: gradient(): "blue" is not an easing (the arguments are kind, direction, from, to, easing?, stops?)), and a gradient where an image does not go (css.color(gradient(…)), an error in the types and at build time).

Limits

  • A stop between two literal colours is clamped to sRGB: a wide-gamut endpoint (color(display-p3 …), a vivid oklch()) keeps its own value at the end, but the colours between are the nearest sRGB ones. Give it as a token to keep the gamut: the stops are then color-mix().
  • steps() and linear() easings are not taken, nor an easing token (there is no easing token type), nor a number token for stops; a repeating-* gradient, more than two colours and a colour interpolation method (in oklch) are not either.
  • The parts are joined with a space for the grammar (see "Functions" in the API reference), so "to", "bottom" as two arguments passes the grammar and is caught only by the argument check; write the direction as one string.
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.

版本与发布记录

@cssints/gradient 共发布 43 个版本,首次发布于 2026/10/11;过去 12 个月发布了 43 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.35.1latest2026/11/073 天前
0.35.02026/11/073 天前
0.34.02026/11/073 天前
0.33.02026/11/046 天前
0.32.02026/11/046 天前
0.31.02026/11/047 天前

关于 @cssints/gradient 的常见问题

@cssints/gradient 是什么?

Eased gradients for cssints: an easing curve expanded into colour stops (mixed in oklab) at build time

如何安装 @cssints/gradient?

在项目目录运行 npm install @cssints/gradient,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:cssints(^0.35.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@cssints/gradient 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@cssints/gradient 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@cssints/gradient 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=22。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@cssints/gradient 可以商用吗?

@cssints/gradient 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@cssints/gradient 的下载量有多少?

根据 npm 官方下载统计,@cssints/gradient 在 2026/10/11 – 2026/11/09 这 30 天内被下载 7,488 次,日均约 250 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@cssints/gradient 还在维护吗?

最新版本 0.35.1 发布于 2026/11/07(3 天前);至今共发布 43 个版本,其中过去 12 个月发布了 43 个;首次发布于 2026/10/11。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。