导航

v2.1.0
内容

表情回应

消息下方的每个表情回应都显示人数,按下即可添加自己的回应。

本页内容

表情回应是读者留在消息下方的表情符号,旁边显示还有多少人用了同一个表情。每条回应都用扁平胶囊包裹原生复选框,色调与胶囊标签一致。按一次添加自己的回应,再按一次撤回。计数由 CSS 计数器根据复选框的选中状态增减,无需脚本。

表情回应

<ol class="max-w(400) w-full mx-auto m(0) p(0)">  <li class="message">    <span class="avatar avatar-sm message-avatar">      <img class="avatar-image" src="/demo/portrait-1.svg" alt="Ada">    </span>    <div class="message-bubble">The new previews are live. Have a look before the review.</div>    <div class="reaction-group" role="group" aria-label="Reactions to Ada's message">      <label class="reaction" style="--count: 3">        <input type="checkbox" class="reaction-input" name="reaction" value="👍" aria-label="Thumbs up, 3 others" checked>        <span class="reaction-emoji" aria-hidden="true">👍</span>        <span class="reaction-count" aria-hidden="true"></span>      </label>      <label class="reaction" style="--count: 2">        <input type="checkbox" class="reaction-input" name="reaction" value="🎉" aria-label="Party popper, 2 others">        <span class="reaction-emoji" aria-hidden="true">🎉</span>        <span class="reaction-count" aria-hidden="true"></span>      </label>      <label class="reaction" style="--count: 1">        <input type="checkbox" class="reaction-input" name="reaction" value="👀" aria-label="Eyes, 1 other">        <span class="reaction-emoji" aria-hidden="true">👀</span>        <span class="reaction-count" aria-hidden="true"></span>      </label>    </div>    <span class="message-meta">Ada · 14:03</span>  </li>  <li class="message message-sent">    <div class="message-bubble">On it. The dark ones look right.</div>    <div class="reaction-group" role="group" aria-label="Reactions to your message">      <label class="reaction" style="--count: 1">        <input type="checkbox" class="reaction-input" name="reaction-reply" value="❤️" aria-label="Heart, 1 other">        <span class="reaction-emoji" aria-hidden="true">❤️</span>        <span class="reaction-count" aria-hidden="true"></span>      </label>    </div>  </li></ol>
<ol className="max-w(400) w-full mx-auto m(0) p(0)">  <li className="message">    <span className="avatar avatar-sm message-avatar">      <img className="avatar-image" src="/demo/portrait-1.svg" alt="Ada" />    </span>    <div className="message-bubble">The new previews are live. Have a look before the review.</div>    <div className="reaction-group" role="group" aria-label="Reactions to Ada's message">      <label className="reaction" style={{ '--count': '3' } as React.CSSProperties}>        <input type="checkbox" className="reaction-input" name="reaction" value="👍" aria-label="Thumbs up, 3 others" defaultChecked />        <span className="reaction-emoji" aria-hidden="true">👍</span>        <span className="reaction-count" aria-hidden="true"></span>      </label>      <label className="reaction" style={{ '--count': '2' } as React.CSSProperties}>        <input type="checkbox" className="reaction-input" name="reaction" value="🎉" aria-label="Party popper, 2 others" />        <span className="reaction-emoji" aria-hidden="true">🎉</span>        <span className="reaction-count" aria-hidden="true"></span>      </label>      <label className="reaction" style={{ '--count': '1' } as React.CSSProperties}>        <input type="checkbox" className="reaction-input" name="reaction" value="👀" aria-label="Eyes, 1 other" />        <span className="reaction-emoji" aria-hidden="true">👀</span>        <span className="reaction-count" aria-hidden="true"></span>      </label>    </div>    <span className="message-meta">Ada · 14:03</span>  </li>  <li className="message message-sent">    <div className="message-bubble">On it. The dark ones look right.</div>    <div className="reaction-group" role="group" aria-label="Reactions to your message">      <label className="reaction" style={{ '--count': '1' } as React.CSSProperties}>        <input type="checkbox" className="reaction-input" name="reaction-reply" value="❤️" aria-label="Heart, 1 other" />        <span className="reaction-emoji" aria-hidden="true">❤️</span>        <span className="reaction-count" aria-hidden="true"></span>      </label>    </div>  </li></ol>
<ol class="max-w(400) w-full mx-auto m(0) p(0)">  <li class="message">    <span class="avatar avatar-sm message-avatar">      <img class="avatar-image" src="/demo/portrait-1.svg" alt="Ada">    </span>    <div class="message-bubble">The new previews are live. Have a look before the review.</div>    <div class="reaction-group" role="group" aria-label="Reactions to Ada's message">      <label class="reaction" style="--count: 3">        <input type="checkbox" class="reaction-input" name="reaction" value="👍" aria-label="Thumbs up, 3 others" checked>        <span class="reaction-emoji" aria-hidden="true">👍</span>        <span class="reaction-count" aria-hidden="true"></span>      </label>      <label class="reaction" style="--count: 2">        <input type="checkbox" class="reaction-input" name="reaction" value="🎉" aria-label="Party popper, 2 others">        <span class="reaction-emoji" aria-hidden="true">🎉</span>        <span class="reaction-count" aria-hidden="true"></span>      </label>      <label class="reaction" style="--count: 1">        <input type="checkbox" class="reaction-input" name="reaction" value="👀" aria-label="Eyes, 1 other">        <span class="reaction-emoji" aria-hidden="true">👀</span>        <span class="reaction-count" aria-hidden="true"></span>      </label>    </div>    <span class="message-meta">Ada · 14:03</span>  </li>  <li class="message message-sent">    <div class="message-bubble">On it. The dark ones look right.</div>    <div class="reaction-group" role="group" aria-label="Reactions to your message">      <label class="reaction" style="--count: 1">        <input type="checkbox" class="reaction-input" name="reaction-reply" value="❤️" aria-label="Heart, 1 other">        <span class="reaction-emoji" aria-hidden="true">❤️</span>        <span class="reaction-count" aria-hidden="true"></span>      </label>    </div>  </li></ol>
<template>  <ol class="max-w(400) w-full mx-auto m(0) p(0)">    <li class="message">      <span class="avatar avatar-sm message-avatar">        <img class="avatar-image" src="/demo/portrait-1.svg" alt="Ada">      </span>      <div class="message-bubble">The new previews are live. Have a look before the review.</div>      <div class="reaction-group" role="group" aria-label="Reactions to Ada's message">        <label class="reaction" style="--count: 3">          <input type="checkbox" class="reaction-input" name="reaction" value="👍" aria-label="Thumbs up, 3 others" checked>          <span class="reaction-emoji" aria-hidden="true">👍</span>          <span class="reaction-count" aria-hidden="true"></span>        </label>        <label class="reaction" style="--count: 2">          <input type="checkbox" class="reaction-input" name="reaction" value="🎉" aria-label="Party popper, 2 others">          <span class="reaction-emoji" aria-hidden="true">🎉</span>          <span class="reaction-count" aria-hidden="true"></span>        </label>        <label class="reaction" style="--count: 1">          <input type="checkbox" class="reaction-input" name="reaction" value="👀" aria-label="Eyes, 1 other">          <span class="reaction-emoji" aria-hidden="true">👀</span>          <span class="reaction-count" aria-hidden="true"></span>        </label>      </div>      <span class="message-meta">Ada · 14:03</span>    </li>    <li class="message message-sent">      <div class="message-bubble">On it. The dark ones look right.</div>      <div class="reaction-group" role="group" aria-label="Reactions to your message">        <label class="reaction" style="--count: 1">          <input type="checkbox" class="reaction-input" name="reaction-reply" value="❤️" aria-label="Heart, 1 other">          <span class="reaction-emoji" aria-hidden="true">❤️</span>          <span class="reaction-count" aria-hidden="true"></span>        </label>      </div>    </li>  </ol></template>
按下表情回应即可添加自己的回应,再按一次即可撤回。

