リッチテキストエディターRich text editor
太字・斜体・箇条書き・リンクのボタンが上に並び、その下の入力欄で見たままの書式を付けながら書ける部品。ブログやメールの本文入力に使う。
表示例
本文
来週の定例会は会議室Bで行います。
- 資料は前日までに共有
- 議事録はこちら
アイコン: Font Awesome 7 Free(fa-solid / fa-regular)
別名・英語表記
- WYSIWYGエディター
- ビジュアルエディター
- 装飾つき入力欄
- リッチエディター
- ウィジウィグ
- 書式つきテキストエリア
- Rich text editor
実装例
<div class="x-rich-text-editor">
<span class="x-rich-text-editor__l" id="x-rich-text-editor-l">本文</span>
<div class="x-rich-text-editor__box">
<!-- Tab で入るのは1か所だけ(tabindex="0")。中は左右の矢印キーで移り、JS が tabindex を付け替える -->
<div class="x-rich-text-editor__bar" role="toolbar" aria-label="書式" aria-controls="x-rich-text-editor-e">
<button class="x-rich-text-editor__b" type="button" tabindex="0" aria-pressed="true" aria-label="太字" aria-keyshortcuts="Control+B">
<i class="fa-solid fa-bold" aria-hidden="true"></i>
</button>
<button class="x-rich-text-editor__b" type="button" tabindex="-1" aria-pressed="false" aria-label="斜体" aria-keyshortcuts="Control+I">
<i class="fa-solid fa-italic" aria-hidden="true"></i>
</button>
<button class="x-rich-text-editor__b" type="button" tabindex="-1" aria-pressed="false" aria-label="箇条書き">
<i class="fa-solid fa-list" aria-hidden="true"></i>
</button>
<span class="x-rich-text-editor__sep" aria-hidden="true"></span>
<button class="x-rich-text-editor__b" type="button" tabindex="-1" aria-haspopup="dialog" aria-label="リンクを挿入">
<i class="fa-solid fa-link" aria-hidden="true"></i>
</button>
</div>
<div class="x-rich-text-editor__e" id="x-rich-text-editor-e" contenteditable="true"
role="textbox" aria-multiline="true" aria-labelledby="x-rich-text-editor-l">
<p>来週の<strong>定例会</strong>は<em>会議室B</em>で行います。</p>
<ul>
<li>資料は前日までに共有</li>
<li>議事録は<a href="/minutes/">こちら</a></li>
</ul>
</div>
</div>
</div>
/* 貼った先のリセットに頼らないよう、この部品の中だけ border-box にする */
.x-rich-text-editor,
.x-rich-text-editor *:not([class*="fa-"]),
.x-rich-text-editor *:not([class*="fa-"])::before,
.x-rich-text-editor *:not([class*="fa-"])::after {
box-sizing: border-box;
/* 貼った先の既定の書体に引きずられないようにする。書体が変わると
文字の幅と高さが変わり、枠のサイズまでずれる */
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Hiragino Sans", "Noto Sans JP", "Yu Gothic UI", Meiryo, sans-serif;
}
.x-rich-text-editor {
width: 300px;
color: #1b2028;
}
.x-rich-text-editor__l {
display: block;
margin-bottom: 6px;
font-size: 12px;
font-weight: 700;
}
.x-rich-text-editor__box {
border: 1px solid #7f8a99;
border-radius: 8px;
background: #fff;
}
.x-rich-text-editor__bar {
display: flex;
align-items: center;
gap: 2px;
padding: 4px 6px;
border-bottom: 1px solid #cfd4dc;
background: #f5f7fa;
border-radius: 8px 8px 0 0;
}
.x-rich-text-editor__b {
width: 32px;
height: 30px;
border: 1px solid transparent;
border-radius: 6px;
background: none;
color: #2d3440;
font-size: 13px;
cursor: pointer;
}
.x-rich-text-editor__b:hover {
background: #e7ebf0;
}
.x-rich-text-editor__b[aria-pressed="true"] {
border-color: #1d4ed8;
background: #dbe6ff;
color: #1d3fae;
}
.x-rich-text-editor__sep {
width: 1px;
height: 18px;
margin: 0 4px;
background: #b4bcc7;
}
.x-rich-text-editor__e {
min-height: 96px;
padding: 8px 12px;
font-size: 13px;
line-height: 1.7;
border-radius: 0 0 8px 8px;
}
.x-rich-text-editor__e p,
.x-rich-text-editor__e ul {
margin: 0;
}
.x-rich-text-editor__e ul {
padding-left: 1.4em;
}
.x-rich-text-editor__e a {
color: #1d4ed8;
}
.x-rich-text-editor__b:focus-visible {
outline: 2px solid #1d4ed8;
outline-offset: 1px;
}
.x-rich-text-editor__e:focus-visible {
outline: 2px solid #1d4ed8;
outline-offset: -2px;
}
AIへの指示文
「リッチテキストエディター(Rich text editor)」を実装してください。
## これは何か
太字・斜体・箇条書き・リンクのボタンが上に並び、その下の入力欄で見たままの書式を付けながら書ける部品。ブログやメールの本文入力に使う。
## 使いどころ
- ブログやお知らせの本文など、書く人が HTML や Markdown を知らなくても見出しや強調を付けたいとき
- 問い合わせの返信やメールの本文で、箇条書きやリンクを入れさせたいとき
- 書式が要らない入力ならテキストエリアで足りる。装飾を付ける必要があるときだけ使う
## 実装の要件
- div[role=toolbar][aria-label] > button[aria-pressed](太字・斜体・箇条書き)+ button[aria-haspopup=dialog](リンク)+ div[contenteditable][role=textbox][aria-multiline=true][aria-labelledby]
- ツールバーのボタンは tabindex を 0 が1つ・残りは -1 にし、矢印キーで付け替える
- 保存する HTML はサーバー側で許可リスト方式で無害化する
- document.execCommand() で作らない。MDN で非推奨とされ、ブラウザごとに結果が揃わない。Tiptap・Lexical・Quill などの実績あるエディターの部品を使う
- 受け取った HTML はサーバー側で許可したタグと属性だけを残して無害化する。貼り付けや改ざんで script や onclick が入り込む(XSS)
- Word やWebページからの貼り付けは、余計な書式やスタイルを持ち込む。貼り付け時に整理するか、書式なしで貼る手段を用意する
- ボタンを増やしすぎない。本文に必要な書式(太字・リンク・箇条書き・見出し程度)に絞る
- 日本語の変換中(IME の確定前)の入力を、書式の処理や自動変換で壊さない。keydown のショートカットと Enter の処理は、event.isComposing が true か event.keyCode が 229 のとき何もしない。入力に合わせた自動変換は compositionend のあとで行う
## アクセシビリティの要件
- ボタンの並びを role="toolbar" でまとめ、aria-label で名前を付ける。Tab で入るのは1か所だけにし(roving tabindex)、中は左右の矢印キーで移る。Home・End で端へ移れるとなおよい(WAI-ARIA APG の Toolbar パターン)
- 太字・斜体のように入と切がある書式のボタンには aria-pressed を付け、カーソル位置の書式に合わせて更新する
- アイコンだけのボタンには aria-label で名前を付ける。ショートカットがあれば aria-keyshortcuts でも示す
- 入力欄には role="textbox" と aria-multiline="true" を付け、ラベルを aria-labelledby で結ぶ
- 押されている状態を色だけで示さない。枠線や背景の形でも区別する
## 参考にする実装
この形を出発点にして、プロジェクトの命名規則・色・余白に合わせて調整してください。
```html
<div class="x-rich-text-editor">
<span class="x-rich-text-editor__l" id="x-rich-text-editor-l">本文</span>
<div class="x-rich-text-editor__box">
<!-- Tab で入るのは1か所だけ(tabindex="0")。中は左右の矢印キーで移り、JS が tabindex を付け替える -->
<div class="x-rich-text-editor__bar" role="toolbar" aria-label="書式" aria-controls="x-rich-text-editor-e">
<button class="x-rich-text-editor__b" type="button" tabindex="0" aria-pressed="true" aria-label="太字" aria-keyshortcuts="Control+B">
<i class="fa-solid fa-bold" aria-hidden="true"></i>
</button>
<button class="x-rich-text-editor__b" type="button" tabindex="-1" aria-pressed="false" aria-label="斜体" aria-keyshortcuts="Control+I">
<i class="fa-solid fa-italic" aria-hidden="true"></i>
</button>
<button class="x-rich-text-editor__b" type="button" tabindex="-1" aria-pressed="false" aria-label="箇条書き">
<i class="fa-solid fa-list" aria-hidden="true"></i>
</button>
<span class="x-rich-text-editor__sep" aria-hidden="true"></span>
<button class="x-rich-text-editor__b" type="button" tabindex="-1" aria-haspopup="dialog" aria-label="リンクを挿入">
<i class="fa-solid fa-link" aria-hidden="true"></i>
</button>
</div>
<div class="x-rich-text-editor__e" id="x-rich-text-editor-e" contenteditable="true"
role="textbox" aria-multiline="true" aria-labelledby="x-rich-text-editor-l">
<p>来週の<strong>定例会</strong>は<em>会議室B</em>で行います。</p>
<ul>
<li>資料は前日までに共有</li>
<li>議事録は<a href="/minutes/">こちら</a></li>
</ul>
</div>
</div>
</div>
```
```css
/* 貼った先のリセットに頼らないよう、この部品の中だけ border-box にする */
.x-rich-text-editor,
.x-rich-text-editor *:not([class*="fa-"]),
.x-rich-text-editor *:not([class*="fa-"])::before,
.x-rich-text-editor *:not([class*="fa-"])::after {
box-sizing: border-box;
/* 貼った先の既定の書体に引きずられないようにする。書体が変わると
文字の幅と高さが変わり、枠のサイズまでずれる */
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Hiragino Sans", "Noto Sans JP", "Yu Gothic UI", Meiryo, sans-serif;
}
.x-rich-text-editor {
width: 300px;
color: #1b2028;
}
.x-rich-text-editor__l {
display: block;
margin-bottom: 6px;
font-size: 12px;
font-weight: 700;
}
.x-rich-text-editor__box {
border: 1px solid #7f8a99;
border-radius: 8px;
background: #fff;
}
.x-rich-text-editor__bar {
display: flex;
align-items: center;
gap: 2px;
padding: 4px 6px;
border-bottom: 1px solid #cfd4dc;
background: #f5f7fa;
border-radius: 8px 8px 0 0;
}
.x-rich-text-editor__b {
width: 32px;
height: 30px;
border: 1px solid transparent;
border-radius: 6px;
background: none;
color: #2d3440;
font-size: 13px;
cursor: pointer;
}
.x-rich-text-editor__b:hover {
background: #e7ebf0;
}
.x-rich-text-editor__b[aria-pressed="true"] {
border-color: #1d4ed8;
background: #dbe6ff;
color: #1d3fae;
}
.x-rich-text-editor__sep {
width: 1px;
height: 18px;
margin: 0 4px;
background: #b4bcc7;
}
.x-rich-text-editor__e {
min-height: 96px;
padding: 8px 12px;
font-size: 13px;
line-height: 1.7;
border-radius: 0 0 8px 8px;
}
.x-rich-text-editor__e p,
.x-rich-text-editor__e ul {
margin: 0;
}
.x-rich-text-editor__e ul {
padding-left: 1.4em;
}
.x-rich-text-editor__e a {
color: #1d4ed8;
}
.x-rich-text-editor__b:focus-visible {
outline: 2px solid #1d4ed8;
outline-offset: 1px;
}
.x-rich-text-editor__e:focus-visible {
outline: 2px solid #1d4ed8;
outline-offset: -2px;
}
```
## 出力の約束
- HTML と CSS を分けて出す。JavaScript が要るときは素の JavaScript で書き、UIフレームワークは使わない。
- 参考実装がアイコンに Font Awesome のクラスを使っている場合は、プロジェクトで使っているアイコンかインライン SVG に置き換えてよい。
- 上のアクセシビリティ要件を満たす。満たせない箇所があるときは、理由を添えて先に知らせる。
- 色・角丸・余白は既存のデザイントークンがあればそれを使う。無ければ CSS 変数として切り出す。
使いどころ
- ブログやお知らせの本文など、書く人が HTML や Markdown を知らなくても見出しや強調を付けたいとき
- 問い合わせの返信やメールの本文で、箇条書きやリンクを入れさせたいとき
- 書式が要らない入力ならテキストエリアで足りる。装飾を付ける必要があるときだけ使う
注意点
- document.execCommand() で作らない。MDN で非推奨とされ、ブラウザごとに結果が揃わない。Tiptap・Lexical・Quill などの実績あるエディターの部品を使う
- 受け取った HTML はサーバー側で許可したタグと属性だけを残して無害化する。貼り付けや改ざんで script や onclick が入り込む(XSS)
- Word やWebページからの貼り付けは、余計な書式やスタイルを持ち込む。貼り付け時に整理するか、書式なしで貼る手段を用意する
- ボタンを増やしすぎない。本文に必要な書式(太字・リンク・箇条書き・見出し程度)に絞る
- 日本語の変換中(IME の確定前)の入力を、書式の処理や自動変換で壊さない。keydown のショートカットと Enter の処理は、event.isComposing が true か event.keyCode が 229 のとき何もしない。入力に合わせた自動変換は compositionend のあとで行う
アクセシビリティの要点
- ボタンの並びを role="toolbar" でまとめ、aria-label で名前を付ける。Tab で入るのは1か所だけにし(roving tabindex)、中は左右の矢印キーで移る。Home・End で端へ移れるとなおよい(WAI-ARIA APG の Toolbar パターン)
- 太字・斜体のように入と切がある書式のボタンには aria-pressed を付け、カーソル位置の書式に合わせて更新する
- アイコンだけのボタンには aria-label で名前を付ける。ショートカットがあれば aria-keyshortcuts でも示す
- 入力欄には role="textbox" と aria-multiline="true" を付け、ラベルを aria-labelledby で結ぶ
- 押されている状態を色だけで示さない。枠線や背景の形でも区別する