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
-
install sidebar using npm
npx shadcn@latest add sidebar -
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
Updating
@types/reactto version 18.3.12 fixed the issue (prod still running on NextJS 14)Doing this works for me and you can keep the Slots:
I got the same error at “next”: “^14.2.15”, “@types/react”: “18.2.46”,
I modified it to
React.ComponentPropsWithRefinstead ofReact.ComponentPropsso thatrefis always recognised.The components I modified are
SidebarGroupLabel,SidebarGroupAction,SidebarMenuButton,SidebarMenuAction, andSidebarMenuSubButton.I was still using
@types/react@18.2, simply upgrading the types package to its latest version fixed the issue for me.I’m able to get rid of the error by removing the Slots
I tried this at first
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 withOmit<React.ComponentProps<'button'>, 'ref'> & {As seen from the commented lines below.
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.
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”,
Same error. My version:
upgrade types/react version to 18.3.12 fixed my issue