给原生滚动加上样式化的滚动条、边缘指示和跟随到末尾的行为。

当内容需要有自己的滚动区域,并且滚动条要匹配你的设计时,使用 Scroll Area。滚动仍由浏览器完成,所以转轮、触摸、键盘和在页面中查找都和平时一样。

import "@ui-sdk/web/scroll-area"

const tags = Array.from(
  { length: 50 },
  (_, index) => `v1.2.0-beta.${50 - index}`
)

function mount(root: HTMLElement) {
  root.innerHTML = `
    <ui-scroll-area>
      <ui-scroll-area-viewport role="region" aria-label="Tags">
        <ui-scroll-area-content>
          <h3>Tags</h3>
          <ul>
            ${tags.map((tag) => `<li>${tag}</li>`).join("")}
          </ul>
        </ui-scroll-area-content>
      </ui-scroll-area-viewport>
      <ui-scroll-area-scrollbar>
        <ui-scroll-area-thumb></ui-scroll-area-thumb>
      </ui-scroll-area-scrollbar>
    </ui-scroll-area>
  `
}

export { mount }
用转轮、键盘或滚动条滑块滚动标签。指针在列表上时,滚动条会显示。

安装

npm install @ui-sdk/web @ui-sdk/core

导入组件会定义它的元素。在服务端,导入不会做任何事。

import "@ui-sdk/web/scroll-area"

组合

用下面的组合构建 ui-scroll-area:

ui-scroll-area
├── ui-scroll-area-viewport
│   └── ui-scroll-area-content
└── ui-scroll-area-scrollbar
    └── ui-scroll-area-thumb

把内容放进视口,然后为每个你想设置样式的轴添加一个滚动条。

视口才是真正负责滚动的元素。给根部件一个大小,让视口占满根部件的宽度和高度。滚动条覆盖在视口边缘上。某个轴溢出时,滑块会出现;type 控制滚动条何时可见。只渲染你想要的滚动条:一个都不渲染时,浏览器自己的滚动条会保留。

scroll-to-end 部件会包住你自己的 <button>,所以它保留原生按钮的一切行为。其他部件就是元素本身,显示为块级元素。

用法

导入元素并把它们组合起来:

<script type="module">
  import "@ui-sdk/web/scroll-area"
</script>

<ui-scroll-area>
  <ui-scroll-area-viewport role="region" aria-label="Release notes">
    <ui-scroll-area-content>Content to scroll</ui-scroll-area-content>
  </ui-scroll-area-viewport>
  <ui-scroll-area-scrollbar orientation="vertical">
    <ui-scroll-area-thumb></ui-scroll-area-thumb>
  </ui-scroll-area-scrollbar>
</ui-scroll-area>

横向

横向滚动条滚动内联轴。有了它,内容会按需要的宽度伸展,而不是换行。这里 type="auto" 让滚动条保持可见,所以这一行看起来就是可滚动的。

import "@ui-sdk/web/scroll-area"

const swatches = [
  { name: "Sand", color: "#e6d8bf" },
  { name: "Clay", color: "#c98b6b" },
  { name: "Rust", color: "#a4502f" },
  { name: "Moss", color: "#7d8b5a" },
  { name: "Sage", color: "#b5c2a3" },
  { name: "Fog", color: "#c9d1d6" },
  { name: "Slate", color: "#5b6b78" },
  { name: "Ink", color: "#2b2f36" },
]

function mount(root: HTMLElement) {
  root.innerHTML = `
    <ui-scroll-area type="auto">
      <ui-scroll-area-viewport role="region" aria-label="Palette">
        <ui-scroll-area-content>
          <ul>
            ${swatches
              .map(
                (swatch) => `
                  <li>
                    <div data-slot="swatch" style="background: ${swatch.color}"></div>
                    ${swatch.name}
                  </li>`
              )
              .join("")}
          </ul>
        </ui-scroll-area-content>
      </ui-scroll-area-viewport>
      <ui-scroll-area-scrollbar orientation="horizontal">
        <ui-scroll-area-thumb></ui-scroll-area-thumb>
      </ui-scroll-area-scrollbar>
    </ui-scroll-area>
  `
}

export { mount }
拖动滚动条,或按住 Shift 使用转轮,把调色板从 Sand 滚动到 Ink。

双轴

两条滚动条都出现时,各自会在它们相交的角落前停住,这个角落由 <ui-scroll-area-corner> 填补。

import "@ui-sdk/web/scroll-area"

const releases = [
  {
    version: "v1.12.0",
    date: "Oct 28, 2026",
    status: "Latest",
    downloads: "18,204",
    size: "4.3 MB",
    node: "20+",
  },
  {
    version: "v1.11.2",
    date: "Oct 14, 2026",
    status: "Stable",
    downloads: "26,931",
    size: "4.2 MB",
    node: "20+",
  },
  {
    version: "v1.11.1",
    date: "Oct 2, 2026",
    status: "Stable",
    downloads: "9,412",
    size: "4.2 MB",
    node: "20+",
  },
  {
    version: "v1.11.0",
    date: "Sep 23, 2026",
    status: "Stable",
    downloads: "31,077",
    size: "4.2 MB",
    node: "20+",
  },
  {
    version: "v1.10.3",
    date: "Sep 9, 2026",
    status: "Stable",
    downloads: "12,640",
    size: "4.1 MB",
    node: "18+",
  },
  {
    version: "v1.10.2",
    date: "Aug 27, 2026",
    status: "Stable",
    downloads: "8,315",
    size: "4.1 MB",
    node: "18+",
  },
  {
    version: "v1.10.1",
    date: "Aug 18, 2026",
    status: "Stable",
    downloads: "5,902",
    size: "4.1 MB",
    node: "18+",
  },
  {
    version: "v1.10.0",
    date: "Aug 5, 2026",
    status: "Stable",
    downloads: "22,486",
    size: "4.0 MB",
    node: "18+",
  },
  {
    version: "v1.9.4",
    date: "Jul 21, 2026",
    status: "Deprecated",
    downloads: "3,118",
    size: "3.9 MB",
    node: "18+",
  },
  {
    version: "v1.9.3",
    date: "Jul 8, 2026",
    status: "Deprecated",
    downloads: "2,764",
    size: "3.9 MB",
    node: "18+",
  },
  {
    version: "v1.9.2",
    date: "Jun 24, 2026",
    status: "Deprecated",
    downloads: "1,950",
    size: "3.9 MB",
    node: "18+",
  },
  {
    version: "v1.9.1",
    date: "Jun 10, 2026",
    status: "Deprecated",
    downloads: "1,208",
    size: "3.8 MB",
    node: "18+",
  },
]

function mount(root: HTMLElement) {
  root.innerHTML = `
    <ui-scroll-area type="auto">
      <ui-scroll-area-viewport role="region" aria-label="Releases">
        <ui-scroll-area-content>
          <table>
            <thead>
              <tr>
                <th>Version</th>
                <th>Released</th>
                <th>Status</th>
                <th>Downloads</th>
                <th>Size</th>
                <th>Node</th>
              </tr>
            </thead>
            <tbody>
              ${releases
                .map(
                  (release) => `
                    <tr>
                      <th scope="row">${release.version}</th>
                      <td>${release.date}</td>
                      <td>${release.status}</td>
                      <td>${release.downloads}</td>
                      <td>${release.size}</td>
                      <td>${release.node}</td>
                    </tr>`
                )
                .join("")}
            </tbody>
          </table>
        </ui-scroll-area-content>
      </ui-scroll-area-viewport>
      <ui-scroll-area-scrollbar>
        <ui-scroll-area-thumb></ui-scroll-area-thumb>
      </ui-scroll-area-scrollbar>
      <ui-scroll-area-scrollbar orientation="horizontal">
        <ui-scroll-area-thumb></ui-scroll-area-thumb>
      </ui-scroll-area-scrollbar>
      <ui-scroll-area-corner></ui-scroll-area-corner>
    </ui-scroll-area>
  `
}

export { mount }
把发布说明向下、向横滚动。表头行保持不动,滚动条在角落前停住。

边缘渐隐

