6.9k

Astro

Previous Next

How to setup shadcn-svelte in an Astro project.

Create project

Start by creating a new Astro project:

pnpm create astro@latest

Configure your Astro project

You will be asked a few questions to configure your project:

- Where should we create your new project?
./your-app-name
- How would you like to start your new project?
Choose a starter template (or Empty)
- Install dependencies?
Yes
- Initialize a new git repository? (optional)
Yes/No

Add Svelte to your project

Install Svelte using the Astro CLI:

pnpm dlx astro add svelte

Add TailwindCSS to your project

Add Tailwind CSS using the Astro CLI:

pnpm dlx astro add tailwind

Import the global CSS file

Import the global.css file in the src/pages/index.astro file:

src/pages/index.astro
---
import "../styles/global.css";
---

Setup path aliases

Add the following code to the tsconfig.json file to resolve paths:

tsconfig.json
{
  "compilerOptions": {
    // ...
    "baseUrl": ".",
    "paths": {
      "$lib": ["./src/lib"],
      "$lib/*": ["./src/lib/*"]
    }
    // ...
  }
}

Run the CLI

Run the shadcn-svelte init command to setup your project:

pnpm dlx shadcn-svelte@latest init

Configure components.json

You will be asked a few questions to configure components.json:

Which base color would you like to use? › Slate
Where is your global CSS file? (this file will be overwritten) › src/styles/global.css
Configure the import alias for lib: › $lib
Configure the import alias for components: › $lib/components
Configure the import alias for utils: › $lib/utils
Configure the import alias for hooks: › $lib/hooks
Configure the import alias for ui: › $lib/components/ui

That's it

You can now start adding components to your project.

pnpm dlx shadcn-svelte@latest add button

The command above will add the Button component to your project. You can then import it like this:

index.astro
---
import { Button } from "$lib/components/ui/button/index.js";
---
 
<html lang="en">
	<head>
		<title>Astro</title>
	</head>
	<body>
		<Button>Hello World</Button>
	</body>
</html>