スクロール領域
コンテンツが続く側の端がフェードするスクロール領域。
このページの内容
スクロール領域は、パネル内の長いリスト、幅の広いテーブル、ログなどをページ内でスクロールできるようにする領域です。プラットフォーム標準のスクロール動作を保ち、細く控えめなスクロールバーを表示します。コンテンツが領域の外に続く側の端にはフェードがかかり、コンテンツの終端ではフェードが消えます。チャットやビルドログのように、読んでいる間に末尾が伸びていく領域では、追加される内容に追従させることもできます。
スクロール領域
<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>#フェードの仕組み
フェードにはマスクを使い、その2つの色停止点をカスタムプロパティ--scroll-area-startと--scroll-area-endで指定します。領域自体のスクロールタイムライン上で2つのアニメーションがこれらの値を変化させます。最初の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を設定します。設定してもイベントは発行されません。
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 })ビヘイビアがない場合、領域は通常のスクロール領域です。読者が最後にスクロールした位置を保ち、ボタンは非表示のままです。ボタンにはスティッキー配置が適用され、領域の下端に張り付いて、内容や端のフェードより手前に表示されます。最後の行の後ではなく上に重なるため、ボタンを表示しても内容の高さは変わりません。
#読んでいる位置の維持
上部に読み込まれる古いメッセージのように、読者が見ている位置より上に内容が挿入されると、読んでいる内容が下に押し下げられてしまいます。ブラウザーはスクロールアンカリングによってこれを自動的に防ぎます。上に追加された内容に合わせて領域内のスクロール位置を補正するため、読んでいるメッセージは表示上の位置を保ちます。スタイルシートは、領域に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"、regionなどのrole、ラベルを指定します。 - マスクは、同じ端で領域自体のスクロールバーにもフェードをかけます。強制カラーモードではマスクが解除されます。
- 領域には高さまたは最大の高さを指定します。指定しないと、領域はコンテンツに合わせて伸び、スクロールしません。
role="log"を指定したライブリージョンでは、aria-live の既定値は polite です。追加された内容はフォーカスを動かさずに読み上げられます。回答がメッセージにストリーミングされている間は、そのメッセージにaria-busy="true"を指定し、回答が完了したら削除します。これにより、メッセージは単語ごとではなく、完成した状態で一度だけ読み上げられます。scroll-area-latestボタンは、表示されている間はタブ順序に含まれる実際のボタンです。押すとボタンは非表示になり、フォーカスは領域に移ります。その後はキー操作で領域をスクロールできます。- 追従によってフォーカスが動くことはなく、読者がスクロールして離れるとすぐに停止します。そのため、内容が次々に追加されても、読んでいる位置を保てます。
#キーボード操作
キー操作はブラウザー標準のもので、ビヘイビアはそれによるスクロールを監視するだけです。
| キー | 操作 |
|---|---|
| ArrowUp, ArrowDown, PageUp, PageDown, Home, End | ブラウザー標準の動作と同様に、フォーカス中の領域をスクロール |
| ArrowUp, PageUp, Home | follow ビヘイビア使用時、先頭方向にスクロールすると追従を止め、scroll-area-latest ボタンを表示します。 |
| End | follow ビヘイビア使用時、末尾に戻ると領域は再び追従します。 |
| Enter, Space | scroll-area-latest 上で、ボタンと同様に末尾までスクロールし、再び追従します。 |
#React、Svelte、Vue
logコンポーネントは、子要素の周りに追従するスクロール領域をボタン付きで描画し、ビヘイビアを適用して、followingを外部とやり取りします。npx master-ui add logでプロジェクトにコピーできます。コンポーネントの構成と使い方はReact、Svelte、Vueを参照してください。
#クラス
| クラス | 説明 |
|---|---|
scroll-area | ブロック軸に沿ってスクロールする領域。細いスクロールバーを備え、コンテンツが続く側の端はフェード表示 |
scroll-area-inline | インライン軸に沿ってスクロールし、始端と終端をフェードさせます。scroll-area と併用します |
scroll-area-latest | 領域のコンテンツの後に置く btn に指定します。追従する領域の末尾に戻るためのボタンです。コンテンツに重ねて下端に固定し、フェードより前面に表示します。領域が追従している間は、ビヘイビアが hidden を付けます。 |
#カスタムプロパティ
| プロパティ | 既定値 | 説明 |
|---|---|---|
--scroll-area-fade | 1.5rem | 各辺のフェードの長さ |