根部件保存内容越过各个边缘的距离,用 --scroll-area-overflow-y-start、--scroll-area-overflow-y-end 和它们的 x 版本表示;也保存视图走了多远,用 --scroll-area-progress-y 和 -x 表示。高度为 min(2rem, var(--scroll-area-overflow-y-start)) 的渐隐在顶部时消失,上方还有内容时则完整显示。对于只需要切换的渐隐,使用 data-at-y-start 和 data-at-y-end。

这些变量不会被继承,所以滚动只会改变根部件的样式,绝不会改变内容的样式。读取这些变量的元素通过 inherit 从根部件取值,就像这里的渐隐,它们是根部件的子元素:--scroll-area-overflow-y-start: inherit。第一个滚动区域会用 CSS.registerProperty 为整个页面注册这六个名称。需要它们可继承的页面要先调用它,并传入 inherits: true。@property 规则做不到,因为脚本中的注册会优先。

import "@ui-sdk/web/scroll-area"

const notes = [
  {
    version: "v1.12.0",
    date: "Oct 28",
    text: "Scroll areas follow streamed content and stop when you scroll up.",
  },
  {
    version: "v1.11.2",
    date: "Oct 14",
    text: "Focus returns to the right trigger when a nested dialog closes.",
  },
  {
    version: "v1.11.1",
    date: "Oct 2",
    text: "Date pickers keep the selected month when the locale changes.",
  },
  {
    version: "v1.11.0",
    date: "Sep 23",
    text: "Menus open on Arrow Down and skip disabled items while typing.",
  },
  {
    version: "v1.10.3",
    date: "Sep 9",
    text: "Toasts pause their timers while the window is in the background.",
  },
  {
    version: "v1.10.2",
    date: "Aug 27",
    text: "Sliders snap to the nearest step when dragged past either end.",
  },
  {
    version: "v1.10.1",
    date: "Aug 18",
    text: "Tooltips wait for a short delay before moving to a new trigger.",
  },
  {
    version: "v1.10.0",
    date: "Aug 5",
    text: "Comboboxes can load their options while you type.",
  },
]

function mount(root: HTMLElement) {
  root.innerHTML = `
    <ui-scroll-area>
      <ui-scroll-area-viewport role="region" aria-label="Release notes">
        <ui-scroll-area-content>
          ${notes
            .map(
              (note) => `
                <article>
                  <h3>${note.version} <span data-slot="hint">${note.date}</span></h3>
                  <p>${note.text}</p>
                </article>`
            )
            .join("")}
        </ui-scroll-area-content>
      </ui-scroll-area-viewport>
      <div data-slot="top-fade"></div>
      <div data-slot="bottom-fade"></div>
      <ui-scroll-area-scrollbar>
        <ui-scroll-area-thumb></ui-scroll-area-thumb>
      </ui-scroll-area-scrollbar>
    </ui-scroll-area>
  `
}

export { mount }
滚动发布说明。离开顶部时,顶部渐隐会逐渐出现;到达末尾时,底部渐隐会逐渐消失。

原生滚动条

省掉滚动条元素,就能保留浏览器自己的滚动条,同时保留其他一切:数据属性、边缘变量、贴住末尾和控制器。

import "@ui-sdk/web/scroll-area"

const messages = [
  { from: "Hollis Grant", subject: "Launch checklist for Thursday" },
  { from: "Inès Moreau", subject: "Updated pricing page copy" },
  { from: "Kofi Mensah", subject: "Design review notes" },
  { from: "Wren Takahashi", subject: "Q4 roadmap draft" },
  { from: "Tomasz Wójcik", subject: "Re: Staging deploy failed" },
  { from: "Hollis Grant", subject: "Invoices for October" },
  { from: "Inès Moreau", subject: "Customer interview recordings" },
  { from: "Kofi Mensah", subject: "New icons for the dashboard" },
  { from: "Wren Takahashi", subject: "Offsite agenda" },
  { from: "Tomasz Wójcik", subject: "Database migration plan" },
]

function mount(root: HTMLElement) {
  root.innerHTML = `
    <ui-scroll-area>
      <ui-scroll-area-viewport role="region" aria-label="Inbox">
        <ui-scroll-area-content>
          <ul>
            ${messages
              .map(
                (message) => `
                  <li>
                    ${message.from}
                    <span data-slot="hint">${message.subject}</span>
                  </li>`
              )
              .join("")}
          </ul>
        </ui-scroll-area-content>
      </ui-scroll-area-viewport>
    </ui-scroll-area>
  `
}

