Skip to content

🎥 Make videos programmatically with React

simplify keybindings

Jonny Burgere808f94cbe63
diff --git a/packages/cli/src/editor/components/Editor.tsx b/packages/cli/src/editor/components/Editor.tsx
index 8e267d7aa8..ac474fd47d 100644
--- a/packages/cli/src/editor/components/Editor.tsx
+++ b/packages/cli/src/editor/components/Editor.tsx
@@ -7,6 +7,7 @@ import {
} from 'remotion';
import styled from 'styled-components';
import {BACKGROUND} from '../helpers/colors';
+import {noop} from '../helpers/noop';
import {
CheckerboardContext,
loadCheckerboardOption,
@@ -111,7 +112,7 @@ export const Editor: React.FC = () => {
value={emitter}
>
<HighestZIndexProvider>
- <HigherZIndex>
+ <HigherZIndex onEscape={noop}>
<Background>
<Root />
<UpdateCheck />
diff --git a/packages/cli/src/editor/components/Menu/MenuItem.tsx b/packages/cli/src/editor/components/Menu/MenuItem.tsx
index 692229f2b1..6224c8bf51 100644
--- a/packages/cli/src/editor/components/Menu/MenuItem.tsx
+++ b/packages/cli/src/editor/components/Menu/MenuItem.tsx
@@ -9,7 +9,7 @@ import React, {
import ReactDOM from 'react-dom';
import {BACKGROUND, getBackgroundFromHoverState} from '../../helpers/colors';
import {FONT_FAMILY} from '../../helpers/font';
-import {useZIndex} from '../../state/z-index';
+import {HigherZIndex, useZIndex} from '../../state/z-index';
import {ComboboxValue} from '../NewComposition/ComboBox';
import {MenuContent} from '../NewComposition/MenuContent';
import {
@@ -159,16 +159,21 @@ export const MenuItem: React.FC<{
</button>
{portalStyle
? ReactDOM.createPortal(
- <div style={outerStyle}>
- <div className={SUBMENU_CONTAINER_CLASS_NAME} style={portalStyle}>
- <MenuContent
- onArrowLeft={onArrowLeft}
- onArrowRight={onArrowRight}
- values={menu.items}
- onHide={onItemQuit}
- />
+ <HigherZIndex onEscape={onItemQuit}>
+ <div style={outerStyle}>
+ <div
+ className={SUBMENU_CONTAINER_CLASS_NAME}
+ style={portalStyle}
+ >
+ <MenuContent
+ onArrowLeft={onArrowLeft}
+ onArrowRight={onArrowRight}
+ values={menu.items}
+ onHide={onItemQuit}
+ />
+ </div>
</div>
- </div>,
+ </HigherZIndex>,
portal
)
: null}
diff --git a/packages/cli/src/editor/components/ModalContainer.tsx b/packages/cli/src/editor/components/ModalContainer.tsx
index 0359c534b5..3474ffa810 100644
--- a/packages/cli/src/editor/components/ModalContainer.tsx
+++ b/packages/cli/src/editor/components/ModalContainer.tsx
@@ -1,7 +1,6 @@
-import React, {useEffect} from 'react';
+import React from 'react';
import {BACKGROUND} from '../helpers/colors';
import {FONT_FAMILY} from '../helpers/font';
-import {useKeybinding} from '../helpers/use-keybinding';
import {HigherZIndex} from '../state/z-index';
const backgroundOverlay: React.CSSProperties = {
@@ -25,26 +24,8 @@ const panel: React.CSSProperties = {
export const ModalContainer: React.FC<{
onEscape: () => void;
}> = ({children, onEscape}) => {
- const keybindings = useKeybinding();
-
- useEffect(() => {
- keybindings.stashOther();
- }, [keybindings]);
-
- useEffect(() => {
- const escape = keybindings.registerKeybinding(
- 'keydown',
- 'Escape',
- onEscape
- );
-
- return () => {
- escape.unregister();
- };
- }, [keybindings, onEscape]);
-
return (
- <HigherZIndex>
+ <HigherZIndex onEscape={onEscape}>
<div style={backgroundOverlay} role="dialog" aria-modal="true">
<div style={panel}>{children}</div>
</div>
diff --git a/packages/cli/src/editor/components/NewComposition/ComboBox.tsx b/packages/cli/src/editor/components/NewComposition/ComboBox.tsx
index d1ed09c2d6..a1904df7a2 100644
--- a/packages/cli/src/editor/components/NewComposition/ComboBox.tsx
+++ b/packages/cli/src/editor/components/NewComposition/ComboBox.tsx
@@ -129,7 +129,7 @@ export const Combobox: React.FC<{
</button>
{portalStyle
? ReactDOM.createPortal(
- <HigherZIndex>
+ <HigherZIndex onEscape={onHide}>
<div style={outerStyle}>
<div
className={SUBMENU_CONTAINER_CLASS_NAME}
diff --git a/packages/cli/src/editor/components/NewComposition/MenuContent.tsx b/packages/cli/src/editor/components/NewComposition/MenuContent.tsx
index 9af53b9d29..019c54da43 100644
--- a/packages/cli/src/editor/components/NewComposition/MenuContent.tsx
+++ b/packages/cli/src/editor/components/NewComposition/MenuContent.tsx
@@ -15,10 +15,6 @@ export const MenuContent: React.FC<{
onHide();
}, [onHide]);
- useEffect(() => {
- keybindings.stashOther();
- }, [keybindings]);
-
useEffect(() => {
const escapeBinding = keybindings.registerKeybinding(
'keydown',
diff --git a/packages/cli/src/editor/helpers/use-keybinding.ts b/packages/cli/src/editor/helpers/use-keybinding.ts
index 53d8398838..34122c92dd 100644
--- a/packages/cli/src/editor/helpers/use-keybinding.ts
+++ b/packages/cli/src/editor/helpers/use-keybinding.ts
@@ -4,18 +4,12 @@ import {
KeyEventType,
RegisteredKeybinding,
} from '../state/keybindings';
+import {useZIndex} from '../state/z-index';
export const useKeybinding = () => {
const [paneId] = useState(() => String(Math.random()));
const context = useContext(KeybindingContext);
-
- const stashOther = useCallback(() => {
- context.stashOtherKeybindings(paneId);
- }, [context, paneId]);
-
- const unstashOther = useCallback(() => {
- context.unstashOtherKeybindings(paneId);
- }, [context, paneId]);
+ const {isHighestContext} = useZIndex();
const registerKeybinding = useCallback(
(
@@ -23,6 +17,12 @@ export const useKeybinding = () => {
key: string,
callback: (e: KeyboardEvent) => void
) => {
+ if (!isHighestContext) {
+ return {
+ unregister: () => undefined,
+ };
+ }
+
const listener = (e: KeyboardEvent) => {
if (e.key === key) {
callback(e);
@@ -42,19 +42,17 @@ export const useKeybinding = () => {
unregister: () => context.unregisterKeybinding(toRegister),
};
},
- [context, paneId]
+ [context, isHighestContext, paneId]
);
useEffect(() => {
return () => {
context.unregisterPane(paneId);
- context.unstashOtherKeybindings(paneId);
};
}, [context, paneId]);
- return useMemo(() => ({registerKeybinding, stashOther, unstashOther}), [
+ return useMemo(() => ({registerKeybinding, isHighestContext}), [
registerKeybinding,
- stashOther,
- unstashOther,
+ isHighestContext,
]);
};
diff --git a/packages/cli/src/editor/state/keybindings.tsx b/packages/cli/src/editor/state/keybindings.tsx
index df319c6cd6..23d7424711 100644
--- a/packages/cli/src/editor/state/keybindings.tsx
+++ b/packages/cli/src/editor/state/keybindings.tsx
@@ -20,8 +20,6 @@ type StashedKeybinding = {
export type KeybindingContextType = {
registerKeybinding: (binding: RegisteredKeybinding) => void;
unregisterKeybinding: (binding: RegisteredKeybinding) => void;
- stashOtherKeybindings: (paneId: string) => void;
- unstashOtherKeybindings: (paneId: string) => void;
unregisterPane: (paneId: string) => void;
};
@@ -29,13 +27,10 @@ export const KeybindingContext = createContext<KeybindingContextType>({
registerKeybinding: () => undefined,
unregisterKeybinding: () => undefined,
unregisterPane: () => undefined,
- stashOtherKeybindings: () => undefined,
- unstashOtherKeybindings: () => undefined,
});
export const KeybindingContextProvider: React.FC = ({children}) => {
const registered = useRef<RegisteredKeybinding[]>([]);
- const stashed = useRef<StashedKeybinding[]>([]);
const registerKeybinding = useCallback((binding: RegisteredKeybinding) => {
registered.current = [...registered.current, binding];
@@ -50,57 +45,12 @@ export const KeybindingContextProvider: React.FC = ({children}) => {
return false;
}
- return true;
- });
- stashed.current = stashed.current.filter((r) => {
- if (r.binding.id === binding.id) {
- window.removeEventListener(binding.event, binding.callback);
-
- return false;
- }
-
return true;
});
}, []);
- const stashOtherKeybindings = useCallback(
- (paneId: string) => {
- const matchedKeybindings = registered.current.filter(
- (r) => r.registeredFromPane !== paneId
- );
- stashed.current = [
- ...stashed.current,
- ...matchedKeybindings.map(
- (binding): StashedKeybinding => ({
- binding,
- stashedPane: paneId,
- })
- ),
- ];
- for (const binding of matchedKeybindings) {
- unregisterKeybinding(binding);
- }
- },
- [unregisterKeybinding]
- );
- const unstashOtherKeybindings = useCallback(
- (paneId: string) => {
- const matchedKeybindings = stashed.current.filter(
- (r) => r.stashedPane === paneId
- );
- for (const {binding} of matchedKeybindings) {
- registerKeybinding(binding);
- }
-
- stashed.current = stashed.current.filter((r) => r.stashedPane !== paneId);
- },
- [registerKeybinding]
- );
-
const unregisterPane = useCallback(
(paneId: string) => {
- unstashOtherKeybindings(paneId);
- stashed.current = stashed.current.filter((r) => r.stashedPane !== paneId);
const matchedKeybindings = registered.current.filter(
(r) => r.registeredFromPane === paneId
);
@@ -108,24 +58,16 @@ export const KeybindingContextProvider: React.FC = ({children}) => {
unregisterKeybinding(matched);
}
},
- [unregisterKeybinding, unstashOtherKeybindings]
+ [unregisterKeybinding]
);
const value = useMemo((): KeybindingContextType => {
return {
registerKeybinding,
unregisterKeybinding,
- stashOtherKeybindings,
- unstashOtherKeybindings,
unregisterPane,
};
- }, [
- registerKeybinding,
- stashOtherKeybindings,
- unregisterKeybinding,
- unregisterPane,
- unstashOtherKeybindings,
- ]);
+ }, [registerKeybinding, unregisterKeybinding, unregisterPane]);
return (
<KeybindingContext.Provider value={value}>
diff --git a/packages/cli/src/editor/state/z-index.tsx b/packages/cli/src/editor/state/z-index.tsx
index f3283cd04b..03a5297dc7 100644
--- a/packages/cli/src/editor/state/z-index.tsx
+++ b/packages/cli/src/editor/state/z-index.tsx
@@ -1,4 +1,5 @@
import React, {createContext, useContext, useEffect, useMemo} from 'react';
+import {useKeybinding} from '../helpers/use-keybinding';
import {HighestZIndexContext} from './highest-z-index';
type ZIndex = {
@@ -9,7 +10,29 @@ export const ZIndexContext = createContext<ZIndex>({
currentIndex: 0,
});
-export const HigherZIndex: React.FC = ({children}) => {
+const EscapeHook: React.FC<{
+ onEscape: () => void;
+}> = ({onEscape}) => {
+ const keybindings = useKeybinding();
+
+ useEffect(() => {
+ const escape = keybindings.registerKeybinding(
+ 'keydown',
+ 'Escape',
+ onEscape
+ );
+
+ return () => {
+ escape.unregister();
+ };
+ }, [keybindings, onEscape]);
+
+ return null;
+};
+
+export const HigherZIndex: React.FC<{
+ onEscape: () => void;
+}> = ({children, onEscape}) => {
const context = useContext(ZIndexContext);
const highestContext = useContext(HighestZIndexContext);
@@ -27,20 +50,25 @@ export const HigherZIndex: React.FC = ({children}) => {
}, [currentIndex]);
return (
- <ZIndexContext.Provider value={value}>{children}</ZIndexContext.Provider>
+ <ZIndexContext.Provider value={value}>
+ <EscapeHook onEscape={onEscape} />
+ {children}
+ </ZIndexContext.Provider>
);
};
export const useZIndex = () => {
const context = useContext(ZIndexContext);
const highestContext = useContext(HighestZIndexContext);
+ const isHighestContext = highestContext.highestIndex === context.currentIndex;
return useMemo(
() => ({
currentZIndex: context.currentIndex,
highestZIndex: highestContext.highestIndex,
- tabIndex: context.currentIndex === highestContext.highestIndex ? 0 : -1,
+ isHighestContext,
+ tabIndex: isHighestContext ? 0 : -1,
}),
- [context.currentIndex, highestContext.highestIndex]
+ [context.currentIndex, highestContext.highestIndex, isHighestContext]
);
};