Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: focusTrap conflict when using QMessageBox inside QDrawer #348

Merged
merged 8 commits into from
Dec 21, 2022
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
23 changes: 20 additions & 3 deletions src/qComponents/QDrawer/src/QDrawerContainer/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ export default defineComponent({
const drawer = ref<Nullable<HTMLElement>>(null);
const isShown = ref<boolean>(false);
const zIndex = getConfig('nextZIndex');
let isFocusTrapEnabled: boolean = false;

const drawerStyle = computed<Record<string, Nullable<string | number>>>(
() => ({
Expand Down Expand Up @@ -213,10 +214,24 @@ export default defineComponent({
});
};

const enableFocusTrap = (): void => {
if (isFocusTrapEnabled) return;

document.addEventListener('focus', handleDocumentFocus, true);
isFocusTrapEnabled = true;
};

const disableFocusTrap = (): void => {
if (!isFocusTrapEnabled) return;

document.removeEventListener('focus', handleDocumentFocus, true);
isFocusTrapEnabled = false;
};

onMounted(async () => {
document.body.appendChild(instance?.vnode.el as Node);
document.documentElement.style.overflow = 'hidden';
document.addEventListener('focus', handleDocumentFocus, true);
enableFocusTrap();

await nextTick();
isShown.value = true;
Expand All @@ -226,13 +241,15 @@ export default defineComponent({

onBeforeUnmount(() => {
document.documentElement.style.overflow = '';
document.removeEventListener('focus', handleDocumentFocus, true);
disableFocusTrap();
if (!props.preventFocusAfterClosing) elementToFocusAfterClosing?.focus();
});

provide<QDrawerContainerProvider>('qDrawerContainer', {
emitDoneEvent,
emitCloseEvent
emitCloseEvent,
enableFocusTrap,
disableFocusTrap
});

return {
Expand Down
3 changes: 3 additions & 0 deletions src/qComponents/QDrawer/src/QDrawerContainer/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { Ref, ComputedRef } from 'vue';
import type { Nullable } from '#/helpers';

import type { QDrawerAction } from '../constants';

import type { QDrawerComponent, QDrawerContent, QDrawerEvent } from '../types';

export type QDrawerContainerPropContent = QDrawerContent;
Expand All @@ -27,6 +28,8 @@ export interface QDrawerContainerProps {
export interface QDrawerContainerProvider {
emitDoneEvent: (props: QDrawerEvent) => Promise<void>;
emitCloseEvent: () => void;
enableFocusTrap: () => void;
disableFocusTrap: () => void;
}

export interface QDrawerContainerInstance {
Expand Down
14 changes: 13 additions & 1 deletion src/qComponents/QMessageBox/src/QMessageBoxContainer/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import {
onMounted,
nextTick,
provide,
inject,
onBeforeUnmount
} from 'vue';
import type { PropType } from 'vue';
Expand All @@ -63,7 +64,9 @@ import { getConfig } from '@/qComponents/config';
import { isServer } from '@/qComponents/constants/isServer';
import { QScrollbar } from '@/qComponents/QScrollbar';

import type { Nullable } from '#/helpers';
import type { QDrawerContainerProvider } from '@/qComponents';

import type { Nullable, Nillable } from '#/helpers';

import { QMessageBoxAction } from '../constants';
import { QMessageBoxContent } from '../QMessageBoxContent';
Expand Down Expand Up @@ -163,6 +166,11 @@ export default defineComponent({
setup(props: QMessageBoxContainerProps, ctx): QMessageBoxContainerInstance {
const instance = getCurrentInstance();

const qDrawerContainer = inject<Nillable<QDrawerContainerProvider>>(
'qDrawerContainer',
null
);

const messageBox = ref<Nullable<HTMLElement>>(null);
const isShown = ref<boolean>(false);
const zIndex = getConfig('nextZIndex');
Expand Down Expand Up @@ -232,6 +240,8 @@ export default defineComponent({
};

onMounted(async () => {
qDrawerContainer?.disableFocusTrap();

document.body.appendChild(instance?.vnode.el as Node);
document.documentElement.style.overflow = 'hidden';
document.addEventListener('focus', handleDocumentFocus, true);
Expand All @@ -246,6 +256,8 @@ export default defineComponent({
document.documentElement.style.overflow = '';
document.removeEventListener('focus', handleDocumentFocus, true);
if (!props.preventFocusAfterClosing) elementToFocusAfterClosing?.focus();

qDrawerContainer?.enableFocusTrap();
});

provide<QMessageBoxContainerProvider>('qMessageBoxContainer', {
Expand Down