Skip to content
Merged
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.
19 changes: 19 additions & 0 deletions packages/ui/src/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1030,10 +1030,29 @@ export class HTMLElement extends Element {
this.setAttributeNS(null, 'tabindex', String(value | 0));
}

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

set inert(value: boolean) {

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.

so if the focused element or an ancestor becomes inert, activeElement still points at it. browsers drop focus back to body here, inert/dynamic-inert-on-focused-element.html covers it. follow-up?

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.

nice catch!

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.

Yes, We should add it as a follow-up.

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

focus(): void {
if (!this.isConnected) {
return;
}
// inert nodes don't get focus
// oxlint-disable-next-line typescript/no-this-alias -- This cursor walks the node's ancestry.
for (let node: Node | null = this; node; node = node.parentNode) {
if (node instanceof HTMLElement && node.inert) {
return;
}
}
transitionActiveElement(this.ownerDocument, this);
}

Expand Down
5 changes: 4 additions & 1 deletion packages/ui/wpt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ const config: WPTConfig<Document> = {
'dom/nodes/querySelector-id-nth-child.html': [
'querySelectorAll matching #id + constant nth-child()',
],
'html/dom/reflection-misc.html': [/\.tabIndex:/],
'html/dom/reflection-misc.html': [/\.tabIndex:/, /^html\.inert:/],
'html/dom/reflection-forms.html': [/\.tabIndex:/],
'html/interaction/focus/sequential-focus-navigation-and-the-tabindex-attribute/focus-tabindex-default-value.html': true,
'html/editing/focus/focus-management/active-element.html': [
Expand All @@ -355,6 +355,9 @@ const config: WPTConfig<Document> = {
'html/editing/focus/focus-management/focusing-disconnected-noop.html': true,
// focusing-focused-noop.html requires the innerHTML setter for setup and cleanup.
'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,
'dom/nodes/Document-createEvent.https.html': [
/^(?:Event|Events|HTMLEvents|SVGEvents) should be an alias for Event\.$/i,
/^createEvent\('(?:Event|Events|HTMLEvents|SVGEvents)'\) should be initialized correctly\.$/i,
Expand Down
Loading