ui: `ScrollArea` doesn't work inside `Combobox`
Problem
Can’t scroll the contents inside the CommandGroup of the Combobox component.
However, the scroll only works in the tiny scrollbar.
Expected
Can scroll if cursor on top of the items
Reproducable
Codesandbox: link
About this issue
- Original URL
- State: open
- Created a year ago
- Reactions: 7
- Comments: 28
Commits related to this issue
- fix: mouse wheel scroll over popover #607 #542 — committed to pa4080/shadcn-ui by pa4080 6 months ago
- fix: mouse wheel and touch scroll over popover #607 #542 — committed to pa4080/shadcn-ui by pa4080 3 months ago
Wrap the
<CommandEmpty />and the<CommandGroup />with the<ScrollArea />Something like this, it works for me:
thanks @atleugim it works fine.
I had to remove the
PopoverPrimitive.Portalfrom the Popover component, too ( replaced it with<>- source: https://github.com/shadcn-ui/ui/issues/607#issuecomment-1610187963 )https://github.com/shadcn-ui/ui/issues/542#issuecomment-1587142689
Setting
modal={true}as suggested by ^ helped fix for me w/o the need of<ScrollArea />I could fix the error adding
className="h-48 overflow-auto"to the className of the ScrollAreaNow I can scroll the items of the
CommandGroupI mean just not render the
PopoverPrimitive.PortalcomponentThe same issue persists if there’s no Portal component. For example, I’m not able to scroll inside the combobox here:
It’s an issue with Radix https://github.com/radix-ui/primitives/issues/1159. In the meantime, you can remove the portal part from Popover
For anyone who just wants to make the combobox scrollable, you can also use
CommandList. Example: https://github.com/shadcn-ui/ui/blob/main/apps/www/components/command-menu.tsx#L77I wanted a solution that allows the
ComboBoxto use a portal unless it’s inside aDialog. To avoid passing props through consumers, I added aDialogContextthat communicates down-tree that aDialogis present. This allows thePopoverto render the portal or not accordingly.I assume there’s a downside to this approach I can’t see, but it seems less destructive than globally removing the popover portal just for the
ComboBoxin some less common scenarios. Thanks to @noxify and @atleugim for the pointers.My combo box code is the same as the reference except I added classes for max height and overflow. I am not using a
ScrollArea.For me 2 solutions worked:
Remove PopoverPrimitive.Portal - Simpler but may affect operation
Use the onWheel configuration in PopoverPrimitive.Content mentioned above by @pa4080 in conjunction with the ScrollArea around CommandEmpty and CommandGroup - More complex but does not need to remove PopoverPrimitive.Portal
Here is a nice workaround, which simulates arrow-up and arrow-down when you using the muse wheel over the the
<PopoverContent />component. Replace the relevant lines in yourui/popover.tsxfile with these:For me, setting max height for Command component and ScrollArea’s overflow to auto worked:
There are 2 solutions outlined and they both work but the styling is not what I expect.
Like @krisantuswanandi mentioned, you can use
CommandListfrom cmdk to create a scrollbar inComboBox. ComboBox uses the Command component and uses cmdk.Use
ScrollAreato create a scrollbar inComboBoxand pass inoverflow-y-autolike everyone else that has mentioned.This is the scrollbar styling I expect from shadcn
ScrollAreahere.However, both solutions creates a Scrollbar that gives me this and the styling can be found from cmdk here.
Is there a fix for this issue? / Can we have a version of the component without using cmdk?
This works great! Hopefully this gets fixed soon!
If it’s helpful for anyone, I’m using it inside a Popover similar to the example given in the docs, the installed component using npx for popover, the PopoverContent component is returning it wrapped in
<PopoverPrimitive.Portal>I just removed the portal and instead wrapped it with<></>and now scrolling works. I haven’t yet figured out if it breaks anything else, I’ll update here if I find an issue with my solution.