Skip to content

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

refactor(cmdk): replace search with command menu provider

Greg Richardson3949190fa7e1
diff --git a/apps/docs/components/Search/SearchButton.tsx b/apps/docs/components/Search/SearchButton.tsx
index 94da639a526..de86f28d0d8 100644
--- a/apps/docs/components/Search/SearchButton.tsx
+++ b/apps/docs/components/Search/SearchButton.tsx
@@ -1,37 +1,14 @@
-import {
- ButtonHTMLAttributes,
- DetailedHTMLProps,
- FC,
- PropsWithChildren,
- useEffect,
- useRef,
-} from 'react'
-import { useActionKey } from '~/hooks/useActionKey'
-import { useSearch } from './SearchProvider'
+import { ButtonHTMLAttributes, DetailedHTMLProps, FC, PropsWithChildren, useRef } from 'react'
+import { useCommandMenu } from '~/../../packages/ui/src/components/Command/CommandMenuProvider'
const SearchButton: FC<
PropsWithChildren<DetailedHTMLProps<ButtonHTMLAttributes<HTMLButtonElement>, HTMLButtonElement>>
> = ({ children, ...props }) => {
const searchButtonRef = useRef<HTMLButtonElement>()
- const { open, setQuery } = useSearch()
-
- useEffect(() => {
- function onKeyDown(event: KeyboardEvent) {
- if (searchButtonRef.current === document.activeElement) {
- if (/[a-zA-Z0-9]/.test(String.fromCharCode(event.keyCode))) {
- open()
- setQuery?.(event.key)
- }
- }
- }
- window.addEventListener('keydown', onKeyDown)
- return () => {
- window.removeEventListener('keydown', onKeyDown)
- }
- }, [open, setQuery])
+ const { setIsOpen } = useCommandMenu()
return (
- <button type="button" ref={searchButtonRef} onClick={open} {...props}>
+ <button type="button" ref={searchButtonRef} onClick={() => setIsOpen(true)} {...props}>
{children}
</button>
)
diff --git a/apps/docs/pages/_app.tsx b/apps/docs/pages/_app.tsx
index d68bc1e5151..f4d0859d366 100644
--- a/apps/docs/pages/_app.tsx
+++ b/apps/docs/pages/_app.tsx
@@ -1,17 +1,14 @@
import { createBrowserSupabaseClient } from '@supabase/auth-helpers-nextjs'
import { SessionContextProvider } from '@supabase/auth-helpers-react'
import { ThemeProvider } from 'common/Providers'
-import { DefaultSeo } from 'next-seo'
-import Head from 'next/head'
import { useRouter } from 'next/router'
import { useEffect, useState } from 'react'
import { AppPropsWithLayout } from 'types'
+import { CommandMenuProvider } from 'ui'
import Favicons from '~/components/Favicons'
-import SearchProvider from '~/components/Search/SearchProvider'
import SiteLayout from '~/layouts/SiteLayout'
import { IS_PLATFORM, LOCAL_SUPABASE } from '~/lib/constants'
import { post } from '~/lib/fetchWrappers'
-import { CommandMenu } from 'ui'
import '../styles/algolia-search.scss'
import '../styles/ch.scss'
import '../styles/docsearch.scss'
@@ -69,23 +66,22 @@ function MyApp({ Component, pageProps }: AppPropsWithLayout) {
{IS_PLATFORM || LOCAL_SUPABASE ? (
<SessionContextProvider supabaseClient={supabase}>
<ThemeProvider>
- <SearchProvider>
+ <CommandMenuProvider>
<SiteLayout>
<Component {...pageProps} />
</SiteLayout>
- </SearchProvider>
+ </CommandMenuProvider>
</ThemeProvider>
</SessionContextProvider>
) : (
<ThemeProvider>
- <SearchProvider>
+ <CommandMenuProvider>
<SiteLayout>
<Component {...pageProps} />
</SiteLayout>
- </SearchProvider>
+ </CommandMenuProvider>
</ThemeProvider>
)}
- <CommandMenu />
</>
)
}
diff --git a/packages/ui/src/components/Command/AiCommand.tsx b/packages/ui/src/components/Command/AiCommand.tsx
index 8460139a9c8..e2e9c42e238 100644
--- a/packages/ui/src/components/Command/AiCommand.tsx
+++ b/packages/ui/src/components/Command/AiCommand.tsx
@@ -30,7 +30,8 @@ import SearchResult, { SearchResultType } from './SearchResult'
import { CommandGroup, CommandItem, CommandInput } from './Command.utils'
import { IconCopy } from '../Icon/icons/IconCopy'
-import { AiIcon, AiIconChat, COMMAND_ROUTES, useCommandMenu } from './Command'
+import { AiIcon, AiIconChat, COMMAND_ROUTES } from './Command'
+import { useCommandMenu } from './CommandMenuProvider'
const questions = [
'How do I get started with Supabase?',
diff --git a/packages/ui/src/components/Command/Command.tsx b/packages/ui/src/components/Command/Command.tsx
index a6ab6dd3125..121b3b55a33 100644
--- a/packages/ui/src/components/Command/Command.tsx
+++ b/packages/ui/src/components/Command/Command.tsx
@@ -26,6 +26,7 @@ import { IconSun } from '../Icon/icons/IconSun'
import { IconMoon } from '../Icon/icons/IconMoon'
import { IconCopy } from '../Icon/icons/IconCopy'
import DocsSearch from './DocsSearch'
+import { useCommandMenu } from './CommandMenuProvider'
// import { SearchProvider } from './SearchProvider'
export const AiIcon = () => (
@@ -93,21 +94,13 @@ const ItemHandler = (props: any) => {
return <CommandItem {...props} />
}
-export interface CommandMenuContextValue {
- isLoading: boolean
- setIsLoading: (isLoading: boolean) => void
-}
-export const CommandMenuContext = createContext<CommandMenuContextValue>(undefined)
-export const useCommandMenu = () => useContext(CommandMenuContext)
-
function CommandMenu({ actions }: { actions: IActions }) {
- const [open, setOpen] = React.useState(false)
const [search, setSearch] = React.useState('')
const [aiSearch, setAiSearch] = React.useState('')
const [pages, setPages] = React.useState([])
const page = pages[pages.length - 1]
- const [isLoading, setIsLoading] = React.useState(false)
const router = useRouter()
+ const { isOpen, setIsOpen } = useCommandMenu()
const ThemeOptions = ({ isSubItem = false }) => {
return (
@@ -116,7 +109,7 @@ function CommandMenu({ actions }: { actions: IActions }) {
isSubItem={isSubItem}
onSelect={() => {
actions.toggleTheme()
- setOpen(false)
+ setIsOpen(false)
}}
>
Change Theme to dark
@@ -125,7 +118,7 @@ function CommandMenu({ actions }: { actions: IActions }) {
isSubItem={isSubItem}
onSelect={() => {
actions.toggleTheme()
- setOpen(false)
+ setIsOpen(false)
}}
>
Change Theme to light
@@ -137,7 +130,7 @@ function CommandMenu({ actions }: { actions: IActions }) {
React.useEffect(() => {
const down = (e: KeyboardEvent) => {
if (e.key === 'k' && e.metaKey) {
- setOpen((open) => !open)
+ setIsOpen((open) => !open)
}
}
@@ -155,18 +148,12 @@ function CommandMenu({ actions }: { actions: IActions }) {
const showCommandInput = page === undefined || !AI_CHAT_ROUTES.includes(page)
return (
- <CommandMenuContext.Provider value={{ isLoading, setIsLoading }}>
- <p className="text-sm text-slate-500 dark:text-slate-400">
- Press{' '}
- <kbd className="pointer-events-none inline-flex h-5 select-none items-center gap-1 rounded border border-slate-100 bg-slate-100 px-1.5 font-mono text-[10px] font-medium text-slate-600 opacity-100 dark:border-slate-700 dark:bg-slate-900 dark:text-slate-400">
- <span className="text-xs">⌘</span>J
- </kbd>
- </p>
+ <>
<CommandDialog
page={page}
- visible={open}
- onOpenChange={setOpen}
- onCancel={() => setOpen(!open)}
+ visible={isOpen}
+ onOpenChange={setIsOpen}
+ onCancel={() => setIsOpen(!open)}
size={'xlarge'}
className={'max-h-[70vh] lg:max-h-[50vh] overflow-hidden overflow-y-auto'}
onKeyDown={(e) => {
@@ -177,7 +164,7 @@ function CommandMenu({ actions }: { actions: IActions }) {
// || (e.key === 'Backspace' && !search)
) {
e.preventDefault()
- if (!page) setOpen(false)
+ if (!page) setIsOpen(false)
setSearch('')
setPages((pages) => pages.slice(0, -1))
}
@@ -423,7 +410,7 @@ function CommandMenu({ actions }: { actions: IActions }) {
<CommandItem
onSelect={() => {
router.push('https://supabase.com/docs')
- setOpen(false)
+ setIsOpen(false)
}}
>
<CommandLabel>Grab api keys</CommandLabel>
@@ -436,7 +423,7 @@ function CommandMenu({ actions }: { actions: IActions }) {
</CommandList>
</CommandDialog>
{/* <AiCommand /> */}
- </CommandMenuContext.Provider>
+ </>
)
}
diff --git a/packages/ui/src/components/Command/Command.utils.tsx b/packages/ui/src/components/Command/Command.utils.tsx
index 1c9f710975d..8efdce3db6e 100644
--- a/packages/ui/src/components/Command/Command.utils.tsx
+++ b/packages/ui/src/components/Command/Command.utils.tsx
@@ -9,7 +9,7 @@ import { Modal } from '../Modal'
import { ModalProps } from '../Modal/Modal'
import { KeyboardEventHandler } from 'react'
import { LoadingLine } from './LoadingLine'
-import { useCommandMenu } from './Command'
+import { useCommandMenu } from './CommandMenuProvider'
const Command = React.forwardRef<
React.ElementRef<typeof CommandPrimitive>,
diff --git a/packages/ui/src/components/Command/CommandMenuProvider.tsx b/packages/ui/src/components/Command/CommandMenuProvider.tsx
new file mode 100644
index 00000000000..7a5f46d2cbf
--- /dev/null
+++ b/packages/ui/src/components/Command/CommandMenuProvider.tsx
@@ -0,0 +1,52 @@
+import * as React from 'react'
+import { createContext, PropsWithChildren, useContext, useEffect, useState } from 'react'
+import { CommandMenu } from './Command'
+
+export interface CommandMenuContextValue {
+ isOpen: boolean
+ setIsOpen: React.Dispatch<React.SetStateAction<boolean>>
+ isLoading: boolean
+ setIsLoading: React.Dispatch<React.SetStateAction<boolean>>
+}
+export const CommandMenuContext = createContext<CommandMenuContextValue>(undefined)
+export const useCommandMenu = () => useContext(CommandMenuContext)
+
+const CommandMenuProvider = ({ children }: PropsWithChildren<{}>) => {
+ const [isOpen, setIsOpen] = useState(false)
+ const [isLoading, setIsLoading] = useState(false)
+
+ useKeyboardEvents({ setIsOpen })
+
+ return (
+ <CommandMenuContext.Provider value={{ isOpen, setIsOpen, isLoading, setIsLoading }}>
+ {children}
+ <CommandMenu />
+ </CommandMenuContext.Provider>
+ )
+}
+
+function useKeyboardEvents({ setIsOpen }: { setIsOpen: (isOpen: boolean) => void }) {
+ useEffect(() => {
+ function onKeyDown(event: KeyboardEvent) {
+ switch (event.key) {
+ case 'Escape':
+ setIsOpen(false)
+ return
+ case 'k':
+ case '/':
+ if (event.metaKey || event.ctrlKey) {
+ setIsOpen(true)
+ }
+ return
+ }
+ }
+
+ window.addEventListener('keydown', onKeyDown)
+
+ return () => {
+ window.removeEventListener('keydown', onKeyDown)
+ }
+ }, [setIsOpen])
+}
+
+export default CommandMenuProvider
diff --git a/packages/ui/src/components/Command/index.tsx b/packages/ui/src/components/Command/index.tsx
index 71923a11d52..4d57b6070e3 100644
--- a/packages/ui/src/components/Command/index.tsx
+++ b/packages/ui/src/components/Command/index.tsx
@@ -1 +1,4 @@
export * from './Command'
+
+export { default as CommandMenuProvider } from './CommandMenuProvider'
+export * from './CommandMenuProvider'