From 7fd878edf668d410df083b6fc19d7bd90292e4d1 Mon Sep 17 00:00:00 2001 From: Sam Becker Date: Mon, 19 Feb 2024 12:55:26 -0600 Subject: [PATCH] Add themes to command-k --- src/components/CommandKClient.tsx | 49 +++++++++++++++++++++++-------- 1 file changed, 36 insertions(+), 13 deletions(-) diff --git a/src/components/CommandKClient.tsx b/src/components/CommandKClient.tsx index c3bc9dbd..c04b4e2a 100644 --- a/src/components/CommandKClient.tsx +++ b/src/components/CommandKClient.tsx @@ -7,9 +7,19 @@ import { clsx } from 'clsx/lite'; import { useDebounce } from 'use-debounce'; import Spinner from './Spinner'; import { useRouter } from 'next/navigation'; +import { useTheme } from 'next-themes'; const LISTENER_KEYDOWN = 'keydown'; +export type CommandKSection = { + heading: string + items: { + label: string + path?: string + action?: () => void + }[] +} + export default function CommandKClient({ isLoading, onQueryChange, @@ -17,18 +27,14 @@ export default function CommandKClient({ }: { isLoading?: boolean onQueryChange?: (query: string) => void - sections?: { - heading: string - items: { - label: string - path: string - }[] - }[] + sections?: CommandKSection[] }) { const [open, setOpen] = useState(false); const [query, setQuery] = useState(''); const [queryDebounced] = useDebounce(query, 1000); + const { setTheme } = useTheme(); + const router = useRouter(); useEffect(() => { @@ -78,12 +84,26 @@ export default function CommandKClient({ aria-hidden="true" className={clsx( 'absolute bottom-4 inset-x-0 h-6 z-10 pointer-events-none', - 'bg-gradient-to-t from-white to-transparent', + 'bg-gradient-to-t to-transparent', + 'from-white dark:from-black', )} /> No results found. - {sections + {[{ + heading: 'Theme', + items: [{ + label: 'System', + action: () => setTheme('system'), + }, { + label: 'Light', + action: () => setTheme('light'), + }, { + label: 'Dark', + action: () => setTheme('dark'), + }], + } as CommandKSection] + .concat(sections) .filter(({ items }) => items.length > 0) .map(({ heading, items }) => - {items.map(({ label, path }) => + {items.map(({ label, path, action }) => { - setOpen(false); - router.push(path); + action?.(); + if (path) { + setOpen(false); + router.push(path); + } }} > {label}