-
-
Notifications
You must be signed in to change notification settings - Fork 425
Expand file tree
/
Copy pathapp.vue
More file actions
194 lines (162 loc) · 4.63 KB
/
app.vue
File metadata and controls
194 lines (162 loc) · 4.63 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
<script setup lang="ts">
import type { Directions } from '@nuxtjs/i18n'
import { useEventListener, onKeyDown, onKeyUp } from '@vueuse/core'
import { isEditableElement } from '~/utils/input'
const route = useRoute()
const router = useRouter()
const { locale, locales } = useI18n()
// Initialize user preferences (accent color, package manager) before hydration to prevent flash/CLS
initPreferencesOnPrehydrate()
const isHomepage = computed(() => route.name === 'index')
const showKbdHints = shallowRef(false)
const localeMap = locales.value.reduce(
(acc, l) => {
acc[l.code] = l.dir ?? 'ltr'
return acc
},
{} as Record<string, Directions>,
)
const darkMode = usePreferredDark()
const colorMode = useColorMode()
const colorScheme = computed(() => {
return {
system: darkMode ? 'dark light' : 'light dark',
light: 'only light',
dark: 'only dark',
}[colorMode.preference]
})
useHead({
htmlAttrs: {
'lang': () => locale.value,
'dir': () => localeMap[locale.value] ?? 'ltr',
'data-kbd-hints': () => showKbdHints.value,
},
titleTemplate: titleChunk => {
return titleChunk ? titleChunk : 'npmx - Better npm Package Browser'
},
meta: [{ name: 'color-scheme', content: colorScheme }],
})
if (import.meta.server) {
setJsonLd(createWebSiteSchema())
}
const keyboardShortcuts = useKeyboardShortcuts()
const { settings } = useSettings()
initKeyShortcuts()
onKeyDown(
'/',
e => {
if (e.ctrlKey) {
e.preventDefault()
settings.value.instantSearch = !settings.value.instantSearch
return
}
if (!keyboardShortcuts.value || isEditableElement(e.target)) return
e.preventDefault()
const searchInput = document.querySelector<HTMLInputElement>(
'input[type="search"], input[name="q"]',
)
if (searchInput) {
searchInput.focus()
return
}
router.push({ name: 'search' })
},
{ dedupe: true },
)
onKeyDown(
'?',
e => {
if (!keyboardShortcuts.value || isEditableElement(e.target)) return
e.preventDefault()
showKbdHints.value = true
},
{ dedupe: true },
)
onKeyUp(
'?',
e => {
if (!keyboardShortcuts.value || isEditableElement(e.target)) return
e.preventDefault()
showKbdHints.value = false
},
{ dedupe: true },
)
// Light dismiss fallback for browsers that don't support closedby="any" (Safari + old Chrome/Firefox)
// https://codepen.io/paramagicdev/pen/gbYompq
// see: https://github.com/npmx-dev/npmx.dev/pull/522#discussion_r2749978022
function handleModalLightDismiss(e: MouseEvent) {
const target = e.target as HTMLElement
if (target.tagName === 'DIALOG' && target.hasAttribute('open')) {
const rect = target.getBoundingClientRect()
const isOutside =
e.clientX < rect.left ||
e.clientX > rect.right ||
e.clientY < rect.top ||
e.clientY > rect.bottom
if (!isOutside) return
;(target as HTMLDialogElement).close()
}
}
if (import.meta.client) {
// Feature check for native light dismiss support via closedby="any"
// https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/dialog#closedby
const supportsClosedBy =
typeof HTMLDialogElement !== 'undefined' &&
typeof HTMLDialogElement.prototype === 'object' &&
'closedBy' in HTMLDialogElement.prototype
if (!supportsClosedBy) {
useEventListener(document, 'click', handleModalLightDismiss)
}
}
// title and description will be inferred
// this will be overridden by upstream pages that use different templates
defineOgImage('Page.takumi', {}, { alt: 'npmx — a fast, modern browser for the npm registry' })
</script>
<template>
<div class="min-h-screen flex flex-col bg-bg text-fg">
<NuxtPwaAssets />
<LinkBase to="#main-content" external variant="button-primary" class="skip-link">{{
$t('common.skip_link')
}}</LinkBase>
<AppHeader :show-logo="!isHomepage" />
<NuxtRouteAnnouncer v-slot="{ message }">
{{ route.name === 'search' ? `${$t('search.title_packages')} - npmx` : message }}
</NuxtRouteAnnouncer>
<div id="main-content" class="flex-1 flex flex-col" tabindex="-1">
<NuxtPage />
</div>
<CommandPalette />
<AppFooter />
<ScrollToTop />
</div>
</template>
<style scoped>
/* Skip link */
.skip-link {
position: fixed;
top: -100%;
z-index: 100;
}
.skip-link:focus {
top: 0;
}
</style>
<style>
/* Keyboard shortcut highlight on "?" key press */
kbd {
position: relative;
}
kbd::before {
content: '';
position: absolute;
inset: 0;
border-radius: inherit;
box-shadow: 0 0 4px 2px var(--accent);
opacity: 0;
transition: opacity 200ms ease-out;
pointer-events: none;
}
html[data-kbd-hints='true'] kbd::before {
opacity: 1;
}
</style>