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-tabindex.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clack/ui': minor
---

Add tabIndex reflection on HTML elements and a default tabIndex of zero for buttons.
27 changes: 26 additions & 1 deletion packages/ui/src/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,7 @@ export class HTMLDocument extends Document {
['head', HTMLHeadElement],
['title', HTMLTitleElement],
['body', HTMLBodyElement],
['button', HTMLButtonElement],
['span', HTMLSpanElement],
['div', HTMLDivElement],
]);
Expand Down Expand Up @@ -860,12 +861,21 @@ export class HTMLElement extends Element {

set inert(value: boolean) {
if (value) {
this.setAttributeNS(null, 'inert', '');
this.setAttributeNS(null, 'inert', String());
} else {
this.removeAttributeNS(null, 'inert');
}
}

get tabIndex(): number {
const attr = this.getAttributeNS(null, 'tabindex');
return attr === null ? -1 : Number.parseInt(attr, 10) || 0;
}

set tabIndex(value: number) {
this.setAttributeNS(null, 'tabindex', String(value));
}

focus(): void {
if (!this.isConnected) {
return;
Expand Down Expand Up @@ -924,12 +934,27 @@ export class HTMLSpanElement extends HTMLElement {}

export class HTMLDivElement extends HTMLElement {}

export class HTMLButtonElement extends Focusable(HTMLElement) {}

export class HTMLUnknownElement extends HTMLElement {}

/** XML */

export class XMLDocument extends Document {}

function Focusable<Ctor extends new (...args: any[]) => HTMLElement>(ElementType: Ctor): Ctor {

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.

I like the mixin approach here honestly!

return class FocusableElement extends ElementType {
override get tabIndex(): number {
const attr = this.getAttributeNS(null, 'tabindex');
return attr ? super.tabIndex : 0;
}

override set tabIndex(value: number) {
super.tabIndex = value;
}
};
}

export class FocusEvent extends Event {
override readonly type: 'focus' | 'focusin' | 'focusout' | 'blur';
readonly relatedTarget: Element | null;
Expand Down
4 changes: 3 additions & 1 deletion packages/ui/wpt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@ const config: WPTConfig = {
// inert/inert-node-is-unfocusable.html requires testdriver click setup:
// 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/dom/reflection-misc.html': [/^html\.inert:/, /^html\.tabIndex:/],
'html/dom/reflection-forms.html': [/^button\.tabIndex:/],
'html/interaction/focus/sequential-focus-navigation-and-the-tabindex-attribute/focus-tabindex-default-value.html': true,
'dom/nodes/Document-createTextNode.html': true,
'dom/nodes/Document-createElement.html': [/^createElement\(.*\) in HTML document$/],
'dom/nodes/Document-createElementNS.html': [
Expand Down