網站導覽

v2.1.0
結構

捲動區域

內容超出哪一側的邊緣,該側就會淡出。

本頁內容

捲動區域是頁面內可獨立捲動的範圍,例如面板中的長清單、寬表格或記錄。它沿用平台的原生捲動行為,搭配纖細低調的捲動條。內容超出哪一側,該側邊緣就會淡出。捲到內容盡頭時,該側邊緣會恢復清晰。閱讀期間內容會在結尾持續增加的區域,例如聊天或建置記錄,也可以跟隨新內容。

捲動區域

<div class="sheet max-w(360) mx-auto">  <div class="scroll-area h(200) p-md text-sm fg-text-body leading-md" tabindex="0" role="region" aria-label="Release notes">    <p class="m(0)">2.1 adds overlays that need no script.</p>    <p class="m(0) mt-sm">Dialogs open with invoker commands and trap focus on their own.</p>    <p class="m(0) mt-sm">      Popovers, menus and tooltips are placed with anchor positioning and flip at the viewport edge.    </p>    <p class="m(0) mt-sm">      The select draws its option list in the same material where the browser allows it.    </p>    <p class="m(0) mt-sm">Segmented controls and tabs glide one indicator between options.</p>    <p class="m(0) mt-sm">      Corners turn with continuous curvature where corner-shape is supported.    </p>    <p class="m(0) mt-sm">Every one of these falls back to a working component.</p>  </div></div>
<div className="sheet max-w(360) mx-auto">  <div className="scroll-area h(200) p-md text-sm fg-text-body leading-md" tabIndex={0} role="region" aria-label="Release notes">    <p className="m(0)">2.1 adds overlays that need no script.</p>    <p className="m(0) mt-sm">Dialogs open with invoker commands and trap focus on their own.</p>    <p className="m(0) mt-sm">      Popovers, menus and tooltips are placed with anchor positioning and flip at the viewport edge.    </p>    <p className="m(0) mt-sm">      The select draws its option list in the same material where the browser allows it.    </p>    <p className="m(0) mt-sm">Segmented controls and tabs glide one indicator between options.</p>    <p className="m(0) mt-sm">      Corners turn with continuous curvature where corner-shape is supported.    </p>    <p className="m(0) mt-sm">Every one of these falls back to a working component.</p>  </div></div>
<div class="sheet max-w(360) mx-auto">  <div class="scroll-area h(200) p-md text-sm fg-text-body leading-md" tabindex="0" role="region" aria-label="Release notes">    <p class="m(0)">2.1 adds overlays that need no script.</p>    <p class="m(0) mt-sm">Dialogs open with invoker commands and trap focus on their own.</p>    <p class="m(0) mt-sm">      Popovers, menus and tooltips are placed with anchor positioning and flip at the viewport edge.    </p>    <p class="m(0) mt-sm">      The select draws its option list in the same material where the browser allows it.    </p>    <p class="m(0) mt-sm">Segmented controls and tabs glide one indicator between options.</p>    <p class="m(0) mt-sm">      Corners turn with continuous curvature where corner-shape is supported.    </p>    <p class="m(0) mt-sm">Every one of these falls back to a working component.</p>  </div></div>
<template>  <div class="sheet max-w(360) mx-auto">    <div class="scroll-area h(200) p-md text-sm fg-text-body leading-md" tabindex="0" role="region" aria-label="Release notes">      <p class="m(0)">2.1 adds overlays that need no script.</p>      <p class="m(0) mt-sm">Dialogs open with invoker commands and trap focus on their own.</p>      <p class="m(0) mt-sm">        Popovers, menus and tooltips are placed with anchor positioning and flip at the viewport edge.      </p>      <p class="m(0) mt-sm">        The select draws its option list in the same material where the browser allows it.      </p>      <p class="m(0) mt-sm">Segmented controls and tabs glide one indicator between options.</p>      <p class="m(0) mt-sm">        Corners turn with continuous curvature where corner-shape is supported.      </p>      <p class="m(0) mt-sm">Every one of these falls back to a working component.</p>    </div>  </div></template>
捲動筆記。上方有更多內容時,頂端會出現淡出效果。捲到底時,底端的淡出效果會消失。

#淡出效果如何運作

