Skip to content

Commit ae8e1b1

Browse files
committed
refactor: use pure CSS for active palette command styling
1 parent 9271263 commit ae8e1b1

File tree

2 files changed

+2
-32
lines changed

2 files changed

+2
-32
lines changed

app/components/CommandPalette.client.vue

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ const inputRef = useTemplateRef<{
2323
}>('inputRef')
2424
const nuxtLinkComponent = resolveComponent('NuxtLink')
2525
26-
const activeIndex = shallowRef(-1)
2726
const previouslyFocused = shallowRef<HTMLElement | null>(null)
2827
2928
const dialogId = 'command-palette-modal'
@@ -63,10 +62,6 @@ const statusMessage = computed(() => {
6362
)
6463
})
6564
66-
const commandIndexMap = computed(() => {
67-
return new Map(flatCommands.value.map((command, index) => [command.id, index]))
68-
})
69-
7065
function getDialog() {
7166
return document.querySelector<HTMLDialogElement>(`#${dialogId}`)
7267
}
@@ -97,7 +92,6 @@ function focusCommand(index: number) {
9792
const element = elements[index]
9893
if (!element) return
9994
100-
activeIndex.value = index
10195
element.focus()
10296
}
10397
@@ -151,7 +145,6 @@ function getCommandAttrs(command: CommandPaletteCommand) {
151145
type: 'button' as const,
152146
}
153147
}
154-
155148
function handleCommandClick(command: CommandPaletteCommand) {
156149
if (isLinkCommand(command)) {
157150
close()
@@ -193,7 +186,6 @@ function handleGlobalKeydown(event: KeyboardEvent) {
193186
event.preventDefault()
194187
const currentIndex = getCommandElements().findIndex(el => el === document.activeElement)
195188
if (currentIndex <= 0) {
196-
activeIndex.value = -1
197189
focusInput()
198190
return
199191
}
@@ -238,7 +230,6 @@ function handleDialogClose() {
238230
return
239231
}
240232
241-
activeIndex.value = -1
242233
previouslyFocused.value?.focus()
243234
previouslyFocused.value = null
244235
}
@@ -261,7 +252,6 @@ watch(
261252
}
262253
await nextTick()
263254
focusInput()
264-
activeIndex.value = -1
265255
return
266256
}
267257
@@ -270,7 +260,6 @@ watch(
270260
return
271261
}
272262
273-
activeIndex.value = -1
274263
previouslyFocused.value?.focus()
275264
previouslyFocused.value = null
276265
},
@@ -286,10 +275,6 @@ watch(
286275
},
287276
)
288277
289-
watch(query, () => {
290-
activeIndex.value = -1
291-
})
292-
293278
useEventListener(document, 'keydown', handleGlobalKeydown)
294279
</script>
295280

@@ -379,17 +364,11 @@ useEventListener(document, 'keydown', handleGlobalKeydown)
379364
<component
380365
:is="getCommandComponent(command)"
381366
v-bind="getCommandAttrs(command)"
382-
class="block min-h-12 w-full rounded-lg border border-transparent px-3 py-2 text-start no-underline text-inherit transition-colors duration-150 hover:border-border/80 hover:bg-bg focus-visible:outline-accent/70"
383-
:class="
384-
activeIndex === (commandIndexMap.get(command.id) ?? -1)
385-
? 'border-border/80 bg-bg'
386-
: ''
387-
"
367+
class="block min-h-12 w-full rounded-lg border border-transparent px-3 py-2 text-start no-underline text-inherit transition-colors duration-150 hover:border-border/80 hover:bg-bg focus-visible:border-border/80 focus-visible:bg-bg focus-visible:outline-accent/70"
388368
data-command-item="true"
389369
:data-command-id="command.id"
390370
:aria-current="command.active ? 'true' : undefined"
391371
@click="handleCommandClick(command)"
392-
@focus="activeIndex = commandIndexMap.get(command.id) ?? -1"
393372
>
394373
<span class="flex items-center gap-3">
395374
<span
@@ -440,17 +419,11 @@ useEventListener(document, 'keydown', handleGlobalKeydown)
440419
<component
441420
:is="getCommandComponent(command)"
442421
v-bind="getCommandAttrs(command)"
443-
class="block min-h-12 w-full rounded-xl border border-border/70 bg-bg-subtle/70 px-3 py-2 text-start no-underline text-inherit transition-colors duration-150 hover:border-border/80 hover:bg-bg focus-visible:outline-accent/70"
444-
:class="
445-
activeIndex === (commandIndexMap.get(command.id) ?? -1)
446-
? 'border-border/80 bg-bg'
447-
: ''
448-
"
422+
class="block min-h-12 w-full rounded-xl border border-border/70 bg-bg-subtle/70 px-3 py-2 text-start no-underline text-inherit transition-colors duration-150 hover:border-border/80 hover:bg-bg focus-visible:border-border/80 focus-visible:bg-bg focus-visible:outline-accent/70"
449423
data-command-item="true"
450424
:data-command-id="command.id"
451425
:aria-current="command.active ? 'true' : undefined"
452426
@click="handleCommandClick(command)"
453-
@focus="activeIndex = commandIndexMap.get(command.id) ?? -1"
454427
>
455428
<span class="flex items-center gap-3">
456429
<span

test/nuxt/components/CommandPalette.spec.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -246,14 +246,11 @@ describe('CommandPalette', () => {
246246
await nextTick()
247247

248248
expect(document.activeElement).toBe(commands[0])
249-
expect(commands[0]?.classList.contains('border-border/80')).toBe(true)
250249

251250
commands[1]?.dispatchEvent(new MouseEvent('mouseenter', { bubbles: true }))
252251
await nextTick()
253252

254253
expect(document.activeElement).toBe(commands[0])
255-
expect(commands[0]?.classList.contains('border-border/80')).toBe(true)
256-
expect(commands[1]?.classList.contains('border-border/80')).toBe(false)
257254
})
258255

259256
it('returns to the root view with ArrowLeft only when the input is empty', async () => {

0 commit comments

Comments
 (0)