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

Most upvoted comments

  1. Add type in front of VariantProps and other types if got the error (temp fix for now)

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.tsx and add type before the VariantProps import to fix it.

import * as React from "react";
import { Slot } from "@radix-ui/react-slot";
+ import { type VariantProps, cva } from "class-variance-authority";
import { PanelLeft } from "lucide-react";

It’s weird because the other shadcn components use type VariantProps while the ui/sidebar.tsx didn’t.

I’m getting the error “useSidebar must be used within a SidebarProvider” when using Astro and a sidebar component. Unfortunately, I’m stuck. Any ideas?

To use sidebar in Astro

  1. Add sidebar you wish by running a command from https://ui.shadcn.com/blocks, for example npx shadcn add sidebar-01
  2. Add type in front of VariantProps and other types if got the error (temp fix for now)
  3. You should use the “Page” component from “/src/app/dashboard/page.tsx” import Page from "@/app/dashboard/page"; then use <Page />

I’m getting the error “useSidebar must be used within a SidebarProvider” when using Astro and a sidebar component. Unfortunately, I’m stuck. Any ideas?

To use sidebar in Astro

  1. Add sidebar you wish by running a command from https://ui.shadcn.com/blocks, for example npx shadcn add sidebar-01
  2. Add type in front of VariantProps and other types if got the error (temp fix for now)
  3. You should use the “Page” component from “/src/app/dashboard/page.tsx” import Page from "@/app/dashboard/page"; then use <Page />

if i import <Page /> how do i pass the content of <slot /> ? thanks for the help

You can accept a children prop by customizing the @/app/dashboard/page and 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.

//src/layouts/Layout.astro
---
// if you want to use the sidebar cookie
const defaultSidebarOpen = Astro.cookies.get('sidebar:state')?.value === 'true';

import Page from "@/app/dashboard/page";

---

<html>
  <head> 
    ...
  </head>

<body>

  <Page client:load defaultSidebarOpen={defaultSidebarOpen}>
      <slot />   
  </Page>

</body>

</html>


// @/app/dashboard/page.tsx
interface Props {
 children: React.ReactNode
 defaultSidebarOpen: boolean // remove this if not using cookies for sidebar state
}

export default function Page(props: Props) {
  return (
     <SidebarProvider defaultSidebarOpen={props.defaultSidebarOpen}>
      <AppSidebar />
      <SidebarInset>
        <header className="flex h-16 shrink-0 items-center gap-2 border-b px-4">
          <SidebarTrigger className="-ml-1" />
          <Separator orientation="vertical" className="mr-2 h-4" />
          <Breadcrumb>
            <BreadcrumbList>
              <BreadcrumbItem className="hidden md:block">
                <BreadcrumbLink href="#">
                  Building Your Application
                </BreadcrumbLink>
              </BreadcrumbItem>
              <BreadcrumbSeparator className="hidden md:block" />
              <BreadcrumbItem>
                <BreadcrumbPage>Data Fetching</BreadcrumbPage>
              </BreadcrumbItem>
            </BreadcrumbList>
          </Breadcrumb>
        </header>
        <div className="flex flex-1 flex-col gap-4 p-4">
          {props.children}
        </div>
      </SidebarInset>
    </SidebarProvider>
  );
}

You can then use it in your page, example, example: src/pages/index.astro

---
import Layout from '@/layouts/Layout.astro'
---

<Layout>
  <h1 class="text-3xl">My Page</h1>
  <p class="text-muted-foreground">I am a subtitle of this page</p>
</Layout>

  1. Add type in front of VariantProps and other types if got the error (temp fix for now)

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.tsx and add type before the VariantProps import to fix it.

import * as React from "react";
import { Slot } from "@radix-ui/react-slot";
+ import { type VariantProps, cva } from "class-variance-authority";
import { PanelLeft } from "lucide-react";

It’s weird because the other shadcn components use type VariantProps while the ui/sidebar.tsx didn’t.

Had 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!

I’m in the same boat! Were you able to figure something out? I’ve been scratching my head on this for days and I’m about ready to create a sidebar with Tailwind and make it a .astro component.

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:

---
const {title} = Astro.props
---

<SidebarProvider transition:persist> // only the sidebar is persisted 👍 
    <Sidebar />
</SidebarProvider>
<slot />

instead of:

---
const {title} = Astro.props
---

<SidebarProvider transition:persist> // transition persist would persist your whole page 👎
    <Sidebar />
    <slot />
</SidebarProvider>

However:

  • you would need to copy the styling of SidebarInset to your <slot /> wrapper to make it look like the sidebar.
  • since SidebarTrigger isn’t in SidebarProvider anymore, 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 provider

Here’s my Layout.astro if my explanations a bit confusing:

---
import '@/styles/global.css';
import Sidebar from '@/components/Sidebar';
import { ClientRouter } from 'astro:transitions';
import { Breadcrumb, BreadcrumbList, BreadcrumbItem, BreadcrumbLink, BreadcrumbSeparator, BreadcrumbPage } from '@/components/ui/breadcrumb';
import { cn } from '@/lib/utils';
import { Toaster } from '@/components/ui/toaster';

const defaultSidebarOpen = Astro.cookies.get('sidebar:state')?.value === 'true';

interface Props {
	title: string;
	className?: string;
}

const { title, className = '' } = Astro.props;
const { APPNAME: appName } = import.meta.env;
---

<!doctype html>
<html lang='en'>
	<head>
		<meta charset='UTF-8' />
		<meta name='viewport' content='width=device-width' />
		<link rel='icon' type='image/svg+xml' href='/favicon.svg' />
		<meta name='generator' content={Astro.generator} />
		<title>{title} - {appName}</title>
		<ClientRouter />
	</head>
	<body class='flex flex-row'>
		<Toaster client:only='react' />
		<Sidebar appName={appName} client:only='react' defaultSidebarOpen={defaultSidebarOpen} url={Astro.url} className={className} transition:persist />

		<main
			class='relative flex min-h-svh flex-1 flex-col bg-background peer-data-[variant=inset]:min-h-[calc(100svh-theme(spacing.4))] md:peer-data-[variant=inset]:m-2 md:peer-data-[state=collapsed]:peer-data-[variant=inset]:ml-2 md:peer-data-[variant=inset]:ml-0 md:peer-data-[variant=inset]:rounded-xl md:peer-data-[variant=inset]:shadow'>
			<header class='flex h-12 shrink-0 items-center gap-2 border-b px-4'>
				<Breadcrumb className='' transition:persist>
					<!-- Breadcrumb component... -->
				</Breadcrumb>
			</header>
			<div class='flex flex-1 flex-col gap-4 p-4'>
				<slot />
			</div>
		</main>
	</body>
</html>

Hope this helps anyone else who comes across this 😃