Skip to content

Commit 04ef921

Browse files
committed
Improve accessibility of placeholder
FIX: Use the `aria-placeholder` attribute to communicate the placeholder text to screen readers. See https://discuss.codemirror.net/t/accessibility-violation-in-placeholder/9165
1 parent 74ac9e6 commit 04ef921

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

src/placeholder.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@ class Placeholder extends WidgetType {
1515
typeof this.content == "string" ? document.createTextNode(this.content) :
1616
typeof this.content == "function" ? this.content(view) :
1717
this.content.cloneNode(true))
18-
if (typeof this.content == "string")
19-
wrap.setAttribute("aria-label", "placeholder " + this.content)
20-
else
21-
wrap.setAttribute("aria-hidden", "true")
18+
wrap.setAttribute("aria-hidden", "true")
2219
return wrap
2320
}
2421

@@ -39,7 +36,7 @@ class Placeholder extends WidgetType {
3936
/// Extension that enables a placeholder—a piece of example content
4037
/// to show when the editor is empty.
4138
export function placeholder(content: string | HTMLElement | ((view: EditorView) => HTMLElement)): Extension {
42-
return ViewPlugin.fromClass(class {
39+
let plugin = ViewPlugin.fromClass(class {
4340
placeholder: DecorationSet
4441

4542
constructor(readonly view: EditorView) {
@@ -52,4 +49,7 @@ export function placeholder(content: string | HTMLElement | ((view: EditorView)
5249

5350
get decorations() { return this.view.state.doc.length ? Decoration.none : this.placeholder }
5451
}, {decorations: v => v.decorations})
52+
return typeof content == "string" ? [
53+
plugin, EditorView.contentAttributes.of({"aria-placeholder": content})
54+
] : plugin
5555
}

0 commit comments

Comments
 (0)