@cssints/placeholder

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

Image placeholders for cssints: a gradient of the dominant colours, or a thumbhash data URL, read from the image at build time

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

@cssints/placeholder

Image placeholders for cssints: placeholder(path) reads an image while the build runs and gives an <image> to paint until the real one loads, either a gradient of the image's colours laid out as the image is (unpic-placeholder's radial gradients) or the image's ThumbHash as a tiny PNG data URL. It is one value member of cssints/plugin scope(), so it goes into backgroundImage (or maskImage). No runtime.

import { backgroundImage, cn, backgroundSize } from "cssints" with { type: "cssints" };
import { placeholder } from "@cssints/placeholder" with { type: "cssints" };

// placeholder(path, { mode? }): the path is relative to the working directory
export const hero = backgroundImage(placeholder("src/hero.jpg"));
export const card = cn(
  backgroundImage(placeholder("src/card.png", { mode: "thumbhash" })),
  backgroundSize("cover"),
);

For a PNG of four quadrants (red, blue / green, white):

/* mode "gradient" (the default): a 4 × 3 grid, a radial gradient per cell, brightest on top, over the average colour */
._11mb38d {
  background-image:
    radial-gradient(at 67% 100%, #ffffff, transparent 50%),
    radial-gradient(at 100% 100%, #ffffff, transparent 50%),
    radial-gradient(at 67% 50%, #bcbcff, transparent 50%),
    /* … one per cell, 12 in all … */ radial-gradient(at 100% 0, #0000ff, transparent 50%),
    linear-gradient(#bc96bc, #bc96bc);
}
/* mode "thumbhash": the ThumbHash rendered, 16 px on the long side, about 0.5 kB */
._179cifn {
  background-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAM…");
}

Formats

JPEG (baseline and progressive, through jpeg-js) and PNG (every colour type and bit depth, through pngjs), both plain JavaScript: nothing to compile, no native binary (sharp is not used). The format is read from the file's first bytes, not its name. WebP, AVIF, GIF and SVG are build errors that name the format (cssints: placeholder: src/hero.webp: a WebP image: JPEG and PNG are read): give a JPEG or PNG copy of the image (it can be small; only its colours are read).

Rules

  • Arguments. path is a string, relative to the working directory (process.cwd(), the project root under Vite), as the fs provider of @cssints/icons reads it. mode is "gradient" (the default) or "thumbhash". Both are checked in the types and at build time.
  • gradient averages the image to a grid of 4 × 3 cells (3 × 4 for a portrait image), in linear light and weighted by alpha, so a transparent part adds no colour and a cell that straddles red and green is the light's mix, not a muddy sRGB average. Each cell is a radial-gradient(at x y, colour, transparent 50%) at its place in the image (corners at 0 and 100%), sorted by luminance with the brightest on top (as unpic-placeholder does), over one linear-gradient() of the whole image's average colour that fills what the gradients leave. A cell that is not opaque is #rrggbbaa. The value is a list of 13 images, about 0.7 kB.
  • thumbhash encodes the image (averaged to at most 100 px a side first) with ThumbHash, renders the hash, averages that to 16 px on the long side and writes it as a deflated PNG: about 0.5 kB, where ThumbHash's own data URL (32 px, uncompressed) is about 5 kB. The browser's smooth upscaling does the blur. It keeps the aspect ratio, so pair it with backgroundSize("cover") or "100% 100%".
  • Deterministic. The same file gives the same CSS on every build. Results are cached for the process by the SHA-256 of the file's content and the mode, so an image used from many modules is decoded once.
  • Errors, at the call: a file that cannot be read (cssints: placeholder: cannot read src/nope.png (paths are relative to /app)), a format it does not read (above), a file that does not decode (cssints: placeholder: src/broken.png: …, the decoder's message), a mode outside the grammar (placeholder(2).mode: unexpected "blurhash" …), and a placeholder where an image does not go (css.color(placeholder(…)), an error in the types and at build time).

Limits

  • In dev, an edited image updates the CSS of the modules that use it (dependsOn of cssints/plugin); a missing image that you add after the build error needs an edit of the module or a reload.
  • The gradient value is a list of images, so it fits a property that takes a list (background-image, mask-image); a property that takes one <image> (list-style-image, border-image-source) rejects it at build time: use thumbhash there.
  • No WebP or AVIF decoder (a WASM one would add a few hundred kB to the build); the grid size and the thumbhash size are fixed; no blurhash mode.
  • The image is decoded whole: a very large JPEG (tens of megapixels) costs its pixels in memory once per build.
cd packages/placeholder && bun run test        # colour extraction on synthetic pixels, fixtures through the engine (output, errors)
cd packages/placeholder && bun run typecheck   # and the type test, test/types/placeholder.ts

Credits

The gradient layout follows unpic-placeholder (MIT, Matt Kane). The thumbhash mode uses thumbhash (MIT, Evan Wallace), jpeg-js (BSD-3-Clause) and pngjs (MIT), dependencies. No code is copied.

版本与发布记录

@cssints/placeholder 共发布 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/placeholder 的常见问题

@cssints/placeholder 是什么?

Image placeholders for cssints: a gradient of the dominant colours, or a thumbhash data URL, read from the image at build time

如何安装 @cssints/placeholder?

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

@cssints/placeholder 支持 TypeScript 吗?

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

@cssints/placeholder 支持 ESM 还是 CommonJS?

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

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

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

@cssints/placeholder 可以商用吗?

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

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

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

@cssints/placeholder 还在维护吗?

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