export { mount }
用浏览器自己的滚动条滚动收件箱。滚动区域仍然跟踪自己的边缘和状态。

滚动条何时显示

type 决定每个滚动条何时获得 data-visible:

type何时显示
"hover" (default)指针在滚动区域上时,或正在滚动时
"scroll"它自己的轴正在滚动时,或它的滑块正被拖动时
"auto"内容在它的轴上溢出时
"always"始终显示,即使内容没有溢出;此时没有滑块

给这个属性设置样式:让滚动条 opacity: 0,在 [data-visible] 下用 opacity: 1。隐藏的滚动条会忽略指针,所以它绝不会接住原本给下方内容的按压。

import "@ui-sdk/web/scroll-area"

const types = ["hover", "scroll", "auto", "always"]

const tags = Array.from(
  { length: 20 },
  (_, index) => `v1.2.0-beta.${50 - index}`
)

function mount(root: HTMLElement) {
  root.innerHTML = `
    <div data-slot="grid">
      ${types
        .map(
          (type) => `
            <div data-slot="stack">
              <code>type="${type}"</code>
              <ui-scroll-area type="${type}">
                <ui-scroll-area-viewport>
                  <ui-scroll-area-content>
                    <ul>
                      ${tags.map((tag) => `<li>${tag}</li>`).join("")}
                    </ul>
                  </ui-scroll-area-content>
                </ui-scroll-area-viewport>
                <ui-scroll-area-scrollbar>
                  <ui-scroll-area-thumb></ui-scroll-area-thumb>
                </ui-scroll-area-scrollbar>
              </ui-scroll-area>
            </div>`
        )
        .join("")}
    </div>
  `
}

export { mount }
逐个悬停和滚动每个列表来比较。auto 和 always 的滚动条从一开始就显示;hover 等待指针,scroll 等待滚动。

隐藏延迟

使用 "scroll" 和 "hover" 时,scroll-hide-delay 会在滚动停止或指针离开后再保留滚动条一段时间,单位是毫秒。默认值为 600。

import "@ui-sdk/web/scroll-area"

const delays = [
  { value: 0, label: "No delay" },
  { value: 2000, label: "2000 ms delay" },
]

const tags = Array.from(
  { length: 20 },
  (_, index) => `v1.2.0-beta.${50 - index}`
)

function mount(root: HTMLElement) {
  root.innerHTML = `
    <div data-slot="grid">
      ${delays
        .map(
          (delay) => `
            <div data-slot="stack">
              <span data-slot="hint">${delay.label}</span>
              <ui-scroll-area type="scroll" scroll-hide-delay="${delay.value}">
                <ui-scroll-area-viewport>
                  <ui-scroll-area-content>
                    <ul>
                      ${tags.map((tag) => `<li>${tag}</li>`).join("")}
                    </ul>
                  </ui-scroll-area-content>
                </ui-scroll-area-viewport>
                <ui-scroll-area-scrollbar>
                  <ui-scroll-area-thumb></ui-scroll-area-thumb>
                </ui-scroll-area-scrollbar>
              </ui-scroll-area>
            </div>`
        )
        .join("")}
    </div>
  `
}

export { mount }
滚动每个列表然后停下。左边的滚动条立刻隐藏;右边的滚动条等待 2000 毫秒。

控制器

元素的 api可以从任何地方滚动这个区域,并读取它当前的位置。

控制台
import "@ui-sdk/web/scroll-area"
import { log } from "./log"

const tags = Array.from(
  { length: 50 },
  (_, index) => `v1.2.0-beta.${50 - index}`
)

function mount(root: HTMLElement) {
  root.innerHTML = `
    <div data-slot="stack">
      <div data-slot="controls">
        <button type="button" data-action="start">Back to Top</button>
        <button type="button" data-action="end">Jump to the End</button>
      </div>
      <ui-scroll-area>
        <ui-scroll-area-viewport role="region" aria-label="Tags">
          <ui-scroll-area-content>
            <h3>Tags</h3>
            <ul>
              ${tags.map((tag) => `<li>${tag}</li>`).join("")}
            </ul>
          </ui-scroll-area-content>
        </ui-scroll-area-viewport>
        <ui-scroll-area-scrollbar>
          <ui-scroll-area-thumb></ui-scroll-area-thumb>
        </ui-scroll-area-scrollbar>
      </ui-scroll-area>
    </div>
  `
  const { api } = root.querySelector("ui-scroll-area")!
  let edge: string | null = "start"
  root.querySelector('[data-action="start"]')!.addEventListener("click", () => {
    api.scrollToStart({ behavior: "smooth" })
  })
  root.querySelector('[data-action="end"]')!.addEventListener("click", () => {
    api.scrollToEnd({ behavior: "instant" })
  })
  root
    .querySelector("ui-scroll-area-viewport")!
    .addEventListener("scroll", () => {
      const next = api.isAtEnd() ? "end" : api.isAtStart() ? "start" : null
      if (next && next !== edge) {
        log(root, `At the ${next}`)
      }
      edge = next
    })
}

export { mount }
按压 Jump to the End,再按压 Back to Top。视图到达列表每一端时,控制台会记录。

对于聊天或日志,设置 stick-to-end 从底部开始,并在用户向上滚动之前让新内容保持可见;<ui-scroll-area-scroll-to-end> 包住一个按钮,可以把视图带回底部。

指针

  • 拖动滑块 会按滑块在轨道上移动的距离移动内容。文字不会被选中,吸附会暂停,直到指针抬起,Escape 会把内容放回拖动开始的位置。
  • 按压轨道 会像原生滚动条一样朝指针方向翻页,每次大约一个视口的大部分;保持按压会继续翻页,直到滑块到达指针处。按住 Alt(Mac 上是 Option)时,则直接跳到那里并改为拖动。
  • 在滚动条上使用转轮 会让视口滚动,距离与在内容上使用转轮相同;视口不能再滚动后,就滚动页面。
  • 按压滚动条 永远不会移动焦点。

自定义渲染

在部件上设置 as-child,把它绑定到它的第一个子元素,比如 <ui-scroll-area as-child><section>…</section></ui-scroll-area>。元素没有渲染函数,所以依赖状态的内容读取部件的数据属性。

这里根部件绑定一个 section,视口绑定一个 article。提示里包含两条消息,根部件的 data-at-start 和 data-at-end 会在到达末尾之前显示正确的那条。选择部件绑定到哪个元素见部件与组合,每个部件的状态见数据属性。

import "@ui-sdk/web/scroll-area"

const tags = Array.from(
  { length: 50 },
  (_, index) => `v1.2.0-beta.${50 - index}`
)

function mount(root: HTMLElement) {
  root.innerHTML = `
    <ui-scroll-area as-child>
      <section>
        <ui-scroll-area-viewport as-child aria-label="Releases">
          <article>
            <ui-scroll-area-content>
              <ul>
                ${tags.map((tag) => `<li>${tag}</li>`).join("")}
              </ul>
            </ui-scroll-area-content>
          </article>
        </ui-scroll-area-viewport>
        <p data-slot="hint" aria-hidden="true">
          <span data-show="start">Scroll for older releases</span>
          <span data-show="more">More releases below</span>
        </p>
        <ui-scroll-area-scrollbar>
          <ui-scroll-area-thumb></ui-scroll-area-thumb>
        </ui-scroll-area-scrollbar>
      </section>
    </ui-scroll-area>
  `
}

export { mount }
滚动发布说明。列表下方的提示读取根部件的状态,离开顶部后会变化,到达末尾时消失。

样式

给根部件一个大小,让视口占满根部件的宽度和高度;各部件会设置自己需要的定位和溢出。给每个滚动条一个宽度或高度,并在 data-visible 下显示它。滑块会自行确定大小并移动。渐隐和阴影读取根部件的 CSS 变量。

每个部件都有一个 data-slot,比如 scroll-area-thumb,它的状态属性列在数据属性下。

<ui-scroll-area>

