Skip to content

Commit

Permalink
Add a screenReaderLabel option to set an aria-label on the editor
Browse files Browse the repository at this point in the history
  • Loading branch information
adityatoshniwal authored Mar 31, 2020
1 parent 629f42d commit 6d38fc2
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 2 deletions.
4 changes: 4 additions & 0 deletions doc/manual.html
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,10 @@ <h2>Configuration</h2>
simply <code>true</code>), focusing of the editor is also
disallowed.</dd>

<dt id="option_screenReaderLabel"><code><strong>screenReaderLabel</strong>: string</code></dt>
<dd>This label is read by the screenreaders when CodeMirror text area is focused. This
is helpful for accessibility.</dd>

<dt id="option_showCursorWhenSelecting"><code><strong>showCursorWhenSelecting</strong>: boolean</code></dt>
<dd>Whether the cursor should be drawn when a selection is
active. Defaults to false.</dd>
Expand Down
6 changes: 6 additions & 0 deletions src/edit/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ export function defineOptions(CodeMirror) {
}
cm.display.input.readOnlyChanged(val)
})

option("screenReaderLabel", null, (cm, val) => {
val = (val === '') ? null : val
cm.display.input.screenReaderLabelChanged(val)
})

option("disableInput", false, (cm, val) => {if (!val) cm.display.input.reset()}, true)
option("dragDrop", true, dragDropChanged)
option("allowDropFileTypes", null)
Expand Down
9 changes: 9 additions & 0 deletions src/input/ContentEditableInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,15 @@ export default class ContentEditableInput {
on(div, "cut", onCopyCut)
}

screenReaderLabelChanged(label) {
// Label for screenreaders, accessibility
if(label) {
this.div.setAttribute('aria-label', label)
} else {
this.div.removeAttribute('aria-label')
}
}

prepareSelection() {
let result = prepareSelection(this.cm, false)
result.focus = document.activeElement == this.div
Expand Down
11 changes: 10 additions & 1 deletion src/input/TextareaInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,21 @@ export default class TextareaInput {

createField(_display) {
// Wraps and hides input textarea
this.wrapper = hiddenTextarea()
this.wrapper = hiddenTextarea(this.cm.options.screenReaderLabel)
// The semihidden textarea that is focused when the editor is
// focused, and receives input.
this.textarea = this.wrapper.firstChild
}

screenReaderLabelChanged(label) {
// Label for screenreaders, accessibility
if(label) {
this.textarea.setAttribute('aria-label', label)
} else {
this.textarea.removeAttribute('aria-label')
}
}

prepareSelection() {
// Redraw the selection and/or cursor
let cm = this.cm, display = cm.display, doc = cm.doc
Expand Down
2 changes: 1 addition & 1 deletion src/input/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,4 @@ export function hiddenTextarea() {
if (ios) te.style.border = "1px solid black"
disableBrowserMagic(te)
return div
}
}

0 comments on commit 6d38fc2

Please sign in to comment.