網站導覽

v2.1.0
結構

滑動操作列

將整列向側邊滑開,即可顯示可用的操作。

本頁內容

滑動操作列可向側邊滑開,露出對該項目可執行的操作。這一列本身就是捲動容器,有兩個停靠位置。一個顯示與整列等寬的內容,另一個露出藏在整列結束側後方的操作。瀏覽器的原生捲動即可處理這些動作,因此用手指、觸控板或 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,以危險狀態的前景色搭配專用的淡色背景。操作請限制在一到兩個,並將破壞性操作放在最後。

搭配圖示

<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 展開操作列,示範腳本如何控制展開或收合。

#無障礙

  • 把滑動當作快捷操作,並提供其他操作方式。 只使用滑鼠、沒有觸控板的讀者無法水平捲動操作列。請在指標可操作的位置提供相同操作,例如在該列或開啟後的項目中加入選單。
  • 各操作使用可透過 Tab 鍵依序取得焦點的按鈕或連結。操作項目取得焦點時,瀏覽器會自動將它捲入檢視區,因此可直接使用鍵盤操作。焦點環畫在操作格內側,避免被捲動容器裁切。
  • 只顯示圖示的操作必須有無障礙名稱。在圖示下方加上文字會更清楚,讓使用者能直接讀出各項操作的用途。
  • 執行破壞性操作前先要求確認,或在浮動通知中提供復原選項。

#類別

類別說明
swipe該列可水平捲動且不顯示捲軸,捲動會停在內容區或操作區。
swipe-content該列在未操作時顯示的內容,與整列同寬,背景色與下方表面一致
swipe-actions藏在末端邊緣後方凹槽中的操作
swipe-action每個按鈕或連結提供一項操作。儲存格至少寬 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;  }}