ui: [feat]: Fix sidebar error in astro
Feature description
TLDR;
Astro will not render if you do not make the following change:
Pls use import { type VariantProps, cva } from "class-variance-authority"; istead of `import { VariantProps, cva } from “class-variance-authority”;
Affected component/components
Sidebar
Additional Context
Error detail:
hook.js:608 [astro-island] Error hydrating /src/dashboard/main.tsx SyntaxError: The requested module ‘/node_modules/.vite/deps/class-variance-authority.js?v=0c4d54d3’ does not provide an export named ‘VariantProps’ (at sidebar.tsx:3:10)
Before submitting
- I’ve made research efforts and searched the documentation
- I’ve searched for existing issues and PRs
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 13
- Comments: 17
Commits related to this issue
- Fix type import for VariantProps in sidebar component See: https://github.com/shadcn-ui/ui/issues/5669#issuecomment-2539873474 — committed to esminc/agile-studio-talent-managment by fossamagna 2 years ago
- shadcn/uiを導入した (#3) * initialize shadcn/ui * Add sidebar with `npx shadcn/canary add sidebar` * Fix type import for VariantProps in sidebar component See: https://github.com/shadcn-ui/ui/issues/56... — committed to esminc/agile-studio-talent-managment by fossamagna 2 years ago
This worked for me after initializing a new shadcn Astro project and running
npx shadcn@latest add sidebar-04.I had to edit
@/components/ui/sidebar.tsxand addtypebefore theVariantPropsimport to fix it.It’s weird because the other shadcn components use
type VariantPropswhile theui/sidebar.tsxdidn’t.To use sidebar in Astro
npx shadcn add sidebar-01typein front ofVariantPropsand other types if got the error (temp fix for now)import Page from "@/app/dashboard/page";then use<Page />You can accept a children prop by customizing the
@/app/dashboard/pageand also adding the slot as a child of the react component in your astro layout or page.The example below assumes you are using it in a layout.
You can then use it in your page, example, example:
src/pages/index.astroHad the same issue on a new React Router v7 project, so it’s not just with Astro. This solved it for me as well.
Thanks @Guo-astro ! I’ve just started learning astro and was literally breaking my head over it.
Thank you again, @plop-dev! I was able to get the sidebar as close to the way I want it with your examples. Hopefully down the line this gets a little more improved so it’s not so much of a hassle to get this to work properly with Astro’s ClientRouter/transitions. Take care!
Yep I did find a solution but not really what I wanted. Since Astro cannot handle framework contexts you need to use the sidebar as a standalone component. Basically:
instead of:
However:
SidebarInsetto your<slot />wrapper to make it look like the sidebar.SidebarTriggerisn’t inSidebarProvideranymore, the button won’t do anything. To get around this I just made the sidebar collapse button within the sidebar, so it’s in the providerHere’s my
Layout.astroif my explanations a bit confusing:Hope this helps anyone else who comes across this 😃