forked from npmx-dev/npmx.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPackagePlaygrounds.vue
More file actions
192 lines (176 loc) · 6.63 KB
/
PackagePlaygrounds.vue
File metadata and controls
192 lines (176 loc) · 6.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
<script setup lang="ts">
import type { PlaygroundLink } from '#shared/types'
const props = defineProps<{
links: PlaygroundLink[]
}>()
// Map provider id to icon class
const providerIcons: Record<string, string> = {
'stackblitz': 'i-simple-icons:stackblitz',
'codesandbox': 'i-simple-icons:codesandbox',
'codepen': 'i-simple-icons:codepen',
'replit': 'i-simple-icons:replit',
'gitpod': 'i-simple-icons:gitpod',
'vue-playground': 'i-simple-icons:vuedotjs',
'nuxt-new': 'i-simple-icons:nuxtdotjs',
'vite-new': 'i-simple-icons:vite',
'jsfiddle': 'i-carbon:code',
}
// Map provider id to color class
const providerColors: Record<string, string> = {
'stackblitz': 'text-provider-stackblitz',
'codesandbox': 'text-provider-codesandbox',
'codepen': 'text-provider-codepen',
'replit': 'text-provider-replit',
'gitpod': 'text-provider-gitpod',
'vue-playground': 'text-provider-vue',
'nuxt-new': 'text-provider-nuxt',
'vite-new': 'text-provider-vite',
'jsfiddle': 'text-provider-jsfiddle',
}
function getIcon(provider: string): string {
return providerIcons[provider] || 'i-carbon:play'
}
function getColor(provider: string): string {
return providerColors[provider] || 'text-fg-muted'
}
// Dropdown state
const isOpen = shallowRef(false)
const dropdownRef = useTemplateRef('dropdownRef')
const menuRef = useTemplateRef('menuRef')
const focusedIndex = shallowRef(-1)
onClickOutside(dropdownRef, () => {
isOpen.value = false
})
// Single vs multiple
const hasSingleLink = computed(() => props.links.length === 1)
const hasMultipleLinks = computed(() => props.links.length > 1)
const firstLink = computed(() => props.links[0])
function closeDropdown() {
isOpen.value = false
focusedIndex.value = -1
}
function handleKeydown(event: KeyboardEvent) {
if (!isOpen.value) {
if (event.key === 'ArrowDown' || event.key === 'Enter' || event.key === ' ') {
event.preventDefault()
isOpen.value = true
focusedIndex.value = 0
nextTick(() => focusMenuItem(0))
}
return
}
switch (event.key) {
case 'Escape':
event.preventDefault()
closeDropdown()
break
case 'ArrowDown':
event.preventDefault()
focusedIndex.value = (focusedIndex.value + 1) % props.links.length
focusMenuItem(focusedIndex.value)
break
case 'ArrowUp':
event.preventDefault()
focusedIndex.value = focusedIndex.value <= 0 ? props.links.length - 1 : focusedIndex.value - 1
focusMenuItem(focusedIndex.value)
break
case 'Home':
event.preventDefault()
focusedIndex.value = 0
focusMenuItem(0)
break
case 'End':
event.preventDefault()
focusedIndex.value = props.links.length - 1
focusMenuItem(props.links.length - 1)
break
case 'Tab':
closeDropdown()
break
}
}
function focusMenuItem(index: number) {
const items = menuRef.value?.querySelectorAll<HTMLElement>('[role="menuitem"]')
items?.[index]?.focus()
}
</script>
<template>
<section v-if="links.length > 0" aria-labelledby="playgrounds-heading">
<h2 id="playgrounds-heading" class="text-xs text-fg-subtle uppercase tracking-wider mb-3">
{{ $t('package.playgrounds.title') }}
</h2>
<div ref="dropdownRef" class="relative">
<!-- Single link: direct button -->
<AppTooltip v-if="hasSingleLink && firstLink" :text="firstLink.providerName" class="w-full">
<a
:href="firstLink.url"
target="_blank"
rel="noopener noreferrer"
class="w-full flex items-center gap-2 px-3 py-2 text-sm font-mono bg-bg-muted border border-border rounded-md hover:border-border-hover hover:bg-bg-elevated focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-border-hover transition-colors duration-200"
>
<span
:class="[getIcon(firstLink.provider), getColor(firstLink.provider), 'w-4 h-4 shrink-0']"
aria-hidden="true"
/>
<span class="truncate text-fg-muted">{{ firstLink.label }}</span>
</a>
</AppTooltip>
<!-- Multiple links: dropdown button -->
<button
v-if="hasMultipleLinks"
type="button"
aria-haspopup="true"
:aria-expanded="isOpen"
class="w-full flex items-center justify-between gap-2 px-3 py-2 text-sm font-mono bg-bg-muted border border-border rounded-md hover:border-border-hover hover:bg-bg-elevated focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-border-hover transition-colors duration-200"
@click="isOpen = !isOpen"
@keydown="handleKeydown"
>
<span class="flex items-center gap-2">
<span class="i-carbon:play w-4 h-4 shrink-0 text-fg-muted" aria-hidden="true" />
<span class="text-fg-muted"
>{{ $t('package.playgrounds.choose') }} ({{ links.length }})</span
>
</span>
<span
class="i-carbon:chevron-down w-3 h-3 text-fg-subtle transition-transform duration-200 motion-reduce:transition-none"
:class="{ 'rotate-180': isOpen }"
aria-hidden="true"
/>
</button>
<!-- Dropdown menu -->
<Transition
enter-active-class="transition duration-150 ease-out motion-reduce:transition-none"
enter-from-class="opacity-0 scale-95 motion-reduce:scale-100"
enter-to-class="opacity-100 scale-100"
leave-active-class="transition duration-100 ease-in motion-reduce:transition-none"
leave-from-class="opacity-100 scale-100"
leave-to-class="opacity-0 scale-95 motion-reduce:scale-100"
>
<div
v-if="isOpen && hasMultipleLinks"
ref="menuRef"
role="menu"
class="absolute top-full inset-is-0 inset-ie-0 mt-1 bg-bg-elevated border border-border rounded-lg shadow-lg z-50 py-1 overflow-visible"
@keydown="handleKeydown"
>
<AppTooltip v-for="link in links" :key="link.url" :text="link.providerName" class="block">
<a
:href="link.url"
target="_blank"
rel="noopener noreferrer"
role="menuitem"
class="flex items-center gap-2 px-3 py-2 text-sm font-mono text-fg-muted hover:text-fg hover:bg-bg-muted focus-visible:outline-none focus-visible:text-fg focus-visible:bg-bg-muted transition-colors duration-150"
@click="closeDropdown"
>
<span
:class="[getIcon(link.provider), getColor(link.provider), 'w-4 h-4 shrink-0']"
aria-hidden="true"
/>
<span class="truncate">{{ link.label }}</span>
</a>
</AppTooltip>
</div>
</Transition>
</div>
</section>
</template>