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;