淡出效果由遮罩繪製,兩個漸層停止點分別由自訂屬性 --scroll-area-start 和 --scroll-area-end 控制。這兩個屬性各由一段動畫驅動,兩段動畫都使用區域自己的捲動時間軸。第一段在最初 1.5rem 的捲動距離內逐漸顯示起始側的淡出效果,第二段則在最後 1.5rem 逐漸移除結束側的淡出效果。整個過程無須使用腳本或偵測用元素。內容沒有溢出時,時間軸不會啟用,兩側邊緣都保持清晰。

瀏覽器若不支援捲動驅動動畫,區域仍可照常捲動,邊緣會保持清晰。

#捲到邊界後

沿區塊軸捲到區域盡頭後,頁面會接著捲動。即使滑鼠滾輪操作或滑動手勢從區域內開始,只要該方向已無內容可捲動,就會像在頁面其他內容上操作一樣捲動頁面。只有行內軸的捲動會限制在區域內,因此側向滑到一列的盡頭時,手勢就此停止,不會觸發瀏覽器的返回手勢。

#沿行內軸捲動

scroll-area-inline 可側向捲動,並在行內起始側與結束側套用淡出效果,適合用於一排卡片或比所在欄位更寬的表格。

沿行內軸捲動

<div class="scroll-area scroll-area-inline" tabindex="0" role="region" aria-label="Regions">  <div class="flex gap-sm w-fit py(4) px(2)">    <span class="chip">Tokyo</span>    <span class="chip">Singapore</span>    <span class="chip">Sydney</span>    <span class="chip">Mumbai</span>    <span class="chip">Frankfurt</span>    <span class="chip">London</span>    <span class="chip">Paris</span>    <span class="chip">Stockholm</span>    <span class="chip">Virginia</span>    <span class="chip">Oregon</span>    <span class="chip">São Paulo</span>    <span class="chip">Cape Town</span>  </div></div>
<div className="scroll-area scroll-area-inline" tabIndex={0} role="region" aria-label="Regions">  <div className="flex gap-sm w-fit py(4) px(2)">    <span className="chip">Tokyo</span>    <span className="chip">Singapore</span>    <span className="chip">Sydney</span>    <span className="chip">Mumbai</span>    <span className="chip">Frankfurt</span>    <span className="chip">London</span>    <span className="chip">Paris</span>    <span className="chip">Stockholm</span>    <span className="chip">Virginia</span>    <span className="chip">Oregon</span>    <span className="chip">São Paulo</span>    <span className="chip">Cape Town</span>  </div></div>
<div class="scroll-area scroll-area-inline" tabindex="0" role="region" aria-label="Regions">  <div class="flex gap-sm w-fit py(4) px(2)">    <span class="chip">Tokyo</span>    <span class="chip">Singapore</span>    <span class="chip">Sydney</span>    <span class="chip">Mumbai</span>    <span class="chip">Frankfurt</span>    <span class="chip">London</span>    <span class="chip">Paris</span>    <span class="chip">Stockholm</span>    <span class="chip">Virginia</span>    <span class="chip">Oregon</span>    <span class="chip">São Paulo</span>    <span class="chip">Cape Town</span>  </div></div>
<template>  <div class="scroll-area scroll-area-inline" tabindex="0" role="region" aria-label="Regions">    <div class="flex gap-sm w-fit py(4) px(2)">      <span class="chip">Tokyo</span>      <span class="chip">Singapore</span>      <span class="chip">Sydney</span>      <span class="chip">Mumbai</span>      <span class="chip">Frankfurt</span>      <span class="chip">London</span>      <span class="chip">Paris</span>      <span class="chip">Stockholm</span>      <span class="chip">Virginia</span>      <span class="chip">Oregon</span>      <span class="chip">São Paulo</span>      <span class="chip">Cape Town</span>    </div>  </div></template>

#跟隨新內容

聊天、建置記錄或串流產生的回答,會在閱讀期間於結尾持續增加。為區域加上 role="log" 並掛載 follow 行為:內容送達時,它會讓結尾保持在視野內;讀者一捲離結尾就停止跟隨,回到結尾時再恢復跟隨。在內容之後放一個帶有 hidden 的 scroll-area-latest 按鈕:它只在區域未跟隨時出現,按下後會捲到結尾並恢復跟隨。

跟隨建置記錄

<div class="sheet max-w(440) mx-auto">  <div class="scroll-area h(240) p-md font-mono text-xs fg-text-body leading-md" tabindex="0" role="log" aria-label="Build log">    <div id="build-lines">      <p class="m(0)">Cloning master-co/ui at 3041b1c</p>      <p class="m(0)">Installing 596 packages</p>      <p class="m(0)">Compiling 112 stylesheets</p>    </div>    <button type="button" class="btn btn-sm btn-pill scroll-area-latest" hidden>      <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">        <path d="M13.6362 7.5189A0.4947 0.4947 0 0 0 13.5 7.5L2.4755 7.5006A0.5 0.5 0 0 0 2.4266 8.4946L2.5 8.5H13.5A0.4947 0.4947 0 0 0 13.6362 8.4811A0.7009 0.7009 0 0 0 13.6362 7.5189Z" fill="currentColor" stroke="none" fill-rule="evenodd" transform="rotate(90 8 8)" /><path d="M8 4.5L13.2348 7.8313A0.2 0.2 0 0 1 13.2348 8.1687L8 11.5" transform="rotate(90 8 8)" />      </svg>Jump to latest    </button>  </div></div><script>const steps = ['Checking contracts', 'Rendering /components', 'Rendering /blocks', 'Rendering /pages', 'Writing llms.txt', 'Writing the registry', 'Checking 2,914 links', 'Compressing assets', 'Uploading 71 pages']const lines = document.getElementById('build-lines')let count = 0const timer = setInterval(() => {  const line = document.createElement('p')  line.className = 'm(0)'  line.textContent = steps[count % steps.length] + (count >= steps.length ? ' (pass ' + (Math.floor(count / steps.length) + 1) + ')' : '')  lines.append(line)  if (++count >= 120) clearInterval(timer)}, 800)</script><script type="module">  import { attach } from '@master/ui/behaviors'  attach()</script>
建置記錄會隨著新資料持續捲到末尾。建置期間往上捲,畫面會停在你正在閱讀的位置,並顯示「跳到最新」按鈕。按下按鈕或捲回末尾,即可恢復跟隨。

判斷方式:

  • 跟隨。 從掛載行為的那一刻起,區域就會跟隨:它會捲到結尾,而每當內容增加,無論是新增一列、串流的文字落在最後一則訊息中,或圖片載入完成,都會再次捲到結尾。
  • 停止。 往起點方向捲動就會停止跟隨,無論是由滑鼠滾輪、手指、捲動條、↑、Page Up 或 Home 造成。往起點方向轉動滾輪時,會在區域移動之前立即停止,因此在那一瞬間送達的文字不會把讀者拉回去。
  • 恢復跟隨。 用 End、捲動條或按鈕再次到達結尾時,就會恢復跟隨。距離結尾只差幾個像素也算是結尾。
  • 事件。 每當讀者讓區域開始或停止跟隨,區域都會派送 change。行為的 value 表示是否正在跟隨;將它設為 true 可讓讀者回到結尾,就像應用程式在讀者送出訊息時所做的那樣。設定它不會派送事件。
app.js
import { follow } from '@master/ui/behaviors/follow.js'const log = document.querySelector('.scroll-area[role="log"]')const following = follow(log)form.addEventListener('submit', () => { following.value = true })

沒有行為時,區域就是一般的捲動區域:它會停在讀者離開的位置,按鈕也會保持隱藏。按鈕以 sticky 定位固定在區域底部,位於內容和淡出邊緣上方,並疊在最後一列上而不是排在它後面,因此顯示按鈕永遠不會讓內容變高。

#保留讀者的位置

在讀者正在看的內容上方插入內容,例如在頂端載入較早的訊息,會把正在閱讀的內容往下推。瀏覽器本身會用捲動錨定避免這種情況:區域會隨著上方的內容移動,讓正在閱讀的訊息留在原處。樣式表在區域上保留 overflow-anchor: auto,並在其按鈕上設定 none,按鈕永遠不會是瀏覽器固定不動的元素。瀏覽器不支援捲動錨定時,follow 行為會自行處理,移動的距離與上方內容移動的距離相同。頂端若有一行內容,新的列會插入在它下方,例如提示正在載入更多內容的說明,也請設定 overflow-anchor: none,讓瀏覽器固定某一列而不是這行說明。

