Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Cmd K hitting backspace while search input is empty should return to first level (#52206)

Joshen Lim2f7ccdd6bc3a
diff --git a/packages/ui-patterns/src/CommandMenu/api/CommandMenu.tsx b/packages/ui-patterns/src/CommandMenu/api/CommandMenu.tsx
index f71cd830cc0..75316524880 100644
--- a/packages/ui-patterns/src/CommandMenu/api/CommandMenu.tsx
+++ b/packages/ui-patterns/src/CommandMenu/api/CommandMenu.tsx
@@ -19,7 +19,6 @@ import {
} from 'ui'
import { useCurrentPage, usePageComponent, usePopPage } from './hooks/pagesHooks'
-import { useQuery, useSetQuery } from './hooks/queryHooks'
import { useCommandMenuTelemetryContext } from './hooks/useCommandMenuTelemetryContext'
import {
useCommandMenuOpen,
@@ -229,12 +228,6 @@ function CommandMenu({ children, trigger }: CommandMenuProps) {
const size = useCommandMenuSize()
- const page = useCurrentPage()
- const popPage = usePopPage()
-
- const query = useQuery()
- const setQuery = useSetQuery()
-
const telemetryContext = useCommandMenuTelemetryContext()
const { ref: contentRef } = useTouchGestures({ toggleOpen: () => setOpen(!open) })
@@ -262,11 +255,12 @@ function CommandMenu({ children, trigger }: CommandMenuProps) {
onInteractOutside={() => handleOpenChange(false)}
onEscapeKeyDown={(e) => {
e.preventDefault()
- return query ? setQuery('') : page ? popPage() : handleOpenChange(false)
+ handleOpenChange(false)
}}
size={size}
className={cn(
'relative flex flex-col my-0 mx-auto rounded-t-lg',
+ 'focus:outline-none focus-visible:outline-none',
'h-[85dvh] mt-[15vh] md:max-h-[500px] md:mt-0 left-0 bottom-0 md:bottom-auto',
'!animate-in !slide-in-from-bottom-[85%] duration-300!',
'data-[state=closed]:!animate-out data-[state=closed]:!slide-out-to-bottom',
diff --git a/packages/ui-patterns/src/CommandMenu/api/CommandMenuInput.tsx b/packages/ui-patterns/src/CommandMenu/api/CommandMenuInput.tsx
index 653c9f7d8ee..474d719a543 100644
--- a/packages/ui-patterns/src/CommandMenu/api/CommandMenuInput.tsx
+++ b/packages/ui-patterns/src/CommandMenu/api/CommandMenuInput.tsx
@@ -5,6 +5,7 @@ import { forwardRef, useCallback, useEffect, useRef, useState } from 'react'
import type React from 'react'
import { cn, CommandInput } from 'ui'
+import { useCurrentPage, usePopPage } from './hooks/pagesHooks'
import { useQuery, useSetQuery } from './hooks/queryHooks'
import { useCommandMenuTelemetryContext } from './hooks/useCommandMenuTelemetryContext'
@@ -44,9 +45,12 @@ function useFocusInputOnWiderScreens(ref: React.ForwardedRef<HTMLInputElement>)
const CommandMenuInput = forwardRef<
React.ElementRef<typeof CommandInput>,
React.ComponentPropsWithoutRef<typeof CommandInput>
->(({ className, wrapperClassName, ...props }, ref) => {
+>(({ className, wrapperClassName, onKeyDown, ...props }, ref) => {
const inputRef = useFocusInputOnWiderScreens(ref)
+ const page = useCurrentPage()
+ const popPage = usePopPage()
+
const query = useQuery()
const setQuery = useSetQuery()
@@ -111,6 +115,17 @@ const CommandMenuInput = forwardRef<
}
}, [inputValue, imeComposing])
+ const handleKeyDown = (event: React.KeyboardEvent<HTMLInputElement>) => {
+ onKeyDown?.(event)
+ if (event.defaultPrevented) return
+
+ // Backspace on an empty input steps back to the previous page
+ if (event.key === 'Backspace' && !inputValue && !imeComposing && page) {
+ event.preventDefault()
+ popPage()
+ }
+ }
+
return (
<div className="relative w-full" cmdk-input-wrapper="">
<CommandInput
@@ -120,6 +135,7 @@ const CommandMenuInput = forwardRef<
ref={inputRef}
value={inputValue}
onValueChange={handleValueChange}
+ onKeyDown={handleKeyDown}
placeholder="Run a command or search..."
onCompositionStart={() => setImeComposing(true)}
onCompositionEnd={() => setImeComposing(false)}
@@ -127,7 +143,7 @@ const CommandMenuInput = forwardRef<
className={cn(
'flex h-11 w-full rounded-md bg-transparent px-2 y-4 md:py-7 outline-hidden',
'focus:shadow-none focus:ring-transparent',
- 'text-base text-foreground-light placeholder:text-foreground-muted disabled:cursor-not-allowed disabled:opacity-50 border-0',
+ 'text-sm text-foreground-light placeholder:text-foreground-muted disabled:cursor-not-allowed disabled:opacity-50 border-0',
className
)}
{...props}