@cssints/stuck

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

Style a sticky element while it is stuck: @container scroll-state(stuck) natively, an IntersectionObserver fallback

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

@cssints/stuck

Style what is inside a sticky element while it is stuck, for cssints: sticky() makes an element sticky and a scroll-state container, stuck(...) wraps styles of its descendants in @container scroll-state(stuck: top) (Chrome 133). Where a target lacks scroll-state queries, stuck(...) also emits the same styles under an attribute that a tiny IntersectionObserver sets, in the way of bramus/sticky-observer.

import { cn, color, hover } from "cssints" with { type: "cssints" };
import { sticky, stuck } from "@cssints/stuck" with { type: "cssints" };

export const header = sticky().top("0"); // <header class={header}>
export const title = cn(color("gray"), stuck(color("red"))); //   <h1 class={title}>
export const footer = sticky().bottom(2);
export const link = stuck("bottom")(color("green"), hover(color("black")));
@layer _.k {
  ._stuck-core {
    position: sticky;
    container-type: scroll-state;
  }
}
@layer _.a {
  ._4k81c2 {
    top: 0;
  }
  ._kg6w97 {
    bottom: 0.5rem;
  }
}
@layer _.b {
  @container scroll-state(stuck: top) {
    ._it4l7h {
      color: red;
    }
  }
  /* only when a browserslist target lacks scroll-state queries */
  :where(._g-stuck[data-stuck~="top"]) ._fec935 {
    color: red;
  }
}

header is _stuck-core _g-stuck _4k81c2, title holds both _it4l7h and _fec935.

Browsers

  • Native: @container scroll-state() and container-type: scroll-state, Chrome and Edge 133. Not in Firefox or Safari yet.
  • Fallback: @cssints/stuck/observer (about 1.3 kB minified, no imports) sets data-stuck="top" (a list of the stuck edges) on each sticky() element while it is stuck, and the fallback rules read it. It checks CSS.supports("container-type", "scroll-state") first and does nothing where the browser has the queries. Nothing to import by hand: the runtime entry names it (script), so the module that uses sticky() imports it through virtual:cssints/runtime/stuck.observer when a target lacks the queries; that module is empty when none does.
  • The fallback rules are in the sheet only when a browserslist target lacks the queries (chrome 133 alone gets the @container rules only): stuck() asks lacking() of cssints/plugin, the same query and rules as the support warnings. The kernel's support warnings (its requires, the queries, and its declaration container-type: scroll-state) are covered by the observer, so a build for the default targets prints none.
  • Checked in Chrome (native, and the observer with the queries cut out), Firefox and WebKit (the observer): bun run test:browsers.

Rules

  • sticky() is position: sticky and container-type: scroll-state, with methods for the insets, each once, in any order: top(), right(), bottom(), left(), a <length-percentage> (a lone number is a spacing step, 0.25rem). Its class list also holds _g-stuck, the group marker the fallback rules read.
  • stuck(...styles) applies the styles while the nearest sticky() ancestor is stuck to its top edge; stuck(side)(...styles) to top, right, bottom or left. It is a condition: it counts as one (layer _.b), wraps others (stuck(hover(...))) and goes inside them.
  • Descendants only. A scroll-state query matches the descendants of the container, never the container itself (the spec's rule), so stuck() styles go on an element inside the sticky one. To style the sticky box itself, put an inner wrapper in it.
  • Conflicts. sticky() sets position and container-type: one cn() with position, container or container-type is a build error, derived from the kernel (written as styles): cssints: stuck: kernel core sets position, container-type (this style also sets position).
  • Errors: a bad inset is the grammar's, in the types and at build time (sticky.top: unexpected "red" in "red", expected <length-percentage>), a method twice is cssints: sticky().top() is used twice, and an unknown side is a type error and cssints: stuck: stuck() takes top, right, bottom or left, got "middle". Each points at the call.

Limits

  • The fallback reads the insets once, when it first sees the element: an inset that a media query changes later is not followed. A sticky element taller than its scroll container is not reported stuck. Shadow roots are not crossed.
  • The fallback's root is the nearest ancestor whose overflow is not visible, else the viewport; the native query follows the real scroll container.
  • No logical sides (block-start, inline-end), and no scrollable or snapped scroll-state queries.
cd packages/stuck && bun run test            # fixtures through the engine (output, targets, errors, observer size)
cd packages/stuck && bun run typecheck       # the type test
cd packages/stuck && bun run test:browsers   # Chrome, Firefox and WebKit through playwright-core

Credits

The fallback follows bramus/sticky-observer (MIT, Bramus Van Damme): an IntersectionObserver whose root is shrunk by the element's inset plus 1px. The observer is written here; no code is copied.

版本与发布记录

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

@cssints/stuck 是什么?

Style a sticky element while it is stuck: @container scroll-state(stuck) natively, an IntersectionObserver fallback

如何安装 @cssints/stuck?

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

@cssints/stuck 支持 TypeScript 吗?

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

@cssints/stuck 支持 ESM 还是 CommonJS?

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

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

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

@cssints/stuck 可以商用吗?

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

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

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

@cssints/stuck 还在维护吗?

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