Skip to content

Commit 03fdbba

Browse files
committed
Add tabIndex reflection and button defaults
1 parent 84d74cc commit 03fdbba

3 files changed

Lines changed: 34 additions & 2 deletions

File tree

‎.changeset/dom-tabindex.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@clack/ui': minor
3+
---
4+
5+
Add tabIndex reflection on HTML elements and a default tabIndex of zero for buttons.

‎packages/ui/src/dom.ts‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,7 @@ export class HTMLDocument extends Document {
814814
['head', HTMLHeadElement],
815815
['title', HTMLTitleElement],
816816
['body', HTMLBodyElement],
817+
['button', HTMLButtonElement],
817818
['span', HTMLSpanElement],
818819
['div', HTMLDivElement],
819820
]);
@@ -860,12 +861,21 @@ export class HTMLElement extends Element {
860861

861862
set inert(value: boolean) {
862863
if (value) {
863-
this.setAttributeNS(null, 'inert', '');
864+
this.setAttributeNS(null, 'inert', String());
864865
} else {
865866
this.removeAttributeNS(null, 'inert');
866867
}
867868
}
868869

870+
get tabIndex(): number {
871+
const attr = this.getAttributeNS(null, 'tabindex');
872+
return attr === null ? -1 : Number.parseInt(attr, 10) || 0;
873+
}
874+
875+
set tabIndex(value: number) {
876+
this.setAttributeNS(null, 'tabindex', String(value));
877+
}
878+
869879
focus(): void {
870880
if (!this.isConnected) {
871881
return;
@@ -924,12 +934,27 @@ export class HTMLSpanElement extends HTMLElement {}
924934

925935
export class HTMLDivElement extends HTMLElement {}
926936

937+
export class HTMLButtonElement extends Focusable(HTMLElement) {}
938+
927939
export class HTMLUnknownElement extends HTMLElement {}
928940

929941
/** XML */
930942

931943
export class XMLDocument extends Document {}
932944

945+
function Focusable<Ctor extends new (...args: any[]) => HTMLElement>(ElementType: Ctor): Ctor {
946+
return class FocusableElement extends ElementType {
947+
override get tabIndex(): number {
948+
const attr = this.getAttributeNS(null, 'tabindex');
949+
return attr ? super.tabIndex : 0;
950+
}
951+
952+
override set tabIndex(value: number) {
953+
super.tabIndex = value;
954+
}
955+
};
956+
}
957+
933958
export class FocusEvent extends Event {
934959
override readonly type: 'focus' | 'focusin' | 'focusout' | 'blur';
935960
readonly relatedTarget: Element | null;

‎packages/ui/wpt.config.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,9 @@ const config: WPTConfig = {
125125
// inert/inert-node-is-unfocusable.html requires testdriver click setup:
126126
// real layout, hit testing, and an input backend are not available yet.
127127
'inert/nested-inert-unfocusable.html': true,
128-
'html/dom/reflection-misc.html': [/^html\.inert:/],
128+
'html/dom/reflection-misc.html': [/^html\.inert:/, /^html\.tabIndex:/],
129+
'html/dom/reflection-forms.html': [/^button\.tabIndex:/],
130+
'html/interaction/focus/sequential-focus-navigation-and-the-tabindex-attribute/focus-tabindex-default-value.html': true,
129131
'dom/nodes/Document-createTextNode.html': true,
130132
'dom/nodes/Document-createElement.html': [/^createElement\(.*\) in HTML document$/],
131133
'dom/nodes/Document-createElementNS.html': [

0 commit comments

Comments
 (0)