fix: keep the language dropdown scrollable within the viewport (#592)

This commit is contained in:
ozil authored and GitHub committed 2026-11-02 13:35:16 +01:00
1 parent 8f94ed3619
commit 324a6be4d0
2 files changed
+91 -1

No files matched your search

+53
View File
@@ -0,0 +1,53 @@
import { expect, test } from '@playwright/test';
for (const viewport of [
{ width: 320, height: 568 },
{ width: 667, height: 375 },
{ width: 1280, height: 720 },
]) {
test.describe(`Language menu at ${viewport.width}x${viewport.height}`, () => {
test.use({ viewport });
test('scrolls to the last language without scrolling the page or dismissing the menu', async ({ page }) => {
await page.goto('/');
if (viewport.width <= 700) {
await page.getByRole('button', { name: 'Toggle menu', exact: true }).click();
}
await page.getByRole('button', { name: 'Language', exact: true }).click();
const menu = page.locator('.n-dropdown-menu');
await expect(menu).toBeVisible();
await menu.evaluate(async (element) => {
await Promise.all(element.getAnimations().map((animation) => animation.finished));
});
const bounds = await menu.boundingBox();
expect(bounds).not.toBeNull();
expect(bounds!.y).toBeGreaterThanOrEqual(0);
expect(bounds!.y + bounds!.height).toBeLessThanOrEqual(viewport.height);
const pageScroll = await page.evaluate(() => window.scrollY);
// Move directly: locator.hover() may scroll the page to align a teleported menu.
await page.mouse.move(bounds!.x + bounds!.width / 2, bounds!.y + bounds!.height / 2);
const lastLanguage = menu.getByText('繁體中文', { exact: true });
await expect(async () => {
await page.mouse.wheel(0, 300);
await expect(lastLanguage).toBeInViewport({ ratio: 1, timeout: 500 });
}).toPass({ timeout: 10_000 });
await expect(menu).toBeVisible();
expect(await page.evaluate(() => window.scrollY)).toBe(pageScroll);
// A second gesture at the boundary must not chain to the page either.
await page.mouse.wheel(0, 500);
await page.evaluate(
() => new Promise<void>((resolve) => requestAnimationFrame(() => requestAnimationFrame(() => resolve()))),
);
await expect(menu).toBeVisible();
expect(await page.evaluate(() => window.scrollY)).toBe(pageScroll);
await lastLanguage.click();
await expect(menu).not.toBeVisible();
await expect(page.getByRole('button', { name: '語言', exact: true })).toBeVisible();
});
});
}
+38 -1
View File
@@ -61,6 +61,23 @@ function onLanguageSelect(key: string) {
locale.value = key;
}
// Keep wheel gestures inside the menu even in engines without overscroll-behavior.
function containLanguageMenuScroll(event: WheelEvent) {
if (event.ctrlKey) {
return;
}
const scroller = (event.currentTarget as HTMLElement).querySelector('.n-scrollbar-container');
if (!scroller) {
return;
}
if (
(event.deltaY < 0 && scroller.scrollTop <= 0) ||
(event.deltaY > 0 && scroller.scrollTop + scroller.clientHeight >= scroller.scrollHeight - 1)
) {
event.preventDefault();
}
}
const themeOptions = computed(() => [
{
key: 'auto',
@@ -120,13 +137,33 @@ function closeMenuOnSmallScreen() {
</c-button>
</n-dropdown>
<n-dropdown :options="languageOptions" trigger="click" @select="onLanguageSelect">
<n-dropdown
class="language-dropdown"
:options="languageOptions"
trigger="click"
scrollable
@select="onLanguageSelect"
@wheel="containLanguageMenuScroll"
>
<c-button circle variant="text" :aria-label="t('home.nav.language', 'Language')">
<n-icon size="25" :component="IconLanguage" />
</c-button>
</n-dropdown>
</template>
<style lang="less">
// The menu is teleported outside this component. Keep it within the viewport,
// with room for its trigger whether the popover opens above or below it.
.language-dropdown {
max-height: min(400px, calc(50vh - 24px));
max-height: min(400px, calc(50dvh - 24px));
.n-scrollbar-container {
overscroll-behavior-y: contain;
}
}
</style>
<style lang="less" scoped>
.n-button {
&:not(:last-child) {