-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(richtext-lexical): allow exiting the RTE with the keyboard in Fir…
…efox (#8654) Closes #8653. Originally this PR was for making the `IndentFeature` opt-in instead of opt-out, which would have been a breaking change. After some discussion it was determined it would be better if we could keep the `IndentFeature` by default and instead come up with a custom escape key solution to prevent keyboard users from becoming trapped in the editor. These changes are my interpretation of how we can solve this problem in a way that feels natural for a keyboard user. When a keyboard user becomes trapped, the usual approach is to press the escape key (e.g. modals) to be able to leave the current context and continue navigating. These changes allow that to happen while minimising the cognitive load by not needing to remember whether the `IndentFeature` is toggled on or off. I've also ensured the `IndentFeature` can actually be turned off if consciously removed from the lexical editor features (previously it was still enabled even if it was removed). Ideally this should be handled on the lexical side in the `TabIndentationPlugin` itself (I will begin to look into the feasibility of this), but for now this should be suitable to ensure the experience for keyboard users isn't completely blocked (there are a number of other improvements that could be made but I will create more specific issues for those). Open to discussion and amendments. Once we're aligned on the approach I'm happy to implement tests as needed. ### Before https://github.com/user-attachments/assets/95183bb6-f36e-4b44-8c3b-d880c822d315 ### After https://github.com/user-attachments/assets/d34be50a-8f31-4b81-83d1-236d5ce9d8b5 --------- Co-authored-by: Germán Jabloñski <43938777+GermanJablo@users.noreply.github.com>
- Loading branch information
1 parent
3961223
commit 3c35d81
Showing
4 changed files
with
118 additions
and
6 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
61 changes: 61 additions & 0 deletions
61
packages/richtext-lexical/src/features/indent/client/plugins/index.tsx
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,61 @@ | ||
'use client' | ||
|
||
import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext.js' | ||
import { TabIndentationPlugin } from '@lexical/react/LexicalTabIndentationPlugin' | ||
import { mergeRegister } from '@lexical/utils' | ||
import { COMMAND_PRIORITY_NORMAL, FOCUS_COMMAND, KEY_ESCAPE_COMMAND } from 'lexical' | ||
import { useEffect, useState } from 'react' | ||
|
||
import type { PluginComponent } from '../../../typesClient.js' | ||
|
||
export const IndentPlugin: PluginComponent<undefined> = () => { | ||
const [editor] = useLexicalComposerContext() | ||
|
||
const [firefoxFlag, setFirefoxFlag] = useState<boolean>(false) | ||
|
||
useEffect(() => { | ||
return mergeRegister( | ||
editor.registerCommand<MouseEvent>( | ||
FOCUS_COMMAND, | ||
() => { | ||
setFirefoxFlag(false) | ||
return true | ||
}, | ||
COMMAND_PRIORITY_NORMAL, | ||
), | ||
editor.registerCommand( | ||
KEY_ESCAPE_COMMAND, | ||
() => { | ||
setFirefoxFlag(true) | ||
editor.getRootElement()?.blur() | ||
return true | ||
}, | ||
COMMAND_PRIORITY_NORMAL, | ||
), | ||
) | ||
}, [editor, setFirefoxFlag]) | ||
|
||
useEffect(() => { | ||
if (!firefoxFlag) { | ||
return | ||
} | ||
|
||
const handleKeyDown = (e: KeyboardEvent) => { | ||
if (!['Escape', 'Shift'].includes(e.key)) { | ||
setFirefoxFlag(false) | ||
} | ||
// Pressing Shift+Tab after blurring refocuses the editor in Firefox | ||
// we focus parent to allow exiting the editor | ||
if (e.shiftKey && e.key === 'Tab') { | ||
editor.getRootElement()?.parentElement?.focus() | ||
} | ||
} | ||
|
||
document.addEventListener('keydown', handleKeyDown) | ||
return () => { | ||
document.removeEventListener('keydown', handleKeyDown) | ||
} | ||
}, [editor, firefoxFlag]) | ||
|
||
return <TabIndentationPlugin /> | ||
} |
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