ナビゲーション

v2.1.0
構造

スワイプ

行を横にスライドすると、その行で使える操作が表示されます。

このページの内容

スワイプは、行を横にずらして操作を表示するコンポーネントです。行はスクロール領域になっており、行と同じ幅のコンテンツを表示する位置と、行の末尾側に隠れた操作を表示する位置の 2 か所で止まります。ブラウザー標準のスクロールを使うため、指、トラックパッド、Tab キーのいずれでも操作を表示でき、スクリプトは不要です。

スワイプ

<div class="sheet sheet-raised overflow-hidden w-full max-w(360)">  <div class="swipe">    <div class="swipe-content flex items-center gap-sm p(12)">      <span class="avatar">AL</span>      <div class="min-w(0)">        <p class="m(0) fg-text-strong font-medium">Ada Lovelace</p>        <p class="m(0) fg-text-muted">The notes on the engine are ready.</p>      </div>    </div>    <div class="swipe-actions">      <button class="swipe-action" type="button">        Archive<span class="sr-only">Ada Lovelace</span>      </button>      <button class="swipe-action swipe-action-danger" type="button">        Delete<span class="sr-only">Ada Lovelace</span>      </button>    </div>  </div>  <div class="swipe bt(1) bt-solid b-line-hair">    <div class="swipe-content flex items-center gap-sm p(12)">      <span class="avatar">AT</span>      <div class="min-w(0)">        <p class="m(0) fg-text-strong font-medium">Alan Turing</p>        <p class="m(0) fg-text-muted">Can machines think? A short reply.</p>      </div>    </div>    <div class="swipe-actions">      <button class="swipe-action" type="button">        Archive<span class="sr-only">Alan Turing</span>      </button>      <button class="swipe-action swipe-action-danger" type="button">        Delete<span class="sr-only">Alan Turing</span>      </button>    </div>  </div>  <div class="swipe bt(1) bt-solid b-line-hair">    <div class="swipe-content flex items-center gap-sm p(12)">      <span class="avatar">GH</span>      <div class="min-w(0)">        <p class="m(0) fg-text-strong font-medium">Grace Hopper</p>        <p class="m(0) fg-text-muted">The compiler passes every test.</p>      </div>    </div>    <div class="swipe-actions">      <button class="swipe-action" type="button">        Archive<span class="sr-only">Grace Hopper</span>      </button>      <button class="swipe-action swipe-action-danger" type="button">        Delete<span class="sr-only">Grace Hopper</span>      </button>    </div>  </div></div>
<div className="sheet sheet-raised overflow-hidden w-full max-w(360)">  <div className="swipe">    <div className="swipe-content flex items-center gap-sm p(12)">      <span className="avatar">AL</span>      <div className="min-w(0)">        <p className="m(0) fg-text-strong font-medium">Ada Lovelace</p>        <p className="m(0) fg-text-muted">The notes on the engine are ready.</p>      </div>    </div>    <div className="swipe-actions">      <button className="swipe-action" type="button">        Archive<span className="sr-only">Ada Lovelace</span>      </button>      <button className="swipe-action swipe-action-danger" type="button">        Delete<span className="sr-only">Ada Lovelace</span>      </button>    </div>  </div>  <div className="swipe bt(1) bt-solid b-line-hair">    <div className="swipe-content flex items-center gap-sm p(12)">      <span className="avatar">AT</span>      <div className="min-w(0)">        <p className="m(0) fg-text-strong font-medium">Alan Turing</p>        <p className="m(0) fg-text-muted">Can machines think? A short reply.</p>      </div>    </div>    <div className="swipe-actions">      <button className="swipe-action" type="button">        Archive<span className="sr-only">Alan Turing</span>      </button>      <button className="swipe-action swipe-action-danger" type="button">        Delete<span className="sr-only">Alan Turing</span>      </button>    </div>  </div>  <div className="swipe bt(1) bt-solid b-line-hair">    <div className="swipe-content flex items-center gap-sm p(12)">      <span className="avatar">GH</span>      <div className="min-w(0)">        <p className="m(0) fg-text-strong font-medium">Grace Hopper</p>        <p className="m(0) fg-text-muted">The compiler passes every test.</p>      </div>    </div>    <div className="swipe-actions">      <button className="swipe-action" type="button">        Archive<span className="sr-only">Grace Hopper</span>      </button>      <button className="swipe-action swipe-action-danger" type="button">        Delete<span className="sr-only">Grace Hopper</span>      </button>    </div>  </div></div>
<div class="sheet sheet-raised overflow-hidden w-full max-w(360)">  <div class="swipe">    <div class="swipe-content flex items-center gap-sm p(12)">      <span class="avatar">AL</span>      <div class="min-w(0)">        <p class="m(0) fg-text-strong font-medium">Ada Lovelace</p>        <p class="m(0) fg-text-muted">The notes on the engine are ready.</p>      </div>    </div>    <div class="swipe-actions">      <button class="swipe-action" type="button">        Archive<span class="sr-only">Ada Lovelace</span>      </button>      <button class="swipe-action swipe-action-danger" type="button">        Delete<span class="sr-only">Ada Lovelace</span>      </button>    </div>  </div>  <div class="swipe bt(1) bt-solid b-line-hair">    <div class="swipe-content flex items-center gap-sm p(12)">      <span class="avatar">AT</span>      <div class="min-w(0)">        <p class="m(0) fg-text-strong font-medium">Alan Turing</p>        <p class="m(0) fg-text-muted">Can machines think? A short reply.</p>      </div>    </div>    <div class="swipe-actions">      <button class="swipe-action" type="button">        Archive<span class="sr-only">Alan Turing</span>      </button>      <button class="swipe-action swipe-action-danger" type="button">        Delete<span class="sr-only">Alan Turing</span>      </button>    </div>  </div>  <div class="swipe bt(1) bt-solid b-line-hair">    <div class="swipe-content flex items-center gap-sm p(12)">      <span class="avatar">GH</span>      <div class="min-w(0)">        <p class="m(0) fg-text-strong font-medium">Grace Hopper</p>        <p class="m(0) fg-text-muted">The compiler passes every test.</p>      </div>    </div>    <div class="swipe-actions">      <button class="swipe-action" type="button">        Archive<span class="sr-only">Grace Hopper</span>      </button>      <button class="swipe-action swipe-action-danger" type="button">        Delete<span class="sr-only">Grace Hopper</span>      </button>    </div>  </div></div>
<template>  <div class="sheet sheet-raised overflow-hidden w-full max-w(360)">    <div class="swipe">      <div class="swipe-content flex items-center gap-sm p(12)">        <span class="avatar">AL</span>        <div class="min-w(0)">          <p class="m(0) fg-text-strong font-medium">Ada Lovelace</p>          <p class="m(0) fg-text-muted">The notes on the engine are ready.</p>        </div>      </div>      <div class="swipe-actions">        <button class="swipe-action" type="button">          Archive<span class="sr-only">Ada Lovelace</span>        </button>        <button class="swipe-action swipe-action-danger" type="button">          Delete<span class="sr-only">Ada Lovelace</span>        </button>      </div>    </div>    <div class="swipe bt(1) bt-solid b-line-hair">      <div class="swipe-content flex items-center gap-sm p(12)">        <span class="avatar">AT</span>        <div class="min-w(0)">          <p class="m(0) fg-text-strong font-medium">Alan Turing</p>          <p class="m(0) fg-text-muted">Can machines think? A short reply.</p>        </div>      </div>      <div class="swipe-actions">        <button class="swipe-action" type="button">          Archive<span class="sr-only">Alan Turing</span>        </button>        <button class="swipe-action swipe-action-danger" type="button">          Delete<span class="sr-only">Alan Turing</span>        </button>      </div>    </div>    <div class="swipe bt(1) bt-solid b-line-hair">      <div class="swipe-content flex items-center gap-sm p(12)">        <span class="avatar">GH</span>        <div class="min-w(0)">          <p class="m(0) fg-text-strong font-medium">Grace Hopper</p>          <p class="m(0) fg-text-muted">The compiler passes every test.</p>        </div>      </div>      <div class="swipe-actions">        <button class="swipe-action" type="button">          Archive<span class="sr-only">Grace Hopper</span>        </button>        <button class="swipe-action swipe-action-danger" type="button">          Delete<span class="sr-only">Grace Hopper</span>        </button>      </div>    </div>  </div></template>
行を横にドラッグするか、トラックパッドで横にスクロールするか、Tab キーを押します。

