テキストの展開
長文の冒頭数行を表示し、操作すると全文を展開します。
このページの内容
長い説明文やレビュー、変更履歴などの冒頭数行を表示し、操作すると残りのテキストを展開します。閉じている間はテキストの下端が徐々に薄くなり、全文が展開されると下にある切り替えボタンの向きが反転します。
テキストの展開
<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>を切り替えボタンにします。<summary>には文言とexpand-chevronアイコンを入れます。キーボード操作、ページ内検索、支援技術に標準で対応します。ブラウザーが::details-contentを提供している場合は、閉じた状態でもプレビューが表示されます。それ以外のブラウザーでは、通常のdetails要素と同様に閉じている間はテキストが非表示になり、切り替えボタンで展開できます。
<details>の要件に従い、マークアップではsummaryを先頭に置きます。画面上ではテキストの下に表示されます。
#プレビューの行数
--expand-linesで、テキスト自体の行の高さを単位としてプレビューの行数を指定します。既定は4行です。
プレビューの行数
<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>#切り替えボタンの文言
2種類の文言をexpand-moreとexpand-lessに入れると、スタイルシートが状態に応じて表示を切り替えます。可能であれば、単に「もっと見る」とするより、「レビュー全文を読む」のように、展開すると何を読めるのかが伝わる文言にします。両方の状態に同じ文言を使う場合は、summaryにプレーンテキストで記述します。
#使いどころ
多くの読者が流し読みする長文に使います。質問と回答の一覧には、閉じた状態で各行の見出しだけを表示するアコーディオンを使います。閉じている間もプレビューに表示されない部分にキーボードでフォーカスを移せるため、その部分にはリンクやコントロールを置かないでください。
#アクセシビリティ
- summaryは支援技術にボタンとして認識され、テキストが展開されているかどうかを伝えます。ページ内検索で内部に一致箇所が見つかると、detailsが展開されます。
- テキストの下端にマスクをかけ、表示されている最後の行を薄くします。冒頭の行は元のコントラストを保ちます。
- 動きの軽減が有効な場合は、テキストが即座に展開されます。
#クラス
| クラス | 説明 |
|---|---|
expand | details 要素。閉じている間は、下端に向かって薄くなるプレビューを表示します。 |
expand-toggle | summary はマークアップの先頭に置き、本文の下に表示します。開閉に合わせてシェブロンが反転します。 |
expand-chevron | 切り替えボタンの文言の後ろに入れるシェブロンアイコンです。テキストが開いている間は反転します。 |
expand-more | 閉じているときのトグルの文言 |
expand-less | 開いているときのトグルの文言 |
#カスタムプロパティ
| プロパティ | 既定値 | 説明 |
|---|---|---|
--expand-lines | 4 | プレビューの高さ。本文の行高を基準に、行数で指定します。 |