網站導覽

v2.1.0
內文

協作者游標

以代表某人的顏色顯示他的指標、文字游標或選取範圍。

本頁內容

協作者游標會顯示正在處理同一份內容的其他人位於何處:他們在畫布或白板上的指標,以及在文字中的文字游標和選取範圍。每個人都以一種識別色繪製,旁邊附上名字,讓讀者一眼就能看出誰在哪裡。請在指標移動範圍所在的定位元素內,為包含 presence-name 的元素加上 presence 和該人的識別色(presence-1 到 presence-8),並以 --presence-x 和 --presence-y 設定它的位置。

協作者游標

<div class="sheet relative overflow-hidden w-full max-w(520) h(264)" id="board">  <div class="texture texture-grid absolute inset(0)"></div>  <div class="sheet sheet-raised absolute p-md text-sm w(168)" style="top: 32px; left: 28px">    <p class="m(0) fg-text-strong">Launch checklist</p>    <p class="m(0) mt-xs fg-text-muted">Docs, changelog and the post</p>  </div>  <div class="sheet sheet-raised absolute p-md text-sm w(168)" style="top: 128px; left: 260px">    <p class="m(0) fg-text-strong">Open questions</p>    <p class="m(0) mt-xs fg-text-muted">Who writes the release notes?</p>  </div>  <div class="presence presence-6" style="--presence-x: 150px; --presence-y: 78px" aria-hidden="true">    <span class="presence-name">Ada Lovelace</span>  </div>  <div class="presence presence-4" id="moving" style="--presence-x: 340px; --presence-y: 40px" aria-hidden="true">    <span class="presence-name">Alan Turing</span>  </div>  <div class="presence presence-8" style="--presence-x: 96px; --presence-y: 176px" aria-hidden="true">    <span class="presence-name">Grace Hopper</span>    <span class="presence-message">I can take the release notes</span>  </div></div><script>const pointer = document.getElementById('moving')const path = [[340, 40], [372, 64], [398, 108], [360, 150], [318, 132], [300, 84]]let at = 0setInterval(() => {  at = (at + 1) % path.length  pointer.style.setProperty('--presence-x', path[at][0] + 'px')  pointer.style.setProperty('--presence-y', path[at][1] + 'px')}, 700)</script>
白板上的三個人。Alan 的指標會依應用程式回報的位置移動,並在收到的各點之間滑順移動;啟用減少動態效果時則直接跳到定點。

#指標的位置

指標的尖端就是它所指的點。--presence-x 和 --presence-y 是它與最近的定位元素左上角之間的距離,以像素計;這個定位元素通常是畫布或白板,會加上 relative 和 overflow-hidden,讓靠近邊緣的指標在邊緣處被裁切。應用程式知道每個人在哪裡、何時離開:有人加入時加上一個 presence,對方指標移動時設定這兩個屬性,對方離開時再將它移除。繪製和動態由樣式表負責;它本身沒有腳本。

app.js
function move(person, x, y) {  const pointer = board.querySelector(`[data-person="${person.id}"]`)  pointer.style.setProperty('--presence-x', `${x}px`)  pointer.style.setProperty('--presence-y', `${y}px`)}
  • 動態。 指標會以 160ms 沿直線滑到每個新位置,將連線每秒傳來的少數幾個位置轉為平穩的移動。在 prefers-reduced-motion: reduce 下則直接跳到定點。
  • 不擋路。 指標不接收任何指標事件,因此絕不會擋在讀者自己的指標和下方內容之間。
  • 座標。 請以畫布本身的單位傳送座標,並在設定屬性前轉換為讀者的檢視座標,這樣視窗大小和縮放比例不同的兩個人,才會指向同一個東西。

#訊息

名字下方的 presence-message 會顯示對方正在輸入的簡短文字,就像游標聊天(cursor chat)一樣,例如針對所指內容提出的問題。訊息寬度最多為 15rem,超出時會換行。請在對方停止輸入幾秒後將它移除。

#識別色

在 Master UI 中,色相用來表示狀態,在這裡則用來代表一個人。主題提供八種識別色,從 --color-identity-1 到 --color-identity-8,明度全都相同:每個人在整個工作階段中保有其中一種,名字也一定顯示在旁邊,因此顏色絕不是區分不同人的唯一方式。--color-identity-ink 是所有識別色上的文字顏色,對比度在淺色配色中至少為 4.8:1,在深色配色中至少為 9:1。

在指標、文字游標、選取範圍或單獨顯示的名字上,用修飾類別 presence-1 到 presence-8 選擇其中一種。若產品有自己用來代表人員的顏色,請改為設定 --presence-color;請選擇明度足以讓 --color-identity-ink 清楚顯示的顏色,或同時設定該 token。

識別色

<span class="presence-name presence-1">Ada</span><span class="presence-name presence-2">Alan</span><span class="presence-name presence-3">Grace</span><span class="presence-name presence-4">Edsger</span><span class="presence-name presence-5">Barbara</span><span class="presence-name presence-6">Donald</span><span class="presence-name presence-7">Margaret</span><span class="presence-name presence-8">Ken</span>
<>  <span className="presence-name presence-1">Ada</span>  <span className="presence-name presence-2">Alan</span>  <span className="presence-name presence-3">Grace</span>  <span className="presence-name presence-4">Edsger</span>  <span className="presence-name presence-5">Barbara</span>  <span className="presence-name presence-6">Donald</span>  <span className="presence-name presence-7">Margaret</span>  <span className="presence-name presence-8">Ken</span></>
<span class="presence-name presence-1">Ada</span><span class="presence-name presence-2">Alan</span><span class="presence-name presence-3">Grace</span><span class="presence-name presence-4">Edsger</span><span class="presence-name presence-5">Barbara</span><span class="presence-name presence-6">Donald</span><span class="presence-name presence-7">Margaret</span><span class="presence-name presence-8">Ken</span>
<template>  <span class="presence-name presence-1">Ada</span>  <span class="presence-name presence-2">Alan</span>  <span class="presence-name presence-3">Grace</span>  <span class="presence-name presence-4">Edsger</span>  <span class="presence-name presence-5">Barbara</span>  <span class="presence-name presence-6">Donald</span>  <span class="presence-name presence-7">Margaret</span>  <span class="presence-name presence-8">Ken</span></template>
八種識別色,依工作階段分配的順序排列。

請在有人加入時依此順序分配顏色,並將離開者的顏色分給下一位加入的人。若每位讀者都必須看到同一個人使用同一種顏色,請從穩定的數字選擇,例如該人 id 的雜湊值。超過八個人時顏色會重複,但名字仍能區分他們。

#在文字中

在共同編輯的文件中,其他人的文字游標是位於兩個字母之間的 presence-caret:一條與行高等高、以對方顏色繪製的 2px 直線,上方立著一面寫著對方名字的旗標。它不占空間,因此它移動時周圍的文字不會跟著移動。對方的選取範圍是包住所選文字的 presence-selection:在文字後方以對方顏色的淡色調鋪底,並延續到跨行的部分。

文字游標與選取範圍

<div class="sheet p-lg max-w(440)">  <p class="m(0) text-sm fg-text-body leading-md">    The release ships <span class="presence-selection presence-4">on Thursday, after the docs build</span><span class="presence-caret presence-4" aria-hidden="true">      <span class="presence-name">Alan</span>    </span> passes, and the post goes out the same afternoon. Ada writes the changelog<span class="presence-caret presence-6" aria-hidden="true">      <span class="presence-name">Ada</span>    </span> while Grace checks the links.  </p></div>
<div className="sheet p-lg max-w(440)">  <p className="m(0) text-sm fg-text-body leading-md">    The release ships <span className="presence-selection presence-4">on Thursday, after the docs build</span><span className="presence-caret presence-4" aria-hidden="true">      <span className="presence-name">Alan</span>    </span> passes, and the post goes out the same afternoon. Ada writes the changelog<span className="presence-caret presence-6" aria-hidden="true">      <span className="presence-name">Ada</span>    </span> while Grace checks the links.  </p></div>
<div class="sheet p-lg max-w(440)">  <p class="m(0) text-sm fg-text-body leading-md">    The release ships <span class="presence-selection presence-4">on Thursday, after the docs build</span><span class="presence-caret presence-4" aria-hidden="true">      <span class="presence-name">Alan</span>    </span> passes, and the post goes out the same afternoon. Ada writes the changelog<span class="presence-caret presence-6" aria-hidden="true">      <span class="presence-name">Ada</span>    </span> while Grace checks the links.  </p></div>
<template>  <div class="sheet p-lg max-w(440)">    <p class="m(0) text-sm fg-text-body leading-md">      The release ships <span class="presence-selection presence-4">on Thursday, after the docs build</span><span class="presence-caret presence-4" aria-hidden="true">        <span class="presence-name">Alan</span>      </span> passes, and the post goes out the same afternoon. Ada writes the changelog<span class="presence-caret presence-6" aria-hidden="true">        <span class="presence-name">Ada</span>      </span> while Grace checks the links.    </p>  </div></template>
Alan 選取了一段文字,他的文字游標位於選取範圍的結尾;Ada 的文字游標則位於她正在輸入的地方。