变量内容
--scroll-area-overflow-y-startHow far the content reaches above the view, in pixels. Grow a top fade or shadow with it. Not inherited, like the other overflow and progress variables, so scrolling never restyles the content. A child of the root that reads it takes it with --scroll-area-overflow-y-start: inherit.
--scroll-area-overflow-y-endHow far the content reaches below the view, in pixels.
--scroll-area-overflow-x-startHow far the content reaches past the inline start, in pixels, left in LTR and right in RTL.
--scroll-area-overflow-x-endHow far the content reaches past the inline end, in pixels.
--scroll-area-progress-yHow far down the view is, from 0 at the top to 1 at the bottom. A reading progress bar can scale with it.
--scroll-area-progress-xHow far along the inline axis the view is, from 0 to 1.
--scroll-area-corner-widthThe vertical scrollbar's width while both scrollbars show, so the horizontal one stops short of it. 0px otherwise.
--scroll-area-corner-heightThe horizontal scrollbar's height while both show, so the vertical one stops short of it. 0px otherwise.

<ui-scroll-area-scrollbar>

变量内容
--scroll-area-thumb-heightOn a vertical scrollbar: its thumb's length in pixels. The thumb's inline height reads it.
--scroll-area-thumb-widthOn a horizontal scrollbar: its thumb's length in pixels. The thumb's inline width reads it.
--scroll-area-thumb-offsetHow far its thumb sits from the track's start, in pixels, negative right to left. The thumb's inline transform reads it.

数据属性

<ui-scroll-area-corner> 没有状态属性。

<ui-scroll-area>

[data-slot="scroll-area"]

属性含义
data-overflow-xPresent while the content is wider than the viewport.
data-overflow-yPresent while the content is taller than the viewport.
data-at-startPresent while the view is at the start: the top, or the inline start when only the horizontal axis overflows. Also when nothing overflows.
data-at-endPresent while the view is at the end: the bottom, or the inline end when only the horizontal axis overflows.
data-at-x-startPresent while the view is at the inline start, within a pixel.
data-at-x-endPresent while the view is at the inline end, within a pixel.
data-at-y-startPresent while the view is at the top, within a pixel.
data-at-y-endPresent while the view is at the bottom, within a pixel.
data-scrollingPresent while the user scrolls, and for a moment after. Keeping the end in view doesn't count.
data-hoveringPresent while a mouse or pen is over the scroll area.
data-stickingPresent while the view follows the end as content grows, with stickToEnd.

<ui-scroll-area-viewport>

[data-slot="scroll-area-viewport"]

属性含义
data-overflow-yPresent while the content is taller than the viewport.
data-at-startPresent while the view is at the start: the top, or the inline start when only the horizontal axis overflows. Also when nothing overflows.
data-at-endPresent while the view is at the end: the bottom, or the inline end when only the horizontal axis overflows.
data-scrollingPresent while the user scrolls, and for a moment after. Keeping the end in view doesn't count.
data-stickingPresent while the view follows the end as content grows, with stickToEnd.

<ui-scroll-area-content>

[data-slot="scroll-area-content"]

属性含义
data-overflow-yPresent while the content is taller than the viewport.
data-at-startPresent while the view is at the start: the top, or the inline start when only the horizontal axis overflows. Also when nothing overflows.
data-at-endPresent while the view is at the end: the bottom, or the inline end when only the horizontal axis overflows.

<ui-scroll-area-scrollbar>

[data-slot="scroll-area-scrollbar"]

属性含义
data-orientationverticalhorizontalThe axis the scrollbar scrolls.
data-visiblePresent while the scrollbar should show, by type. Hidden scrollbars let presses through.
data-scrollingPresent while its own axis scrolls.
data-hoveringPresent while a mouse or pen is over the scroll area.
data-draggingPresent while the thumb is dragged.
data-overflow-yPresent while the content is taller than the viewport.

<ui-scroll-area-thumb>

[data-slot="scroll-area-thumb"]

属性含义
data-orientationverticalhorizontalThe axis the scrollbar scrolls.
data-draggingPresent while the thumb is dragged.

<ui-scroll-area-scroll-to-end>

[data-slot="scroll-area-scroll-to-end"]

属性含义
data-visiblePresent while the view is away from the end and not heading there. Otherwise the button is inert.

API 参考

props 是 kebab 写法的属性,比如 scroll-hide-delay。元素没有 controller attribute:它的 api property 就是控制器,controller property 会再附加一个。

ScrollArea

type?ScrollAreaType
When the scrollbars show: "auto" whenever the content overflows, "always" even when it doesn't, "scroll" while scrolling, and "hover" while scrolling or while the pointer is over the scroll area. Each scrollbar has data-visible while it should show; style that.
scrollHideDelay?number
How long scrollbars stay after they stop being needed, in ms, with "scroll" and "hover".
stickToEnd?boolean
Keeps the end in view while the content grows, like a chat or streamed output: it starts at the bottom and stays there. Scrolling toward the top lets go, and scrolling back to the bottom sticks again.
controller?{ scrollToEnd: (options?: ScrollAreaScrollOptions) => void; scrollToStart: (options?: ScrollAreaScrollOptions) => void; isAtEnd: () => boolean; isAtStart: () => boolean; isSticking: () => boolean; }
Drives it from anywhere. Create one with createScrollAreaController().
debug?boolean
Logs every state change with its reason to the console, in development builds.

ScrollAreaScrollbar

orientation?"horizontal" | "vertical"
The axis the scrollbar scrolls.

ScrollAreaViewport、ScrollAreaContent、ScrollAreaThumb、ScrollAreaCorner 和 ScrollAreaScrollToEnd

这些部件没有自己的选项,只有它们渲染的元素的 attribute。滑块跟随其滚动条的方向。

状态

同样的状态会在每个元素上显示为数据属性。根部件、视口和内容会显示滚动区域的状态。

overflowXboolean
Whether the content is wider than the viewport.
overflowYboolean
Whether the content is taller than the viewport.
atStartboolean
Whether the view is at the start of the scroll area: the top, or the inline start when only the horizontal axis overflows.
atEndboolean
Whether the view is at the end of the scroll area: the bottom, or the inline end when only the horizontal axis overflows.
scrollingboolean
Whether the user is scrolling now.
hoveringboolean
Whether a mouse or pen is over the scroll area.
stickingboolean
Whether the view follows the end as the content grows, with stickToEnd.

ScrollAreaScrollbar:

orientation"vertical" | "horizontal"
The axis it scrolls.
visibleboolean
Whether it should show now, by type.
scrollingboolean
Whether its axis is scrolling now.
hoveringboolean
Whether a mouse or pen is over the scroll area.
draggingboolean
Whether its thumb is being dragged.

ScrollAreaThumb:

orientation"vertical" | "horizontal"
The axis it scrolls.
draggingboolean
Whether it's being dragged.

ScrollAreaScrollToEnd:

visibleboolean
Whether it should show: the view is away from the end and isn't heading there.

控制器

元素的 api property 有相同的方法。

scrollToEnd(options?)(options?: ScrollAreaScrollOptions | undefined) => void
Scrolls to the end: the bottom, or the inline end when only the horizontal axis overflows. With stickToEnd, it sticks to the end again.
scrollToStart(options?)(options?: ScrollAreaScrollOptions | undefined) => void
Scrolls to the start: the top, or the inline start when only the horizontal axis overflows.
isAtEnd()() => boolean
Whether the view is at the end now, even in a scroll event, before the attributes catch up.
isAtStart()() => boolean
Whether the view is at the start now, even in a scroll event, before the attributes catch up.
isSticking()() => boolean
Whether the view follows the end as the content grows, with stickToEnd.

scrollToEnd 和 scrollToStart 接受一个可选的 { behavior }:

behavior?"instant" | "smooth"
"smooth" eases there, following content that keeps growing. It's instant when the user prefers reduced motion.

无障碍

没有 WAI-ARIA 模式覆盖滚动区域:视口本身是原生滚动元素,键盘访问遵循 axe 的 scrollable-region-focusable 规则。滚动条和角落对屏幕阅读器隐藏,由屏幕阅读器自己滚动视口。值得被找到时,用 role="region" 和 aria-label 把视口变成地标,或者给聊天加上 role="log"。

键盘

视口以原生方式滚动,所以焦点进入视口后,方向键、Page Up 和 Page Down、Home、End 和 Space 都能用。一个会滚动、里面又没有任何可聚焦内容的视口本身就是 Tab 停靠点,键盘用户因此能到达它;里面放有链接或字段的视口则不是,因为 Tab 会到达它们,由它们来滚动视口。

