An open source design system that's fully customizable and agent ready
TypeScript
fix: keep layer portals inside nearest dialog (#7172)
Humberto Virtudesfcf255b26869
diff --git a/.changeset/keep-layers-inside-dialogs.md b/.changeset/keep-layers-inside-dialogs.mdnew file mode 100644index 0000000000..2ab66a8ee7--- /dev/null+++ b/.changeset/keep-layers-inside-dialogs.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[fix] Keep layer portals inside their nearest dialog++@humbertovirtudesdiff --git a/packages/core/src/Layer/layerHost.test.ts b/packages/core/src/Layer/layerHost.test.tsindex 8ad56dc5de..f6466417a4 100644--- a/packages/core/src/Layer/layerHost.test.ts+++ b/packages/core/src/Layer/layerHost.test.ts@@ -84,6 +84,24 @@ describe('resolveLayerPortalTarget', () => {expect(target).toBe(root.querySelector('#host'));});+ it('keeps a safe layer position inside its nearest dialog', () => {+ const root = mount(+ '<div id="host"><table><tbody><tr><td><dialog><div id="inline"><template></template></div></dialog></td></tr></tbody></table></div>',+ );+ const target = resolveLayerPortalTarget(root.querySelector('#inline'));++ expect(target).toBeNull();+ });++ it('walks out of unsafe hosts without crossing the nearest dialog', () => {+ const root = mount(+ '<div id="host"><table><tbody><tr><td><dialog id="dialog"><p><span id="inline"><template></template></span></p></dialog></td></tr></tbody></table></div>',+ );+ const target = resolveLayerPortalTarget(root.querySelector('#inline'));++ expect(target).toBe(root.querySelector('#dialog'));+ });+it('stops at the nearest safe ancestor rather than the body', () => {const root = mount('<section id="outer"><li id="host"><p><span id="t">t</span></p></li></section>',diff --git a/packages/core/src/Layer/layerHost.ts b/packages/core/src/Layer/layerHost.tsindex 3339142da9..5eb7313a0d 100644--- a/packages/core/src/Layer/layerHost.ts+++ b/packages/core/src/Layer/layerHost.ts@@ -117,9 +117,12 @@ const UNSAFE_HOSTS = new Set([* the trigger as the popover's invoker `source`, which pins focus order to* the invoker in browsers that support it.)*- * The outermost unsafe ancestor matters. A safe div may itself sit inside an- * anchor; stopping at that div would still put the layer's buttons inside the- * link. Walking the whole chain ensures the target is outside both.+ * The outermost unsafe ancestor matters up to the nearest native dialog. A safe+ * div may itself sit inside an anchor; stopping at that div would still put the+ * layer's buttons inside the link. A dialog is the opposite boundary: portaling+ * beyond it removes the layer from the browser's modal top layer and makes the+ * layer inert. Walking the chain within that boundary keeps the target outside+ * every relevant unsafe ancestor and inside its nearest dialog.*/export function resolveLayerPortalTarget(inlineParent: HTMLElement | null,@@ -131,7 +134,11 @@ export function resolveLayerPortalTarget(let outermostUnsafe: HTMLElement | null = null;let node: HTMLElement | null = inlineParent;while (node) {- if (UNSAFE_HOSTS.has(node.tagName.toLowerCase())) {+ const tagName = node.tagName.toLowerCase();+ if (tagName === 'dialog') {+ break;+ }+ if (UNSAFE_HOSTS.has(tagName)) {outermostUnsafe = node;}node = node.parentElement;