feat(ui): pin the chat dock to a side edge (#1853)
This commit is contained in:
2 files changed
+145
-6
No files matched your search
+1
-1
@@ -96,7 +96,7 @@ watch(() => accounts.status?.user, (user) => {
|
||||
<SidebarProvider class="bg-[var(--workspace)]">
|
||||
<a href="#main-content" class="sr-only focus:not-sr-only focus:fixed focus:top-2 focus:left-4 focus:z-[60] focus:rounded-md focus:bg-card focus:px-3 focus:py-2">Skip to main content</a>
|
||||
<AppSidebar :hidden="hidden" />
|
||||
<SidebarInset class="min-w-0 bg-[var(--workspace)]" :class="workflowsPage && 'max-md:h-dvh'">
|
||||
<SidebarInset class="min-w-0 bg-[var(--workspace)] md:ml-[var(--chat-pin-l,0px)] md:mr-[var(--chat-pin-r,0px)]" :class="workflowsPage && 'max-md:h-dvh'">
|
||||
<header class="bg-card border-border-strong sticky top-0 z-30 flex h-14 shrink-0 items-center gap-2 border-b px-3 shadow-[var(--shadow-sm)] sm:px-4">
|
||||
<SidebarTrigger class="-ml-1 md:hidden" />
|
||||
<Brand />
|
||||
|
||||
+144
-5
@@ -1,9 +1,11 @@
|
||||
<script setup lang="ts">
|
||||
import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip";
|
||||
import { ChevronDown, ChevronUp } from "@lucide/vue";
|
||||
import { nextTick, onMounted, onUnmounted, ref, watch } from "vue";
|
||||
import { useMediaQuery, useStorage, useWindowSize } from "@vueuse/core";
|
||||
import { computed, nextTick, onMounted, onUnmounted, ref, watch } from "vue";
|
||||
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { cn } from "@/lib/utils";
|
||||
import ChatBar from "./ChatBar.vue";
|
||||
import ChatComposer from "./ChatComposer.vue";
|
||||
import ChatTranscript from "./ChatTranscript.vue";
|
||||
@@ -19,6 +21,11 @@ import { composerText, escapeOwnedByLayer, isSending, useChat } from "./state";
|
||||
* the conversation opens above it. Hiding the conversation leaves the bar. It is
|
||||
* deliberately not a modal: no scrim, no scroll lock, no focus trap.
|
||||
*
|
||||
* On a desktop-width screen the expanded conversation can be pinned to the left
|
||||
* or right edge, where the page reflows beside it instead of sitting under it.
|
||||
* Dragging the header to an edge pins it; dragging it away, or double-clicking
|
||||
* the header, unpins it. The side and width are remembered per browser.
|
||||
*
|
||||
* It stays mounted, so the session and any turn in flight survive a navigation.
|
||||
* It is mounted outside the app shell: `.shell`'s backdrop-filter makes it a
|
||||
* containing block for `position: fixed`, so a bar nested inside would anchor to
|
||||
@@ -27,6 +34,21 @@ import { composerText, escapeOwnedByLayer, isSending, useChat } from "./state";
|
||||
useChat();
|
||||
|
||||
const expanded = ref(false);
|
||||
const MIN_WIDTH = 320;
|
||||
const EDGE = 64;
|
||||
type Side = "left" | "right";
|
||||
const saved = useStorage<{ side: Side | null; width: number }>(
|
||||
"archie.chat.dock",
|
||||
{ side: null, width: 420 },
|
||||
localStorage,
|
||||
{ mergeDefaults: true },
|
||||
);
|
||||
const desktop = useMediaQuery("(min-width: 768px)");
|
||||
const { width: viewport } = useWindowSize();
|
||||
const width = computed(() =>
|
||||
Math.round(Math.min(Math.max(saved.value.width, MIN_WIDTH), Math.max(MIN_WIDTH, Math.min(720, viewport.value - 480)))),
|
||||
);
|
||||
const side = computed(() => (desktop.value && expanded.value ? saved.value.side : null));
|
||||
const composer = ref<InstanceType<typeof ChatComposer> | null>(null);
|
||||
|
||||
// Nothing outside the dock closes it: every navigation control is outside it,
|
||||
@@ -72,6 +94,81 @@ onUnmounted(() => {
|
||||
document.documentElement.style.removeProperty("--chat-dock-clear");
|
||||
});
|
||||
|
||||
// The page makes room for a pinned dock through these two variables.
|
||||
watch([side, width], ([s, w]) => {
|
||||
const root = document.documentElement.style;
|
||||
root.setProperty("--chat-pin-l", s === "left" ? `${w}px` : "0px");
|
||||
root.setProperty("--chat-pin-r", s === "right" ? `${w}px` : "0px");
|
||||
}, { immediate: true });
|
||||
onUnmounted(() => {
|
||||
document.documentElement.style.removeProperty("--chat-pin-l");
|
||||
document.documentElement.style.removeProperty("--chat-pin-r");
|
||||
});
|
||||
|
||||
function edgeAt(x: number): Side | null {
|
||||
if (x <= EDGE) return "left";
|
||||
if (x >= window.innerWidth - EDGE) return "right";
|
||||
return null;
|
||||
}
|
||||
|
||||
// Pointer travel before a press on the header becomes a drag, so a click or a
|
||||
// double-click on it never moves the dock.
|
||||
const DRAG_START = 4;
|
||||
const offset = ref<{ x: number; y: number } | null>(null);
|
||||
const target = ref<Side | null>(null);
|
||||
function startDrag(event: PointerEvent): void {
|
||||
if (!desktop.value || event.button !== 0) return;
|
||||
if ((event.target as HTMLElement).closest("button, a, input, select, textarea, [role=combobox]")) return;
|
||||
const grip = event.currentTarget as HTMLElement;
|
||||
const { clientX: x0, clientY: y0 } = event;
|
||||
grip.setPointerCapture(event.pointerId);
|
||||
const move = (e: PointerEvent): void => {
|
||||
const dx = e.clientX - x0;
|
||||
const dy = e.clientY - y0;
|
||||
if (!offset.value && Math.hypot(dx, dy) < DRAG_START) return;
|
||||
offset.value = { x: dx, y: dy };
|
||||
target.value = edgeAt(e.clientX);
|
||||
};
|
||||
const end = (e: PointerEvent): void => {
|
||||
grip.removeEventListener("pointermove", move);
|
||||
grip.removeEventListener("pointerup", end);
|
||||
grip.removeEventListener("pointercancel", end);
|
||||
if (offset.value && e.type === "pointerup") saved.value.side = edgeAt(e.clientX);
|
||||
offset.value = null;
|
||||
target.value = null;
|
||||
};
|
||||
grip.addEventListener("pointermove", move);
|
||||
grip.addEventListener("pointerup", end);
|
||||
grip.addEventListener("pointercancel", end);
|
||||
}
|
||||
function togglePin(event: MouseEvent): void {
|
||||
if (!desktop.value || (event.target as HTMLElement).closest("button, a, input, select, textarea, [role=combobox]")) return;
|
||||
saved.value.side = saved.value.side ? null : "right";
|
||||
}
|
||||
|
||||
function resize(event: PointerEvent): void {
|
||||
const handle = event.currentTarget as HTMLElement;
|
||||
handle.setPointerCapture(event.pointerId);
|
||||
const move = (e: PointerEvent): void => {
|
||||
saved.value.width = side.value === "right" ? window.innerWidth - e.clientX : e.clientX;
|
||||
};
|
||||
const end = (): void => {
|
||||
handle.removeEventListener("pointermove", move);
|
||||
handle.removeEventListener("pointerup", end);
|
||||
handle.removeEventListener("pointercancel", end);
|
||||
};
|
||||
handle.addEventListener("pointermove", move);
|
||||
handle.addEventListener("pointerup", end);
|
||||
handle.addEventListener("pointercancel", end);
|
||||
}
|
||||
function resizeByKey(event: KeyboardEvent): void {
|
||||
const grow = event.key === (side.value === "right" ? "ArrowLeft" : "ArrowRight");
|
||||
const shrink = event.key === (side.value === "right" ? "ArrowRight" : "ArrowLeft");
|
||||
if (!grow && !shrink) return;
|
||||
event.preventDefault();
|
||||
saved.value.width = width.value + (grow ? 16 : -16);
|
||||
}
|
||||
|
||||
function usePrompt(text: string): void {
|
||||
composerText.value = text;
|
||||
composer.value?.focus();
|
||||
@@ -79,13 +176,54 @@ function usePrompt(text: string): void {
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="pointer-events-none fixed inset-x-0 bottom-4 z-40 flex justify-center px-3 max-md:bottom-2">
|
||||
<div
|
||||
v-if="target"
|
||||
aria-hidden="true"
|
||||
:class="cn('pointer-events-none fixed inset-y-0 z-40 border-primary/40 bg-primary/10', target === 'left' ? 'left-0 border-r-2' : 'right-0 border-l-2')"
|
||||
:style="{ width: `${width}px` }"
|
||||
/>
|
||||
<div
|
||||
:class="cn(
|
||||
'pointer-events-none fixed z-40',
|
||||
side ? cn('inset-y-0', side === 'left' ? 'left-0' : 'right-0') : 'inset-x-0 bottom-4 flex justify-center px-3 max-md:bottom-2',
|
||||
)"
|
||||
:style="side ? { width: `${width}px` } : undefined"
|
||||
>
|
||||
<aside
|
||||
ref="dock"
|
||||
aria-label="Chat with Archie"
|
||||
class="pointer-events-auto flex w-full max-w-xl flex-col rounded-lg border border-border-strong bg-popover shadow-[var(--shadow-md)]"
|
||||
:class="cn(
|
||||
'pointer-events-auto relative flex flex-col border-border-strong bg-popover shadow-[var(--shadow-md)]',
|
||||
side
|
||||
? cn('h-full w-full', side === 'left' ? 'border-r' : 'border-l')
|
||||
: 'w-full max-w-xl rounded-lg border',
|
||||
offset && 'z-50 opacity-90',
|
||||
)"
|
||||
:style="offset ? { transform: `translate(${offset.x}px, ${offset.y}px)` } : undefined"
|
||||
>
|
||||
<ChatBar v-if="expanded">
|
||||
<div
|
||||
v-if="side"
|
||||
role="separator"
|
||||
aria-orientation="vertical"
|
||||
aria-label="Resize chat"
|
||||
tabindex="0"
|
||||
:aria-valuenow="width"
|
||||
:aria-valuemin="MIN_WIDTH"
|
||||
class="absolute inset-y-0 z-10 w-1.5 cursor-col-resize hover:bg-primary/30 focus-visible:bg-primary/30 focus-visible:outline-none"
|
||||
:class="side === 'left' ? '-right-0.5' : '-left-0.5'"
|
||||
@pointerdown.prevent="resize"
|
||||
@keydown="resizeByKey"
|
||||
/>
|
||||
<div
|
||||
v-if="expanded"
|
||||
:class="desktop && 'cursor-grab select-none active:cursor-grabbing'"
|
||||
@pointerdown="startDrag"
|
||||
@dblclick="togglePin"
|
||||
>
|
||||
<div v-if="desktop" class="flex justify-center pt-1.5" aria-hidden="true">
|
||||
<span class="h-1 w-8 rounded-full bg-border-strong" />
|
||||
</div>
|
||||
<ChatBar>
|
||||
<Tooltip>
|
||||
<TooltipTrigger as-child>
|
||||
<Button
|
||||
@@ -101,7 +239,8 @@ function usePrompt(text: string): void {
|
||||
<TooltipContent>Hide conversation</TooltipContent>
|
||||
</Tooltip>
|
||||
</ChatBar>
|
||||
<div v-if="expanded" class="flex h-[min(28rem,55dvh)] min-h-0 flex-col">
|
||||
</div>
|
||||
<div v-if="expanded" :class="cn('flex min-h-0 flex-col', side ? 'flex-1' : 'h-[min(28rem,55dvh)]')">
|
||||
<ChatTranscript @prompt="usePrompt" />
|
||||
</div>
|
||||
<div @focusin="expanded = true">
|
||||
|
||||
Reference in new issue
Block a user