将 reaction-group 放在消息气泡之后。收到的消息下方,回应组与气泡起始边对齐。发出的消息下方,回应组与气泡结束边对齐。用 aria-label 为回应组命名,屏幕上有多条消息时,注明这组回应对应谁的消息。

#计数

将每条回应的 --count 设为已给出该回应的其他读者人数,不计当前读者。胶囊从这个值开始计数,复选框选中时加一,因此读者已给出的回应会显示比 --count 大一的数字。将 reaction-count 留空,计数器就会填入数字。人数太多、不适合逐个计数时,可在其中写入 1.2k 之类的文本,界面会显示这段文本。

#状态

默认状态下,回应呈扁平胶囊形,带一圈轮廓,计数用较淡的墨色显示。指针悬停时轮廓更明显,按下时胶囊沉入凹槽,表情符号随之下沉。给出回应后,胶囊采用库的选中样式,显示墨色细线和淡墨色底,计数改用较深的墨色。复选框禁用时,例如讨论串关闭后,胶囊保持平坦,表情符号颜色变淡。

状态

<div class="reaction-group" role="group" aria-label="Reactions">  <label class="reaction" style="--count: 2">    <input type="checkbox" class="reaction-input" name="reaction-states" value="👏" aria-label="Clapping, 2 others">    <span class="reaction-emoji" aria-hidden="true">👏</span>    <span class="reaction-count" aria-hidden="true"></span>  </label>  <label class="reaction" style="--count: 2">    <input type="checkbox" class="reaction-input" name="reaction-states" value="❤️" aria-label="Heart, 2 others" checked>    <span class="reaction-emoji" aria-hidden="true">❤️</span>    <span class="reaction-count" aria-hidden="true"></span>  </label>  <label class="reaction">    <input type="checkbox" class="reaction-input" name="reaction-states" value="🔥" aria-label="Fire, 1,214 others">    <span class="reaction-emoji" aria-hidden="true">🔥</span>    <span class="reaction-count" aria-hidden="true">1.2k</span>  </label>  <label class="reaction" style="--count: 4">    <input type="checkbox" class="reaction-input" name="reaction-states" value="👍" aria-label="Thumbs up, 4 others" disabled>    <span class="reaction-emoji" aria-hidden="true">👍</span>    <span class="reaction-count" aria-hidden="true"></span>  </label></div>
<div className="reaction-group" role="group" aria-label="Reactions">  <label className="reaction" style={{ '--count': '2' } as React.CSSProperties}>    <input type="checkbox" className="reaction-input" name="reaction-states" value="👏" aria-label="Clapping, 2 others" />    <span className="reaction-emoji" aria-hidden="true">👏</span>    <span className="reaction-count" aria-hidden="true"></span>  </label>  <label className="reaction" style={{ '--count': '2' } as React.CSSProperties}>    <input type="checkbox" className="reaction-input" name="reaction-states" value="❤️" aria-label="Heart, 2 others" defaultChecked />    <span className="reaction-emoji" aria-hidden="true">❤️</span>    <span className="reaction-count" aria-hidden="true"></span>  </label>  <label className="reaction">    <input type="checkbox" className="reaction-input" name="reaction-states" value="🔥" aria-label="Fire, 1,214 others" />    <span className="reaction-emoji" aria-hidden="true">🔥</span>    <span className="reaction-count" aria-hidden="true">1.2k</span>  </label>  <label className="reaction" style={{ '--count': '4' } as React.CSSProperties}>    <input type="checkbox" className="reaction-input" name="reaction-states" value="👍" aria-label="Thumbs up, 4 others" disabled />    <span className="reaction-emoji" aria-hidden="true">👍</span>    <span className="reaction-count" aria-hidden="true"></span>  </label></div>
<div class="reaction-group" role="group" aria-label="Reactions">  <label class="reaction" style="--count: 2">    <input type="checkbox" class="reaction-input" name="reaction-states" value="👏" aria-label="Clapping, 2 others">    <span class="reaction-emoji" aria-hidden="true">👏</span>    <span class="reaction-count" aria-hidden="true"></span>  </label>  <label class="reaction" style="--count: 2">    <input type="checkbox" class="reaction-input" name="reaction-states" value="❤️" aria-label="Heart, 2 others" checked>    <span class="reaction-emoji" aria-hidden="true">❤️</span>    <span class="reaction-count" aria-hidden="true"></span>  </label>  <label class="reaction">    <input type="checkbox" class="reaction-input" name="reaction-states" value="🔥" aria-label="Fire, 1,214 others">    <span class="reaction-emoji" aria-hidden="true">🔥</span>    <span class="reaction-count" aria-hidden="true">1.2k</span>  </label>  <label class="reaction" style="--count: 4">    <input type="checkbox" class="reaction-input" name="reaction-states" value="👍" aria-label="Thumbs up, 4 others" disabled>    <span class="reaction-emoji" aria-hidden="true">👍</span>    <span class="reaction-count" aria-hidden="true"></span>  </label></div>
<template>  <div class="reaction-group" role="group" aria-label="Reactions">    <label class="reaction" style="--count: 2">      <input type="checkbox" class="reaction-input" name="reaction-states" value="👏" aria-label="Clapping, 2 others">      <span class="reaction-emoji" aria-hidden="true">👏</span>      <span class="reaction-count" aria-hidden="true"></span>    </label>    <label class="reaction" style="--count: 2">      <input type="checkbox" class="reaction-input" name="reaction-states" value="❤️" aria-label="Heart, 2 others" checked>      <span class="reaction-emoji" aria-hidden="true">❤️</span>      <span class="reaction-count" aria-hidden="true"></span>    </label>    <label class="reaction">      <input type="checkbox" class="reaction-input" name="reaction-states" value="🔥" aria-label="Fire, 1,214 others">      <span class="reaction-emoji" aria-hidden="true">🔥</span>      <span class="reaction-count" aria-hidden="true">1.2k</span>    </label>    <label class="reaction" style="--count: 4">      <input type="checkbox" class="reaction-input" name="reaction-states" value="👍" aria-label="Thumbs up, 4 others" disabled>      <span class="reaction-emoji" aria-hidden="true">👍</span>      <span class="reaction-count" aria-hidden="true"></span>    </label>  </div></template>

#添加表情回应

reaction-add 是回应行末尾带笑脸和加号的圆形按钮,用于打开页面的表情选择器。选择器是一个弹出层,内含样式低调的方形按钮,每个按钮都包含一个 reaction-emoji。紧随按钮的弹出层打开期间,按钮保持下沉。选择表情即可给出相应的回应。如果此前无人给出该回应,还会新增对应的胶囊。新增胶囊由页面负责,因此选择器需要几行脚本。本页其余功能均可在无脚本的情况下运行。

添加表情回应

<div class="reaction-group" id="reactions" role="group" aria-label="Reactions">  <label class="reaction" style="--count: 4">    <input type="checkbox" class="reaction-input" name="reaction" value="👍" aria-label="Thumbs up, 4 others">    <span class="reaction-emoji" aria-hidden="true">👍</span>    <span class="reaction-count" aria-hidden="true"></span>  </label>  <label class="reaction" style="--count: 1">    <input type="checkbox" class="reaction-input" name="reaction" value="🚀" aria-label="Rocket, 1 other">    <span class="reaction-emoji" aria-hidden="true">🚀</span>    <span class="reaction-count" aria-hidden="true"></span>  </label>  <button type="button" class="reaction-add" popovertarget="reaction-picker" aria-label="Add a reaction"></button>  <div class="popover p(4)" id="reaction-picker" popover>    <div class="grid grid-cols(6) gap(2)">      <button type="button" class="btn btn-quiet btn-square btn-sm" value="👍" aria-label="Thumbs up">        <span class="reaction-emoji" aria-hidden="true">👍</span>      </button>      <button type="button" class="btn btn-quiet btn-square btn-sm" value="❤️" aria-label="Heart">        <span class="reaction-emoji" aria-hidden="true">❤️</span>      </button>      <button type="button" class="btn btn-quiet btn-square btn-sm" value="😂" aria-label="Laughing">        <span class="reaction-emoji" aria-hidden="true">😂</span>      </button>      <button type="button" class="btn btn-quiet btn-square btn-sm" value="🎉" aria-label="Party popper">        <span class="reaction-emoji" aria-hidden="true">🎉</span>      </button>      <button type="button" class="btn btn-quiet btn-square btn-sm" value="👀" aria-label="Eyes">        <span class="reaction-emoji" aria-hidden="true">👀</span>      </button>      <button type="button" class="btn btn-quiet btn-square btn-sm" value="🚀" aria-label="Rocket">        <span class="reaction-emoji" aria-hidden="true">🚀</span>      </button>    </div>  </div></div><script>const picker = document.getElementById('reaction-picker')const reactions = document.getElementById('reactions')picker.addEventListener('click', function (event) {  const option = event.target.closest('button')  if (!option) return  let input = Array.from(reactions.querySelectorAll('.reaction-input')).find(function (each) { return each.value === option.value })  if (!input) {    const reaction = document.createElement('label')    const emoji = document.createElement('span')    const count = document.createElement('span')    input = document.createElement('input')    reaction.className = 'reaction'    input.type = 'checkbox'    input.className = 'reaction-input'    input.name = 'reaction'    input.value = option.value    input.setAttribute('aria-label', option.getAttribute('aria-label') + ', no others')    emoji.className = 'reaction-emoji'    emoji.textContent = option.value    emoji.setAttribute('aria-hidden', 'true')    count.className = 'reaction-count'    count.setAttribute('aria-hidden', 'true')    reaction.append(input, emoji, count)    reactions.insertBefore(reaction, reactions.querySelector('.reaction-add'))  }  if (!input.checked) input.click()  picker.hidePopover()})</script>
打开选择器,选择一个回应行中尚未出现的表情。

#在讨论串中

在讨论串中,回应行紧随评论正文,与正文的间距略大于段落间距。

在讨论串中

<div class="card max-w(480) mx-auto">  <ol class="thread">    <li class="thread-item">      <span class="thread-avatar"><span class="avatar">GH</span></span>      <div class="thread-body">        <p class="thread-header">          <span class="thread-author">Grace Hopper</span>          <time class="thread-meta" datetime="2026-11-12T09:12">2h</time>        </p>        <p class="thread-text">          The contracts ship on Tuesday, the framework components the week after.        </p>        <div class="reaction-group" role="group" aria-label="Reactions to Grace Hopper's comment">          <label class="reaction" style="--count: 5">            <input type="checkbox" class="reaction-input" name="reaction-thread" value="👍" aria-label="Thumbs up, 5 others">            <span class="reaction-emoji" aria-hidden="true">👍</span>            <span class="reaction-count" aria-hidden="true"></span>          </label>          <label class="reaction" style="--count: 2">            <input type="checkbox" class="reaction-input" name="reaction-thread" value="🚀" aria-label="Rocket, 2 others" checked>            <span class="reaction-emoji" aria-hidden="true">🚀</span>            <span class="reaction-count" aria-hidden="true"></span>          </label>        </div>      </div>      <ol class="thread-replies">        <li class="thread-item">          <span class="thread-avatar"><span class="avatar avatar-sm">AL</span></span>          <div class="thread-body">            <p class="thread-header">              <span class="thread-author">Ada Lovelace</span>              <time class="thread-meta" datetime="2026-11-12T10:05">1h</time>            </p>            <p class="thread-text">Then the release notes go out on Monday.</p>          </div>        </li>      </ol>    </li>  </ol></div>
<div className="card max-w(480) mx-auto">  <ol className="thread">    <li className="thread-item">      <span className="thread-avatar"><span className="avatar">GH</span></span>      <div className="thread-body">        <p className="thread-header">          <span className="thread-author">Grace Hopper</span>          <time className="thread-meta" dateTime="2026-11-12T09:12">2h</time>        </p>        <p className="thread-text">          The contracts ship on Tuesday, the framework components the week after.        </p>        <div className="reaction-group" role="group" aria-label="Reactions to Grace Hopper's comment">          <label className="reaction" style={{ '--count': '5' } as React.CSSProperties}>            <input type="checkbox" className="reaction-input" name="reaction-thread" value="👍" aria-label="Thumbs up, 5 others" />            <span className="reaction-emoji" aria-hidden="true">👍</span>            <span className="reaction-count" aria-hidden="true"></span>          </label>          <label className="reaction" style={{ '--count': '2' } as React.CSSProperties}>            <input type="checkbox" className="reaction-input" name="reaction-thread" value="🚀" aria-label="Rocket, 2 others" defaultChecked />            <span className="reaction-emoji" aria-hidden="true">🚀</span>            <span className="reaction-count" aria-hidden="true"></span>          </label>        </div>      </div>      <ol className="thread-replies">        <li className="thread-item">          <span className="thread-avatar"><span className="avatar avatar-sm">AL</span></span>          <div className="thread-body">            <p className="thread-header">              <span className="thread-author">Ada Lovelace</span>              <time className="thread-meta" dateTime="2026-11-12T10:05">1h</time>            </p>            <p className="thread-text">Then the release notes go out on Monday.</p>          </div>        </li>      </ol>    </li>  </ol></div>
<div class="card max-w(480) mx-auto">  <ol class="thread">    <li class="thread-item">      <span class="thread-avatar"><span class="avatar">GH</span></span>      <div class="thread-body">        <p class="thread-header">          <span class="thread-author">Grace Hopper</span>          <time class="thread-meta" datetime="2026-11-12T09:12">2h</time>        </p>        <p class="thread-text">          The contracts ship on Tuesday, the framework components the week after.        </p>        <div class="reaction-group" role="group" aria-label="Reactions to Grace Hopper's comment">          <label class="reaction" style="--count: 5">            <input type="checkbox" class="reaction-input" name="reaction-thread" value="👍" aria-label="Thumbs up, 5 others">            <span class="reaction-emoji" aria-hidden="true">👍</span>            <span class="reaction-count" aria-hidden="true"></span>          </label>          <label class="reaction" style="--count: 2">            <input type="checkbox" class="reaction-input" name="reaction-thread" value="🚀" aria-label="Rocket, 2 others" checked>            <span class="reaction-emoji" aria-hidden="true">🚀</span>            <span class="reaction-count" aria-hidden="true"></span>          </label>        </div>      </div>      <ol class="thread-replies">        <li class="thread-item">          <span class="thread-avatar"><span class="avatar avatar-sm">AL</span></span>          <div class="thread-body">            <p class="thread-header">              <span class="thread-author">Ada Lovelace</span>              <time class="thread-meta" datetime="2026-11-12T10:05">1h</time>            </p>            <p class="thread-text">Then the release notes go out on Monday.</p>          </div>        </li>      </ol>    </li>  </ol></div>
<template>  <div class="card max-w(480) mx-auto">    <ol class="thread">      <li class="thread-item">        <span class="thread-avatar"><span class="avatar">GH</span></span>        <div class="thread-body">          <p class="thread-header">            <span class="thread-author">Grace Hopper</span>            <time class="thread-meta" datetime="2026-11-12T09:12">2h</time>          </p>          <p class="thread-text">            The contracts ship on Tuesday, the framework components the week after.          </p>          <div class="reaction-group" role="group" aria-label="Reactions to Grace Hopper's comment">            <label class="reaction" style="--count: 5">              <input type="checkbox" class="reaction-input" name="reaction-thread" value="👍" aria-label="Thumbs up, 5 others">              <span class="reaction-emoji" aria-hidden="true">👍</span>              <span class="reaction-count" aria-hidden="true"></span>            </label>            <label class="reaction" style="--count: 2">              <input type="checkbox" class="reaction-input" name="reaction-thread" value="🚀" aria-label="Rocket, 2 others" checked>              <span class="reaction-emoji" aria-hidden="true">🚀</span>              <span class="reaction-count" aria-hidden="true"></span>            </label>          </div>        </div>        <ol class="thread-replies">          <li class="thread-item">            <span class="thread-avatar"><span class="avatar avatar-sm">AL</span></span>            <div class="thread-body">              <p class="thread-header">                <span class="thread-author">Ada Lovelace</span>                <time class="thread-meta" datetime="2026-11-12T10:05">1h</time>              </p>              <p class="thread-text">Then the release notes go out on Monday.</p>            </div>          </li>        </ol>      </li>    </ol>  </div></template>

