-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5281 from nextcloud/fix/sidebar-focustrap
fix: Fix tab focus when other elements are displayed next to text that are within a focus trap
- Loading branch information
Showing
6 changed files
with
49 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { Extension } from '@tiptap/core' | ||
|
||
const toggleFocusTrap = ({ editor }) => { | ||
const trapStack = window._nc_focus_trap ?? [] | ||
const activeTrap = trapStack[trapStack.length - 1] | ||
|
||
const possibleEditorTabCommand = editor.can().sinkListItem('listItem') | ||
|| editor.can().goToNextCell() | ||
|| editor.can().goToPreviousCell() | ||
|
||
if (possibleEditorTabCommand) { | ||
activeTrap?.pause() | ||
} else { | ||
activeTrap?.unpause() | ||
} | ||
} | ||
|
||
const unpauseFocusTrap = ({ editor }) => { | ||
const trapStack = window._nc_focus_trap ?? [] | ||
const activeTrap = trapStack[trapStack.length - 1] | ||
|
||
activeTrap?.unpause() | ||
} | ||
|
||
/** | ||
* The viewer focus trap needs to be paused on the fly in order to properly handle tab commands in the editor, | ||
* as we have no control over if a tab key event is reaching the editor otherwise. This is because the focus trap | ||
* registeres a capture listener (https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#capture), so whenever we reach a tab command in the editor the focus trap will already have captured the event. | ||
* | ||
* We also cannot work around this by pushing our own focus trap to the stack, as the focus trap package does not offer any reliable way to programmatically focus the next element of the parent trap if we allow tabbing out of the editor. | ||
*/ | ||
const FocusTrap = Extension.create({ | ||
name: 'focustrap', | ||
onFocus: toggleFocusTrap, | ||
onBlur: unpauseFocusTrap, | ||
onSelectionUpdate: toggleFocusTrap, | ||
onTransaction: toggleFocusTrap, | ||
onUpdate: toggleFocusTrap, | ||
}) | ||
|
||
export default FocusTrap |
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