Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
181 changes: 181 additions & 0 deletions apps/website/src/components/workshop/HeaderWorkspaceMenu.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
import {
queryHelpers,
render,
screen,
waitFor,
within
} from '@testing-library/vue'
import { describe, expect, it, vi } from 'vitest'
import { defineComponent, h, nextTick, ref } from 'vue'
import {
DropdownMenuContent,
DropdownMenuPortal,
DropdownMenuRoot,
DropdownMenuTrigger
} from 'reka-ui'

import type { WorkshopSession } from '../../config/workshop-session-state'
import type { WorkspaceWithRole } from '../../lib/workshop/workspaces'
import HeaderWorkspaceMenu from './HeaderWorkspaceMenu.vue'

const GAP = 12
const PANEL_LEFT = 100
const PANEL_WIDTH = 300
const NARROWER_PANEL_LEFT = 180
const TRIGGER_LEFT = 360

class FakeResizeObserver {
static observed: { target: Element; report: () => void }[] = []

constructor(private readonly report: () => void) {}

observe(target: Element) {
FakeResizeObserver.observed.push({ target, report: this.report })
}

unobserve(target: Element) {
FakeResizeObserver.observed = FakeResizeObserver.observed.filter(
(entry) => entry.target !== target || entry.report !== this.report
)
}

disconnect() {
FakeResizeObserver.observed = FakeResizeObserver.observed.filter(
(entry) => entry.report !== this.report
)
}
}

const workspace = { id: 'ws', name: 'Personal', type: 'personal' as const }

const session: WorkshopSession = {
uid: 'u',
token: 'token',
expiresAt: Date.now() + 60_000,
workspace,
role: 'owner',
permissions: []
}

const workspaces: readonly WorkspaceWithRole[] = [
{
...workspace,
role: 'owner',
created_at: '2026-01-01T00:00:00Z',
joined_at: '2026-01-01T00:00:00Z'
}
]

function renderMenu() {
FakeResizeObserver.observed = []
vi.stubGlobal('ResizeObserver', FakeResizeObserver)
const open = ref(false)
render(
defineComponent({
setup: () => () =>
h(DropdownMenuRoot, { open: true }, () => [
h(DropdownMenuTrigger, null, () => 'Account'),
h(DropdownMenuPortal, null, () =>
h(
DropdownMenuContent,
{ 'data-testid': 'header-account-menu' },
() =>
h(HeaderWorkspaceMenu, {
open: open.value,
'onUpdate:open': (isOpen: boolean) => (open.value = isOpen),
session,
workspaces
})
)
)
])
})
)
return open
}

/**
* A DOM without layout puts every edge at zero, which would leave the menu,
* the button inside it and the submenu beside it on top of one another.
*/
async function placeAt(testId: string, left: number, width: number) {
const element = await screen.findByTestId(testId)
const rect = {
x: left,
y: 0,
left,
right: left + width,
top: 0,
bottom: 40,
width,
height: 40
}
element.getBoundingClientRect = () => rect as DOMRect
return {
watchers: () =>
FakeResizeObserver.observed.filter((entry) => entry.target === element)
.length,
// The menu hangs off the avatar, so narrowing it moves its left edge and
// leaves its right one where it was.
narrowTo: (narrower: number) => {
const left = rect.right - narrower
Object.assign(rect, { x: left, left, width: narrower })
for (const entry of FakeResizeObserver.observed)
if (entry.target === element) entry.report()
}
}
}

/** Where the submenu ends up: floating-ui writes it onto the wrapper. */
function submenuEdge() {
const positioned = queryHelpers
.queryAllByAttribute('data-reka-popper-content-wrapper', document.body, '')
.find((wrapper) => within(wrapper).queryByTestId('account-workspaces'))
return Number(
/translate\((-?[\d.]+)px/.exec(positioned?.getAttribute('style') ?? '')?.[1]
)
}

describe('HeaderWorkspaceMenu', () => {
it('opens clear of the menu the switcher sits in', async () => {
const open = renderMenu()
await placeAt('header-account-menu', PANEL_LEFT, PANEL_WIDTH)
await placeAt('account-workspace', TRIGGER_LEFT, 32)

open.value = true
await nextTick()

await waitFor(() => expect(submenuEdge()).toBe(PANEL_LEFT - GAP))
})

it('follows the menu edge when the menu is resized under it', async () => {
const open = renderMenu()
const panel = await placeAt('header-account-menu', PANEL_LEFT, PANEL_WIDTH)
await placeAt('account-workspace', TRIGGER_LEFT, 32)

open.value = true
await nextTick()
await waitFor(() => expect(submenuEdge()).toBe(PANEL_LEFT - GAP))

panel.narrowTo(PANEL_WIDTH - (NARROWER_PANEL_LEFT - PANEL_LEFT))

await waitFor(() => expect(submenuEdge()).toBe(NARROWER_PANEL_LEFT - GAP))
})

it('watches the menu only while the submenu is open', async () => {
const open = renderMenu()
const panel = await placeAt('header-account-menu', PANEL_LEFT, PANEL_WIDTH)
await placeAt('account-workspace', TRIGGER_LEFT, 32)

// The menu is its own floating element, so it arrives already watched.
const others = panel.watchers()

open.value = true
await nextTick()
expect(panel.watchers()).toBe(others + 1)

open.value = false
await nextTick()
expect(panel.watchers()).toBe(others)
})
})
24 changes: 12 additions & 12 deletions apps/website/src/components/workshop/HeaderWorkspaceMenu.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script setup lang="ts">
import { ArrowLeftRight, Check } from '@lucide/vue'
import { useEventListener } from '@vueuse/core'
import { ref, useTemplateRef, watch } from 'vue'
import { useResizeObserver } from '@vueuse/core'
import { ref, shallowRef, useTemplateRef, watch } from 'vue'
import {
DropdownMenuItem,
DropdownMenuPortal,
Expand Down Expand Up @@ -51,29 +51,29 @@ const GAP = 12
const trigger = useTemplateRef<{ $el: HTMLElement }>('trigger')
const sideOffset = ref(GAP)

const panel = shallowRef<HTMLElement>()

// The switcher is a button inside the menu, so a submenu placed beside it lands
// on top of the menu. It is offset to the menu's own left edge instead, and
// measured each time the list opens, since the menu has a maximum width the
// viewport constrains and its left edge moves when the window does.
// on top of the menu. It is offset to the menu's own left edge instead. The
// menu has a maximum width the viewport constrains, so that edge moves on its
// own: watching the panel rather than the window measures it after the new
// width is in the layout, which a resize listener would race.
function measure() {
const el = trigger.value?.$el
if (!el) return
const panel = el.closest('[data-testid="header-account-menu"]')
panel.value =
el.closest<HTMLElement>('[data-testid="header-account-menu"]') ?? undefined
sideOffset.value = submenuOffset(
el.getBoundingClientRect(),
panel?.getBoundingClientRect(),
panel.value?.getBoundingClientRect(),
GAP
)
}

watch(open, (isOpen) => {
if (isOpen) measure()
})
useEventListener(
() => (open.value ? globalThis.window : undefined),
'resize',
measure
)
useResizeObserver(() => (open.value ? panel.value : undefined), measure)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

const itemClass =
'flex w-full cursor-pointer items-center gap-3 rounded-xl px-3 py-3 text-sm text-primary-comfy-canvas outline-none hover:bg-transparency-white-t4 focus-visible:bg-transparency-white-t4'
Expand Down
Loading