-
Notifications
You must be signed in to change notification settings - Fork 8
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
Problem displaying custom AlertDialog (shadcn) in Next 14 / app router #20
Comments
I also hit this when attempting to fork the library for Next13. |
I've implemented it like this in my project, and it works. It might help you
|
I was also facing this and then realized that Based on the MDN beforeunload event docs, there's no way around this limitation. However, you can still ensure that all links on your page are using client-side routing, so that your custom dialog appears when clicking those. |
@jackstenglein that is exactly what I found out as well. Ended up having to build a wrapper component for both internal and external links and firing our own modal dialog instead. I don't think the approach used in this library is valid anymore unfortunately. |
As the title says, I'm using Next 14 w/ the app router, and want to protect a specific page from having unsaved changes accidentally discarded when the user navigates away.
I am wrapping all child elements in my
layout.tsx
file with theNavigationGuardProvider
element.I have a page/component where the user can modify data. Let's call it
EditView.tsx
.EditView.tsx
includes the component whose source is below (asSaveAlertDialog.tsx
), added as:dataChanged
is auseState<boolean>
in the EditView page component whose value is initially false; when the user changes data, I have a callback that setsdataChanged
to true usingsetDataChanged(true)
. This is working, because I have on-screen elements that toggle on/off (like a save button) whendataChanged
istrue
.As you can see, my
SaveAlertDialog
component fully encapsulates the logic of using theNavigationGuardContext
via the hookuseNavigationGuard
, and takes a boolean to indicate whether the data on the page has changed. The intent is to open theAlertDialog
when the navGuard decides a user is navigating away and the data is in a changed state.I've verified that I'm getting updates to the
changed
variable. But it doesn't appear thatnavGuard.active
ever toggles totrue
.I've looked over the example app provided with this module, and I can't see anything obvious that I'm doing differently.
The current symptom is that a built-in browser confirmation dialog displays whenever the user tries to navigate away and the data has changed. I want it to display the custom AlertDialog component instead.
I've tried instantiating
useNavigationGuard
both with and without theconfirm
callback prop.I'd appreciate any advice on getting this working.
Thanks in advance!
The text was updated successfully, but these errors were encountered: