forked from npmx-dev/npmx.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppHeader.vue
More file actions
208 lines (187 loc) · 6.48 KB
/
AppHeader.vue
File metadata and controls
208 lines (187 loc) · 6.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<script setup lang="ts">
import { isEditableElement } from '~/utils/input'
withDefaults(
defineProps<{
showLogo?: boolean
}>(),
{
showLogo: true,
},
)
const { isConnected, npmUser } = useConnector()
const showFullSearch = shallowRef(false)
const showMobileMenu = shallowRef(false)
// On mobile, clicking logo+search button expands search
const route = useRoute()
const isMobile = useIsMobile()
const isSearchExpandedManually = shallowRef(false)
const searchBoxRef = useTemplateRef('searchBoxRef')
// On search page, always show search expanded on mobile
const isOnHomePage = computed(() => route.name === 'index')
const isOnSearchPage = computed(() => route.name === 'search')
const isSearchExpanded = computed(() => isOnSearchPage.value || isSearchExpandedManually.value)
function expandMobileSearch() {
isSearchExpandedManually.value = true
nextTick(() => {
searchBoxRef.value?.focus()
})
}
watch(
isOnSearchPage,
visible => {
if (!visible) return
searchBoxRef.value?.focus()
nextTick(() => {
searchBoxRef.value?.focus()
})
},
{ flush: 'sync' },
)
function handleSearchBlur() {
showFullSearch.value = false
// Collapse expanded search on mobile after blur (with delay for click handling)
// But don't collapse if we're on the search page
if (isMobile.value && !isOnSearchPage.value) {
setTimeout(() => {
isSearchExpandedManually.value = false
}, 150)
}
}
function handleSearchFocus() {
showFullSearch.value = true
}
onKeyStroke(
e => isKeyWithoutModifiers(e, ',') && !isEditableElement(e.target),
e => {
e.preventDefault()
navigateTo('/settings')
},
{ dedupe: true },
)
onKeyStroke(
e =>
isKeyWithoutModifiers(e, 'c') &&
!isEditableElement(e.target) &&
// Allow more specific handlers to take precedence
!e.defaultPrevented,
e => {
e.preventDefault()
navigateTo('/compare')
},
{ dedupe: true },
)
</script>
<template>
<header class="sticky top-0 z-50 bg-bg/80 backdrop-blur-md border-b border-border">
<nav
:aria-label="$t('nav.main_navigation')"
class="container min-h-14 flex items-center gap-2"
:class="isOnHomePage ? 'justify-end' : 'justify-between'"
>
<!-- Mobile: Logo + search button (expands search, doesn't navigate) -->
<button
v-if="!isSearchExpanded && !isOnHomePage"
type="button"
class="sm:hidden flex-shrink-0 inline-flex items-center gap-2 font-mono text-lg font-medium text-fg hover:text-fg transition-colors duration-200 rounded"
:aria-label="$t('nav.tap_to_search')"
@click="expandMobileSearch"
>
<AppLogo class="w-8 h-8 rounded-lg" />
<span class="i-carbon:search w-4 h-4 text-fg-subtle" aria-hidden="true" />
</button>
<!-- Desktop: Logo (navigates home) -->
<div v-if="showLogo" class="hidden sm:flex flex-shrink-0 items-center">
<NuxtLink
to="/"
:aria-label="$t('header.home')"
dir="ltr"
class="inline-flex items-center gap-1 header-logo font-mono text-lg font-medium text-fg hover:text-fg/90 transition-colors duration-200 rounded"
>
<AppLogo class="w-8 h-8 rounded-lg" />
<span>npmx</span>
</NuxtLink>
</div>
<!-- Spacer when logo is hidden on desktop -->
<span v-else class="hidden sm:block w-1" />
<!-- Center: Search bar + nav items -->
<div
class="flex-1 flex items-center justify-center md:gap-6"
:class="{ 'hidden sm:flex': !isSearchExpanded }"
>
<!-- Search bar (hidden on mobile unless expanded) -->
<HeaderSearchBox
ref="searchBoxRef"
:inputClass="isSearchExpanded ? 'w-full' : ''"
:class="{ 'max-w-md': !isSearchExpanded }"
@focus="handleSearchFocus"
@blur="handleSearchBlur"
/>
<ul
v-if="!isSearchExpanded && isConnected && npmUser"
:class="{ hidden: showFullSearch }"
class="hidden sm:flex items-center gap-4 sm:gap-6 list-none m-0 p-0"
>
<!-- Packages dropdown (when connected) -->
<li v-if="isConnected && npmUser" class="flex items-center">
<HeaderPackagesDropdown :username="npmUser" />
</li>
<!-- Orgs dropdown (when connected) -->
<li v-if="isConnected && npmUser" class="flex items-center">
<HeaderOrgsDropdown :username="npmUser" />
</li>
</ul>
</div>
<!-- End: Desktop nav items + Mobile menu button -->
<div class="flex-shrink-0 flex items-center gap-0.5 sm:gap-2">
<!-- Desktop: Compare link -->
<NuxtLink
to="/compare"
class="hidden sm:inline-flex link-subtle font-mono text-sm items-center gap-2 px-2 py-1.5 hover:bg-bg-subtle focus-visible:outline-accent/70 rounded"
aria-keyshortcuts="c"
>
{{ $t('nav.compare') }}
<kbd
class="inline-flex items-center justify-center w-5 h-5 text-xs bg-bg-muted border border-border rounded"
aria-hidden="true"
>
c
</kbd>
</NuxtLink>
<!-- Desktop: Settings link -->
<NuxtLink
to="/settings"
class="hidden sm:inline-flex link-subtle font-mono text-sm items-center gap-2 px-2 py-1.5 hover:bg-bg-subtle focus-visible:outline-accent/70 rounded"
aria-keyshortcuts=","
>
{{ $t('nav.settings') }}
<kbd
class="inline-flex items-center justify-center w-5 h-5 text-xs bg-bg-muted border border-border rounded"
aria-hidden="true"
>
,
</kbd>
</NuxtLink>
<!-- Desktop: Account menu -->
<div class="hidden sm:block">
<HeaderAccountMenu />
</div>
<!-- Mobile: Menu button (always visible, toggles menu) -->
<button
type="button"
class="sm:hidden flex items-center p-2 -m-2 text-fg-subtle hover:text-fg transition-colors duration-200 focus-visible:outline-accent/70 rounded"
:aria-label="showMobileMenu ? $t('common.close') : $t('nav.open_menu')"
:aria-expanded="showMobileMenu"
@click="showMobileMenu = !showMobileMenu"
>
<span
class="w-6 h-6 inline-block"
:class="showMobileMenu ? 'i-carbon:close' : 'i-carbon:menu'"
aria-hidden="true"
/>
</button>
</div>
</nav>
<!-- Mobile menu -->
<HeaderMobileMenu v-model:open="showMobileMenu" />
</header>
</template>