Repository navigation
fix(models): measure the workspace menu when it resizes, not when the window does #17865
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mariana953
wants to merge
5
commits into
main
Choose a base branch
from
mariana/workspace-submenu-resize
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0493d55
fix(models): measure the menu when it resizes, not when the window does
mariana953 c7a7a22
test(models): drop the resize step that crosses the header breakpoint
mariana953 d47be11
test(models): cover the submenu placement the menu's own width drives
mariana953 8c723f4
test(models): narrow the menu instead of sliding it
mariana953 a7adfc0
test(models): hold the menu to being watched only while open
mariana953 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
181 changes: 181 additions & 0 deletions
181
apps/website/src/components/workshop/HeaderWorkspaceMenu.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.