From bc14c8b7c55e7c4945839acbe6de42c2b55bb3d5 Mon Sep 17 00:00:00 2001 From: Charles Lowell Date: Sat, 5 Dec 2026 21:31:10 -0600 Subject: [PATCH] Clear focus when an HTML subtree becomes inert --- .changeset/inert-focus-cleanup.md | 5 +++++ packages/ui/src/dom.ts | 9 +++++++++ 2 files changed, 14 insertions(+) create mode 100644 .changeset/inert-focus-cleanup.md diff --git a/.changeset/inert-focus-cleanup.md b/.changeset/inert-focus-cleanup.md new file mode 100644 index 0000000..b952091 --- /dev/null +++ b/.changeset/inert-focus-cleanup.md @@ -0,0 +1,5 @@ +--- +'@clack/ui': patch +--- + +Clear focus when the focused element or one of its ancestors becomes inert through the property or attribute APIs. diff --git a/packages/ui/src/dom.ts b/packages/ui/src/dom.ts index 571900d..d526144 100644 --- a/packages/ui/src/dom.ts +++ b/packages/ui/src/dom.ts @@ -588,6 +588,15 @@ function attributeChanged(attr: Attr, value: string | null): void { return; } switch (attr.localName) { + case 'inert': + if (value !== null && element instanceof HTMLElement) { + const document = element.ownerDocument; + const activeElement = getActiveElement(document); + if (activeElement && element.contains(activeElement)) { + transitionActiveElement(document, null); + } + } + break; case 'style': element.style.cssText = value ?? ''; break;