-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
front: operationalstudies: focus added on the name input when modal o…
…pens - added focus on mapSearchStation - added focus on addOrEditProjectModal - added focus on addOrEditStudyModal - added focus on addOrEditScenarioModal - create custom hook useModalFocusTrap in utils/hooks
- Loading branch information
Showing
6 changed files
with
81 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { useEffect } from 'react'; | ||
|
||
/** | ||
* Allow the user to escape the modal by pressing escape and to trap the focus inside it | ||
* */ | ||
|
||
export default function useModalFocusTrap( | ||
modalRef: React.RefObject<HTMLDivElement>, | ||
closeModal: () => void | ||
) { | ||
useEffect(() => { | ||
const modalElement = modalRef.current; | ||
|
||
const focusableElements = modalElement?.querySelectorAll( | ||
// last declaration stands for all elements not natively focusable like li | ||
'input, button, [tabindex]:not([tabindex="-1"])' | ||
) as NodeListOf<Element>; | ||
|
||
const firstElement = focusableElements[0] as HTMLElement; | ||
const lastElement = focusableElements[focusableElements?.length - 1] as HTMLElement; | ||
|
||
/** | ||
* | ||
* Prevent the tab event and set focus on : | ||
* - last element if we are pressing on "shift" in addition to "tab" and are on the first element | ||
* - first element if we are only pressing "tab" and are on the last element | ||
*/ | ||
const handleTabKeyPress = (event: KeyboardEvent) => { | ||
if (event.key === 'Tab') { | ||
if (event.shiftKey && document.activeElement === firstElement) { | ||
event.preventDefault(); | ||
lastElement.focus(); | ||
} else if (!event.shiftKey && document.activeElement === lastElement) { | ||
event.preventDefault(); | ||
firstElement.focus(); | ||
} | ||
} | ||
}; | ||
|
||
const handleEscapeKeyPress: (event: KeyboardEvent) => void = (event: KeyboardEvent) => { | ||
if (event.key === 'Escape') { | ||
closeModal(); | ||
} | ||
}; | ||
|
||
modalElement?.addEventListener('keydown', handleTabKeyPress); | ||
modalElement?.addEventListener('keydown', handleEscapeKeyPress); | ||
|
||
return () => { | ||
modalElement?.removeEventListener('keydown', handleTabKeyPress); | ||
modalElement?.removeEventListener('keydown', handleEscapeKeyPress); | ||
}; | ||
}, [modalRef, closeModal]); | ||
} |