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

Feature/#375 make modal accessible #378

Merged
merged 3 commits into from
Mar 8, 2024
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
19 changes: 15 additions & 4 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
import { MainScene } from '@/scenes';
import { useThemeContext } from './core/providers';
import {
CanvasSchemaProvider,
CanvasViewSettingsProvider,
ModalDialogProvider,
useThemeContext,
} from './core/providers';

function App() {
const { theme } = useThemeContext();

return (
<div className={theme.themeMode}>
<MainScene />
</div>
<CanvasViewSettingsProvider>
<CanvasSchemaProvider>
<ModalDialogProvider>
<div className={theme.themeMode}>
<MainScene />
</div>
</ModalDialogProvider>
</CanvasSchemaProvider>
</CanvasViewSettingsProvider>
);
}

Expand Down
36 changes: 34 additions & 2 deletions src/common/components/modal-dialog/modal-dialog.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,42 @@ import React from 'react';
import styles from './modal-dialog.styles.module.css';
import { useModalDialogContext } from '@/core/providers/modal-dialog-provider';
import { AnimatePresence, motion } from 'framer-motion';
import {
handleEscapeKeyDown,
handleFocus,
handleNextFocus,
handleTabsInsideDialog,
} from './modal.dialog.bussines';

export const ModalDialog: React.FC = () => {
const { closeModal, modalDialog } = useModalDialogContext();
const containerRef = React.useRef<HTMLDivElement>(null);
const previousFocusedElement = React.useRef<Element | null>(null);

const handleClick = () => {
closeModal();
};

const handleEscapeKeyDownEvent = (e: KeyboardEvent) =>
handleEscapeKeyDown(e, closeModal);

const handleTabsInsideDialogEvent = (e: KeyboardEvent) =>
handleTabsInsideDialog(e, containerRef);

React.useEffect(() => {
if (modalDialog.isOpen) {
document.body.classList.add('overflow-hidden');
document.addEventListener('keydown', handleEscapeKeyDownEvent);
document.addEventListener('keydown', handleTabsInsideDialogEvent);
handleFocus(previousFocusedElement, containerRef);
} else {
handleNextFocus(previousFocusedElement);
}

return () => {
document.body.classList.remove('overflow-hidden');
document.removeEventListener('keydown', handleEscapeKeyDownEvent);
document.removeEventListener('keydown', handleTabsInsideDialogEvent);
};
}, [modalDialog.isOpen]);

Expand All @@ -36,10 +59,19 @@ export const ModalDialog: React.FC = () => {
animate={{ opacity: 1, scale: 1 }}
exit={{ opacity: 0, scale: 0.8 }}
transition={{ duration: 0.4 }}
ref={containerRef}
tabIndex={-1}
aria-labelledby="modal-modal-title"
>
<div className={styles.dialogHeader}>
<h2 className={styles.dialogTitle}>{modalDialog.title}</h2>
<button className={styles.dialogButton} onClick={handleClick}>
<h2 className={styles.dialogTitle} id="modal-modal-title">
{modalDialog.title}
</h2>
<button
className={styles.dialogButton}
onClick={handleClick}
aria-label="close modal dialog"
>
<svg
xmlns="http://www.w3.org/2000/svg"
width="1em"
Expand Down
58 changes: 58 additions & 0 deletions src/common/components/modal-dialog/modal.dialog.bussines.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
export const handleTabsInsideDialog = (
e: KeyboardEvent,
containerRef: React.RefObject<HTMLDivElement>
) => {
const focusableElements = containerRef.current?.querySelectorAll(
'a[href], button, textarea, input[type="text"], input[type="radio"], input[type="checkbox"], select'
);
const lastFocusableElement =
focusableElements?.[focusableElements.length - 1];

if (
e.key === 'Tab' &&
!e.shiftKey &&
document.activeElement === lastFocusableElement
) {
e.preventDefault();
containerRef.current?.focus();
}
};

export const handleEscapeKeyDown = (
e: KeyboardEvent,
closeModal: () => void
) => {
e.key === 'Escape' && closeModal();
};

export const handleFocus = (
previousFocusedElement: React.MutableRefObject<Element | null>,
containerRef: React.RefObject<HTMLDivElement>
) => {
previousFocusedElement.current = document.activeElement;
containerRef.current?.focus();
const focusableElements = document.querySelectorAll(
'a[href], button, textarea, input[type="text"], input[type="radio"], input[type="checkbox"], select'
);
focusableElements.forEach((element: Element) => {
element.setAttribute('tabindex', '-1');
});
const focusableElementsInside = containerRef.current?.querySelectorAll(
'a[href], button, textarea, input[type="text"], input[type="radio"], input[type="checkbox"], select'
);
focusableElementsInside?.forEach((element: Element) => {
element.removeAttribute('tabindex');
});
};

export const handleNextFocus = (
previousFocusedElement: React.MutableRefObject<Element | null>
) => {
const focusableElements = document.querySelectorAll(
'a[href], button, textarea, input[type="text"], input[type="radio"], input[type="checkbox"], select'
);
focusableElements.forEach((element: Element) => {
element.removeAttribute('tabindex');
});
(previousFocusedElement.current as HTMLElement)?.focus();
};
28 changes: 11 additions & 17 deletions src/scenes/main.scene.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,21 @@
import { CanvasPod } from '@/pods/canvas/canvas.pod';
import { ToolbarPod } from '@/pods/toolbar/toolbar.pod';
import {
CanvasViewSettingsProvider,
ModalDialogProvider,
} from '@/core/providers';
import { useModalDialogContext } from '@/core/providers';
import { ModalDialog } from '@/common/components';
import { CanvasSchemaProvider } from '@/core/providers/canvas-schema';

import classes from './main.scene.module.css';
import { FooterPod } from '@/pods/footer';

export const MainScene: React.FC = () => {
const { modalDialog } = useModalDialogContext();
return (
<div className={classes.container}>
<CanvasViewSettingsProvider>
<CanvasSchemaProvider>
<ModalDialogProvider>
<ToolbarPod />
<CanvasPod />
<ModalDialog />
<FooterPod />
</ModalDialogProvider>
</CanvasSchemaProvider>
</CanvasViewSettingsProvider>
</div>
<>
<div className={classes.container} aria-hidden={modalDialog.isOpen}>
<ToolbarPod />
<CanvasPod />
<FooterPod />
</div>
<ModalDialog />
</>
);
};
Loading