This episode is for members only

Sign up to access "Build a Starter Kit With Inertia and Fortify" right now.

Get started
Already a member? Sign in to continue
Playing
40. Closing the user navigation on click

Episodes

0%
Your progress
  • Total: 5h 21m
  • Played: 0m
  • Remaining: 5h 21m
Join or sign in to track your progress

Transcript

00:00
So I promised we'd come back to this issue with our user navigation, where when we click on something like account or security, sure enough,
00:08
we get forwarded over to the right page. But this doesn't close the menu. Now, let's just take a look at what we were doing before here. Before, we had a V on mouse up, which
00:18
did work to actually invoke that close function within our menu item in headless UI. But this prevented the behavior of the link with going through to a new inertia page
00:29
and re-rendering everything. So let's just remind ourselves that that did work to close the menu, but it didn't let us progress any further.
00:37
Now, with inertia, links are pretty much just router requests. They work in exactly the same way. And we've looked at tons of router requests
00:46
where we've posted through or get something, and then we've used an on success callback. So let's just remind ourselves one of the places that we've done that.
00:55
And that was, for example, in our two factor where we enabled this when we, let's just have a look here, went ahead and posted through without form, which is the same as a router request.
01:07
On success, we did something. Now, because a link's working exactly the same way, we can actually do the same thing. So we can bind in an on success, so
01:17
when this request to this new page was successful, we can do something. So this is the correct way to hook into an event when a link successfully goes through.
01:27
So we bind in on success, and we just invoke close. So that should work nicely. Let's head over and just try this out. So I'm going to go ahead and hit account.
01:36
Sure enough, we go through to the right place. And because that router request was successful, it invokes close, and it closes the menu. So really, what we need to do now
01:46
is just take this and do it for the security link here as well, do exactly the same thing. And that should nicely close this off too. For log out, slightly different,
01:57
because this redirects us anyway, but that should be good. Let's just make sure we're not missing anything else. I think these should work nicely.
02:04
And there we go. We have successfully fixed up the issue with that navigation not closing when we go through to new pages.

Episode summary

In this episode, we tackle a little annoyance with our user navigation menu. Right now, when you click on a link (like "Account" or "Security"), the app takes you to the right page, but the dropdown menu stubbornly stays open. Previously, we tried closing it on mouse up, but that stopped the link from navigating properly—definitely not ideal.

So in this video, we walk through why the menu isn't closing and how Inertia.js links handle navigation as router requests. We remember how, with forms, we can hook into the onSuccess callback after a router request, and realize we can use exactly the same approach here!

By adding an onSuccess handler to the navigation links, we can tell the menu to close only after the new page has successfully loaded. We implement this change, try things out, and confirm it works as expected for both the "Account" and "Security" links. There's a quick note that logging out is handled a bit differently, but for these links, the fix is clean and reliable.

So by the end of this episode, our navigation menu behaves as users would expect—it closes itself as soon as you navigate somewhere new.

Episode discussion

No comments, yet. Be the first!