导航

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用 button 或链接承载单个操作。操作单元宽度至少为 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;  }}