feat(ui): topbar crumb menus open on hover
This commit is contained in:
3 files changed
+50
-6
No files matched your search
@@ -11,6 +11,7 @@ import {
|
||||
DropdownMenuTrigger,
|
||||
} from "@/components/ui/dropdown-menu";
|
||||
import { useOrgStore } from "@/stores/org";
|
||||
import { useHoverOpen } from "./hover-open";
|
||||
|
||||
const orgStore = useOrgStore();
|
||||
const { homeOrg, memberOrgs, orgs } = storeToRefs(orgStore);
|
||||
@@ -30,17 +31,19 @@ async function choose(id: string): Promise<void> {
|
||||
failed.value = String((cause as Error).message || cause);
|
||||
}
|
||||
}
|
||||
|
||||
const hover = useHoverOpen();
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<DropdownMenu v-if="choices.length > 1">
|
||||
<DropdownMenu v-if="choices.length > 1" :open="hover.open.value" :modal="false" @update:open="hover.update">
|
||||
<DropdownMenuTrigger as-child>
|
||||
<Button variant="ghost" size="sm" class="gap-1 px-2" :aria-label="`Organisation: ${name}`" :title="failed || undefined">
|
||||
<Button variant="ghost" size="sm" class="gap-1 px-2" :aria-label="`Organisation: ${name}`" :title="failed || undefined" @pointerenter="hover.enterTrigger" @pointerleave="hover.leaveTrigger">
|
||||
<span class="max-w-40 truncate">{{ name }}</span>
|
||||
<ChevronDown class="size-4" aria-hidden="true" />
|
||||
</Button>
|
||||
</DropdownMenuTrigger>
|
||||
<DropdownMenuContent align="start" class="min-w-48">
|
||||
<DropdownMenuContent align="start" class="min-w-48" @pointerenter="hover.enter" @pointerleave="hover.leave">
|
||||
<DropdownMenuItem v-for="o in choices" :key="o.id" @select="choose(o.id)">
|
||||
{{ o.name }}
|
||||
<Check v-if="o.id === homeOrg" class="ml-auto" aria-label="Current" />
|
||||
|
||||
@@ -17,6 +17,7 @@ import { navIcons } from "@/lib/nav-icons";
|
||||
import { attentionStatusIds } from "@/lib/task-meta";
|
||||
import { useLiveResource } from "@/stores/session";
|
||||
import { useTasksStore } from "@/stores/tasks";
|
||||
import { useHoverOpen } from "./hover-open";
|
||||
|
||||
const props = defineProps<{ hidden: string[] }>();
|
||||
|
||||
@@ -36,18 +37,20 @@ const attention = computed(() => {
|
||||
for (const id of attentionStatusIds()) total += counts[id] ?? 0;
|
||||
return total;
|
||||
});
|
||||
|
||||
const hover = useHoverOpen();
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<DropdownMenu>
|
||||
<DropdownMenu :open="hover.open.value" :modal="false" @update:open="hover.update">
|
||||
<DropdownMenuTrigger as-child>
|
||||
<Button variant="ghost" size="sm" class="gap-1 px-2" aria-label="Sections">
|
||||
<Button variant="ghost" size="sm" class="gap-1 px-2" aria-label="Sections" @pointerenter="hover.enterTrigger" @pointerleave="hover.leaveTrigger">
|
||||
<span>{{ current?.label ?? route.meta.label ?? "Sections" }}</span>
|
||||
<Badge v-if="activePath !== '/' && attention" variant="warn" class="tabular-nums" :aria-label="`${attention} need attention`">{{ attention }}</Badge>
|
||||
<ChevronDown class="size-4" aria-hidden="true" />
|
||||
</Button>
|
||||
</DropdownMenuTrigger>
|
||||
<DropdownMenuContent align="start" class="min-w-48">
|
||||
<DropdownMenuContent align="start" class="min-w-48" @pointerenter="hover.enter" @pointerleave="hover.leave">
|
||||
<DropdownMenuItem v-for="entry in links" :key="entry.path" as-child :class="entry.path === activePath && 'bg-accent'">
|
||||
<RouterLink :to="entry.path" :aria-current="entry.path === activePath ? 'page' : undefined">
|
||||
<component :is="navIcons[entry.path]" v-if="navIcons[entry.path]" aria-hidden="true" />
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
import { onUnmounted, ref } from "vue";
|
||||
|
||||
/**
|
||||
* Opens a topbar crumb's menu on hover as well as on click. Closing waits a
|
||||
* moment so the pointer can cross from the crumb to the menu. While the pointer
|
||||
* is on the trigger, the menu's own close request is ignored, so a click on a
|
||||
* crumb the hover already opened does not snap it shut. Use with
|
||||
* `:modal="false"`: a modal menu blocks the pointer events this relies on.
|
||||
*/
|
||||
export function useHoverOpen(closeDelay = 150) {
|
||||
const open = ref(false);
|
||||
let overTrigger = false;
|
||||
let timer: ReturnType<typeof setTimeout> | undefined;
|
||||
|
||||
function enter(): void {
|
||||
clearTimeout(timer);
|
||||
open.value = true;
|
||||
}
|
||||
function leave(): void {
|
||||
clearTimeout(timer);
|
||||
timer = setTimeout(() => (open.value = false), closeDelay);
|
||||
}
|
||||
function enterTrigger(): void {
|
||||
overTrigger = true;
|
||||
enter();
|
||||
}
|
||||
function leaveTrigger(): void {
|
||||
overTrigger = false;
|
||||
leave();
|
||||
}
|
||||
function update(value: boolean): void {
|
||||
if (!value && overTrigger) return;
|
||||
open.value = value;
|
||||
}
|
||||
onUnmounted(() => clearTimeout(timer));
|
||||
|
||||
return { open, enter, leave, enterTrigger, leaveTrigger, update };
|
||||
}
|
||||
Reference in new issue
Block a user