ui: [bug]: sidebar type error with some refs

Describe the bug

Getting build error on multiple refs in sidebar. Tried adjusting my linting rules, my tsconfig, and even having AI try to fix the error. No dice. Please help

Affected component/components

sidebar (SidebarGroupAction, SidebarMenuButton, etc)

How to reproduce

  1. install sidebar using npm npx shadcn@latest add sidebar

  2. build npm run build

Codesandbox/StackBlitz link

No response

Logs

From Command Line
----

Linting and checking validity of types  ...Failed to compile.

./components/ui/sidebar.tsx:440:7
Type error: Type 'string | ((instance: HTMLDivElement | null) => void) | RefObject<HTMLDivElement> | null' is not assignable to type '((string | ((instance: HTMLDivElement | null) => void) | RefObject<HTMLDivElement>) & (((instance: HTMLElement | null) => void) | RefObject<...>)) | null | undefined'.
  Type 'string' is not assignable to type '((string | ((instance: HTMLDivElement | null) => void) | RefObject<HTMLDivElement>) & (((instance: HTMLElement | null) => void) | RefObject<...>)) | null | undefined'.

  438 |   return (
  439 |     <Comp
> 440 |       ref={ref}
      |       ^
  441 |       data-sidebar="group-label"
  442 |       className={cn(
  443 |         "duration-200 flex h-8 shrink-0 items-center rounded-md px-2 text-xs font-medium text-sidebar-foreground/70 outline-none ring-sidebar-ring transition-[margin,opa] ease-linear focus-visible:ring-2 [&>svg]:size-4 [&>svg]:shrink-0",

System Info

"next": "^14.2.7",

Before submitting

  • I’ve made research efforts and searched the documentation
  • I’ve searched for existing issues

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 9
  • Comments: 16

Most upvoted comments

Updating @types/react to version 18.3.12 fixed the issue (prod still running on NextJS 14)

Doing this works for me and you can keep the Slots:

type SidebarGroupActionProps = Omit<React.ComponentProps<'button'>, 'ref'> & {
  asChild?: boolean;
};

const SidebarGroupAction = React.forwardRef<
  HTMLButtonElement,
  SidebarGroupActionProps
>(({ className, asChild = false, ...props }, ref) => {
  const Comp = asChild ? Slot : 'button';

  return (
    <Comp
      ref={ref}
      data-sidebar="group-action"
      className={cn(
        'absolute right-3 top-3.5 flex aspect-square w-5 items-center justify-center rounded-md p-0 text-sidebar-foreground outline-none ring-sidebar-ring transition-transform hover:bg-sidebar-accent hover:text-sidebar-accent-foreground focus-visible:ring-2 [&>svg]:size-4 [&>svg]:shrink-0',
        // Increases the hit area of the button on mobile.
        'after:absolute after:-inset-2 after:md:hidden',
        'group-data-[collapsible=icon]:hidden',
        className,
      )}
      {...props}
    />
  );
});
SidebarGroupAction.displayName = 'SidebarGroupAction';

I got the same error at “next”: “^14.2.15”, “@types/react”: “18.2.46”,

I modified it to React.ComponentPropsWithRef instead of React.ComponentProps so that ref is always recognised.

const SidebarGroupAction = React.forwardRef<
  HTMLButtonElement,
  React.ComponentPropsWithRef<"button"> & { asChild?: boolean }
>(({ className, asChild = false, ...props }, ref) => {
  const Comp = asChild ? Slot : "button";

  return (
    <Comp
      ref={ref}
      data-sidebar="group-action"
      className={cn(
        "absolute right-3 top-3.5 flex aspect-square w-5 items-center justify-center rounded-md p-0 text-sidebar-foreground outline-none ring-sidebar-ring transition-transform hover:bg-sidebar-accent hover:text-sidebar-accent-foreground focus-visible:ring-2 [&>svg]:size-4 [&>svg]:shrink-0",
        // Increases the hit area of the button on mobile.
        "after:absolute after:-inset-2 after:md:hidden",
        "group-data-[collapsible=icon]:hidden",
        className
      )}
      {...props}
    />
  );
});
SidebarGroupAction.displayName = "SidebarGroupAction";

The components I modified are SidebarGroupLabel, SidebarGroupAction, SidebarMenuButton, SidebarMenuAction, and SidebarMenuSubButton.

I was still using @types/react@18.2, simply upgrading the types package to its latest version fixed the issue for me.

npm update @types/react
yarn up -R @types/react

I’m able to get rid of the error by removing the Slots

//const Comp = asChild ? Slot : "a"
    const Comp = 'a';

I tried this at first

//const Comp = asChild ? Slot : "a"
    const Comp = 'a';

But that just broke the component. Then tried upgrading and that didn’t work.

Finally I was able to do it by removing React.ComponentProps<"button"> & { and replacing it with Omit<React.ComponentProps<'button'>, 'ref'> & {

As seen from the commented lines below.

const SidebarMenuButton = React.forwardRef<
  HTMLButtonElement,
  //React.ComponentProps<"button"> & { //Removed this
  Omit<React.ComponentProps<'button'>, 'ref'> & { //Added this
    asChild?: boolean
    isActive?: boolean
    tooltip?: string | React.ComponentProps<typeof TooltipContent>
  } & VariantProps<typeof sidebarMenuButtonVariants>
>(
  (
    {
      asChild = false,
      isActive = false,
      variant = "default",
      size = "default",
      tooltip,
      className,
      ...props
    },
    ref
  ) => {
    const Comp = asChild ? Slot : "button"

    const { isMobile, state } = useSidebar()

    const button = (
      <Comp
        ref={ref}
        data-sidebar="menu-button"
        data-size={size}
        data-active={isActive}
        className={cn(sidebarMenuButtonVariants({ variant, size }), className)}
        {...props}
      />
    )

I did this to all the code with issues in sidebar.tsx

same error

Changing the React.ComponentProps lines to React.ComponentPropsWithRef did solve everything for me after updating @types/react.

I modified it to React.ComponentPropsWithRef instead of React.ComponentProps so that ref is always recognised.

Hello, I also have a issue with the ref in the components (SidebarGroupLabel, SidebarGroupAction, SidebarMenuButton, SidebarMenuAction, SidebarMenuSubButton).

“react”: “^18.2.0”, “next”: “^14.2.15”,

@doriancollier which react version?

Same error. My version:

Next.js 14.2.8
@types/react 18.2.13

upgrade types/react version to 18.3.12 fixed my issue