Below is a step-by-step guide on how to use the shadcn CLI to add a Button component to your project:
1. Ensure You Have the shadcn CLI Installed
If you haven't already installed the shadcn CLI globally, you can use npx to run it directly without installing globally. Most guides assume you run it via npx or your preferred package manager (e.g., pnpm or yarn). Make sure your project is already set up with shadcn's configuration (e.g., using Tailwind CSS, etc.) as per the shadcn-ui docs.
2. Navigate to Your Project Directory
Open your terminal and change to your project’s root directory:
bash
cd /path/to/your/project
3. Run the Command to Add a Button
The shadcn CLI uses a simple command structure to add components. To add a Button component, run:
bash
npx shadcn-ui add button
Note:
If you’re using pnpm or yarn, and have set up the CLI accordingly, you can also run pnpm shadcn-ui add button or yarn shadcn-ui add button.
The CLI will generate the button code in your components folder, typically under a path such as src/components/ui/button.tsx, depending on your project configuration.
4. Follow Any Interactive Prompts
Sometimes the CLI might prompt you with questions (for example, to confirm overwriting an existing file or selecting certain variants). Follow these prompts to complete the setup.
5. Review and Use Your New Button Component
Once the command successfully completes:
You should see the Button component files created in your project.
Open the file (e.g., src/components/ui/button.tsx) to review the generated code.
Now you can import and use the button in your project as needed:
tsx
import { Button } from "@/components/ui/button";
function MyComponent() {
return <Button>Click Me</Button>;
}
export default MyComponent;
Additional Tips
Customization: The generated component typically includes basic styling using Tailwind CSS. You can customize the styles, variants, or functionality as needed.
Further Reading: Consult the shadcn-ui documentation for more detailed instructions or for adding other components.
By following these steps, you’ll be able to add and start working with the Button component provided by shadcn UI in your project. Happy coding!