Where to declare my handle_event's for live layout for toggling mobile nav menu without putting the handle_event in every live view

G’day!

First time posting here and fairly new to phoenix. Just need some help optimizing my project with handling events coming from the layouts.ex file, I just need to toggle my mobile navigation side bar, and I can grossly achieve this by putting the handle_event in every live view, but when I try to copy the Live beats project with the nav.ex setup with an attach_hook, I can’t seem to get it to work

edit: I realised live beats achieves this using functions in the core_components directory.

Toggling your menu state seems better suited for javascript and not liveview there is no need for the server to know the state of the mobile side menu and it adds unneeded latency to your application.

Hello and welcome!

As @sanswork pointed out, menu toggling—or any UI interactions that don’t need the server—is best done with JS. Check out JS Commands.

Otherwise, it’s not a terrible exercise to store menu state on the server in terms of learning hooks. Can you post what you have tried? It’s not really possible to help if we can’t see code!

1 Like

Ahh I see!

Thanks a bunch!! :slight_smile:

1 Like