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
feat: function to trigger open/close dialog #386
Comments
If you want control the opening state by yourself, the Example: const [open, setOpen] = useState(false);
return <Dialog open={open} onOpenChange={setOpen} /> Reference: |
I am using Next13.4 app router in which all the components and pages are RSC and SSR respectively by default. I am rendering the Dialog in the custom component lets say |
@OsamaQureshi147 It seems like you need JS on client side to me tho, this is the time where you have to use client component instead. |
@OsamaQureshi147 For a basic requirement, you can probably use the
export default function Page({
searchParams,
}: {
searchParams: { [key: string]: string | string[] | undefined }
}) {
const { productId, modal } = searchParams
return (
<div>
<h1>Some Page</h1>
<YourDialog open={modal === "true"} productId={productId} />
</div>
)
} Then use Other than that, client component. Demo: Screen.Recording.2023-09-29.at.4.14.55.PM.mov |
@wobsoriano The problem is you can't use this approach if you're trying to open a modal from any components that generally live in an applications root layout such as a Navbar since search params are not available in that context, but I think it's good approach for subpages. |
Right @msbeeman, I guess you can still use @OsamaQureshi147 this might be worth checking as well - Intercepting Routes |
How can I make a required fields in the modal? |
thanks, not so clear in the doc! |
Thank you very much, after searching and trying for couples of hours, I finally could reach it! |
Might help some - adapting shadcn/ui dialog for parallel and intercepting routes |
I'm actually trying a different approach (https://github.com/chungweileong94/nextjs-parallel-route-dialog) by using the NextJS parallel route to control the dialog open state. The thing that I focus in my code example is to preserve the dialog closing animation. |
This is how we can do, hope it helps "use client";
// Imports here
export function AddProductModal() {
const router = useRouter();
const [open, setOpen] = useState(false);
return (
<Dialog open={open} onOpenChange={setOpen}>
<DialogTrigger asChild>
<Button
className="bg-red-50 text-red-600"
>
Add Product
</Button>
</DialogTrigger>
<DialogContent className="sm:max-w-[425px]">
// ADD PRODUCT FORM HERE
<DialogFooter>
<Button
onClick={() => {
addProduct(data).then(() => setOpen(false));
router.refresh();
}}
className="bg-red-50 text-red-600 hover:bg-red-100"
type="submit"
>
Add
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
);
} |
I was doing it like you, but slightly differently. I changed my code to match your approach though and I think it's more manageable/closer to what I want. The thing is, the closing animation does work, but sometimes. I wonder if there is some sort of race condition that is causing it to sometimes not work? |
Well, it works fine to fit my use-case. But I don't expect the close animation to work in cases like hard navigation. You could try to use the browser history API with NextJS 14.1, to see if that helps, https://nextjs.org/blog/next-14-1#windowhistorypushstate-and-windowhistoryreplacestate, haven't try it myself personally. But I will say if you really want to get the animation right, move things to client-side is the way to go. |
I'll give this a go, I'll report back! I've already been messing around with the router.
Either way, my code and then the changes based on your reorganisation of the folder structure based on your example has been really useful. So has Ariakit's Dialog with App Router documentation. I'll let you know if I get further with the exit animations. |
bruh, i suck |
this worked for me :) |
const [open, setOpen] = React.useState(false)
const { reset } = form;
const { isSubmitting, isSubmitSuccessful } = form.formState;
React.useEffect(() => {
isSubmitSuccessful && reset()
}, [isSubmitSuccessful, reset])
<Dialog open={open} onOpenChange={setOpen}> Dialog closing and form reset (react-hook-form) working properly with this. Didn't add anything like |
Very useful! |
Does this only work on submit? I have been trying it in other parts of the code but it never works... |
This worked for me !!
|
I had a slightly different requirement to be able to open a modal using the normal DialogTrigger but also from my navigation bar component which was in my root layout. I was able use this approach with some slight adjustments to get it to work. This preserves all the nice animations for opening and closing the modal while allowing the modal to be opened or closed from anywhere in the component tree. Code is here in-case anyone is curious: https://github.com/pnavk/nextjs-rsc-modal-dialog-example |
Alert Dialog support https://medium.com/@descometusah/mastering-dialog-components-in-shadcn-ui-library-9420ac736b9e shadcn-ui/ui#386 (comment) https://frontendshape.com/post/how-to-use-alert-dialog-next-13-with-shadcn-ui CRUD support shadcn-ui/ui#2017 (comment) https://medium.com/@bhairabpatra.iitd/crud-create-read-update-delete-application-in-react-566bf229aaee
Tried to search for it but didn't find a way to close the dialog with a method or function. This is useful after processing data like - making a fetch request, mutation, or something that is awaitable, or can change dialog state from outside
In the example below, let's assume when the user clicks to "save changes", I want to handle some submit function as a result close dialog from that function.
Currently, the way I see is used after exporting
Close
from radix primitive. But this is a JSX element, cant be used as function to change dialog stateThere is 2nd way to close Dialog by creating a state and binding it to
open={open}
prop in theDialog
component. We can open or close Dialog by changing that state. But when do so, the "X" button and clicking outside of the dialog to close it stops workingWhy we need that feature?
I think this will solve the issue we have in Note section which asserts that we have to encase the trigger button to Dialog itself. I take this as a big problem because if I have a Dialog component, it's not possible to trigger it from multiple places. If I need to have that dialog in multiple places I need to copy/paste the same dialog code which is a duplication issue.
Sample use case: I have "Newsletter dialog" and I need to trigger this dialog with the button in the navbar, footer, or inside the main content. Or if the user stays for 5 mins on the website, I want to trigger that newsletter dialog
The text was updated successfully, but these errors were encountered: