Files
archie/ui/src/chat/ChatSlashPalette.vue
T

73 lines
2.6 KiB
Vue

<script setup lang="ts">
import { nextTick, ref, watch } from "vue";
import { commandScrollTop } from "./command-scroll";
import { Button } from "@/components/ui/button";
import type { Completion } from "./commands";
/**
* The composer palette, floating above it: the commands the server accepts
* after `/`, the tasks that can be attached after `#`. The arrow keys and the
* pointer both land here. A failed read shows in place of the rows.
*/
const props = defineProps<{
matches: Completion[];
selection: number;
label: string;
error?: string;
}>();
const emit = defineEmits<{ choose: [index: number]; retry: [] }>();
const menu = ref<HTMLElement | null>(null);
// Arrow keys move the selection, and the list scrolls so the highlight stays
// visible once the matches run past the rows that fit. The arithmetic is
// command-scroll.ts's; this only applies what it returns. flush: 'post' so the
// option being measured is the one just selected, not the one leaving.
watch(
() => [props.selection, props.matches.length],
async () => {
await nextTick();
const el = menu.value;
const option = el?.children[props.selection] as HTMLElement | undefined;
if (!el || !option) return;
el.scrollTop = commandScrollTop({
scrollTop: el.scrollTop,
viewportHeight: el.clientHeight,
optionTop: option.offsetTop,
optionHeight: option.offsetHeight,
});
},
{ flush: "post" },
);
</script>
<template>
<div
ref="menu"
id="slash-palette"
role="listbox"
:aria-label="label"
class="absolute inset-x-3 bottom-[calc(100%-0.25rem)] z-30 grid max-h-72 gap-0.5 overflow-auto rounded-lg border border-border-strong bg-popover p-1.5 text-label shadow-md"
>
<div v-if="error" role="alert" class="flex items-center justify-between gap-2 px-2.5 py-2 text-danger">
<span class="truncate">{{ error }}</span>
<Button variant="outline" size="sm" @mousedown.prevent @click="emit('retry')">Retry</Button>
</div>
<button
v-for="(item, index) in matches"
:key="item.key"
type="button"
role="option"
:id="`slash-palette-option-${index}`"
:aria-selected="index === selection"
class="flex flex-col gap-0.5 rounded-md border border-transparent px-2.5 py-1.5 text-left text-fg-muted hover:bg-muted hover:text-foreground aria-selected:border-border aria-selected:bg-muted aria-selected:text-foreground"
@mousedown.prevent
@click="emit('choose', index)"
>
<code class="truncate text-foreground">{{ item.label }}</code>
<span v-if="item.detail" class="line-clamp-2 text-caption">{{ item.detail }}</span>
</button>
</div>
</template>