导航

v2.1.0
结构

展开区域

先显示长文本的开头几行,按需展开全文。

本页内容

展开区域先显示长文本的前几行,用户可按需查看余下内容,适用于描述、评论或更新日志条目。收起时,文本底部逐渐淡出。文本展开至完整高度时,下方的切换控件会翻转。

展开区域

<details class="expand max-w(420) mx-auto text-sm leading-md fg-text-body">  <summary class="expand-toggle">    <span class="expand-more">Show more</span><span class="expand-less">Show less</span><svg class="icon expand-chevron" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">      <path d="M6 3.5L10.3586 7.8586A0.2 0.2 0 0 1 10.3586 8.1414L6 12.5" transform="rotate(90 8 8)" />    </svg>  </summary>  <p class="m(0)">    Moonstone is a feldspar with a soft glow that seems to move under its surface as the stone turns. The effect comes from light scattering between thin layers inside it. Cut as a smooth dome, it shows a band of light that glides across the top. The theme takes its name from that glow: surfaces are pale and even, edges are found by a line of light before a line of ink, and nothing ends in a hard corner. Obsidian is its counterpart, a volcanic glass, dark and smooth, with edges that catch the light.  </p></details>
import ChevronDownIcon from '@master/icons-react/native/16/soft/chevron-down'<details className="expand max-w(420) mx-auto text-sm leading-md fg-text-body">  <summary className="expand-toggle">    <span className="expand-more">Show more</span><span className="expand-less">Show less</span><ChevronDownIcon className="icon expand-chevron" />  </summary>  <p className="m(0)">    Moonstone is a feldspar with a soft glow that seems to move under its surface as the stone turns. The effect comes from light scattering between thin layers inside it. Cut as a smooth dome, it shows a band of light that glides across the top. The theme takes its name from that glow: surfaces are pale and even, edges are found by a line of light before a line of ink, and nothing ends in a hard corner. Obsidian is its counterpart, a volcanic glass, dark and smooth, with edges that catch the light.  </p></details>
<script>  import ChevronDownIcon from '@master/icons-svelte/native/16/soft/chevron-down'</script><details class="expand max-w(420) mx-auto text-sm leading-md fg-text-body">  <summary class="expand-toggle">    <span class="expand-more">Show more</span><span class="expand-less">Show less</span><ChevronDownIcon class="icon expand-chevron" />  </summary>  <p class="m(0)">    Moonstone is a feldspar with a soft glow that seems to move under its surface as the stone turns. The effect comes from light scattering between thin layers inside it. Cut as a smooth dome, it shows a band of light that glides across the top. The theme takes its name from that glow: surfaces are pale and even, edges are found by a line of light before a line of ink, and nothing ends in a hard corner. Obsidian is its counterpart, a volcanic glass, dark and smooth, with edges that catch the light.  </p></details>
<script setup>import ChevronDownIcon from '@master/icons-vue/native/16/soft/chevron-down'</script><template>  <details class="expand max-w(420) mx-auto text-sm leading-md fg-text-body">    <summary class="expand-toggle">      <span class="expand-more">Show more</span><span class="expand-less">Show less</span><ChevronDownIcon class="icon expand-chevron" />    </summary>    <p class="m(0)">      Moonstone is a feldspar with a soft glow that seems to move under its surface as the stone turns. The effect comes from light scattering between thin layers inside it. Cut as a smooth dome, it shows a band of light that glides across the top. The theme takes its name from that glow: surfaces are pale and even, edges are found by a line of light before a line of ink, and nothing ends in a hard corner. Obsidian is its counterpart, a volcanic glass, dark and smooth, with edges that catch the light.    </p>  </details></template>

组件使用原生 <details>,切换控件是它的 <summary>,其中包含文字和一个 expand-chevron 图标,因此原生支持键盘操作、页内查找和辅助技术。浏览器提供 ::details-content 时,收起状态会显示预览。其他浏览器沿用普通 details 的行为,收起时隐藏正文,仍可通过切换控件展开。

按 <details> 的要求,summary 在标记中排在最前面,视觉上显示在正文下方。

#预览行数

--expand-lines 以正文自身的行高为单位设置预览高度,默认显示四行。

预览行数

<details class="expand max-w(420) mx-auto text-sm leading-md fg-text-body" style="--expand-lines: 2">  <summary class="expand-toggle">    <span class="expand-more">Read the full note</span><span class="expand-less">Show less</span><svg class="icon expand-chevron" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">      <path d="M6 3.5L10.3586 7.8586A0.2 0.2 0 0 1 10.3586 8.1414L6 12.5" transform="rotate(90 8 8)" />    </svg>  </summary>  <p class="m(0)">    Moonstone is a feldspar with a soft glow that seems to move under its surface as the stone turns. The effect comes from light scattering between thin layers inside it. Cut as a smooth dome, it shows a band of light that glides across the top. The theme takes its name from that glow: surfaces are pale and even, edges are found by a line of light before a line of ink, and nothing ends in a hard corner. Obsidian is its counterpart, a volcanic glass, dark and smooth, with edges that catch the light.  </p></details>
import ChevronDownIcon from '@master/icons-react/native/16/soft/chevron-down'<details className="expand max-w(420) mx-auto text-sm leading-md fg-text-body" style={{ '--expand-lines': '2' } as React.CSSProperties}>  <summary className="expand-toggle">    <span className="expand-more">Read the full note</span><span className="expand-less">Show less</span><ChevronDownIcon className="icon expand-chevron" />  </summary>  <p className="m(0)">    Moonstone is a feldspar with a soft glow that seems to move under its surface as the stone turns. The effect comes from light scattering between thin layers inside it. Cut as a smooth dome, it shows a band of light that glides across the top. The theme takes its name from that glow: surfaces are pale and even, edges are found by a line of light before a line of ink, and nothing ends in a hard corner. Obsidian is its counterpart, a volcanic glass, dark and smooth, with edges that catch the light.  </p></details>
<script>  import ChevronDownIcon from '@master/icons-svelte/native/16/soft/chevron-down'</script><details class="expand max-w(420) mx-auto text-sm leading-md fg-text-body" style="--expand-lines: 2">  <summary class="expand-toggle">    <span class="expand-more">Read the full note</span><span class="expand-less">Show less</span><ChevronDownIcon class="icon expand-chevron" />  </summary>  <p class="m(0)">    Moonstone is a feldspar with a soft glow that seems to move under its surface as the stone turns. The effect comes from light scattering between thin layers inside it. Cut as a smooth dome, it shows a band of light that glides across the top. The theme takes its name from that glow: surfaces are pale and even, edges are found by a line of light before a line of ink, and nothing ends in a hard corner. Obsidian is its counterpart, a volcanic glass, dark and smooth, with edges that catch the light.  </p></details>
<script setup>import ChevronDownIcon from '@master/icons-vue/native/16/soft/chevron-down'</script><template>  <details class="expand max-w(420) mx-auto text-sm leading-md fg-text-body" style="--expand-lines: 2">    <summary class="expand-toggle">      <span class="expand-more">Read the full note</span><span class="expand-less">Show less</span><ChevronDownIcon class="icon expand-chevron" />    </summary>    <p class="m(0)">      Moonstone is a feldspar with a soft glow that seems to move under its surface as the stone turns. The effect comes from light scattering between thin layers inside it. Cut as a smooth dome, it shows a band of light that glides across the top. The theme takes its name from that glow: surfaces are pale and even, edges are found by a line of light before a line of ink, and nothing ends in a hard corner. Obsidian is its counterpart, a volcanic glass, dark and smooth, with edges that catch the light.    </p>  </details></template>

#切换控件的文案

将两种状态的文案分别放在 expand-more 和 expand-less 中,样式表会显示与当前状态对应的文案。条件允许时,用“阅读完整评论”这类文案明确说明展开后能看到什么,单写“更多”则过于笼统。如果同一句文案适用于两种状态,直接把它作为纯文本写在 summary 中。

#使用场景

展开区域适合呈现大多数读者只会快速浏览的长文本。问答列表适合用折叠面板,每一项收起时只显示标题。把链接和控件放在收起时仍可见的部分,因为收起时,预览范围之外被裁切的内容仍可通过键盘访问。

#无障碍

  • 辅助技术将 summary 识别为按钮,并通过它获知正文是否已展开。页内查找命中其中的内容时,会展开 details。
  • 底部的淡出效果由覆盖在文本上的遮罩实现,因此最后一条可见文本行会变淡,前几行则保持完整的对比度。
  • 启用减少动态效果时,文本会立即展开。

#类名

类名说明
expanddetails 元素,折叠时显示底部渐隐的预览。
expand-togglesummary 在标记中排在最前,显示在正文下方,带可翻转的箭头。
expand-chevron切换控件在文字之后所含的箭头图标,正文展开时翻转
expand-more区域折叠时切换控件显示的文案
expand-less区域展开时切换控件显示的文案

#自定义属性

属性默认值说明
--expand-lines4预览显示的行数,按正文自身的行高计算

#源码

@master/ui/components/expand.css
/* * Expand: a long text that shows its first lines and opens on request. * It is a <details>: the toggle is its <summary>, so the keyboard, find-in-page and assistive technology * already know how it works. Where the browser exposes ::details-content, the closed state is a preview, * --expand-lines tall, that fades out at its foot and opens to its full height. Elsewhere the text is * hidden while closed, as any details is. On paper there is no toggle to press: where the preview is drawn, a * closed text prints whole and the toggle is left out. */@property --expand-fade {  syntax: '<length>';  inherits: false;  initial-value: 0px;}@layer components.ui {  .expand {    --expand-lines: 4;    display: flex;    flex-direction: column;    min-width: 0;    interpolate-size: allow-keywords;    @supports selector(::details-content) {      &::details-content {        --expand-fade: 2lh;        display: block;        block-size: calc(var(--expand-lines) * 1lh);        overflow: clip;        content-visibility: visible;        mask-image: linear-gradient(black calc(100% - var(--expand-fade)), transparent);        transition-property: block-size, --expand-fade;        transition-duration: var(--duration-base);        transition-timing-function: var(--easing-glide);      }      &[open]::details-content {        --expand-fade: 0px;        block-size: auto;      }      @media (prefers-reduced-motion: reduce) {        &::details-content {          transition: none;        }      }      /* A page cannot be opened on paper: the whole text prints, without the fade. */      @media print {        &::details-content {          --expand-fade: 0px;          block-size: auto;        }      }    }  }  /* The toggle sits under the text it opens: a quiet word with a chevron that turns over. */  .expand-toggle {    order: 1;    display: inline-flex;    align-self: start;    align-items: center;    gap: .25rem;    margin-block-start: .5rem;    border-radius: calc((var(--radius-chip) - 2px) * var(--corner-scale));    corner-shape: var(--corner-shape);    color: var(--color-text-strong);    -webkit-tap-highlight-color: transparent;    font-size: .8125rem;    font-weight: var(--font-weight-control);    line-height: 1.4;    list-style: none;    user-select: none;    -webkit-user-select: none;    cursor: pointer;    touch-action: manipulation;    &::-webkit-details-marker {      display: none;    }    .expand-chevron {      flex: none;      width: 14px;      height: 14px;      color: var(--color-text-muted);      transition: rotate var(--duration-base) var(--easing-glide);    }    /* Under the pointer and the press the chevron takes the strong ink, as an accordion's and a trace's do. */    @media (hover: hover) {      &:hover .expand-chevron {        color: var(--color-text-strong);      }    }    &:active .expand-chevron {      color: var(--color-text-strong);    }    &:focus-visible {      @apply --focus-ring;    }    @media (prefers-reduced-motion: reduce) {      .expand-chevron {        transition: none;      }    }    /* As heavy as the pointer's and the press's rules, so none of them keeps a theme color. */    @media (forced-colors: active) {      .expand-chevron, &:is(:hover, :active) .expand-chevron {        color: CanvasText;      }    }    /* On touch the target reaches 44px around the toggle; the line of text keeps its height. */    @media (pointer: coarse) {      position: relative;      &::before {        @apply --touch-target;      }    }  }  /* Printed whole, the text needs no Show more or Show less under it. */  @supports selector(::details-content) {    @media print {      .expand-toggle {        display: none;      }    }  }  .expand[open] .expand-toggle .expand-chevron {    rotate: x 180deg;  }  /* The toggle's two wordings: one shows while closed, the other while open. */  .expand[open] .expand-more {    display: none;  }  .expand:not([open]) .expand-less {    display: none;  }}