較早的訊息與串流回答

<div class="sheet max-w(440) mx-auto">  <div class="scroll-area h(320) p-md" tabindex="0" role="log" aria-label="Conversation with Ada">    <p class="m(0) pb-sm text-center text-xs fg-text-muted" style="overflow-anchor: none" id="earlier">      Earlier messages load as you scroll up    </p>    <ol class="m(0) p(0)" id="turns">      <li class="message message-sent">        <div class="message-bubble">Can the docs build run on every push?</div>      </li>      <li class="message">        <div class="message-bubble">Yes. It takes about two minutes.</div>      </li>    </ol>    <button type="button" class="btn btn-sm btn-pill scroll-area-latest" hidden>      <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">        <path d="M13.6362 7.5189A0.4947 0.4947 0 0 0 13.5 7.5L2.4755 7.5006A0.5 0.5 0 0 0 2.4266 8.4946L2.5 8.5H13.5A0.4947 0.4947 0 0 0 13.6362 8.4811A0.7009 0.7009 0 0 0 13.6362 7.5189Z" fill="currentColor" stroke="none" fill-rule="evenodd" transform="rotate(90 8 8)" /><path d="M8 4.5L13.2348 7.8313A0.2 0.2 0 0 1 13.2348 8.1687L8 11.5" transform="rotate(90 8 8)" />      </svg>Jump to latest    </button>  </div></div><script>const turns = document.getElementById('turns')const turn = (text, sent) => {  const item = document.createElement('li')  item.className = sent ? 'message message-sent' : 'message'  const bubble = document.createElement('div')  bubble.className = 'message-bubble'  bubble.textContent = text  item.append(bubble)  return item}const older = [['Morning! Is the release still today?', false], ['It is, once the docs build passes.', true], ['Which build?', false], ['The one that checks every page and link.', true], ['Do we wait for it?', false], ['No, it reports here when it is done.', true]]const log = turns.closest('.scroll-area')log.addEventListener('scroll', () => {  if (log.scrollTop > 48 || !older.length) return  for (const [text, sent] of older.splice(-2).reverse()) turns.prepend(turn(text, sent))  if (!older.length) document.getElementById('earlier').textContent = 'The start of the conversation'})const exchanges = [  ['What does it check?', 'It renders every page in five languages, checks each preview against the contracts, follows every link and compares the registry with the package, then writes the search index.'],  ['And if a link is broken?', 'The build stops and names the page and the link, so nothing half checked reaches the site. Fix it and push again; the next run starts from the beginning.'],  ['Thanks!', 'Any time. I will post here when this one is done.']]let at = 0const next = () => {  if (at >= exchanges.length) return  const [question, answer] = exchanges[at++]  turns.append(turn(question, true))  const reply = turn('', false)  const bubble = reply.firstChild  bubble.setAttribute('aria-busy', 'true')  turns.append(reply)  const words = answer.split(' ')  const stream = setInterval(() => {    bubble.textContent += (bubble.textContent ? ' ' : '') + words.shift()    if (words.length) return    clearInterval(stream)    bubble.removeAttribute('aria-busy')    setTimeout(next, 2500)  }, 140)}setTimeout(next, 1200)</script><script type="module">  import { attach } from '@master/ui/behaviors'  attach()</script>
回答以串流方式逐步顯示,對話會隨之捲動。往上捲到接近頂端時,較早的對話輪次會載入到上方,你正在閱讀的那一輪仍會留在原處。

#無障礙

  • 使用者必須能透過鍵盤將焦點移到可捲動區域。若區域內沒有可取得焦點的內容,請加上 tabindex="0"、適當的 role(例如 region)和標籤。
  • 遮罩也會讓區域自己的捲動條在相同的邊緣淡出。強制色彩模式下會移除遮罩。
  • 為區域設定高度或最大高度,否則它會隨內容增高,不會產生捲動。
  • role="log" 是禮貌型即時區域:新增到其中的內容會被播報,但不會移動焦點。回答正在串流進某則訊息時,請為該訊息加上 aria-busy="true",並在回答完成後移除,讓它完整地播報一次,而不是逐字播報。
  • scroll-area-latest 按鈕顯示時,是位於 Tab 順序中的真正按鈕。按下後它會隱藏,焦點會移到區域,之後即可用按鍵捲動區域。
  • 跟隨永遠不會移動焦點,而且讀者一捲離就會停止,因此持續送達的內容永遠不會讓正在閱讀的人失去所在的位置。

