Beautiful pdf components, built on Takumi and Forme. 100% Free, Zero config, one command setup.
feat(code-block, command-box): enhance command tab functionality and UI
Aniket-50875587813e476
diff --git a/apps/web/components/code-block-command.tsx b/apps/web/components/code-block-command.tsxindex 55c52ee..7a6d7b4 100644--- a/apps/web/components/code-block-command.tsx+++ b/apps/web/components/code-block-command.tsx@@ -1,17 +1,25 @@"use client";-import { useCallback, useMemo } from "react";+import { useCallback, useMemo, useState } from "react";import { CopyButton } from "@/components/copy-button";import { getIconForCommandTab } from "@/components/icons";+import {+ Select,+ SelectContent,+ SelectItem,+ SelectTrigger,+ SelectValue,+} from "@/components/ui/select";import { Tabs, TabsList, TabsTrigger, TabsContent } from "@/components/ui/tabs";import type { PackageManager } from "@/hooks/use-package-manager";-import { usePackageManager, useCommandTab } from "@/hooks/use-package-manager";+import { usePackageManager } from "@/hooks/use-package-manager";import type { Event } from "@/lib/events";import { cn } from "@/lib/utils";-const PROMPT_TEMPLATE = (cmd: string) =>- `Run \`${cmd}\` in this project to install it with the shadcn CLI. Don't rewrite the files it adds; if the command fails, show me the error.`;+type CommandTab = PackageManager | "shadcn" | "prompt";++const SHADCN_NPX_PREFIX = "npx shadcn@latest ";export const CodeBlockCommand = ({__npm__,@@ -29,68 +37,51 @@ export const CodeBlockCommand = ({copyEvent?: Event["name"];}) => {const [packageManager, setPackageManager] = usePackageManager();- const [commandTab, setCommandTab] = useCommandTab();-- const commandTabs = useMemo(- () => ({- bun: __bun__,- npm: __npm__,- pnpm: __pnpm__,- yarn: __yarn__,- }),- [__npm__, __pnpm__, __yarn__, __bun__]- );+ const [commandTab, setCommandTab] = useState<CommandTab | null>(null);- const isShadcnCommand = useMemo(- () => __npm__?.startsWith("npx shadcn@latest") ?? false,- [__npm__]- );+ const tabs = useMemo(() => {+ const packageManagerTabs = [+ { command: __bun__, value: "bun" },+ { command: __npm__, value: "npm" },+ { command: __pnpm__, value: "pnpm" },+ { command: __yarn__, value: "yarn" },+ ] satisfies { command: string | undefined; value: CommandTab }[];- const allTabs = useMemo(() => {- if (!isShadcnCommand) {- return ["bun", "npm", "pnpm", "yarn"];+ if (!__npm__?.startsWith(SHADCN_NPX_PREFIX)) {+ return packageManagerTabs;}- return ["bun", "npm", "pnpm", "yarn", "shadcn", "prompt"];- }, [isShadcnCommand]);- const handleCommandTabChange = useCallback(+ return [+ {+ command: `Run \`${__npm__}\` in this project to install it with the shadcn CLI. Don't rewrite the files it adds; if the command fails, show me the error.`,+ value: "prompt",+ },+ {+ command: __npm__.replace(SHADCN_NPX_PREFIX, "shadcn "),+ value: "shadcn",+ },+ ...packageManagerTabs,+ ];+ }, [__bun__, __npm__, __pnpm__, __yarn__]);++ const handleTabChange = useCallback((value: string) => {- if (["bun", "npm", "pnpm", "yarn"].includes(value)) {- setCommandTab(value as PackageManager);- setPackageManager(value as PackageManager);- } else {- setCommandTab(value as "shadcn" | "prompt");+ const tab = value as CommandTab;+ setCommandTab(tab);++ if (tab === "npm" || tab === "yarn" || tab === "pnpm" || tab === "bun") {+ setPackageManager(tab);}},- [setCommandTab, setPackageManager]+ [setPackageManager]);- const shadcnCommand = useMemo(- () => __npm__?.replace("npx shadcn@latest ", "shadcn ") ?? "",- [__npm__]- );-- const promptCommand = useMemo(- () => (__npm__ ? PROMPT_TEMPLATE(__npm__) : ""),- [__npm__]- );-- const copyValue = useMemo(() => {- if (commandTab === "shadcn") {- return shadcnCommand;- }- if (commandTab === "prompt") {- return promptCommand;- }- return commandTabs[packageManager] || "";- }, [commandTab, shadcnCommand, promptCommand, commandTabs, packageManager]);-- const copyEventForTab = useMemo(() => {- if (commandTab === "prompt") {- return "copy_agent_prompt";- }- return copyEvent;- }, [commandTab, copyEvent]);+ const selectedTab = commandTab ?? packageManager;+ const activeTab = tabs.some((tab) => tab.value === selectedTab)+ ? selectedTab+ : "npm";+ const copyValue = tabs.find((tab) => tab.value === activeTab)?.command ?? "";+ const isPromptTab = activeTab === "prompt";return (<div@@ -99,45 +90,57 @@ export const CodeBlockCommand = ({className)}>- <Tabs- className="gap-0"- onValueChange={handleCommandTabChange}- value={commandTab}- >+ <Tabs className="gap-0" onValueChange={handleTabChange} value={activeTab}><div className="border-border/50 flex items-center gap-2 border-b px-3 py-1">- <TabsList className="rounded-none bg-transparent p-0 [&_svg]:me-2 [&_svg]:size-4 [&_svg]:text-muted-foreground">- {getIconForCommandTab(commandTab)}+ <TabsList className="hidden rounded-none bg-transparent p-0 md:inline-flex [&_svg]:me-2 [&_svg]:size-4 [&_svg]:text-muted-foreground">+ {getIconForCommandTab(activeTab)}- {allTabs.map((key) => (+ {tabs.map((tab) => (<TabsTrigger- key={key}+ key={tab.value}className="data-[state=active]:border-input h-7 border border-transparent pt-0.5 data-[state=active]:shadow-none"sound="tabSwitch"- value={key}+ value={tab.value}>- {key}+ {tab.value}</TabsTrigger>))}</TabsList>+ <div className="flex items-center gap-2 md:hidden">+ {getIconForCommandTab(activeTab)}+ <Select onValueChange={handleTabChange} value={activeTab}>+ <SelectTrigger+ aria-label="Command"+ size="sm"+ className="font-sans bg-background px-2.5 my-0.5 shadow-none dark:bg-background dark:hover:bg-background"+ >+ <SelectValue />+ </SelectTrigger>+ <SelectContent>+ {tabs.map((tab) => (+ <SelectItem key={tab.value} value={tab.value}>+ {tab.value}+ </SelectItem>+ ))}+ </SelectContent>+ </Select>+ </div></div>- <div className="no-scrollbar overflow-x-auto">- {Object.entries(commandTabs).map(([key, value]) => (- <TabsContent key={key} className="mt-0 px-4 py-3.5" value={key}>- <pre>- <code+ <div className={cn("no-scrollbar", !isPromptTab && "overflow-x-auto")}>+ {tabs.map((tab) => (+ <TabsContent+ key={tab.value}+ className="mt-0 px-4 py-3.5"+ value={tab.value}+ >+ {tab.value === "prompt" ? (+ <pdata-slot="code-block"- data-language="bash"- className="font-mono text-sm/none"+ className="text-sm/relaxed whitespace-normal">- <span className="select-none">$ </span>- {value}- </code>- </pre>- </TabsContent>- ))}- {isShadcnCommand && (- <>- <TabsContent className="mt-0 px-4 py-3.5" value="shadcn">+ {tab.command}+ </p>+ ) : (<pre><codedata-slot="code-block"@@ -145,23 +148,18 @@ export const CodeBlockCommand = ({className="font-mono text-sm/none"><span className="select-none">$ </span>- {shadcnCommand}+ {tab.command}</code></pre>- </TabsContent>- <TabsContent className="mt-0 px-4 py-3.5" value="prompt">- <p className="whitespace-normal text-sm leading-relaxed">- {promptCommand}- </p>- </TabsContent>- </>- )}+ )}+ </TabsContent>+ ))}</div></Tabs><CopyButtonclassName="absolute top-2 right-2 z-10 size-7 opacity-70 hover:opacity-100 focus-visible:opacity-100"value={copyValue}- event={copyEventForTab}+ event={isPromptTab ? "copy_agent_prompt" : copyEvent}/></div>);diff --git a/apps/web/components/command-box.tsx b/apps/web/components/command-box.tsxindex dd43688..6a88e26 100644--- a/apps/web/components/command-box.tsx+++ b/apps/web/components/command-box.tsx@@ -1,11 +1,18 @@"use client";import { motion } from "motion/react";-import { useRef } from "react";+import { useCallback, useRef } from "react";import { CopyButton } from "@/components/copy-button";import { getIconForPackageManager } from "@/components/icons";import { RegistryAddButton } from "@/components/registry-add-button";+import {+ Select,+ SelectContent,+ SelectItem,+ SelectTrigger,+ SelectValue,+} from "@/components/ui/select";import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";import { TextFlip } from "@/components/ui/text-flip";import { SITE } from "@/constants/site";@@ -34,6 +41,13 @@ export const CommandBox = ({ className }: { className?: string }) => {const currentItemRef = useRef(registryItemNames[0]);+ const handlePackageManagerChange = useCallback(+ (value: string) => {+ setPackageManager(value as PackageManager);+ },+ [setPackageManager]+ );+return (<divclassName={cn(@@ -43,13 +57,11 @@ export const CommandBox = ({ className }: { className?: string }) => {><TabsclassName="gap-0"- onValueChange={(value: string) => {- setPackageManager(value as PackageManager);- }}+ onValueChange={handlePackageManagerChange}value={packageManager}><div className="border-border/50 flex items-center gap-2 border-b px-3 py-1">- <TabsList className="rounded-none bg-transparent p-0 [&_svg]:me-2 [&_svg]:size-4 [&_svg]:text-muted-foreground">+ <TabsList className="hidden rounded-none bg-transparent p-0 md:inline-flex [&_svg]:me-2 [&_svg]:size-4 [&_svg]:text-muted-foreground">{getIconForPackageManager(packageManager)}{Object.entries(pmCommands).map(([key]) => (@@ -63,6 +75,28 @@ export const CommandBox = ({ className }: { className?: string }) => {</TabsTrigger>))}</TabsList>+ <div className="flex items-center gap-2 md:hidden">+ {getIconForPackageManager(packageManager)}+ <Select+ onValueChange={handlePackageManagerChange}+ value={packageManager}+ >+ <SelectTrigger+ aria-label="Package manager"+ size="sm"+ className="font-sans bg-background px-2.5 my-0.5 shadow-none dark:bg-background dark:hover:bg-background"+ >+ <SelectValue />+ </SelectTrigger>+ <SelectContent>+ {Object.keys(pmCommands).map((key) => (+ <SelectItem key={key} value={key}>+ {key}+ </SelectItem>+ ))}+ </SelectContent>+ </Select>+ </div></div><pre className="-translate-y-px px-4 py-3.5"><codediff --git a/apps/web/components/icons.tsx b/apps/web/components/icons.tsxindex 432fe27..b29f5f6 100644--- a/apps/web/components/icons.tsx+++ b/apps/web/components/icons.tsx@@ -1,4 +1,4 @@-import { FileIcon, TerminalSquareIcon } from "lucide-react";+import { FileIcon, TerminalSquareIcon, TextIcon } from "lucide-react";import type { PackageManager } from "@/hooks/use-package-manager";import { cn } from "@/lib/utils";@@ -551,6 +551,9 @@ export const getIconForCommandTab = (tab: string) => {case "shadcn": {return <ShadcnIcon />;}+ case "prompt": {+ return <TextIcon className="size-4" />;+ }default: {return <TerminalSquareIcon />;}diff --git a/apps/web/hooks/use-package-manager.ts b/apps/web/hooks/use-package-manager.tsindex 8343f59..1467472 100644--- a/apps/web/hooks/use-package-manager.ts+++ b/apps/web/hooks/use-package-manager.ts@@ -3,15 +3,9 @@ import { atomWithStorage } from "jotai/utils";export type PackageManager = "npm" | "yarn" | "pnpm" | "bun";-export type CommandTab = PackageManager | "shadcn" | "prompt";-const packageManagerAtom = atomWithStorage<PackageManager>("package-manager","pnpm");-const commandTabAtom = atomWithStorage<CommandTab>("command-tab", "pnpm");-export const usePackageManager = () => useAtom(packageManagerAtom);--export const useCommandTab = () => useAtom(commandTabAtom);