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
4 changes: 4 additions & 0 deletions resources/js/components/portals/Portal.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
7 changes: 7 additions & 0 deletions resources/js/components/portals/Portal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ export default {

created() {
this.portal = this.$portals.create(this.name);
this.portal.setDisabled(this.disabled);
},

watch: {
disabled(disabled) {
this.portal.setDisabled(disabled);
},
},

mounted() {
Expand Down
20 changes: 19 additions & 1 deletion resources/js/components/portals/Portals.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import Portal from './Portal';
import { ref } from 'vue';
import { reactive, ref } from 'vue';

export default class Portals {
#disabled = reactive(new Set());

constructor() {
this.portals = ref([]);
}
Expand All @@ -10,6 +12,12 @@ export default class Portals {
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.isDisabled(portal.id)).at(-1);
}

create(name, data = {}) {
let portal = new Portal(this, name, data);

Expand All @@ -18,10 +26,20 @@ 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);
}

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() {
Expand Down
4 changes: 3 additions & 1 deletion resources/js/components/ui/Modal/Modal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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 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);

function open() {
if (!modal.value) modal.value = portals.create('modal');
Expand Down
36 changes: 36 additions & 0 deletions resources/js/tests/components/portals/Portals.test.js
Original file line number Diff line number Diff line change
@@ -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.isDisabled(fullscreen.id)).toBe(false);
});
18 changes: 18 additions & 0 deletions resources/js/tests/components/ui/Modal.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Loading