From 710667ca8ccd9eb9b0de1afae7db9e56af9f1a5f Mon Sep 17 00:00:00 2001 From: Jay George Date: Mon, 19 Oct 2026 09:44:47 +0100 Subject: [PATCH 1/3] Ignore inline portals when deciding whether a modal traps focus --- resources/js/components/portals/Portal.vue | 7 +++++++ resources/js/components/portals/Portals.js | 15 ++++++++++++++- resources/js/components/ui/Modal/Modal.vue | 4 +++- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/resources/js/components/portals/Portal.vue b/resources/js/components/portals/Portal.vue index 86ca1ba9615..f76973eb817 100644 --- a/resources/js/components/portals/Portal.vue +++ b/resources/js/components/portals/Portal.vue @@ -43,6 +43,13 @@ export default { created() { this.portal = this.$portals.create(this.name); + this.$portals.setDisabled(this.portal.id, this.disabled); + }, + + watch: { + disabled(disabled) { + this.$portals.setDisabled(this.portal.id, disabled); + }, }, mounted() { diff --git a/resources/js/components/portals/Portals.js b/resources/js/components/portals/Portals.js index 62e5cb9ca29..5b5aba95aff 100644 --- a/resources/js/components/portals/Portals.js +++ b/resources/js/components/portals/Portals.js @@ -1,15 +1,22 @@ import Portal from './Portal'; -import { ref } from 'vue'; +import { reactive, ref } from 'vue'; export default class Portals { constructor() { this.portals = ref([]); + this.disabled = reactive(new Set()); } all() { return this.portals.value; } + // The topmost portal that's actually rendering into its target. Disabled portals + // (e.g. fullscreen fieldtypes not in fullscreen) render inline, so they don't count. + top() { + return this.all().filter((portal) => !this.disabled.has(portal.id)).at(-1); + } + create(name, data = {}) { let portal = new Portal(this, name, data); @@ -18,10 +25,16 @@ export default class Portals { return portal; } + setDisabled(id, disabled) { + disabled ? this.disabled.add(id) : this.disabled.delete(id); + } + destroy(id) { const i = this.portals.value.findIndex((portal) => portal.id === id); if (i !== -1) this.portals.value.splice(i, 1); + + this.disabled.delete(id); } stacks() { diff --git a/resources/js/components/ui/Modal/Modal.vue b/resources/js/components/ui/Modal/Modal.vue index 6e1a65f15f8..171a36db855 100644 --- a/resources/js/components/ui/Modal/Modal.vue +++ b/resources/js/components/ui/Modal/Modal.vue @@ -72,7 +72,9 @@ const titleId = useId(); const labelledBy = computed(() => hasModalTitleComponent.value || props.title ? titleId : undefined); const isUsingOpenProp = computed(() => instance?.vnode.props?.hasOwnProperty('open')); const portal = computed(() => modal.value ? `#portal-target-${modal.value.id}` : null); -const isTopPortal = computed(() => portals.all()[portals.all().length - 1].id === modal.value.id); +// Not just the last portal created: fields like Bard register inline portals as they mount, +// which would switch off the focus trap of a modal opened on page load (e.g. the licensing alert). +const isTopPortal = computed(() => portals.top()?.id === modal.value.id); function open() { if (!modal.value) modal.value = portals.create('modal'); From 8317cd9f2d38fe580224c2d16f736d9625bccf18 Mon Sep 17 00:00:00 2001 From: Jay George Date: Mon, 19 Oct 2026 10:06:00 +0100 Subject: [PATCH 2/3] Add portal.setDisabled() and tests for modals staying on top of disabled portals --- resources/js/components/portals/Portal.js | 4 +++ resources/js/components/portals/Portal.vue | 4 +-- resources/js/components/ui/Modal/Modal.vue | 2 +- .../tests/components/portals/Portals.test.js | 36 +++++++++++++++++++ .../js/tests/components/ui/Modal.test.js | 18 ++++++++++ 5 files changed, 61 insertions(+), 3 deletions(-) create mode 100644 resources/js/tests/components/portals/Portals.test.js diff --git a/resources/js/components/portals/Portal.js b/resources/js/components/portals/Portal.js index a26cd5b1e8e..978fb135c3b 100644 --- a/resources/js/components/portals/Portal.js +++ b/resources/js/components/portals/Portal.js @@ -7,6 +7,10 @@ export default class Portal { this.data = data; } + setDisabled(disabled) { + this.portals.setDisabled(this.id, disabled); + } + destroy() { this.portals.destroy(this.id); } diff --git a/resources/js/components/portals/Portal.vue b/resources/js/components/portals/Portal.vue index f76973eb817..ea6b80fbe2e 100644 --- a/resources/js/components/portals/Portal.vue +++ b/resources/js/components/portals/Portal.vue @@ -43,12 +43,12 @@ export default { created() { this.portal = this.$portals.create(this.name); - this.$portals.setDisabled(this.portal.id, this.disabled); + this.portal.setDisabled(this.disabled); }, watch: { disabled(disabled) { - this.$portals.setDisabled(this.portal.id, disabled); + this.portal.setDisabled(disabled); }, }, diff --git a/resources/js/components/ui/Modal/Modal.vue b/resources/js/components/ui/Modal/Modal.vue index 171a36db855..6448b34a705 100644 --- a/resources/js/components/ui/Modal/Modal.vue +++ b/resources/js/components/ui/Modal/Modal.vue @@ -72,7 +72,7 @@ const titleId = useId(); const labelledBy = computed(() => hasModalTitleComponent.value || props.title ? titleId : undefined); const isUsingOpenProp = computed(() => instance?.vnode.props?.hasOwnProperty('open')); const portal = computed(() => modal.value ? `#portal-target-${modal.value.id}` : null); -// Not just the last portal created: fields like Bard register inline portals as they mount, +// Not just the last portal created: fields like Bard register inline portals as they load, // which would switch off the focus trap of a modal opened on page load (e.g. the licensing alert). const isTopPortal = computed(() => portals.top()?.id === modal.value.id); diff --git a/resources/js/tests/components/portals/Portals.test.js b/resources/js/tests/components/portals/Portals.test.js new file mode 100644 index 00000000000..44f7561e0a0 --- /dev/null +++ b/resources/js/tests/components/portals/Portals.test.js @@ -0,0 +1,36 @@ +import { expect, test } from 'vitest'; +import Portals from '@/components/portals/Portals.js'; + +test('top is the last portal created', () => { + const portals = new Portals(); + portals.create('modal'); + const stack = portals.create('stack'); + + expect(portals.top().id).toBe(stack.id); +}); + +test('top is undefined when there are no portals', () => { + expect(new Portals().top()).toBeUndefined(); +}); + +test('top skips disabled portals', () => { + const portals = new Portals(); + const modal = portals.create('modal'); + const fullscreen = portals.create('bard-fullscreen'); + + fullscreen.setDisabled(true); + expect(portals.top().id).toBe(modal.id); + + fullscreen.setDisabled(false); + expect(portals.top().id).toBe(fullscreen.id); +}); + +test('destroying a portal forgets that it was disabled', () => { + const portals = new Portals(); + const fullscreen = portals.create('bard-fullscreen'); + + fullscreen.setDisabled(true); + fullscreen.destroy(); + + expect(portals.disabled.has(fullscreen.id)).toBe(false); +}); diff --git a/resources/js/tests/components/ui/Modal.test.js b/resources/js/tests/components/ui/Modal.test.js index 638020adfd5..3cd904fdbe1 100644 --- a/resources/js/tests/components/ui/Modal.test.js +++ b/resources/js/tests/components/ui/Modal.test.js @@ -83,3 +83,21 @@ test('hides the page behind from the accessibility tree', async () => { vi.useRealTimers(); }); + +test('keeps focus trapped when a disabled portal is created after it', async () => { + await openModal({ title: 'Licensing Alert', autofocus: false }); + await nextTick(); + + // e.g. a Bard field's fullscreen portal, registered as the page behind loads + const fullscreen = portals.create('bard-fullscreen'); + fullscreen.setDisabled(true); + await nextTick(); + + const input = document.createElement('input'); + document.body.appendChild(input); + input.focus(); + + expect(document.activeElement).toBe(document.querySelector('[data-ui-modal-content]')); + + fullscreen.destroy(); +}); From 3d50eeb20583156b58de8b693f472fd31afee0a5 Mon Sep 17 00:00:00 2001 From: Jay George Date: Mon, 19 Oct 2026 10:11:42 +0100 Subject: [PATCH 3/3] Keep the disabled portal set private behind isDisabled() --- resources/js/components/portals/Portals.js | 13 +++++++++---- .../js/tests/components/portals/Portals.test.js | 2 +- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/resources/js/components/portals/Portals.js b/resources/js/components/portals/Portals.js index 5b5aba95aff..21d04e8c80f 100644 --- a/resources/js/components/portals/Portals.js +++ b/resources/js/components/portals/Portals.js @@ -2,9 +2,10 @@ import Portal from './Portal'; import { reactive, ref } from 'vue'; export default class Portals { + #disabled = reactive(new Set()); + constructor() { this.portals = ref([]); - this.disabled = reactive(new Set()); } all() { @@ -14,7 +15,7 @@ export default class Portals { // The topmost portal that's actually rendering into its target. Disabled portals // (e.g. fullscreen fieldtypes not in fullscreen) render inline, so they don't count. top() { - return this.all().filter((portal) => !this.disabled.has(portal.id)).at(-1); + return this.all().filter((portal) => !this.isDisabled(portal.id)).at(-1); } create(name, data = {}) { @@ -25,8 +26,12 @@ export default class Portals { return portal; } + isDisabled(id) { + return this.#disabled.has(id); + } + setDisabled(id, disabled) { - disabled ? this.disabled.add(id) : this.disabled.delete(id); + disabled ? this.#disabled.add(id) : this.#disabled.delete(id); } destroy(id) { @@ -34,7 +39,7 @@ export default class Portals { if (i !== -1) this.portals.value.splice(i, 1); - this.disabled.delete(id); + this.#disabled.delete(id); } stacks() { diff --git a/resources/js/tests/components/portals/Portals.test.js b/resources/js/tests/components/portals/Portals.test.js index 44f7561e0a0..543fc731157 100644 --- a/resources/js/tests/components/portals/Portals.test.js +++ b/resources/js/tests/components/portals/Portals.test.js @@ -32,5 +32,5 @@ test('destroying a portal forgets that it was disabled', () => { fullscreen.setDisabled(true); fullscreen.destroy(); - expect(portals.disabled.has(fullscreen.id)).toBe(false); + expect(portals.isDisabled(fullscreen.id)).toBe(false); });