Disable keyboard shortcuts when focus is in a modal
This commit is contained in:
		
					parent
					
						
							
								18fa1371d3
							
						
					
				
			
			
				commit
				
					
						56bd54a645
					
				
			
		
					 2 changed files with 22 additions and 14 deletions
				
			
		| 
						 | 
					@ -157,7 +157,7 @@ export function InCallView({
 | 
				
			||||||
  const { hideScreensharing } = useUrlParams();
 | 
					  const { hideScreensharing } = useUrlParams();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  useCallViewKeyboardShortcuts(
 | 
					  useCallViewKeyboardShortcuts(
 | 
				
			||||||
    !feedbackModalState.isOpen,
 | 
					    containerRef1,
 | 
				
			||||||
    toggleMicrophoneMuted,
 | 
					    toggleMicrophoneMuted,
 | 
				
			||||||
    toggleLocalVideoMuted,
 | 
					    toggleLocalVideoMuted,
 | 
				
			||||||
    setMicrophoneMuted
 | 
					    setMicrophoneMuted
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -14,30 +14,41 @@ See the License for the specific language governing permissions and
 | 
				
			||||||
limitations under the License.
 | 
					limitations under the License.
 | 
				
			||||||
*/
 | 
					*/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import { useCallback, useRef } from "react";
 | 
					import { RefObject, useCallback, useRef } from "react";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import { getSetting } from "./settings/useSetting";
 | 
					import { getSetting } from "./settings/useSetting";
 | 
				
			||||||
import { useEventTarget } from "./useEvents";
 | 
					import { useEventTarget } from "./useEvents";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * Determines whether focus is in the same part of the tree as the given
 | 
				
			||||||
 | 
					 * element (specifically, if an ancestor or descendant of it is focused).
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					const mayReceiveKeyEvents = (e: HTMLElement): boolean => {
 | 
				
			||||||
 | 
					  const focusedElement = document.activeElement
 | 
				
			||||||
 | 
					  return focusedElement !== null && (focusedElement.contains(e) || e.contains(focusedElement))
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export function useCallViewKeyboardShortcuts(
 | 
					export function useCallViewKeyboardShortcuts(
 | 
				
			||||||
  enabled: boolean,
 | 
					  focusElement: RefObject<HTMLElement | null>,
 | 
				
			||||||
  toggleMicrophoneMuted: () => void,
 | 
					  toggleMicrophoneMuted: () => void,
 | 
				
			||||||
  toggleLocalVideoMuted: () => void,
 | 
					  toggleLocalVideoMuted: () => void,
 | 
				
			||||||
  setMicrophoneMuted: (muted: boolean) => void
 | 
					  setMicrophoneMuted: (muted: boolean) => void
 | 
				
			||||||
) {
 | 
					) {
 | 
				
			||||||
  const spacebarHeld = useRef(false);
 | 
					  const spacebarHeld = useRef(false);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  // These event handlers are set on the window because we want users to be able
 | 
				
			||||||
 | 
					  // to trigger them without going to the trouble of focusing something
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  useEventTarget(
 | 
					  useEventTarget(
 | 
				
			||||||
    window,
 | 
					    window,
 | 
				
			||||||
    "keydown",
 | 
					    "keydown",
 | 
				
			||||||
    useCallback(
 | 
					    useCallback(
 | 
				
			||||||
      (event: KeyboardEvent) => {
 | 
					      (event: KeyboardEvent) => {
 | 
				
			||||||
        if (!enabled) return;
 | 
					        if (focusElement.current === null) return
 | 
				
			||||||
 | 
					        if (!mayReceiveKeyEvents(focusElement.current)) return;
 | 
				
			||||||
        // Check if keyboard shortcuts are enabled
 | 
					        // Check if keyboard shortcuts are enabled
 | 
				
			||||||
        const keyboardShortcuts = getSetting("keyboard-shortcuts", true);
 | 
					        const keyboardShortcuts = getSetting("keyboard-shortcuts", true);
 | 
				
			||||||
        if (!keyboardShortcuts) {
 | 
					        if (!keyboardShortcuts) return;
 | 
				
			||||||
          return;
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (event.key === "m") {
 | 
					        if (event.key === "m") {
 | 
				
			||||||
          toggleMicrophoneMuted();
 | 
					          toggleMicrophoneMuted();
 | 
				
			||||||
| 
						 | 
					@ -49,8 +60,6 @@ export function useCallViewKeyboardShortcuts(
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
      },
 | 
					      },
 | 
				
			||||||
      [
 | 
					      [
 | 
				
			||||||
        enabled,
 | 
					 | 
				
			||||||
        spacebarHeld,
 | 
					 | 
				
			||||||
        toggleLocalVideoMuted,
 | 
					        toggleLocalVideoMuted,
 | 
				
			||||||
        toggleMicrophoneMuted,
 | 
					        toggleMicrophoneMuted,
 | 
				
			||||||
        setMicrophoneMuted,
 | 
					        setMicrophoneMuted,
 | 
				
			||||||
| 
						 | 
					@ -63,19 +72,18 @@ export function useCallViewKeyboardShortcuts(
 | 
				
			||||||
    "keyup",
 | 
					    "keyup",
 | 
				
			||||||
    useCallback(
 | 
					    useCallback(
 | 
				
			||||||
      (event: KeyboardEvent) => {
 | 
					      (event: KeyboardEvent) => {
 | 
				
			||||||
        if (!enabled) return;
 | 
					        if (focusElement.current === null) return
 | 
				
			||||||
 | 
					        if (!mayReceiveKeyEvents(focusElement.current)) return;
 | 
				
			||||||
        // Check if keyboard shortcuts are enabled
 | 
					        // Check if keyboard shortcuts are enabled
 | 
				
			||||||
        const keyboardShortcuts = getSetting("keyboard-shortcuts", true);
 | 
					        const keyboardShortcuts = getSetting("keyboard-shortcuts", true);
 | 
				
			||||||
        if (!keyboardShortcuts) {
 | 
					        if (!keyboardShortcuts) return;
 | 
				
			||||||
          return;
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (event.key === " ") {
 | 
					        if (event.key === " ") {
 | 
				
			||||||
          spacebarHeld.current = false;
 | 
					          spacebarHeld.current = false;
 | 
				
			||||||
          setMicrophoneMuted(true);
 | 
					          setMicrophoneMuted(true);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
      },
 | 
					      },
 | 
				
			||||||
      [enabled, setMicrophoneMuted]
 | 
					      [setMicrophoneMuted]
 | 
				
			||||||
    )
 | 
					    )
 | 
				
			||||||
  );
 | 
					  );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue