Repository navigation
Add HTML focusability and disabled reflection #102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@clack/ui': minor | ||
| --- | ||
|
|
||
| Add HTML focusability checks for [HTMLElement.focus()](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/focus) and reflected [disabled attributes](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Attributes/disabled) on form controls. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -823,9 +823,19 @@ export class HTMLDocument extends Document { | |
| ['head', HTMLHeadElement], | ||
| ['title', HTMLTitleElement], | ||
| ['body', HTMLBodyElement], | ||
| ['a', HTMLAnchorElement], | ||
| ['button', HTMLButtonElement], | ||
| ['input', HTMLInputElement], | ||
| ['select', HTMLSelectElement], | ||
| ['textarea', HTMLTextAreaElement], | ||
| ['option', HTMLOptionElement], | ||
| ['optgroup', HTMLOptGroupElement], | ||
| ['fieldset', HTMLFieldSetElement], | ||
| ['span', HTMLSpanElement], | ||
| ['div', HTMLDivElement], | ||
| ['iframe', HTMLIFrameElement], | ||
| ['details', HTMLDetailsElement], | ||
| ['summary', HTMLSummaryElement], | ||
| ]); | ||
| constructor(notify: Node['notify']) { | ||
| super(notify, (owner, localName, notifyElement, namespace) => { | ||
|
|
@@ -883,16 +893,24 @@ export class HTMLElement extends Element { | |
| } | ||
|
|
||
| get tabIndex(): number { | ||
| const attr = this.getAttributeNS(null, 'tabindex'); | ||
| return attr === null ? -1 : Number.parseInt(attr, 10) || 0; | ||
| const attr = this.getAttributeNodeNS(null, 'tabindex'); | ||
| if (!attr) { | ||
| return -1; | ||
| } else { | ||
| const value = Number.parseInt(attr.value, 10); | ||
| if (Number.isNaN(value)) { | ||
| return -1; | ||
| } | ||
| return value === 0 ? 0 : value; | ||
| } | ||
| } | ||
|
|
||
| set tabIndex(value: number) { | ||
| this.setAttributeNS(null, 'tabindex', String(value)); | ||
| } | ||
|
|
||
| focus(): void { | ||
| if (!this.isConnected) { | ||
| if (!isFocusable(this)) { | ||
| return; | ||
| } | ||
| // inert nodes don't get focus | ||
|
|
@@ -937,6 +955,20 @@ export class HTMLElement extends Element { | |
| } | ||
| } | ||
|
|
||
| export class DisableableElement extends HTMLElement { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Curious why we're not using a
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's so that we could make an We could have a hidden
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like so everything is fully resolved at compile-time! |
||
| get disabled(): boolean { | ||
| return this.hasAttributeNS(null, 'disabled'); | ||
| } | ||
|
|
||
| set disabled(value: boolean) { | ||
| if (value) { | ||
| this.setAttributeNS(null, 'disabled', ''); | ||
| } else { | ||
| this.removeAttributeNS(null, 'disabled'); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| export class HTMLHtmlElement extends HTMLElement {} | ||
|
|
||
| export class HTMLHeadElement extends HTMLElement {} | ||
|
|
@@ -949,7 +981,27 @@ export class HTMLSpanElement extends HTMLElement {} | |
|
|
||
| export class HTMLDivElement extends HTMLElement {} | ||
|
|
||
| export class HTMLButtonElement extends Focusable(HTMLElement) {} | ||
| export class HTMLAnchorElement extends Focusable(HTMLElement) {} | ||
|
|
||
| export class HTMLButtonElement extends Focusable(DisableableElement) {} | ||
|
|
||
| export class HTMLInputElement extends Focusable(DisableableElement) {} | ||
|
|
||
| export class HTMLSelectElement extends Focusable(DisableableElement) {} | ||
|
|
||
| export class HTMLTextAreaElement extends Focusable(DisableableElement) {} | ||
|
|
||
| export class HTMLFieldSetElement extends DisableableElement {} | ||
|
|
||
| export class HTMLOptionElement extends DisableableElement {} | ||
|
|
||
| export class HTMLOptGroupElement extends DisableableElement {} | ||
|
|
||
| export class HTMLIFrameElement extends Focusable(HTMLElement) {} | ||
|
|
||
| export class HTMLDetailsElement extends HTMLElement {} | ||
|
|
||
| export class HTMLSummaryElement extends HTMLElement {} | ||
|
|
||
| export class HTMLUnknownElement extends HTMLElement {} | ||
|
|
||
|
|
@@ -987,6 +1039,47 @@ export class FocusEvent extends Event { | |
| } | ||
| } | ||
|
|
||
| function isFocusable(element: HTMLElement): boolean { | ||
| if (!element.isConnected) { | ||
| return false; | ||
| } | ||
|
|
||
| for (const node of lineage(element)) { | ||
| if (node instanceof HTMLElement && node.inert) { | ||
| return false; | ||
| } | ||
| if (node instanceof DisableableElement && node.disabled) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| const hasTabIndex = (target: HTMLElement): boolean => { | ||
| const attr = target.getAttributeNode('tabindex'); | ||
| return !!attr && !isNaN(Number(attr.value)); | ||
| }; | ||
|
|
||
| switch (element.localName) { | ||
| case 'a': | ||
| return !!element.getAttributeNodeNS(null, 'href') || hasTabIndex(element); | ||
| case 'input': | ||
| return element.getAttributeNS(null, 'type') !== 'hidden'; | ||
| case 'summary': | ||
| return ( | ||
| hasTabIndex(element) || | ||
| (element.parentElement instanceof HTMLDetailsElement && | ||
| element.parentElement.children[0] === element) | ||
| ); | ||
| default: { | ||
| const editable = element.getAttribute('contenteditable'); | ||
| return ( | ||
| (editable && ['true', '', 'plaintext-only'].includes(editable)) || | ||
| hasTabIndex(element) || | ||
| element.tabIndex === 0 | ||
| ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| function dispatchEvent( | ||
| node: Node, | ||
| { event, isTrusted }: { event: Event; isTrusted: boolean }, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we add
aria-disabledsupport now or are we planning on doing a separate accessibility tree pass?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmmm, lemme see how hard it would be to add now....