Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e450f99

Browse files
authored
adding details on accessibility of input type=search and opting for different error handling of clear button
1 parent e5f01b1 commit e450f99

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ With a script tag:
3333
Optional clear button:
3434
- id must match the id of the input or the name of the input plus "-clear"
3535
- recommended to be *before* UL elements to avoid conflicting with their blur logic
36+
37+
Please see Note below on this button for more details
3638
-->
3739
<button id="users-clear">X</button>
3840
<ul id="users-popup"></ul>
@@ -67,6 +69,12 @@ item whose display text needs to be different:
6769
<li role="option" data-autocomplete-value="bb8">BB-8 (astromech)</li>
6870
```
6971
72+
### A Note on Clear button
73+
While `input type="search"` comes with an `x` that clears the content of the field and refocuses it on many browsers, the implementation for this control is not keyboard accessible, and so we've opted to enable a customizable clear button so that your keyboard users will be able to interact with it.
74+
75+
As an example:
76+
> In Chrome, this 'x' isn't a button but a div with a pseudo="-webkit-search-cancel-button". It doesn't have a tab index or a way to navigate to it without a mouse. Additionally, this control is only visible on mouse hover.
77+
7078
## Attributes
7179
7280
- `open` is true when the auto-complete result list is visible

src/autocomplete.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,18 @@ export default class Autocomplete {
4242
this.feedback.setAttribute('aria-atomic', 'true')
4343
}
4444

45-
// if clearButton is not a button, make it one
45+
// if clearButton is not a button, show error
4646
if (this.clearButton && this.clearButton.tagName.toLowerCase() !== 'button') {
47-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
48-
const [tagName, ...otherAttributes] = this.clearButton.attributes
49-
const newClearButton = document.createElement('button')
50-
newClearButton.innerHTML = this.clearButton.innerHTML
51-
newClearButton.id = this.clearButton.id
52-
for (const attr of otherAttributes) {
53-
newClearButton.setAttribute(attr.name, attr.value)
54-
}
55-
this.clearButton.parentNode?.replaceChild(newClearButton, this.clearButton)
56-
this.clearButton = newClearButton
47+
const buttonErrorMessage = `Accessibility violation: ${this.clearButton.tagName} provided for clear button. Please use a "button" element.`
48+
const buttonErrorText = '\u26a0 Error: See console'
49+
const buttonError = document.createElement('span')
50+
buttonError.setAttribute('style', 'color:#92140C')
51+
buttonError.textContent = buttonErrorText
52+
this.clearButton.parentNode?.replaceChild(buttonError, this.clearButton)
53+
this.clearButton = buttonError
54+
// eslint-disable-next-line no-console
55+
console.log(this.clearButton)
56+
throw new Error(buttonErrorMessage)
5757
}
5858

5959
// if clearButton doesn't have an accessible label, give it one

0 commit comments

Comments
 (0)