#鍵盤操作

按鍵都是瀏覽器原生的;行為只監聽按鍵造成的捲動。

按鍵動作
ArrowUp, ArrowDown, PageUp, PageDown, Home, End以瀏覽器的預設方式捲動取得焦點的區域
ArrowUp, PageUp, Home搭配跟隨行為時:往開頭捲動會停止跟隨,並顯示 scroll-area-latest 按鈕
End搭配跟隨行為時:回到末端後,區域會重新開始跟隨
Enter, Space在 scroll-area-latest 上:捲動到末端並重新開始跟隨,行為與按鈕相同

#React、Svelte 和 Vue

log 元件會以支援跟隨功能的捲動區域包住子元素,並提供按鈕,掛載行為,並接收和傳出 following。使用 npx master-ui add log 將元件複製到專案中;元件的實作方式與用法請見 React、Svelte 和 Vue。

ui/log.tsx
'use client'import { useState, type ComponentProps } from 'react'import { follow } from '@master/ui/behaviors/follow.js'import { useBehavior } from './use-behavior'export interface LogProps extends Omit<ComponentProps<'div'>, 'ref'> {  /** Whether the region keeps its end in view. It stops when the reader scrolls away and starts again at the end. */  following?: boolean  defaultFollowing?: boolean  onFollowingChange?: (following: boolean) => void  /** The words of the button that leads back to the end, such as "Jump to latest". */  latestLabel: string}/** * A scroll area that follows what arrives at its end: a chat, a build log, a streamed answer. Its children * are the content; name it with `aria-label` and give it a height. Set `following` to true to bring the * reader back to the end, as when they send a message. */export function Log({ following, defaultFollowing = true, onFollowingChange, latestLabel, className, children, ...root }: LogProps) {  const ref = useBehavior(follow, undefined, { value: following, defaultValue: defaultFollowing, onValueChange: onFollowingChange })  const [first] = useState(following ?? defaultFollowing)  return (    <div className={className ? `scroll-area ${className}` : 'scroll-area'} role="log" tabIndex={0} ref={ref} {...root}>      {children}      <button type="button" className="btn btn-sm btn-pill scroll-area-latest" hidden={first}>        <svg className="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true"><path d="M13.6362 7.5189A0.4947 0.4947 0 0 0 13.5 7.5L2.4755 7.5006A0.5 0.5 0 0 0 2.4266 8.4946L2.5 8.5H13.5A0.4947 0.4947 0 0 0 13.6362 8.4811A0.7009 0.7009 0 0 0 13.6362 7.5189Z" fill="currentColor" stroke="none" fillRule="evenodd" transform="rotate(90 8 8)" /><path d="M8 4.5L13.2348 7.8313A0.2 0.2 0 0 1 13.2348 8.1687L8 11.5" transform="rotate(90 8 8)" /></svg>        {latestLabel}      </button>    </div>  )}
ui/Log.svelte
<script lang="ts" module>  import type { Snippet } from 'svelte'  import type { HTMLAttributes } from 'svelte/elements'  export interface LogProps extends Omit<HTMLAttributes<HTMLDivElement>, 'children'> {    /** What the region holds. */    children?: Snippet    /**     * Whether the region keeps its end in view. It stops when the reader scrolls away and starts again at the     * end. Bind it, or leave it out: it starts following.     */    following?: boolean    onFollowingChange?: (following: boolean) => void    /** The words of the button that leads back to the end, such as "Jump to latest". */    latestLabel: string  }</script><script lang="ts">  import { untrack } from 'svelte'  import { follow } from '@master/ui/behaviors/follow.js'  import { behavior } from './behavior.svelte.js'  let { children, following = $bindable(), onFollowingChange, latestLabel, class: className, ...root }: LogProps = $props()  const first = untrack(() => following) ?? true  const attach = behavior(follow, undefined, {    value: () => following,    change: (next) => { following = next; onFollowingChange?.(next) },  })</script><!--  @component  A scroll area that follows what arrives at its end: a chat, a build log, a streamed answer. Its children  are the content; name it with `aria-label` and give it a height. Set `following` to true to bring the  reader back to the end, as when they send a message.--><!-- A region that scrolls takes the focus, so the keys scroll it. --><!-- svelte-ignore a11y_no_noninteractive_tabindex --><div class={['scroll-area', className]} role="log" tabindex={0} {@attach attach} {...root}>  {@render children?.()}  <button type="button" class="btn btn-sm btn-pill scroll-area-latest" hidden={first}>    <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M13.6362 7.5189A0.4947 0.4947 0 0 0 13.5 7.5L2.4755 7.5006A0.5 0.5 0 0 0 2.4266 8.4946L2.5 8.5H13.5A0.4947 0.4947 0 0 0 13.6362 8.4811A0.7009 0.7009 0 0 0 13.6362 7.5189Z" fill="currentColor" stroke="none" fill-rule="evenodd" transform="rotate(90 8 8)" /><path d="M8 4.5L13.2348 7.8313A0.2 0.2 0 0 1 13.2348 8.1687L8 11.5" transform="rotate(90 8 8)" /></svg>    {latestLabel}  </button></div>
ui/Log.vue
<script setup lang="ts">import { useTemplateRef } from 'vue'import { follow } from '@master/ui/behaviors/follow.js'import { useBehavior } from './use-behavior'defineProps<{  /** The words of the button that leads back to the end, such as "Jump to latest". */  latestLabel: string}>()/** * Whether the region keeps its end in view: `v-model:following`. It stops when the reader scrolls away and * starts again at the end. */const following = defineModel<boolean>('following', { default: true })defineSlots<{  /** What the region holds. */  default?: () => unknown}>()/* * A scroll area that follows what arrives at its end: a chat, a build log, a streamed answer. Name it with * `aria-label` and give it a height. Set `following` to true to bring the reader back to the end, as when * they send a message. */// The button's hidden is the first state's attribute: after that it is the behavior's to move.const first = following.valueconst root = useTemplateRef<HTMLDivElement>('root')useBehavior(root, follow, undefined, following)</script><template>  <div class="scroll-area" role="log" tabindex="0" ref="root">    <slot />    <button type="button" class="btn btn-sm btn-pill scroll-area-latest" :hidden="first">      <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M13.6362 7.5189A0.4947 0.4947 0 0 0 13.5 7.5L2.4755 7.5006A0.5 0.5 0 0 0 2.4266 8.4946L2.5 8.5H13.5A0.4947 0.4947 0 0 0 13.6362 8.4811A0.7009 0.7009 0 0 0 13.6362 7.5189Z" fill="currentColor" stroke="none" fill-rule="evenodd" transform="rotate(90 8 8)" /><path d="M8 4.5L13.2348 7.8313A0.2 0.2 0 0 1 13.2348 8.1687L8 11.5" transform="rotate(90 8 8)" /></svg>      {{ latestLabel }}    </button>  </div></template>

#類別

類別說明
scroll-area沿區塊軸捲動的區域,使用細捲軸。若某側還有未顯示的內容,該側邊緣會淡出。
scroll-area-inline沿行內軸捲動,並在起始側與結束側的邊緣套用淡出效果。搭配 scroll-area 使用
scroll-area-latest套用於區域內容之後的 btn:用來回到正在跟隨的區域末端。以黏貼方式固定在內容底部、位於淡出效果上方;區域跟隨中時,行為會為它加上 hidden

#自訂屬性

屬性預設值說明
--scroll-area-fade1.5rem各邊緣的淡出長度

#原始碼

@master/ui/components/scroll-area.css
/* * Scroll area: a region that scrolls inside the page, with a thin quiet scrollbar. * Where content continues past an edge, that edge fades out; where it ends, the edge is clean. The fade is * read from the scroll position itself by a scroll-driven animation, with no script and no sentinel elements. * Without scroll-driven animations the region scrolls the same and its edges stay clean. * A region that follows what arrives (role="log", with the follow behavior) keeps its end in view. Its * scroll-area-latest button is the way back to the end once the reader has scrolled away: a small raised pill * that stands over the content at the foot of the region, above the faded edge, and takes no room of its own. */@property --scroll-area-start {  syntax: '<length>';  inherits: false;  initial-value: 0px;}@property --scroll-area-end {  syntax: '<length>';  inherits: false;  initial-value: 0px;}@layer components.ui {  .scroll-area {    --scroll-area-fade: 1.5rem;    /* The latest button stands over the content inside the region, not over a sticky header outside it. */    isolation: isolate;    min-width: 0;    min-height: 0;    overflow: auto;    /*     * Only the inline axis keeps its scrolling to itself, so a sideways swipe never turns into the browser's back     * gesture. Along the block axis the page goes on scrolling past the region: a contained axis stops the wheel     * even where nothing overflows, which would leave a short region as a dead patch in the page.     */    overscroll-behavior-inline: contain;    scrollbar-width: thin;    scrollbar-color: var(--color-line-strong) transparent;    /* Content inserted above what the reader is looking at moves the region with it, so the reader's place holds. */    overflow-anchor: auto;    &:focus-visible {      @apply --focus-ring;    }    /* Scrolls along the inline axis: a row of cards, a wide table. */    &.scroll-area-inline {      overflow: auto hidden;    }    /*     * Two animations on the region's own scroll timeline. The first runs over the first stretch of scrolling     * and opens the fade at the start edge; the second runs over the last stretch and closes the fade at the     * end edge. While nothing overflows the timeline is inactive and both lengths stay 0.     */    @supports (animation-timeline: scroll()) {      /* The way the mask runs, from the start edge to the end edge of the scrolling axis. */      --scroll-area-run: to bottom;      mask-image: linear-gradient(var(--scroll-area-run), transparent, black var(--scroll-area-start), black calc(100% - var(--scroll-area-end)), transparent);      /* Longhands: tools that fold the timeline into the shorthand write a value no browser reads. */      animation-name: scroll-area-start, scroll-area-end;      animation-timing-function: linear;      animation-fill-mode: both;      animation-timeline: scroll(self block), scroll(self block);      animation-range: 0 var(--scroll-area-fade), calc(100% - var(--scroll-area-fade)) 100%;      /*       * What the keys or a link bring into view stops clear of the fade, so a focused control and its ring are       * never dimmed at the edge it was scrolled to.       */      scroll-padding-block: var(--scroll-area-fade);      /* Right to left, the start edge is on the right, so the mask runs the other way. */      &.scroll-area-inline {        --scroll-area-run: to right;        scroll-padding-block: 0;        scroll-padding-inline: var(--scroll-area-fade);        animation-timeline: scroll(self inline), scroll(self inline);        &:dir(rtl) {          --scroll-area-run: to left;        }      }      /* A mask cuts the region's own focus ring, which stands outside it: while the keys hold it, the fade steps aside. */      &:focus-visible {        mask-image: none;      }    }    /*     * While the way back to the end stands over the last rows, what the keys bring into view at the foot stops     * above it instead of under it: the room it stands in, a small button's 32px and a few pixels more.     */    &:has(.scroll-area-latest:not([hidden])) {      scroll-padding-block-end: calc(max(.75rem, var(--scroll-area-fade)) + 2.25rem);    }    @media (forced-colors: active) {      scrollbar-color: auto;      mask-image: none;    }    /*     * On a btn inside the region, after its content, and written inside the region so it outweighs the btn's     * own rules. Sticky at the foot, it stands above the fade, where nothing dims it; a negative margin of its     * own height lays it over the last row instead of after it, so showing it never makes the content taller.     * It is never the element scroll anchoring holds still.     */    .scroll-area-latest {      position: sticky;      inset-block-end: max(.75rem, var(--scroll-area-fade));      z-index: 1;      display: flex;      width: fit-content;      margin: calc(-1 * var(--btn-height, 2rem)) auto 0;      overflow-anchor: none;      transition-property: box-shadow, translate, color, opacity, display;      transition-behavior: allow-discrete;      &[hidden] {        display: none;        opacity: 0;        translate: 0 .375rem;      }      @starting-style {        opacity: 0;        translate: 0 .375rem;      }      @media (prefers-reduced-motion: reduce) {        &[hidden] {          translate: none;        }        @starting-style {          translate: none;        }      }    }  }}@keyframes scroll-area-start {  from {    --scroll-area-start: 0px;  }  to {    --scroll-area-start: var(--scroll-area-fade);  }}@keyframes scroll-area-end {  from {    --scroll-area-end: var(--scroll-area-fade);  }  to {    --scroll-area-end: 0px;  }}