-
Notifications
You must be signed in to change notification settings - Fork 452
♿(frontend) add focus trap and enter key support to remove doc modal #1531
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,17 +6,21 @@ import { | |
| useToastProvider, | ||
| } from '@openfun/cunningham-react'; | ||
| import { useRouter } from 'next/router'; | ||
| import { useEffect } from 'react'; | ||
| import { Trans, useTranslation } from 'react-i18next'; | ||
|
|
||
| import { Box, ButtonCloseModal, Text, TextErrors } from '@/components'; | ||
| import { useConfig } from '@/core'; | ||
| import { KEY_LIST_DOC_TRASHBIN } from '@/docs/docs-grid'; | ||
| import { useKeyboardAction } from '@/hooks'; | ||
|
|
||
| import { KEY_LIST_DOC } from '../api/useDocs'; | ||
| import { useRemoveDoc } from '../api/useRemoveDoc'; | ||
| import { useDocUtils } from '../hooks'; | ||
| import { Doc } from '../types'; | ||
|
|
||
| const CANCEL_BUTTON_ID = 'modal-remove-doc-cancel-button'; | ||
|
|
||
| interface ModalRemoveDocProps { | ||
| doc: Doc; | ||
| onClose: () => void; | ||
|
|
@@ -57,32 +61,51 @@ export const ModalRemoveDoc = ({ | |
| }, | ||
| }); | ||
|
|
||
| useEffect(() => { | ||
| const cancelButton = document.getElementById(CANCEL_BUTTON_ID); | ||
| if (cancelButton instanceof HTMLButtonElement) { | ||
|
Comment on lines
+65
to
+66
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it is better to use |
||
| cancelButton.focus(); | ||
| } | ||
| }, []); | ||
|
Comment on lines
+64
to
+69
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand the purpose of this |
||
|
|
||
| const handleClose = () => { | ||
| onClose(); | ||
| }; | ||
|
|
||
| const handleDelete = () => { | ||
| removeDoc({ | ||
| docId: doc.id, | ||
| }); | ||
| }; | ||
|
|
||
| const handleCloseKeyDown = useKeyboardAction(handleClose); | ||
| const handleDeleteKeyDown = useKeyboardAction(handleDelete); | ||
|
|
||
| return ( | ||
| <Modal | ||
| isOpen | ||
| closeOnClickOutside | ||
| hideCloseButton | ||
| onClose={() => onClose()} | ||
| onClose={handleClose} | ||
| aria-describedby="modal-remove-doc-title" | ||
| rightActions={ | ||
| <> | ||
| <Button | ||
| id={CANCEL_BUTTON_ID} | ||
| aria-label={t('Cancel the deletion')} | ||
| color="secondary" | ||
| fullWidth | ||
| onClick={() => onClose()} | ||
| onClick={handleClose} | ||
| onKeyDown={handleCloseKeyDown} | ||
| > | ||
| {t('Cancel')} | ||
| </Button> | ||
| <Button | ||
| aria-label={t('Delete document')} | ||
| color="danger" | ||
| fullWidth | ||
| onClick={() => | ||
| removeDoc({ | ||
| docId: doc.id, | ||
| }) | ||
| } | ||
| onClick={handleDelete} | ||
| onKeyDown={handleDeleteKeyDown} | ||
| > | ||
| {t('Delete')} | ||
| </Button> | ||
|
|
@@ -108,7 +131,8 @@ export const ModalRemoveDoc = ({ | |
| </Text> | ||
| <ButtonCloseModal | ||
| aria-label={t('Close the delete modal')} | ||
| onClick={() => onClose()} | ||
| onClick={handleClose} | ||
| onKeyDown={handleCloseKeyDown} | ||
| /> | ||
| </Box> | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export * from './useKeyboardAction'; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import { KeyboardEvent, useCallback } from 'react'; | ||
|
|
||
| /** | ||
| * Hook to handle keyboard actions (Enter and Space) on interactive elements. | ||
| * Prevents default behavior and stops propagation to avoid conflicts. | ||
| * | ||
| * @param callback - The function to execute when Enter or Space is pressed | ||
| * @returns A keyboard event handler function | ||
|
|
||
| */ | ||
| export const useKeyboardAction = (callback: () => void) => { | ||
| return useCallback( | ||
| (event: KeyboardEvent<HTMLElement>) => { | ||
| if (event.key === 'Enter' || event.key === ' ') { | ||
| event.preventDefault(); | ||
| event.stopPropagation(); | ||
| callback(); | ||
| } | ||
| }, | ||
| [callback], | ||
| ); | ||
| }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You just created the hook
useKeyboardAction, you should use it.