经过测试的行为

下面每一行都是一个自动化测试。

Thumb
  • The wheel scrolls the viewport and moves the thumb.
  • The thumb is as long as the visible share of the content.
  • At the end of the content, the thumb is at the end of its track.
  • Dragging the thumb scrolls the content as far along.
  • Dragging the thumb past the track's end stops at the end.
  • Dragging the thumb selects no text.
  • The horizontal thumb follows horizontal scrolling.
  • The thumb shrinks as the content grows.
  • An image loading late resizes the thumb.
Track
  • Pressing the track after the thumb scrolls toward the press.
  • Pressing the track before the thumb scrolls back toward the press.
  • Holding the track keeps paging until the thumb reaches the pointer.
  • Alt with a press on the track jumps there.
  • Pressing a scrollbar leaves focus where it was.
  • A wheel over a scrollbar scrolls the viewport, not the page.
  • A wheel over a scrollbar at the end scrolls the page.
Overflow
  • Custom scrollbars hide the native ones.
  • The root says while the user scrolls, and stops after.
  • Content that fits shows no scrollbar.
  • Content that overflows both ways shows both scrollbars and the corner between them.
Keyboard
  • A viewport that scrolls and holds nothing focusable is a tab stop.
  • Keys scroll a focused viewport natively.
  • A viewport that doesn't scroll is no tab stop.
  • Focusing content scrolls it into view.
Web components
  • A scrollbar whose orientation changes sizes its thumb along the new axis. 仅在 Web components 运行。

运行于 React、Vue 和 web components。

基线之外:

  • A scroll area in another document times its scrollbars with that document's window. 仅在 Web components 运行。
Track
  • Pressing the track pages by most of a viewport, like a native scrollbar.
  • Escape during a thumb drag puts the content back.
Keyboard
  • A viewport with focusable content is no tab stop: Tab goes to the content.
Native scrollbars
  • Without custom scrollbars, the native ones stay.
  • Hiding native scrollbars injects no style element.
State
  • The root says which axes overflow and which edges the view is at.
  • CSS variables say how far the content reaches past each edge, and the progress.
  • The variables don't inherit, and a child of the root takes them with inherit.
Scrollbar types
  • Hover shows the scrollbar while the pointer is over the area, then hides it after the delay.
  • Scroll shows the scrollbar only while scrolling, not on hover.
  • Scroll shows only the scrollbar of the axis that scrolls.
  • Auto shows the scrollbar whenever the content overflows.
  • Always shows the scrollbar even when the content fits.
  • A hidden scrollbar lets presses through to the content under it.
Sticking to the end
  • It starts at the end.
  • New messages, streamed words and late images keep the end in view.
  • A wheel toward the start lets go, and growth no longer moves the view.
  • Content streaming in during a wheel toward the start doesn't pull the view back.
  • PageUp in the viewport lets go.
  • A wheel toward the start inside a scroller in the content keeps following.
  • A key a widget inside uses keeps following.
  • The controller reads where the view is from a scroll listener. 仅在 Web components 运行。
  • A scroll toward the start from the app or find in page lets go too.
  • Scrolling back to the end follows again.
  • Dragging the thumb toward the start lets go, and back to the end follows.
  • Keeping the end in view never scrolls the page.
  • Keeping the end in view doesn't flash the scrollbars as scrolling.
  • A smooth scroll-behavior doesn't slow down keeping the end in view.
  • Text selection in progress isn't pulled away by growth.
Jump to latest
  • It's out of reach at the end, and shows once the view moves away.
  • It scrolls to the end, follows the end again, and hides at once.
  • From the keyboard, focus moves to the viewport instead of the page.
  • With reduced motion, it jumps at once.
Hidden scrollbars
  • A scrollbar shows its thumb as soon as it's hovered.

运行于 React、Vue 和 web components。

在每个经过测试的框架里,屏幕阅读器听到的内容相同,由虚拟屏幕阅读器记录:

  • Reading a scroll area announces its region and content, and no scrollbar.

运行于 React、Vue 和 web components。