編輯器會依協作層回報的其他人位置放置這些標記,作為它自行算繪和移除的裝飾;它們不屬於文件內容。請在第一行上方保留放置名字旗標的空間,或讓第一行文字游標的旗標延伸到文字容器的內距中。

#何時改用其他元件

  • 若要顯示誰在場,而不是在哪裡,請在標題列使用一疊頭像。
  • 應用程式仍在寫入的文字結尾標記,請使用文字游標。
  • 為讀者醒目標示的文字,例如符合搜尋條件的文字,請使用醒目標記。

#無障礙

  • 指標和文字游標屬於裝飾:請為每一個加上 aria-hidden="true",避免在畫布或句子中間讀出它們的名字。請在其他地方以文字說明誰在場,例如在標題列放上附有名字的頭像;若協作者的加入或離開會影響目前的工作,請在設為 polite 的即時區域中宣告這項變動。
  • 選取範圍不會隱藏:其中的文字屬於文件內容。淡色調不會改變文字顏色,因此文字仍保有原本的對比度。
  • 顏色絕不是唯一的線索:每個指標和文字游標旁都有名字。
  • 強制色彩模式下,箭頭和文字游標以系統色繪製,旗標和對話泡泡則會加上外框線。

#類別

類別說明
presence指標:以該人識別色繪製的箭頭,箭頭尖端位於所在定位元素中的 --presence-x 和 --presence-y。會平滑移動到每個新位置,且不接收指標事件
presence-name該人的名稱,顯示在其顏色的旗標中,文字使用識別色搭配的前景色:可放在箭頭尖端旁、立在文字游標上,或單獨出現在在場者清單中
presence-message該人正在輸入的簡短訊息,以其顏色的對話泡泡顯示在名稱下方
presence-caret其他人在文字中的游標:與行高相同的行內直條,使用其識別色,不占空間。內含的 presence-name 會立在游標頂端
presence-selection其他人的選取範圍:在所選文字後方鋪上其識別色的淡色,可跨行延續
presence-1識別色 1,--color-identity-1:紅色色相。未使用修飾類別或 --presence-color 時,presence 會採用此顏色搭配 presence 或 presence-caret 或 presence-selection 或 presence-name 使用
presence-2識別色 2,--color-identity-2:橙色色相搭配 presence 或 presence-caret 或 presence-selection 或 presence-name 使用
presence-3識別色 3,--color-identity-3:琥珀色色相搭配 presence 或 presence-caret 或 presence-selection 或 presence-name 使用
presence-4識別色 4,--color-identity-4:綠色色相搭配 presence 或 presence-caret 或 presence-selection 或 presence-name 使用
presence-5識別色 5,--color-identity-5:青綠色色相搭配 presence 或 presence-caret 或 presence-selection 或 presence-name 使用
presence-6識別色 6,--color-identity-6:藍色色相搭配 presence 或 presence-caret 或 presence-selection 或 presence-name 使用
presence-7識別色 7,--color-identity-7:紫色色相搭配 presence 或 presence-caret 或 presence-selection 或 presence-name 使用
presence-8識別色 8,--color-identity-8:粉紅色色相搭配 presence 或 presence-caret 或 presence-selection 或 presence-name 使用

#自訂屬性

屬性預設值說明
--presence-colorvar(--color-identity-1)該人的顏色。presence-1 到 presence-8 修飾類別會將它設為對應的識別色;若要使用產品自有的顏色,請直接設定。presence、presence-caret、presence-selection 和 presence-name 都會讀取此值
--presence-x0px箭頭尖端的位置,從所在定位元素的左緣起算。由應用程式設定
--presence-y0px箭頭尖端的位置,從該元素的上緣起算。由應用程式設定
--presence-arrow1.25rem箭頭的大小;名稱會顯示在箭頭尖端旁

#原始碼

@master/ui/components/presence.css
/* * Presence: where another person is, drawn in the color that names them. * A pointer is an arrow standing at the point the page gives it, with the person's name in a flag down and to * the side of its tip and, when they write one, a short message in a bubble under the name. In text, another * person's caret is a 2px bar in the line with their name above it, and their selection is a tint of their color * behind the words. * The color is the person's identity: one of --color-identity-1 to 8, chosen with presence-1 to presence-8, * or any color set as --presence-color. Every identity takes --color-identity-ink as its text. Hue means a * person here and nothing else. * Geometry: a 20px arrow whose tip is the point, cut out of the page by a ring of the identity ink so it reads * on any content; a 12px name in a flag with the chip's corner; a 13px message in a bubble with the control's * corner, at most 15rem wide. The flag and the bubble stand on the page with the raised shadow. * Coordinates and presence are the application's: it sets --presence-x and --presence-y, in pixels from the * top-left corner of the positioned element that holds the pointers, and adds and removes the elements. A * pointer glides to each new point and jumps under reduced motion. Nothing here takes a pointer event, so a * pointer never stands in the way of the reader's own. */@layer components.ui {  .presence {    --presence-color: var(--color-identity-1);    --presence-x: 0px;    --presence-y: 0px;    --presence-arrow: 1.25rem;    position: absolute;    top: 0;    left: 0;    z-index: 1;    display: flex;    flex-direction: column;    align-items: flex-start;    gap: .25rem;    width: max-content;    max-width: 15rem;    /* The flag starts under the arrow's tail, beside its point. */    padding: calc(var(--presence-arrow) * .85) 0 0 calc(var(--presence-arrow) * .6);    translate: var(--presence-x) var(--presence-y);    pointer-events: none;    transition-property: translate;    transition-duration: var(--duration-quick);    transition-timing-function: linear;    /* The point is placed from the left whatever the direction, so right to left the flag still starts beside it. */    &:dir(rtl) {      align-items: flex-end;    }    /*     * The arrow: a ring of the identity ink under a face of the identity color. Its tip, 2 of its 20 units in     * from the corner, is the point.     */    &::before, &::after {      content: '';      position: absolute;      top: calc(var(--presence-arrow) * -.1);      left: calc(var(--presence-arrow) * -.1);      width: var(--presence-arrow);      height: var(--presence-arrow);    }    &::before {      background: var(--color-identity-ink);      mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 20 20'%3E%3Cpath d='M2 2 18 8.6 10.6 10.6 8.6 18Z' fill='black' stroke='black' stroke-width='3.5' stroke-linejoin='round'/%3E%3C/svg%3E") center / contain no-repeat;    }    &::after {      background: var(--presence-color, var(--color-identity-1));      mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 20 20'%3E%3Cpath d='M2 2 18 8.6 10.6 10.6 8.6 18Z' fill='black' stroke='black' stroke-width='1' stroke-linejoin='round'/%3E%3C/svg%3E") center / contain no-repeat;    }    @media (prefers-reduced-motion: reduce) {      transition-duration: 0s;    }    @media (forced-colors: active) {      &::before, &::after {        forced-color-adjust: none;      }      &::before {        background: Canvas;      }      &::after {        background: CanvasText;      }    }  }  /* The person's name, in a flag of their color. */  .presence-name {    max-width: 100%;    margin: 0;    padding: .125rem .375rem;    overflow: hidden;    border-radius: calc(var(--radius-chip) * var(--corner-scale));    corner-shape: var(--corner-shape);    background: var(--presence-color, var(--color-identity-1));    color: var(--color-identity-ink);    box-shadow: 0 0 0 .5px var(--color-line-on-color), 0 1px 2px -1px var(--color-shade-1), 0 2px 4px -2px var(--color-shade-2), 0 4px 8px -4px var(--color-shade-3);    font: var(--font-weight-control) .75rem/1.35 var(--font-family-sans);    font-feature-settings: inherit;    letter-spacing: 0;    text-overflow: ellipsis;    white-space: nowrap;    @media (forced-colors: active) {      outline: 1px solid CanvasText;      outline-offset: -1px;    }  }  /* What the person is saying, in a bubble under the name: a few words, as cursor chat has them. */  .presence-message {    max-width: 100%;    margin: 0;    padding: .375rem .625rem;    border-radius: calc(var(--radius-control) * var(--corner-scale));    corner-shape: var(--corner-shape);    background: var(--presence-color, var(--color-identity-1));    color: var(--color-identity-ink);    box-shadow: 0 0 0 .5px var(--color-line-on-color), 0 1px 2px -1px var(--color-shade-1), 0 2px 4px -2px var(--color-shade-2), 0 4px 8px -4px var(--color-shade-3);    font: 400 .8125rem/1.4 var(--font-family-sans);    font-feature-settings: inherit;    text-wrap: pretty;    overflow-wrap: anywhere;    @media (forced-colors: active) {      outline: 1px solid CanvasText;      outline-offset: -1px;    }  }  /*   * Another person's caret in text: an inline mark between two letters, a bar as tall as the line of letters,   * with the name in a flag that stands on it. It takes no room in the line.   */  .presence-caret {    --presence-color: var(--color-identity-1);    position: relative;    display: inline-block;    width: 2px;    height: 1.2em;    margin-inline: -1px;    border-radius: 999px;    corner-shape: round;    background: var(--presence-color, var(--color-identity-1));    vertical-align: -.2em;    pointer-events: none;    .presence-name {      position: absolute;      bottom: 100%;      inset-inline-start: 0;      max-width: 12rem;      padding: 0 .3125rem;      border-end-start-radius: 0;      font-size: .6875rem;      line-height: 1.45;    }    /*     * The bar keeps the system's highlight. forced-color-adjust is inherited, so the name on it is handed back     * to forced colors and drawn as a name standing alone is.     */    @media (forced-colors: active) {      forced-color-adjust: none;      background: Highlight;      .presence-name {        forced-color-adjust: auto;      }    }  }  /*   * The eight identities, by class: presence-1 to presence-8 on a pointer, a caret, a selection or a name   * that stands alone. Naming each token here also keeps it in a stylesheet compiled for the classes a page   * uses. --presence-color takes any other color a product gives a person.   */  :is(.presence, .presence-caret, .presence-selection, .presence-name) {    &.presence-1 {      --presence-color: var(--color-identity-1);    }    &.presence-2 {      --presence-color: var(--color-identity-2);    }    &.presence-3 {      --presence-color: var(--color-identity-3);    }    &.presence-4 {      --presence-color: var(--color-identity-4);    }    &.presence-5 {      --presence-color: var(--color-identity-5);    }    &.presence-6 {      --presence-color: var(--color-identity-6);    }    &.presence-7 {      --presence-color: var(--color-identity-7);    }    &.presence-8 {      --presence-color: var(--color-identity-8);    }  }  /* Another person's selection: a tint of their color behind the words, carried across lines. */  .presence-selection {    --presence-color: var(--color-identity-1);    padding-block: .1em;    border-radius: calc(2px * var(--corner-scale));    corner-shape: var(--corner-shape);    background: color-mix(in oklab, var(--presence-color) 24%, transparent);    color: inherit;    box-decoration-break: clone;    -webkit-box-decoration-break: clone;    @media (forced-colors: active) {      outline: 1px dotted CanvasText;    }  }}