-
Notifications
You must be signed in to change notification settings - Fork 241
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add tab gate triggered by ESC key and disabled by refocusing the
editor or container
- Loading branch information
1 parent
0156fdb
commit cb98b51
Showing
3 changed files
with
68 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"react-live": patch | ||
--- | ||
|
||
Add tab gate to disable focus trap ([#399](https://github.com/FormidableLabs/react-live/pull/399)) |
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,58 @@ | ||
import { RefObject, useEffect, useState } from "react"; | ||
|
||
export default function useTabGate( | ||
container: RefObject<HTMLElement>, | ||
editor: RefObject<HTMLElement>, | ||
enabled = true | ||
) { | ||
const [tabGate, setTabGate] = useState(false); | ||
|
||
const setTabIndex = (element: RefObject<HTMLElement>, index: number) => | ||
element.current && (element.current.tabIndex = index); | ||
|
||
const resetTabIndexes = () => { | ||
if (container.current?.hasAttribute("tabIndex")) { | ||
container.current?.removeAttribute("tabIndex"); | ||
} | ||
setTabIndex(editor, 0); | ||
}; | ||
|
||
const containerBlur = (event: FocusEvent) => { | ||
if (event.relatedTarget !== container.current) resetTabIndexes(); | ||
}; | ||
|
||
const catchEscape = (event: KeyboardEvent) => { | ||
if (event.code === "Escape" && enabled) setTabGate(true); | ||
}; | ||
|
||
const containerFocus = () => editor.current?.focus(); | ||
|
||
const editorFocus = () => { | ||
resetTabIndexes(); | ||
setTabGate(false); | ||
}; | ||
|
||
useEffect(() => { | ||
container.current?.addEventListener("blur", containerBlur); | ||
container.current?.addEventListener("focus", containerFocus); | ||
editor.current?.addEventListener("keydown", catchEscape); | ||
editor.current?.addEventListener("focus", editorFocus); | ||
|
||
return () => { | ||
container.current?.removeEventListener("blur", containerBlur); | ||
container.current?.removeEventListener("focus", containerFocus); | ||
editor.current?.removeEventListener("keydown", catchEscape); | ||
editor.current?.removeEventListener("focus", editorFocus); | ||
}; | ||
}, []); | ||
|
||
useEffect(() => { | ||
if (!tabGate) return; | ||
|
||
setTabIndex(container, 0); | ||
editor.current?.blur(); | ||
setTabIndex(editor, -1); | ||
}, [tabGate]); | ||
|
||
return tabGate; | ||
} |