mirror of
https://github.com/sharevb/it-tools.git
synced 2026-11-30 00:50:47 +03:00
fix: keep the language dropdown scrollable within the viewport (#592)
This commit is contained in:
2 files changed
+91
-1
No files matched your search
@@ -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();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -61,6 +61,23 @@ function onLanguageSelect(key: string) {
|
|||||||
locale.value = key;
|
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(() => [
|
const themeOptions = computed(() => [
|
||||||
{
|
{
|
||||||
key: 'auto',
|
key: 'auto',
|
||||||
@@ -120,13 +137,33 @@ function closeMenuOnSmallScreen() {
|
|||||||
</c-button>
|
</c-button>
|
||||||
</n-dropdown>
|
</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')">
|
<c-button circle variant="text" :aria-label="t('home.nav.language', 'Language')">
|
||||||
<n-icon size="25" :component="IconLanguage" />
|
<n-icon size="25" :component="IconLanguage" />
|
||||||
</c-button>
|
</c-button>
|
||||||
</n-dropdown>
|
</n-dropdown>
|
||||||
</template>
|
</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>
|
<style lang="less" scoped>
|
||||||
.n-button {
|
.n-button {
|
||||||
&:not(:last-child) {
|
&:not(:last-child) {
|
||||||
|
|||||||
Reference in new issue
Block a user