#无障碍

  • 用文字写出表情名称和其他回应者的人数,作为每个复选框的无障碍名称,例如 aria-label="Thumbs up, 3 others"。用 aria-hidden="true" 隐藏表情符号和计数。选中状态会表明读者是否也给出了该回应,每次按下后,无障碍名称仍然准确。
  • 不要指望屏幕阅读器读出界面上显示的计数。浏览器引擎会在计算无障碍名称和描述时忽略 CSS 计数器生成的文本。在 Chromium 中,复选框的无障碍名称仅包含标签中的文字,省略计数器生成的数字。
  • 为回应组命名,并将 reaction-add 命名为“添加表情回应”。选择器中的每个按钮都用对应的表情名称命名,并隐藏表情符号本身。
  • 整个胶囊都是操作目标,尺寸为 28px;在触屏上,胶囊保持原有尺寸,操作目标在其周围扩展至 44px。在强制颜色模式下,胶囊保留轮廓,已给出的回应使用更粗的高亮色轮廓。

#平台说明

用 text-box: trim-both cap alphabetic 将计数数字的排版高度裁到大写字母高度,使胶囊以数字本身为基准居中,与表情符号的中心对齐。未启用时,数字距中心的偏差在半个像素以内。Chromium 和 Safari 支持 text-box。

