Setting up Shadcn UI
When using Shadcn UI within an Astro project, there is a common mistake that prevents Shadcn UI from working properly. With Astro’s islands architecture, React context is not shared between islands. That means that calling Shacdn UI components directly in a .astro
file will fail because islands are not able to communicate together. This can be easily solved by moving those components calls to a .jsx/tsx
file and importing this one instead.
Examples
If you want to see a more interactive example that might help explain or let you test things out better you can:
Working with Interactive Components
-
Follow the getting started guide from the Shadcn UI documentation to set up the library in your project.
-
Add the Drawer component to your project:
-
Create a
Drawer
Component in your project: -
Import the
MyDrawer
component in thesrc/pages/index.astro
file:
To learn more about Shadcn UI: