fix(mobile-menu): transition to global Alpine store for reliable state management
All checks were successful
Deploy Theme / deploy (push) Successful in 14s
All checks were successful
Deploy Theme / deploy (push) Successful in 14s
This commit is contained in:
17
default.hbs
17
default.hbs
@@ -2,7 +2,6 @@
|
||||
<html lang="{{@site.lang}}"
|
||||
x-data="{
|
||||
theme: localStorage.getItem('theme') || (window.matchMedia('(prefers-color-scheme: light)').matches ? 'light' : 'dark'),
|
||||
mobileMenuOpen: false,
|
||||
init() {
|
||||
$watch('theme', val => {
|
||||
localStorage.setItem('theme', val);
|
||||
@@ -13,7 +12,7 @@
|
||||
}
|
||||
}"
|
||||
:data-theme="theme"
|
||||
@htmx:after-on-load.window="mobileMenuOpen = false"
|
||||
@htmx:after-on-load.window="Alpine.store('mobileMenu').open = false"
|
||||
class="hidden">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
@@ -25,6 +24,20 @@
|
||||
<script defer src="https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.min.js"></script>
|
||||
<script src="https://unpkg.com/htmx.org@2.0.0"></script>
|
||||
|
||||
<script>
|
||||
document.addEventListener('alpine:init', () => {
|
||||
Alpine.store('mobileMenu', {
|
||||
open: false,
|
||||
toggle() {
|
||||
this.open = !this.open;
|
||||
},
|
||||
close() {
|
||||
this.open = false;
|
||||
}
|
||||
})
|
||||
})
|
||||
</script>
|
||||
|
||||
{{ghost_head}}
|
||||
</head>
|
||||
<body class="{{body_class}} font-sans antialiased" hx-boost="true">
|
||||
|
||||
@@ -22,7 +22,7 @@
|
||||
</button>
|
||||
</nav>
|
||||
|
||||
<button @click="mobileMenuOpen = true" class="md:hidden p-2 rounded-full bg-[var(--bg-secondary)] text-[var(--text-primary)] hover:bg-[var(--bg-tertiary)] transition-colors duration-200">
|
||||
<button @click="$store.mobileMenu.toggle()" class="md:hidden p-2 rounded-full bg-[var(--bg-secondary)] text-[var(--text-primary)] hover:bg-[var(--bg-tertiary)] transition-colors duration-200">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-6 h-6">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" d="M3.75 6.75h16.5M3.75 12h16.5m-16.5 5.25h16.5" />
|
||||
</svg>
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
<div id="mobile-menu"
|
||||
x-show="mobileMenuOpen"
|
||||
x-show="$store.mobileMenu.open"
|
||||
x-transition:enter="transition ease-out duration-300"
|
||||
x-transition:enter-start="-translate-x-full"
|
||||
x-transition:enter-end="translate-x-0"
|
||||
@@ -7,7 +7,7 @@
|
||||
x-transition:leave-start="translate-x-0"
|
||||
x-transition:leave-end="-translate-x-full"
|
||||
class="fixed inset-0 z-50 bg-[var(--bg-primary)] flex flex-col md:hidden"
|
||||
@click.away="mobileMenuOpen = false"
|
||||
@click.away="$store.mobileMenu.close()"
|
||||
x-cloak>
|
||||
|
||||
<!-- Mobile Menu Header -->
|
||||
@@ -20,7 +20,7 @@
|
||||
{{/if}}
|
||||
<span class="ml-3 text-xl font-bold">{{@site.title}}</span>
|
||||
</a>
|
||||
<button @click="mobileMenuOpen = false" class="p-2 rounded-full bg-[var(--bg-secondary)] text-[var(--text-primary)] hover:bg-[var(--bg-tertiary)] transition-colors duration-200">
|
||||
<button @click="$store.mobileMenu.close()" class="p-2 rounded-full bg-[var(--bg-secondary)] text-[var(--text-primary)] hover:bg-[var(--bg-tertiary)] transition-colors duration-200">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-6 h-6">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" d="M6 18L18 6M6 6l12 12" />
|
||||
</svg>
|
||||
@@ -28,8 +28,8 @@
|
||||
</div>
|
||||
|
||||
<!-- Mobile Menu Navigation -->
|
||||
<nav class="flex-grow flex flex-col items-center justify-center p-10 overflow-y-auto" @click="mobileMenuOpen = false">
|
||||
<div class="w-full">
|
||||
<nav class="flex-grow flex flex-col items-center justify-center p-10 overflow-y-auto" @click="$store.mobileMenu.close()">
|
||||
<div class="w-full text-center">
|
||||
{{navigation type="primary"}}
|
||||
</div>
|
||||
</nav>
|
||||
|
||||
Reference in New Issue
Block a user