导航

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 显示此人正在输入的几个字,就像光标聊天那样:例如针对其所指内容的一个问题。消息宽度上限为 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 指定的文字颜色有足够的对比度,或者同时设置该自定义属性。

身份色

<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",以免在画布或句子中间读出它们的名字。请在其他地方用文字说明谁在这里,例如标题栏中带名字的头像;如果到来和离开对任务很重要,请在礼貌型实时区域中播报。
  • 选区不隐藏:其中的文字属于文档。浅色调不会改变文字颜色,因此文字保持原有的对比度。
  • 颜色从来不是唯一的线索:每个指针和插入光标旁边都有名字。
  • 在强制颜色模式下,箭头和插入光标使用系统颜色绘制,小旗和气泡带有轮廓线。

#类名

类名说明
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;    }  }}