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

Reflect the inert attribute on HTML elements and prevent programmatic focus inside inert subtrees.
24 changes: 24 additions & 0 deletions packages/ui/src/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -789,10 +789,28 @@ export class HTMLElement extends Element {
#onfocus: EventListener | undefined = undefined;
#onblur: EventListener | undefined = undefined;

get inert(): boolean {
return this.hasAttributeNS(null, 'inert');
}

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

focus(): void {
if (!this.isConnected) {
return;
}
// inert nodes don't get focus
for (const node of lineage(this)) {
if (node instanceof HTMLElement && node.inert) {
return;
}
}
this.ownerDocument._setActiveElement(this);
}

Expand Down Expand Up @@ -915,3 +933,9 @@ function dispatchEvent(
}
}
}

function* lineage(node: Node): Iterable<Node> {
for (let current: Node | null = node; current; current = current.parentNode) {
yield current;
}
}
4 changes: 4 additions & 0 deletions packages/ui/wpt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ const config: WPTConfig = {
'html/editing/focus/focus-management/focusing-disconnected-noop.html': true,
'html/editing/focus/focus-management/focusing-focused-noop.html': true,
'html/interaction/focus/focus-management/focus-events.html': true,
// 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:/],
'dom/nodes/Document-createTextNode.html': true,
'dom/nodes/Document-createElement.html': [/^createElement\(.*\) in HTML document$/],
'dom/nodes/Document-createElementNS.html': [
Expand Down
Loading