Skip to content

Commit c519ddd

Browse files
committed
Preserve inert focus handling on the native WPT stack
1 parent 97a7748 commit c519ddd

3 files changed

Lines changed: 31 additions & 0 deletions

File tree

‎.changeset/dom-inert.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+
Reflect the inert attribute on HTML elements and prevent programmatic focus inside inert subtrees.

‎packages/ui/src/dom.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -892,10 +892,28 @@ export class HTMLElement extends Element {
892892
#onfocus: EventListener | undefined = undefined;
893893
#onblur: EventListener | undefined = undefined;
894894

895+
get inert(): boolean {
896+
return this.hasAttributeNS(null, 'inert');
897+
}
898+
899+
set inert(value: boolean) {
900+
if (value) {
901+
this.setAttributeNS(null, 'inert', '');
902+
} else {
903+
this.removeAttributeNS(null, 'inert');
904+
}
905+
}
906+
895907
focus(): void {
896908
if (!this.isConnected) {
897909
return;
898910
}
911+
// inert nodes don't get focus
912+
for (const node of lineage(this)) {
913+
if (node instanceof HTMLElement && node.inert) {
914+
return;
915+
}
916+
}
899917
this.ownerDocument._setActiveElement(this);
900918
}
901919

@@ -1020,3 +1038,9 @@ function dispatchEvent(
10201038
}
10211039
}
10221040
}
1041+
1042+
function* lineage(node: Node): Iterable<Node> {
1043+
for (let current: Node | null = node; current; current = current.parentNode) {
1044+
yield current;
1045+
}
1046+
}

‎packages/ui/wpt.config.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ const config: WPTConfig = {
9797
'html/editing/focus/focus-management/focusing-disconnected-noop.html': true,
9898
'html/editing/focus/focus-management/focusing-focused-noop.html': true,
9999
'html/interaction/focus/focus-management/focus-events.html': true,
100+
'inert/inert-node-is-unfocusable.html': true,
101+
'inert/nested-inert-unfocusable.html': true,
100102
'dom/nodes/Document-getElementsByTagName.html': [
101103
'Caching is allowed',
102104
"Shouldn't be able to set unsigned properties on a HTMLCollection (non-strict mode)",

0 commit comments

Comments
 (0)