<script>
	const button = document.querySelector('button[popovertarget=demo-mobile-nav-vt]');
	const popover = document.querySelector('#demo-mobile-nav-vt');

	button.addEventListener('click', toggle);

	function toggle(e) {
		const is_opening = !popover.matches(':popover-open');
		e.preventDefault();
		document.startViewTransition(() => {
			if (is_opening) {
				return popover.showPopover();
			}
			return popover.hidePopover();
		});
	}

	document.addEventListener('keydown', (e) => {
		if (e.key === 'Escape' && popover.hasAttribute('popover') && popover.matches(':popover-open')) {
			toggle(e);
		}
	});
</script>

<button popovertarget="demo-mobile-nav-vt">NAV</button>

<nav popover id="demo-mobile-nav-vt">
	<button class="demo-button" popovertarget="demo-mobile-nav-vt" popovertargetaction="hide">
		Close Nav
	</button>
	<ul>
		<li><a href="#">Home</a></li>
		<li><a href="#">About</a></li>
		<li><a href="#">Store</a></li>
		<li><a href="#">Contact</a></li>
	</ul>
</nav>

<style>
	#demo-mobile-nav-vt[popover] {
		margin: 0;
		block-size: 100vb;
		inline-size: 90vi;
		inset-inline-start: unset;
		inset-inline-end: 0;
		view-transition-name: slide;
	}

	@keyframes slide {
		from {
			translate: 100vi;
		}
	}

	::view-transition-old(slide) {
		animation: 300ms ease-in reverse forwards slide;
	}

	::view-transition-new(slide) {
		animation: 300ms ease-out forwards slide;
	}
</style>