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
Original file line number Diff line number Diff line change
Expand Up @@ -158,15 +158,16 @@ export default class extends Controller {
}

// Anchor to the trigger's inline-start, but shift so the content never overflows the viewport.
// The edge is rounded to a whole pixel: text placed on a fractional pixel is not rendered the same way every time.
#viewportInlineStart(navRect, itemRect, width, rtl) {
const margin = 8;
if (rtl) {
const right = Math.max(width + margin, Math.min(itemRect.right, window.innerWidth - margin));
const right = Math.round(Math.max(width + margin, Math.min(itemRect.right, window.innerWidth - margin)));

return navRect.right - right;
}

const left = Math.max(margin, Math.min(itemRect.left, window.innerWidth - margin - width));
const left = Math.round(Math.max(margin, Math.min(itemRect.left, window.innerWidth - margin - width)));

return left - navRect.left;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,27 @@ describeRecipe('shadcn/navigation-menu', () => {
await expect(page.getByRole('link', { name: /Introduction/ })).toBeVisible();
});

for (const { example, trigger, edge } of [
{ example: 'default', trigger: 'Getting started', edge: 'left' },
{ example: 'rtl', trigger: 'ابدأ', edge: 'right' },
] as const) {
test(`aligns the ${edge} edge of the panel on a whole pixel in the ${example} example`, async ({
page,
gotoExample,
}) => {
await gotoExample(`shadcn/navigation-menu/${example}`);
const viewport = page.locator('[data-slot="navigation-menu-viewport"][data-state="open"]');

await page.getByRole('button', { name: trigger }).click();
await expect(viewport).toBeVisible();
await page.evaluate(() => Promise.all(document.getAnimations().map((animation) => animation.finished)));

const box = (await viewport.boundingBox())!;
const position = 'left' === edge ? box.x : box.x + box.width;
expect(position).toBe(Math.round(position));
});
}

test('switches to another item at once while one is open', async ({ page, gotoExample }) => {
await gotoExample('shadcn/navigation-menu/default', { timers: 'fake' });
const gettingStarted = page.getByRole('button', { name: 'Getting started' });
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading