-
Notifications
You must be signed in to change notification settings - Fork 3.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Shadcn popover not working in Modal Dialog #1511
Comments
similar issue on the below codesandbox but it was in bootstrap |
I think it happened because the Dialog and Popover components are using the body element as a portal. try changing the portal element to the Dialog itself solves the issue. |
yeah thanks, this approach worked for me |
Hi there, how can I do this? |
@coded58 @Josevictor2 Would you mind sharing how this is done? The closest I've gotten is creating a ref on |
Do you mean remove the portal component from Popover? |
It's working for me: |
Go inside your Shadcn UI |
Set the modal prop to true on the Popover root. |
That worked for me too. But is there any side effect that might occur because of it? After removing the portal, I anticipated the popover to not overflow correctly (I thought it will start clipping), but its working fine. |
I've had the same concern when I found that removing |
this solution is working for me |
Setting modal={false} will allow users to interact with elements outside of the dialog while it is open which I don't think is usually desirable |
best solution, worked correctly as expected. |
Worked For Me! Good people, I'm glad you're here. |
When using a modal Popover, closing the dialog retains focus on the Popover, blocking interaction with other elements. This happens when clicking outside to close. How can I ensure the Popover fully closes and releases focus when the dialog closes, especially from clicking outside? Looking for guidance on the best approach to resolve this issue and allow users to interact with the page freely again after closing the modal Popover. |
This worked for me with or without removing the PopoverPrimitive.Portal inside the popover file. I'm using Datepicker inside a modal/dialog. Hope it helps you. |
adding modal prop as true to Popover fixed the issue for me, thanks.
|
this is the way |
In my case the modal prop worked to enable a popover in a dialog, but caused inconsistencies with additional components within the popover. The alternative solution was to use the Portal container prop, pass a ref of a valid HTMLElement to the portal. The result was the same behavior as the Here's the core parts of the implementation. //... Component Prep
const formRef = React.useRef<HTMLFormElement>(null);
//... Within my component
<form ref={formRef}>
<PopoverContent className="w-[200px] p-0" asChild containerRef={formRef}>
</PopoverContent>
</form>
//.. popover.tsx
const PopoverContent = React.forwardRef<
React.ElementRef<typeof PopoverPrimitive.Content>,
React.ComponentPropsWithoutRef<typeof PopoverPrimitive.Content> & {
containerRef?: React.RefObject<HTMLElement>
}
>(({ className, align = "center", sideOffset = 4, containerRef, ...props }, ref) => (
<PopoverPrimitive.Portal container={containerRef?.current}> |
@jmccarthy86 I tried the approach with
Unfortunately hovering over the commands do not work and I can only use arrow keys to select anything. Do you have any insight to what I can do here? |
Ah, sorry, I forgot to mention that I also made an adjustment to the className in the CommandItem component. //command.tsx
const CommandItem = React.forwardRef<
React.ElementRef<typeof CommandPrimitive.Item>,
React.ComponentPropsWithoutRef<typeof CommandPrimitive.Item>
>(({ className, ...props }, ref) => (
<CommandPrimitive.Item
ref={ref}
className={cn(
"relative flex cursor-default select-none items-center rounded-sm px-2 py-1.5 text-sm outline-none aria-selected:bg-accent aria-selected:text-accent-foreground data-[disabled='true']:pointer-events-none data-[disabled='true']:opacity-50",
className
)}
{...props}
/>
)) See where |
Actually I changed "cmdk" package to 0.2.0, and hover works, but I still cannot scroll in command menu |
Difficult to comment on that as the scenario I am working with did not change the cmdk package to 0.2.0. And, your issue could be due to an unrelated issue with your implementation. I can say
These had a me a working dialog component with a popover command combobox setup without any further adjustments. To note, I did not use the model=true in this implementation. If you have a repo you can share I'm happy to take a look. |
Just a note - Instead of creating 2 files, one with the portal and one without, just keep one file with the portal removed. If you need to use portal, just go to the component where you are using the Popover and manually wrap the This helps if you update Shadcn Popover later on, you'd only need to make a change in this one file (instead of maintaining two). The change will of course be removing the portal after the update. |
If this is related to the method I highlighted. Whilst this is also a good option. The solution I shared is in essence the same it just added the option to change the portal the popover uses. There's no creation or two files or removing of any portals. |
My bad, I thought it spoke in essence of keeping the 2 versions separate. Nonetheless, the method I shared could be helpful to some people who have created 2 versions :) |
@Fanoflix 100% sir, both valid methods |
@jmccarthy86 thanks so much, the command bar changes were what did it for me! |
This works thanks |
work like a charm |
How can i use shadcn popover in modal correctly? i copied the date-picker component and tried using it in a modal-form but i closes without user interaction
https://github.com/shadcn-ui/ui/assets/99658156/8a08a6de-cca0-4e26-98df-973559637934
The text was updated successfully, but these errors were encountered: