Skip to content

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

refactor(cmdk): component exports and theme toggle

Greg Richardsona8ed7f646651
diff --git a/apps/docs/components/Navigation/NavBar.tsx b/apps/docs/components/Navigation/NavBar.tsx
index 5096d3589b6..eb7ee10434f 100644
--- a/apps/docs/components/Navigation/NavBar.tsx
+++ b/apps/docs/components/Navigation/NavBar.tsx
@@ -35,14 +35,6 @@ const NavBar: FC = () => {
{ text: 'Reference', key: 'reference', link: '/reference' },
]
- const toggleDarkMode = () => {
- localStorage.setItem('supabaseDarkMode', (!isDarkMode).toString())
- toggleTheme()
-
- const key = localStorage.getItem('supabaseDarkMode')
- document.documentElement.className = key === 'true' ? 'dark' : ''
- }
-
const onSelectVersion = (version: string) => {
// [Joshen] Ideally we use <Link> but this works for now
if (!library) return
@@ -182,7 +174,7 @@ const NavBar: FC = () => {
</a>
</li>
<li className="px-4">
- <div className="cursor-pointer" onClick={toggleDarkMode}>
+ <div className="cursor-pointer" onClick={() => toggleTheme()}>
{isDarkMode ? (
<IconMoon size={18} strokeWidth={2} className="text-scale-1200" />
) : (
diff --git a/apps/docs/components/Navigation/NavigationMenu/TopNavBar.tsx b/apps/docs/components/Navigation/NavigationMenu/TopNavBar.tsx
index a3c1a9707e3..270269d5085 100644
--- a/apps/docs/components/Navigation/NavigationMenu/TopNavBar.tsx
+++ b/apps/docs/components/Navigation/NavigationMenu/TopNavBar.tsx
@@ -44,14 +44,6 @@ const TopNavBar: FC = () => {
{ text: 'Reference', key: 'reference', link: '/reference' },
]
- const toggleDarkMode = () => {
- localStorage.setItem('supabaseDarkMode', (!isDarkMode).toString())
- toggleTheme()
-
- const key = localStorage.getItem('supabaseDarkMode')
- document.documentElement.className = key === 'true' ? 'dark' : ''
- }
-
const onSelectVersion = (version: string) => {
// [Joshen] Ideally we use <Link> but this works for now
if (!library) return
@@ -155,7 +147,7 @@ const TopNavBar: FC = () => {
</Button>
<ul className="flex items-center">
<li className="px-4">
- <div className="cursor-pointer" onClick={toggleDarkMode}>
+ <div className="cursor-pointer" onClick={() => toggleTheme()}>
{isDarkMode ? (
<IconMoon
size={16}
diff --git a/apps/docs/components/Navigation/NavigationMenu/TopNavBarRef.tsx b/apps/docs/components/Navigation/NavigationMenu/TopNavBarRef.tsx
index bb655bd3c6c..7501f116c2a 100644
--- a/apps/docs/components/Navigation/NavigationMenu/TopNavBarRef.tsx
+++ b/apps/docs/components/Navigation/NavigationMenu/TopNavBarRef.tsx
@@ -35,15 +35,6 @@ const TopNavBarRef: FC = () => {
{ text: 'Reference', key: 'reference', link: '/reference' },
]
- const toggleDarkMode = () => {
- localStorage.setItem('supabaseDarkMode', (!isDarkMode).toString())
- toggleTheme()
-
- const key = localStorage.getItem('supabaseDarkMode')
- document.documentElement.className = key === 'true' ? 'dark' : ''
- document.documentElement.style.colorScheme = key === 'true' ? 'dark' : ''
- }
-
const onSelectVersion = (version: string) => {
// [Joshen] Ideally we use <Link> but this works for now
if (!library) return
@@ -148,7 +139,7 @@ const TopNavBarRef: FC = () => {
</ul>
<ul className="flex items-center">
<li className="px-4">
- <div className="cursor-pointer" onClick={toggleDarkMode}>
+ <div className="cursor-pointer" onClick={() => toggleTheme()}>
{isDarkMode ? (
<IconMoon
size={16}
diff --git a/apps/www/components/DarkModeToggle.tsx b/apps/www/components/DarkModeToggle.tsx
index 789bdd7a5a4..4b3b03cc7aa 100644
--- a/apps/www/components/DarkModeToggle.tsx
+++ b/apps/www/components/DarkModeToggle.tsx
@@ -4,14 +4,6 @@ import { IconSun, IconMoon } from 'ui'
function DarkModeToggle() {
const { isDarkMode, toggleTheme } = useTheme()
- const toggleDarkMode = () => {
- localStorage.setItem('supabaseDarkMode', (!isDarkMode).toString())
- toggleTheme()
-
- const key = localStorage.getItem('supabaseDarkMode')
- document.documentElement.className = key === 'true' ? 'dark' : ''
- }
-
return (
<div className="flex items-center">
<IconSun className="text-scale-900" strokeWidth={2} />
@@ -24,7 +16,7 @@ function DarkModeToggle() {
isDarkMode ? 'bg-scale-500' : 'bg-scale-900'
} mx-5
`}
- onClick={() => toggleDarkMode()}
+ onClick={() => toggleTheme()}
>
<span className="sr-only">Toggle Themes</span>
<span
diff --git a/packages/common/Providers.tsx b/packages/common/Providers.tsx
index ab023562c17..b32f378b8cb 100644
--- a/packages/common/Providers.tsx
+++ b/packages/common/Providers.tsx
@@ -1,8 +1,18 @@
import { createContext, useContext, useEffect, useState } from 'react'
-interface UseThemeProps {
+export interface UseThemeProps {
isDarkMode?: boolean
- toggleTheme: () => void
+ /**
+ * Toggle between dark mode and light mode.
+ *
+ * ---
+ *
+ * If `darkMode` left `undefined`, toggles between modes.
+ *
+ * If `darkMode` set, forces
+ * dark mode if `true` and light mode if `false`.
+ */
+ toggleTheme: (darkMode?: boolean) => void
}
interface ThemeProviderProps {
@@ -25,8 +35,15 @@ export const ThemeProvider = ({ children }: ThemeProviderProps) => {
setIsDarkMode(!key || key === 'true')
}, [])
- const toggleTheme = () => {
- setIsDarkMode(!isDarkMode)
+ const toggleTheme: UseThemeProps['toggleTheme'] = (darkMode) => {
+ const newMode = typeof darkMode === 'boolean' ? darkMode : !isDarkMode
+
+ localStorage.setItem('supabaseDarkMode', newMode.toString())
+
+ const key = localStorage.getItem('supabaseDarkMode')
+ document.documentElement.className = key === 'true' ? 'dark' : ''
+
+ setIsDarkMode(newMode)
}
return (
diff --git a/packages/ui/src/components/Command/AiCommand.tsx b/packages/ui/src/components/Command/AiCommand.tsx
index 993ff42a773..c8dfcdf1e67 100644
--- a/packages/ui/src/components/Command/AiCommand.tsx
+++ b/packages/ui/src/components/Command/AiCommand.tsx
@@ -1,34 +1,19 @@
-import React, { useEffect, useReducer } from 'react'
import type { CreateCompletionResponse } from 'openai'
-import { FC, useCallback, useRef, useState } from 'react'
+import * as React from 'react'
+import { useCallback, useEffect, useReducer, useRef, useState } from 'react'
import ReactMarkdown from 'react-markdown'
import remarkGfm from 'remark-gfm'
-// @ts-ignore
import { SSE } from 'sse.js'
-// import clippyImageDark from '../../public/img/clippy-dark.png'
-// import clippyImage from '../../public/img/clippy.png'
import { useSupabaseClient } from '@supabase/auth-helpers-react'
import { useTheme } from 'common/Providers'
-import Image from 'next/image'
-import {
- Button,
- IconAlertCircle,
- IconAlertTriangle,
- IconLoader,
- IconSearch,
- IconUser,
- Input,
- Loading,
- Modal,
- Tabs,
-} from 'ui'
+import { IconAlertCircle, IconAlertTriangle, IconLoader, IconUser, Input } from 'ui'
// import components from '~/components'
// import { IS_PLATFORM } from '~/lib/constants'
-import { CommandGroup, CommandItem, CommandInput } from './Command.utils'
import { IconCopy } from '../Icon/icons/IconCopy'
+import { CommandGroup, CommandItem } from './Command.utils'
-import { AiIcon, AiIconChat, COMMAND_ROUTES } from './Command'
+import { AiIcon, AiIconChat, COMMAND_ROUTES } from './CommandMenu'
import { useCommandMenu } from './CommandMenuProvider'
const questions = [
@@ -119,13 +104,13 @@ function promptDataReducer(
throw Error('Unknown action.')
}
-interface IAiCommand {
+export interface AiCommandProps {
query?: string
setQuery?: () => void
page?: string
}
-const AiCommand: FC<IAiCommand> = ({ query, setQuery, page }) => {
+const AiCommand = ({ query, setQuery, page }: AiCommandProps) => {
const { isDarkMode } = useTheme()
// const { close, query, setQuery } = useSearch()
@@ -690,4 +675,4 @@ const AiCommand: FC<IAiCommand> = ({ query, setQuery, page }) => {
)
}
-export { AiCommand }
+export default AiCommand
diff --git a/packages/ui/src/components/Command/Command.tsx b/packages/ui/src/components/Command/CommandMenu.tsx
similarity index 93%
rename from packages/ui/src/components/Command/Command.tsx
rename to packages/ui/src/components/Command/CommandMenu.tsx
index 1ff973f7950..a1b0d65b22f 100644
--- a/packages/ui/src/components/Command/Command.tsx
+++ b/packages/ui/src/components/Command/CommandMenu.tsx
@@ -1,32 +1,25 @@
-import * as React from 'react'
-import { createContext, useContext } from 'react'
import { useRouter } from 'next/router'
+import * as React from 'react'
-import { Calculator, Calendar, CreditCard, Settings, Smile, User } from 'lucide-react'
import { useCommandState } from 'cmdk-supabase'
+import { CreditCard, Settings, User } from 'lucide-react'
+import { IconBook } from '../Icon/icons/IconBook'
+import { IconCopy } from '../Icon/icons/IconCopy'
+import { IconInbox } from '../Icon/icons/IconInbox'
+import { IconMonitor } from '../Icon/icons/IconMonitor'
+import AiCommand from './AiCommand'
import {
CommandDialog,
CommandEmpty,
CommandGroup,
CommandInput,
CommandItem,
- CommandItemStale,
CommandLabel,
CommandList,
- CommandSeparator,
CommandShortcut,
} from './Command.utils'
-import { IconCloudDrizzle } from '../Icon/icons/IconCloudDrizzle'
-import { IconBook } from '../Icon/icons/IconBook'
-import { IconInbox } from '../Icon/icons/IconInbox'
-import { IconMonitor } from '../Icon/icons/IconMonitor'
-import { Input } from '../Input'
-import { AiCommand } from './AiCommand'
-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 DocsSearch from './DocsSearch'
export const AiIcon = () => (
<svg
@@ -83,23 +76,18 @@ export const AiIconChat = () => (
</div>
)
-interface IActions {
- toggleTheme: () => void
-}
-
const ItemHandler = (props: any) => {
const search = useCommandState((state) => state.search)
if (!search && props.isSubItem) return null
return <CommandItem {...props} />
}
-function CommandMenu({ actions }: { actions: IActions }) {
+const CommandMenu = () => {
const [search, setSearch] = React.useState('')
- const [aiSearch, setAiSearch] = React.useState('')
const [pages, setPages] = React.useState([])
const page = pages[pages.length - 1]
const router = useRouter()
- const { isOpen, setIsOpen } = useCommandMenu()
+ const { isOpen, setIsOpen, actions } = useCommandMenu()
const ThemeOptions = ({ isSubItem = false }) => {
return (
@@ -107,7 +95,7 @@ function CommandMenu({ actions }: { actions: IActions }) {
<ItemHandler
isSubItem={isSubItem}
onSelect={() => {
- actions.toggleTheme()
+ actions.toggleTheme(true)
setIsOpen(false)
}}
>
@@ -116,7 +104,7 @@ function CommandMenu({ actions }: { actions: IActions }) {
<ItemHandler
isSubItem={isSubItem}
onSelect={() => {
- actions.toggleTheme()
+ actions.toggleTheme(false)
setIsOpen(false)
}}
>
@@ -126,24 +114,11 @@ function CommandMenu({ actions }: { actions: IActions }) {
)
}
- React.useEffect(() => {
- const down = (e: KeyboardEvent) => {
- if (e.key === 'k' && e.metaKey) {
- setIsOpen((open) => !open)
- }
- }
-
- document.addEventListener('keydown', down)
- return () => document.removeEventListener('keydown', down)
- }, [])
-
function handleSetPages(pages: any, keepSearch: any) {
setPages(pages)
if (!keepSearch) setSearch('')
}
- console.log('page', page)
-
const showCommandInput = page === undefined || !AI_CHAT_ROUTES.includes(page)
return (
@@ -421,9 +396,8 @@ function CommandMenu({ actions }: { actions: IActions }) {
{page !== 'Ask anything' || (!page && <CommandEmpty>No results found.</CommandEmpty>)}
</CommandList>
</CommandDialog>
- {/* <AiCommand /> */}
</>
)
}
-export { CommandMenu }
+export default CommandMenu
diff --git a/packages/ui/src/components/Command/CommandMenuProvider.tsx b/packages/ui/src/components/Command/CommandMenuProvider.tsx
index 7ffc283ca9f..bfafc12dcb3 100644
--- a/packages/ui/src/components/Command/CommandMenuProvider.tsx
+++ b/packages/ui/src/components/Command/CommandMenuProvider.tsx
@@ -1,12 +1,14 @@
+import { useTheme, UseThemeProps } from 'common'
import * as React from 'react'
import { createContext, PropsWithChildren, useContext, useEffect, useState } from 'react'
-import { CommandMenu } from './Command'
+import CommandMenu from './CommandMenu'
export interface CommandMenuContextValue {
isOpen: boolean
setIsOpen: React.Dispatch<React.SetStateAction<boolean>>
isLoading: boolean
setIsLoading: React.Dispatch<React.SetStateAction<boolean>>
+ actions: CommandMenuActions
}
export const CommandMenuContext = createContext<CommandMenuContextValue>(undefined)
export const useCommandMenu = () => {
@@ -19,14 +21,23 @@ export const useCommandMenu = () => {
return context
}
+export interface CommandMenuActions {
+ toggleTheme: UseThemeProps['toggleTheme']
+}
+
const CommandMenuProvider = ({ children }: PropsWithChildren<{}>) => {
const [isOpen, setIsOpen] = useState(false)
const [isLoading, setIsLoading] = useState(false)
+ const { toggleTheme } = useTheme()
+
+ const actions: CommandMenuActions = {
+ toggleTheme,
+ }
useKeyboardEvents({ setIsOpen })
return (
- <CommandMenuContext.Provider value={{ isOpen, setIsOpen, isLoading, setIsLoading }}>
+ <CommandMenuContext.Provider value={{ isOpen, setIsOpen, isLoading, setIsLoading, actions }}>
{children}
<CommandMenu />
</CommandMenuContext.Provider>
diff --git a/packages/ui/src/components/Command/index.tsx b/packages/ui/src/components/Command/index.tsx
index f0302798aee..13ce4393e14 100644
--- a/packages/ui/src/components/Command/index.tsx
+++ b/packages/ui/src/components/Command/index.tsx
@@ -1,6 +1,13 @@
-export * from './Command'
-
export { default as CommandMenuProvider } from './CommandMenuProvider'
export * from './CommandMenuProvider'
+export { default as CommandMenu } from './CommandMenu'
+export * from './CommandMenu'
+
+export { default as AiCommand } from './AiCommand'
+export * from './AiCommand'
+
+export { default as DocsSearch } from './DocsSearch'
+export * from './DocsSearch'
+
export { default as SearchButton } from './SearchButton'