Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/dom-focusability.md
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.
101 changes: 97 additions & 4 deletions packages/ui/src/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -937,6 +955,20 @@ export class HTMLElement extends Element {
}
}

export class DisableableElement extends HTMLElement {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add aria-disabled support now or are we planning on doing a separate accessibility tree pass?

Copy link
Copy Markdown
Contributor Author

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....

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious why we're not using a Disablable mixin like Focusable!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's so that we could make an instanceof check in the isFocusable() method. I wanted to make this a mixing, but couldn't quite figure it out.

We could have a hidden Symbol.for('@bomb.sh/ui/disablable') that marks the prototype of the mixin instead?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cowboyd how about something like this? 8a91634

I'm not totally sure if there are TypeScript performance considerations for this model, but I know lib.dom.d.ts inlines everything. For maintenance, I think the mixins approach is probably good for now

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like mixin(HTMLElement, [Disableable, Focusable]) produces this

declare const HTMLButtonElement_base: typeof HTMLElement & Constructor<Disableable> & Constructor<Focusable>;
export declare class HTMLButtonElement extends HTMLButtonElement_base {}

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 {}
Expand All @@ -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 {}

Expand Down Expand Up @@ -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 },
Expand Down
36 changes: 35 additions & 1 deletion packages/ui/wpt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,41 @@ const config: WPTConfig = {
// real layout, hit testing, and an input backend are not available yet.
'inert/nested-inert-unfocusable.html': true,
'html/dom/reflection-misc.html': [/^html\.inert:/, /^html\.tabIndex:/],
'html/dom/reflection-forms.html': [/^button\.tabIndex:/],
'html/dom/reflection-forms.html': [
/^button\.tabIndex:/,
/^(button|input|select|textarea|fieldset|optgroup|option)\.disabled:/,
],
'html/interaction/focus/tabindex-focus-flag.html': [
'a should not be focusable by default.',
'a[href] should be focusable by default.',
'button should be focusable by default.',
'input[type="hidden"] should not be focusable by default.',
'input[type="button"] should be focusable by default.',
'select should be focusable by default.',
'textarea should be focusable by default.',
'#summary-out should not be focusable by default.',
'#summary-first should be focusable by default.',
'#summary-second should not be focusable by default.',
'[contenteditable] should be focusable by default.',
'iframe should be focusable by default.',
'img should not be focusable by default.',
'A with tabindex=0 should be focusable.',
'SUMMARY#summary-out-tabindex0 with tabindex=0 should be focusable.',
'SUMMARY#summary-second-tabindex0 with tabindex=0 should be focusable.',
'IMG with tabindex=0 should be focusable.',
'A with tabindex=-1 should be focusable.',
'SUMMARY#summary-out-tabindex-negative with tabindex=-1 should be focusable.',
'SUMMARY#summary-second-tabindex-negative with tabindex=-1 should be focusable.',
'IMG with tabindex=-1 should be focusable.',
'A with tabindex=invalid should not be focusable.',
'A#with-href with tabindex=invalid should be focusable.',
'DIV with tabindex=invalid should not be focusable.',
'SUMMARY#summary-out-tabindex-invalid with tabindex=invalid should not be focusable.',
'IMG with tabindex=invalid should not be focusable.',
// SVG focus coverage awaits its element interfaces.
],
'html/semantics/forms/the-input-element/number-disabled.html': true,
'html/semantics/disabled-elements/disabledElement.html': true,
'html/interaction/focus/sequential-focus-navigation-and-the-tabindex-attribute/focus-tabindex-default-value.html': true,
'dom/nodes/NodeList-Iterable.html': [/^NodeList /],
'dom/nodes/ParentNode-querySelector-All-comma-selector.html': [
Expand Down