#类名

类名说明
reaction-group在带有 role="group" 和名称的元素上排列回应项,空间不足时换行。用于消息时与上方的气泡对齐,用于讨论串时放在正文下方
reaction单个回应项是一个 28px 的胶囊形控件,内含 reaction-input、表情符号和计数。将 --count 设为除当前读者外的回应人数。
reaction-input用于记录读者本人回应的复选框。它透明覆盖整个胶囊形控件。用表情符号及其他回应者的人数为它命名,例如“点赞,已有 3 位其他读者点赞”。
reaction-emoji表情符号可用 span 中的字符,也可用带此类的 img 图像。输入元素已有可访问名称时,对辅助技术隐藏表情符号。此类也可用于选择器按钮上的表情符号
reaction-count元素为空时,由计数器显示 --count 的值,当前读者已添加回应时加 1。元素内已有文本时,直接显示该文本,例如 1.2k。
reaction-add位于行尾的圆形按钮,带有表情图案和加号,用于打开页面的表情符号选择器。紧随其后的 popover 打开时,按钮保持凹入状态

#自定义属性

属性默认值说明
--count0其他人给出此回应的人数,复选框选中时计入读者本人在 reaction 上设置

#源码

@master/ui/components/reaction.css
/* * Reaction: an emoji answer to a message, with how many gave it. * Each reaction is a <label> around a native checkbox, so a press, Space and the form all work as they do for a * checkbox. The count is a CSS counter: the pill starts it at --count, the reactions of everyone else, and a * checked box adds one, so the number is right after every press with no script. Engines leave a counter out of * an accessible name, so the box is named in words (the emoji and the others' count) and the pill's drawing is * hidden from assistive technology; the checked state says whether the reader is one of them. * At rest a reaction is a flat pill of the chip tone on one contour; under a press it sinks into a well and the * emoji is pushed in with it; given, it takes the selected language of the library: an ink hairline, a faint * tint of ink and the count in strong ink. * Geometry: a 28px pill with true half circles at its ends; on a touch screen its target reaches 44px above and below it. The emoji is 16px and stands * as far from the start as from the top and bottom, so it is concentric with the round end; the count's figures * are tabular, and trimmed to their cap height where text-box is supported, so flex centres the figures rather * than the line around them (elsewhere they stand within half a pixel of the middle). * A count written in the markup (1.2k) replaces the counter, for numbers too large to count by one. * Under a message the row stands under the bubble, and in a thread under the words. */@layer components.ui {  /* The row of reactions, with the button that adds one at its end. */  .reaction-group {    display: flex;    flex-wrap: wrap;    align-items: center;    gap: .375rem;    min-width: 0;    margin: 0;    padding: 0;    border: 0;    /*     * A finger's target reaches 8px over and under a pill. Rows that wrap stand that far apart twice over, so     * a target of the second row does not lie on the foot of the pill above it; one row is drawn as it was.     */    @media (pointer: coarse) {      row-gap: 1rem;    }  }  /* Under a message the row stands where the bubble does: at the start of a received row, at the end of a sent one. */  .message .reaction-group {    grid-column: 2;    margin-block-start: .125rem;  }  .message-sent .reaction-group {    grid-column: 1;    justify-content: flex-end;  }  .message:not(.message-sent, :has(.message-avatar)) .reaction-group {    grid-column: 1 / -1;  }  /* In a thread it follows the words, a little further from them than one paragraph from the next. */  .thread-body .reaction-group {    margin-block-start: .375rem;  }  .reaction {    --reaction-height: 1.75rem;    --reaction-emoji-size: 1rem;    --count: 0;    position: relative;    display: inline-flex;    flex: none;    align-items: center;    gap: .25rem;    height: var(--reaction-height);    margin: 0;    padding-block: 0;    padding-inline: calc((var(--reaction-height) - var(--reaction-emoji-size)) / 2) .5rem;    border-radius: 999px;    corner-shape: round;    background: var(--color-surface-chip);    color: var(--color-text-muted);    box-shadow: inset 0 0 0 .5px var(--color-line-contour);    font: var(--font-weight-control) .78125rem/1 var(--font-family-sans);    font-feature-settings: inherit;    font-variant-numeric: tabular-nums;    white-space: nowrap;    vertical-align: middle;    counter-reset: reaction var(--count);    user-select: none;    -webkit-user-select: none;    -webkit-tap-highlight-color: transparent;    cursor: pointer;    touch-action: manipulation;    transition-property: background-color, box-shadow, color;    transition-duration: var(--duration-quick);    transition-timing-function: var(--easing-glide);    @media (hover: hover) {      &:hover:not(:has(.reaction-input:disabled)) {        color: var(--color-text-body);        box-shadow: inset 0 0 0 .5px var(--color-line-mark);      }    }    /* Given: the reader's own reaction is counted, and the pill takes an ink hairline over a faint tint of ink. */    &:has(.reaction-input:checked) {      counter-increment: reaction;      background: color-mix(in oklab, var(--color-text-strong) 10%, transparent);      color: var(--color-text-strong);      box-shadow: inset 0 0 0 .5px var(--color-text-strong), 0 0 0 .5px color-mix(in oklab, var(--color-text-strong) 22%, transparent);      @media (hover: hover) {        &:hover:not(:has(.reaction-input:disabled)) {          background: color-mix(in oklab, var(--color-text-strong) 13%, transparent);          color: var(--color-text-strong);          box-shadow: inset 0 0 0 .5px var(--color-text-strong), 0 0 0 .5px color-mix(in oklab, var(--color-text-strong) 22%, transparent);        }      }    }    /*     * Pressed: the pill sinks into a well and the emoji is pushed in with it, given or not, so a tap is     * answered before the state changes.     */    &:active:not(:has(.reaction-input:disabled)) {      background: var(--color-surface-inset);      box-shadow: var(--shadow-pressed);      transition-duration: var(--duration-press);      .reaction-emoji {        scale: .88;        transition-duration: var(--duration-press);      }    }    &:has(.reaction-input:focus-visible) {      @apply --focus-ring;    }    &:has(.reaction-input:disabled) {      background: var(--color-surface-inset);      color: var(--color-text-disabled);      box-shadow: var(--shadow-flat);      cursor: not-allowed;      .reaction-emoji {        opacity: .6;      }    }    /* Given and disabled: the hairline stays, in the disabled ink, so the reader still sees the reaction is theirs. */    &:has(.reaction-input:checked:disabled) {      box-shadow: inset 0 0 0 .5px var(--color-text-disabled);    }    /* As specific as the press above, so it is not overruled by it. */    @media (prefers-reduced-motion: reduce) {      &:active:not(:has(.reaction-input:disabled)) .reaction-emoji {        scale: none;      }    }    /*     * Reactions stand 6px apart, closer than the 8px a 44px target would reach to the side: on touch a pill's     * target grows above and below only, so it never takes a tap meant for its neighbor. The pill keeps its size.     */    @media (pointer: coarse) {      &::after {        @apply --touch-target-block;      }    }    @media (forced-colors: active) {      outline: 1px solid CanvasText;      outline-offset: -1px;      &:has(.reaction-input:checked):not(:has(.reaction-input:focus-visible)) {        outline: 2px solid Highlight;        outline-offset: -2px;      }      &:has(.reaction-input:focus-visible) {        outline: 2px solid Highlight;        outline-offset: 2px;      }      &:has(.reaction-input:disabled) {        outline-color: GrayText;        color: GrayText;      }    }  }  /* The checkbox covers the pill invisibly: the label is the target, the input keeps the state. */  .reaction-input {    position: absolute;    inset: 0;    width: 100%;    height: 100%;    margin: 0;    padding: 0;    border: 0;    border-radius: inherit;    corner-shape: inherit;    opacity: 0;    appearance: none;    cursor: inherit;  }  /* The emoji, a character or a picture, in a square as tall as its font; also an option of the picker. */  .reaction-emoji {    display: inline-grid;    flex: none;    place-items: center;    width: var(--reaction-emoji-size, 1rem);    height: var(--reaction-emoji-size, 1rem);    font-size: var(--reaction-emoji-size, 1rem);    line-height: 1;    transition: scale var(--duration-quick) var(--easing-glide);  }  /* The count, from the counter; text written inside it is shown instead. */  .reaction-count {    min-width: 1ch;    text-align: center;    /* Trimmed to its figures, so flex centres the figures and not the line around them. */    text-box: trim-both cap alphabetic;    &:empty::before {      content: counter(reaction);    }  }  /*   * Add a reaction: a round button of the same pill, with a face and a plus. It opens the page's emoji picker,   * a popover, which adds the chosen reaction.   */  .reaction-add {    --reaction-height: 1.75rem;    position: relative;    display: inline-grid;    flex: none;    place-items: center;    width: var(--reaction-height);    height: var(--reaction-height);    margin: 0;    padding: 0;    border: 0;    border-radius: 999px;    corner-shape: round;    background: var(--color-surface-chip);    color: var(--color-text-muted);    box-shadow: inset 0 0 0 .5px var(--color-line-contour);    appearance: none;    -webkit-tap-highlight-color: transparent;    cursor: pointer;    touch-action: manipulation;    transition-property: background-color, box-shadow, color;    transition-duration: var(--duration-quick);    transition-timing-function: var(--easing-glide);    &::before {      content: '';      width: 1rem;      height: 1rem;      background: currentColor;      mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M13.07 5.27A8 8 0 1 0 18.73 10.93M19.5 2v5M17 4.5h5M8 15.5q3 2.5 6 0'/%3E%3Ccircle cx='8.5' cy='11' r='1' fill='black' stroke='none'/%3E%3Ccircle cx='13.5' cy='11' r='1' fill='black' stroke='none'/%3E%3C/svg%3E") center / contain no-repeat;    }    @media (hover: hover) {      &:hover:not(:disabled) {        color: var(--color-text-strong);        box-shadow: inset 0 0 0 .5px var(--color-line-mark);      }    }    &:active:not(:disabled), &:has(+ .popover:popover-open) {      background: var(--color-surface-inset);      color: var(--color-text-strong);      box-shadow: var(--shadow-pressed);    }    &:active:not(:disabled) {      transition-duration: var(--duration-press);    }    &:focus-visible {      @apply --focus-ring;    }    /* Disabled: the flat well of a reaction that cannot be given. */    &:disabled {      background: var(--color-surface-inset);      color: var(--color-text-disabled);      box-shadow: var(--shadow-flat);      cursor: not-allowed;    }    /*     * It stands 6px after the last reaction, closer than the 8px a 44px target would reach to the side: on touch     * its target grows above and below only, and keeps its 28px width, so it never takes a tap meant for the pill.     */    @media (pointer: coarse) {      &::after {        @apply --touch-target-block;      }    }    @media (forced-colors: active) {      outline: 1px solid ButtonText;      outline-offset: -1px;      &::before {        background: ButtonText;      }      /* The well it sinks into while the picker is open is gone with its shadow: a firmer outline keeps it. */      &:has(+ .popover:popover-open):not(:focus-visible) {        outline: 2px solid Highlight;        outline-offset: -2px;      }      &:disabled {        outline-color: GrayText;        &::before {          background: GrayText;        }      }    }  }}