Shivamyadav's avatar

How to close the sidebar for mobile with the inertia ja link?

I have a sidebar containing links for the pages. Build the my portfolio with Laravel vue and inertia, while using inertia link the request works as a ajax for the spa.

Navigation works without page reloading but how could I close the sidebar on the mobile after a successful navigation on the mobile screens ?

0 likes
5 replies
Tray2's avatar

just add hidden to the sidebars classList.

Shivamyadav's avatar

that the programatic way of doing but for each link on click i have to add it.

Tray2's avatar

not really, you only have to define it once.

<nav>
	<a href="#" >Link 1</a>
	<a href="#" >Link 2</a>
</nav>

<script>
	let navs = document.querySelectorAll('nav a');

    navs.forEach((nav) => {
		nav.addEventListener('click', (event) => {
		    event.target.parentNode.classList.add('hidden');
	});
   });
</script>
JussiMannisto's avatar

Direct DOM manipulation like that doesn't work in Vue, since it uses a virtual DOM. Those changes may get overwritten on the next render.

Hiding the sidebar is probably really easy in a Vue layout, but @shivamyadav didn't say how the sidebar is implemented. Typically it's just a matter of setting some state variable like showSidebar to false.

Please or to participate in this conversation.