#仕組み

swipe は、スクロールバーを表示せず、スナップを必須とする横スクロール領域です。swipe-content は行と同じ幅で、先頭側の端でスナップします。その後ろに続く swipe-actions は、末尾側の端でスナップします。途中でスクロールを止めると近い方の位置に収まり、元の位置まで戻すと行が閉じます。

コンテンツの背景は不透明です。シート やカードが公開する --surface-color から配置先の背景色を取得し、操作が現れるくぼみに浅い影を落とします。スクロール駆動アニメーションに対応するブラウザーでは、操作が現れるにつれてフェードインします。非対応のブラウザーでは、そのまま表示されます。

別の行を開いたときに元の行を閉じる処理や、操作を実行したときに行を削除する処理は、ページ側で実装します。スクリプトで行を閉じるには、その行の scrollLeft を 0 に設定します。

#操作

操作にはボタンまたはリンクを使い、ラベルだけを表示するか、ラベルの上に アイコン を配置します。各セルは幅 72px 以上で行全体の高さを占めるため、行の高さにかかわらずセル全体がタッチ領域になります。swipe-action-danger は取り消せない操作を示し、専用の淡い背景色と危険を示す前景色を使います。操作は 1 つか 2 つに絞り、破壊的な操作は最後に置きます。

アイコン付き

<div class="sheet sheet-raised overflow-hidden w-full max-w(360)">  <div class="swipe" id="swipe-open">    <div class="swipe-content flex items-center gap-sm p(16)">      <div class="min-w(0)">        <p class="m(0) fg-text-strong font-medium">Quarterly report.pdf</p>        <p class="m(0) fg-text-muted">2.4 MB · Edited yesterday</p>      </div>    </div>    <div class="swipe-actions">      <button class="swipe-action" type="button">        <svg class="icon" viewBox="0 0 20 20" fill="none" stroke="currentColor" stroke-width="1.25" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">          <path d="M10.625 11L10.6242 2.4693A0.625 0.625 0 0 0 9.3818 2.4083L9.375 2.5L9.377 11.05A0.6255 0.6255 0 0 0 9.4989 11.3735Q9.7145 11.5422 10 11.5422Q10.2855 11.5422 10.5011 11.3735A0.625 0.625 0 0 0 10.625 11Z" fill="currentColor" stroke="none" fill-rule="evenodd" /><path d="M6.5 7.5L9.8586 10.8586A0.2 0.2 0 0 0 10.1414 10.8586L13.5 7.5" /><path d="M3 13.5L3 15.75A1.25 1.25 0 0 0 4.25 17L15.75 17A1.25 1.25 0 0 0 17 15.75L17 13.5" />        </svg> Save      </button>      <button class="swipe-action swipe-action-danger" type="button">        <svg class="icon" viewBox="0 0 20 20" fill="none" stroke="currentColor" stroke-width="1.25" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">          <path d="M3 5.5L17 5.5" /><path d="M5 5.5L5.4501 16.302A1.25 1.25 0 0 0 6.699 17.5L13.301 17.5A1.25 1.25 0 0 0 14.5499 16.302L15 5.5" /><path d="M7.5 5.5L7.5 3.5A1 1 0 0 1 8.5 2.5L11.5 2.5A1 1 0 0 1 12.5 3.5L12.5 5.5" /><path d="M8.5 9L8.5 14" /><path d="M11.5 9L11.5 14" />        </svg> Delete      </button>    </div>  </div></div><script>document.getElementById('swipe-open').scrollLeft = 999</script>
この例では、スクリプトで行を開閉するときと同じように、scrollLeft を設定して行を開いています。

#アクセシビリティ

  • スワイプはショートカットとして使い、必ず別の操作手段も用意します。 トラックパッドを使わずマウスだけで操作するユーザーは、行を横にスクロールできません。同じ操作を、行内または項目を開いた先の メニュー にも用意し、ポインターで操作できるようにします。
  • 操作には、タブ順序に含まれるボタン要素を使います。ボタンにフォーカスが移ると、ブラウザーが自動でスクロールして表示するため、キーボード操作に追加の実装は不要です。フォーカスリングはセルの内側に描画されるため、スクロール領域の端で切れません。
  • アイコンだけで表示する操作には、アクセシブルな名前が必要です。アイコンの下にラベルを添えると、文字を読んで操作を見つけやすくなります。
  • 破壊的な操作は実行前に確認するか、トースト から取り消せるようにします。

#クラス

クラス説明
swipeスクロールバーのない横スクロール行。コンテンツまたはアクションの位置で止まります。
swipe-content通常時に行が表示する内容。行全体の幅を使い、行が載る面と同じ背景色で表示する
swipe-actions末尾側の縁の奥にあるくぼみに配置したアクション
swipe-actionボタンまたはリンクに1つのアクション。行と同じ高さで幅72px以上のセルに、語句のみ、または語句の上にアイコンを配置
swipe-action-danger取り消せないアクションです。専用の淡い背景に、危険を示す前景色で表示します。swipe-action と併用します

#ソース

@master/ui/components/swipe.css
/* * Swipe: a row that slides aside to show what can be done to it. * The row is a scroller with two places to rest: its content, as wide as the row, and the actions that wait * behind its end edge. A finger, a trackpad or the Tab key brings the actions in. The browser's own scrolling * does the work, so there is no script, and letting go between the two settles on the nearer one. * The content keeps the color of the surface it lies on and casts a short shade on what it uncovers; the * actions lie in a well, each at least 72px wide and as tall as the row. Where scroll-driven animations are * supported the actions come forward as they are uncovered. * Swiping is a shortcut and is never the only way: offer the same actions in a menu or on the opened item. */@layer components.ui {  .swipe {    --swipe-surface: var(--surface-color, var(--color-surface-base));    position: relative;    /* The content stands over the actions inside the row, not over a sticky header that scrolls past it. */    isolation: isolate;    display: flex;    min-width: 0;    overflow: auto hidden;    overscroll-behavior-x: contain;    scroll-snap-type: x mandatory;    scrollbar-width: none;    /* The well the actions lie in shows where the content has left. */    background: var(--color-surface-inset);    &::-webkit-scrollbar {      display: none;    }    @media (forced-colors: active) {      background: Canvas;    }  }  /* What the row shows at rest. It is opaque, so nothing of the well shows through it. */  .swipe-content {    position: relative;    z-index: 1;    flex: none;    width: 100%;    min-width: 0;    background: var(--swipe-surface);    /* A contour and a short shade on both edges; the scroller cuts the one that is not uncovered. */    box-shadow: 0 0 0 .5px var(--color-line-contour), 0 0 6px -1px var(--color-shade-1), 0 0 12px -2px var(--color-shade-2);    scroll-snap-align: start;    /* A row that opens its item is a link or a button: its ring stays inside, where the scroller does not cut it. */    &:focus-visible {      outline: 2px solid var(--color-focus);      outline-offset: -2px;    }    @media (forced-colors: active) {      outline: 1px solid CanvasText;      outline-offset: -1px;      &:focus-visible {        outline: 2px solid Highlight;        outline-offset: -2px;      }    }  }  /* The actions, behind the end edge. */  .swipe-actions {    display: flex;    flex: none;    /* The shade the row's upper edge casts into the well. */    box-shadow: inset 0 1px 2px -1px var(--color-shade-2);    scroll-snap-align: end;    @supports (animation-timeline: scroll()) {      @media (prefers-reduced-motion: no-preference) {        /* Longhands: tools that fold the timeline into the shorthand write a value no browser reads. */        animation-name: swipe-arrive;        animation-timing-function: linear;        animation-fill-mode: both;        animation-timeline: scroll(inline nearest);      }    }  }  /* One action: a word, or an icon over a word. The whole cell is the target. */  .swipe-action {    --icon-size: 1.125rem;    display: inline-flex;    flex: none;    flex-direction: column;    align-items: center;    justify-content: center;    gap: .25rem;    min-width: 4.5rem;    margin: 0;    padding: 0 .875rem;    border: 0;    border-inline-start: 1px solid var(--color-line-hair);    border-radius: 0;    background: transparent;    color: var(--color-text-body);    font: var(--font-weight-control) .78125rem/1.2 var(--font-family-sans);    font-feature-settings: inherit;    text-decoration: none;    white-space: nowrap;    appearance: none;    cursor: pointer;    touch-action: manipulation;    user-select: none;    -webkit-user-select: none;    -webkit-tap-highlight-color: transparent;    @media (hover: hover) {      &:hover:not(:disabled, [aria-disabled="true"]) {        background: var(--color-surface-inset-hover);        color: var(--color-text-strong);      }    }    &:active:not(:disabled, [aria-disabled="true"]) {      background: color-mix(in oklab, currentColor 9%, transparent);    }    /* The ring stays inside the cell: the scroller would cut one drawn outside it. */    &:focus-visible {      outline: 2px solid var(--color-focus);      outline-offset: -2px;    }    /* The action that cannot be undone, in the danger ink on its own tint. */    &.swipe-action-danger {      background: color-mix(in oklab, var(--color-danger) 9%, transparent);      color: var(--color-danger);      @media (hover: hover) {        &:hover:not(:disabled, [aria-disabled="true"]) {          background: color-mix(in oklab, var(--color-danger) 14%, transparent);          color: var(--color-danger);        }      }      &:active:not(:disabled, [aria-disabled="true"]) {        background: color-mix(in oklab, var(--color-danger) 18%, transparent);      }    }    /* An action that cannot be taken now: disabled ink on the bare well, answering neither pointer nor press. */    &:is(:disabled, [aria-disabled="true"]) {      background: transparent;      color: var(--color-text-disabled);      cursor: not-allowed;    }    @media (forced-colors: active) {      border-inline-start-color: CanvasText;      &:focus-visible {        outline-color: Highlight;      }      &:is(:disabled, [aria-disabled="true"]) {        color: GrayText;      }    }  }}@keyframes swipe-arrive {  from {    opacity: 0;  }  60% {    opacity: 1;  }}