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
215 lines (190 loc) · 6.04 KB
/
AppHeader.vue
File metadata and controls
215 lines (190 loc) · 6.04 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
209
210
211
212
213
214
215
<script setup lang="ts">
import { LinkBase } from '#components'
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')
const { searchQuery, updateSearchQuery } = usePackageSearchQuery()
const router = useRouter()
function handleSubmitSearch(queryValue: string) {
if (queryValue === '') {
return
}
router.replace({
name: 'search',
query: { ...route.query, q: queryValue },
})
}
// 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({ name: 'settings' })
},
{ dedupe: true },
)
onKeyStroke(
e =>
isKeyWithoutModifiers(e, 'c') &&
!isEditableElement(e.target) &&
// Allow more specific handlers to take precedence
!e.defaultPrevented,
e => {
e.preventDefault()
navigateTo({ name: 'compare' })
},
{ dedupe: true },
)
</script>
<template>
<header class="sticky top-0 z-50 border-b border-border">
<div class="absolute inset-0 bg-bg/80 backdrop-blur-md" />
<nav
:aria-label="$t('nav.main_navigation')"
class="relative container min-h-14 flex items-center gap-4 z-1"
: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="{ name: 'index' }"
: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 md:gap-6"
:class="{
'hidden sm:flex': !isSearchExpanded,
'justify-end': isOnHomePage,
'justify-center': !isOnHomePage,
}"
>
<!-- Search bar (hidden on mobile unless expanded) -->
<SearchBox
v-if="!isOnHomePage"
ref="searchBoxRef"
class="max-w-sm"
compact
:model-value="searchQuery"
@update:model-value="updateSearchQuery"
@submit="handleSubmitSearch"
@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="hidden sm:flex flex-shrink-0">
<!-- Desktop: Compare link -->
<LinkBase
class="border-none"
variant="button-secondary"
:to="{ name: 'compare' }"
keyshortcut="c"
>
{{ $t('nav.compare') }}
</LinkBase>
<!-- Desktop: Settings link -->
<LinkBase
class="border-none"
variant="button-secondary"
:to="{ name: 'settings' }"
keyshortcut=","
>
{{ $t('nav.settings') }}
</LinkBase>
<HeaderAccountMenu />
</div>
<!-- Mobile: Menu button (always visible, click to open menu) -->
<ButtonBase
type="button"
class="sm:hidden flex"
:aria-label="$t('nav.open_menu')"
:aria-expanded="showMobileMenu"
@click="showMobileMenu = !showMobileMenu"
classicon="i-carbon:menu"
/>
</nav>
<!-- Mobile menu -->
<HeaderMobileMenu v-model:open="showMobileMenu" />
</header>
</template>