Skip to content

🎥 Make videos programmatically with React

: Add dropdown type-to-select navigation

Cursor Agent805c56a3a5ac
diff --git a/packages/studio/src/components/NewComposition/MenuContent.tsx b/packages/studio/src/components/NewComposition/MenuContent.tsx
index 380caf8de8..dc1144ec6c 100644
--- a/packages/studio/src/components/NewComposition/MenuContent.tsx
+++ b/packages/studio/src/components/NewComposition/MenuContent.tsx
@@ -14,6 +14,7 @@ import {
MENU_VERTICAL_PADDING,
} from '../Menu/styles';
import type {ComboboxValue} from './ComboBox';
+import {findTypeaheadMenuItem} from './menu-typeahead';
const BORDER_SIZE = 1;
@@ -53,6 +54,8 @@ export const MenuContent: React.FC<{
const [subMenuActivated, setSubMenuActivated] =
useState<SubMenuActivated>(false);
+ const typeaheadQueryRef = useRef('');
+ const typeaheadTimeoutRef = useRef<number | null>(null);
if (values[0].type === 'divider') {
throw new Error('first value cant be divide');
@@ -72,6 +75,14 @@ export const MenuContent: React.FC<{
setSelectedItem(id);
}, []);
+ const clearTypeahead = useCallback(() => {
+ typeaheadQueryRef.current = '';
+ if (typeaheadTimeoutRef.current !== null) {
+ window.clearTimeout(typeaheadTimeoutRef.current);
+ typeaheadTimeoutRef.current = null;
+ }
+ }, []);
+
const isItemSelectable = useCallback((v: ComboboxValue) => {
return v.type !== 'divider' && !v.disabled;
}, []);
@@ -171,6 +182,47 @@ export const MenuContent: React.FC<{
setSubMenuActivated('without-mouse');
}, [onNextMenu, selectedItem, values]);
+ const onTypeahead = useCallback(
+ (event: KeyboardEvent) => {
+ if (
+ event.ctrlKey ||
+ event.metaKey ||
+ event.altKey ||
+ event.key.length !== 1 ||
+ event.key.trim().length === 0
+ ) {
+ return;
+ }
+
+ const {activeElement} = document;
+ if (
+ activeElement instanceof HTMLInputElement ||
+ activeElement instanceof HTMLTextAreaElement
+ ) {
+ return;
+ }
+
+ typeaheadQueryRef.current = `${typeaheadQueryRef.current}${event.key}`;
+ const matchedId = findTypeaheadMenuItem({
+ query: typeaheadQueryRef.current,
+ values,
+ });
+ if (matchedId !== null) {
+ setSelectedItem(matchedId);
+ }
+
+ if (typeaheadTimeoutRef.current !== null) {
+ window.clearTimeout(typeaheadTimeoutRef.current);
+ }
+
+ typeaheadTimeoutRef.current = window.setTimeout(() => {
+ typeaheadQueryRef.current = '';
+ typeaheadTimeoutRef.current = null;
+ }, 700);
+ },
+ [values],
+ );
+
const containerWithHeight: React.CSSProperties = useMemo(() => {
const containerStyles = {...container};
if (fixedHeight === null) {
@@ -186,6 +238,31 @@ export const MenuContent: React.FC<{
return containerStyles;
}, [fixedHeight, isMobileLayout]);
+ useEffect(() => {
+ if (
+ !keybindings.isHighestContext ||
+ !process.env.KEYBOARD_SHORTCUTS_ENABLED
+ ) {
+ return;
+ }
+
+ const onKeyDown = (event: KeyboardEvent) => {
+ onTypeahead(event);
+ };
+
+ window.addEventListener('keydown', onKeyDown);
+ return () => {
+ window.removeEventListener('keydown', onKeyDown);
+ clearTypeahead();
+ };
+ }, [clearTypeahead, keybindings.isHighestContext, onTypeahead]);
+
+ useEffect(() => {
+ return () => {
+ clearTypeahead();
+ };
+ }, [clearTypeahead]);
+
useEffect(() => {
const escapeBinding = keybindings.registerKeybinding({
event: 'keydown',
diff --git a/packages/studio/src/components/NewComposition/menu-typeahead.ts b/packages/studio/src/components/NewComposition/menu-typeahead.ts
new file mode 100644
index 0000000000..8e97afde2a
--- /dev/null
+++ b/packages/studio/src/components/NewComposition/menu-typeahead.ts
@@ -0,0 +1,46 @@
+import type {ComboboxValue} from './ComboBox';
+
+const getLabelToMatch = (value: ComboboxValue): string | null => {
+ if (value.type === 'divider' || value.disabled) {
+ return null;
+ }
+
+ if (value.quickSwitcherLabel) {
+ return value.quickSwitcherLabel;
+ }
+
+ if (typeof value.label === 'string') {
+ return value.label;
+ }
+
+ if (typeof value.value === 'string') {
+ return value.value;
+ }
+
+ return null;
+};
+
+export const findTypeaheadMenuItem = ({
+ query,
+ values,
+}: {
+ query: string;
+ values: ComboboxValue[];
+}): string | null => {
+ const normalizedQuery = query.trim().toLowerCase();
+
+ if (normalizedQuery.length === 0) {
+ return null;
+ }
+
+ const matched = values.find((value) => {
+ const label = getLabelToMatch(value);
+ return label ? label.toLowerCase().startsWith(normalizedQuery) : false;
+ });
+
+ if (!matched || matched.type === 'divider') {
+ return null;
+ }
+
+ return matched.id;
+};
diff --git a/packages/studio/src/test/menu-typeahead.test.ts b/packages/studio/src/test/menu-typeahead.test.ts
new file mode 100644
index 0000000000..192450ada2
--- /dev/null
+++ b/packages/studio/src/test/menu-typeahead.test.ts
@@ -0,0 +1,65 @@
+import {expect, test} from 'bun:test';
+import type {ComboboxValue} from '../components/NewComposition/ComboBox';
+import {findTypeaheadMenuItem} from '../components/NewComposition/menu-typeahead';
+
+const values: ComboboxValue[] = [
+ {
+ type: 'divider',
+ id: 'divider-1',
+ },
+ {
+ type: 'item',
+ id: 'apple',
+ label: 'Apple',
+ value: 'apple',
+ onClick: () => undefined,
+ keyHint: null,
+ leftItem: null,
+ subMenu: null,
+ quickSwitcherLabel: null,
+ },
+ {
+ type: 'item',
+ id: 'banana-disabled',
+ label: 'Banana',
+ value: 'banana',
+ onClick: () => undefined,
+ keyHint: null,
+ leftItem: null,
+ subMenu: null,
+ quickSwitcherLabel: null,
+ disabled: true,
+ },
+ {
+ type: 'item',
+ id: 'custom',
+ label: 'Not used',
+ value: 'fallback',
+ onClick: () => undefined,
+ keyHint: null,
+ leftItem: null,
+ subMenu: null,
+ quickSwitcherLabel: 'Custom Label',
+ },
+];
+
+test('finds item by label prefix', () => {
+ expect(findTypeaheadMenuItem({query: 'ap', values})).toBe('apple');
+});
+
+test('matches case-insensitive', () => {
+ expect(findTypeaheadMenuItem({query: 'AP', values})).toBe('apple');
+});
+
+test('ignores divider and disabled items', () => {
+ expect(findTypeaheadMenuItem({query: 'ban', values})).toBe(null);
+});
+
+test('prefers quickSwitcherLabel if available', () => {
+ expect(findTypeaheadMenuItem({query: 'custom', values})).toBe('custom');
+});
+
+test('returns null for empty or unmatched query', () => {
+ expect(findTypeaheadMenuItem({query: ' ', values})).toBe(null);
+ expect(findTypeaheadMenuItem({query: 'zzz', values})).toBe(null);
+});