AI-native design editor. Open-source Figma alternative.
fix(app): keep the new-tab button and active tab in view when tabs overflow (#998)
Danila Poyarkov45c066ce107f
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 6192e794a..e0e4f06bb 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -134,6 +134,7 @@### Fixed+- Keep the new-tab button and the active tab in view when many documents are open. Tabs shrink before the tab bar scrolls, the clipped side fades with a chevron that scrolls to hidden tabs, and the new-tab button is a compact button beside the last tab instead of scrolling away with the tabs.- Show when the browser menubar has more menus than fit in the left panel. The clipped side fades out and a chevron scrolls to the hidden menus; before, Arrange and Text could be cut off with no sign the bar scrolls.- List storage documents as soon as storage settings are saved. The home tab kept asking to configure storage until it next refreshed, which happened on window focus or up to a minute later.- Keep a Figma file's exposed nested instances when it is saved from OpenPencil; their properties stopped showing on the outer component's instances in Figma.diff --git a/src/components/TabBar.vue b/src/components/TabBar.vueindex 09aa8b198..b4df2617b 100644--- a/src/components/TabBar.vue+++ b/src/components/TabBar.vue@@ -1,13 +1,14 @@<script setup lang="ts">import { TabsList, TabsRoot, TabsTrigger } from 'reka-ui'import { tv } from 'tailwind-variants'-import { computed } from 'vue'+import { computed, useTemplateRef, watch } from 'vue'import { useI18n } from '@open-pencil/vue'+import { animationsEnabled } from '@/app/shell/motion'import { useTabsStore, createHomeTab } from '@/app/tabs'import PreparationIndicator from '@/components/preparation/tab/Indicator.vue'-import IconButton from '@/components/ui/button/IconButton.vue'+import { useScrollOverflow } from '@/components/shell/useScrollOverflow'import Tip from '@/components/ui/overlay/Tip.vue'import tabBarTheme from '@/theme/tab-bar'@@ -17,6 +18,24 @@ const { tabs, activeTabId, switchTab, closeTab } = useTabsStore()const tabBarStyles = tv(tabBarTheme)const baseStyles = tabBarStyles()+const scroller = useTemplateRef<HTMLElement>('scroller')+const { overflowing, overflowStart, overflowEnd, scrollTowardHidden } = useScrollOverflow(scroller)++// Keep the active tab in view when it opens, when another one is chosen, and when the row starts+// or stops overflowing, since the chevron that appears then narrows the visible row. Scrolling+// alone never triggers it.+watch(+ [activeTabId, overflowing],+ () => {+ scroller.value?.querySelector('[data-slot="tab-item"][data-active]')?.scrollIntoView({+ block: 'nearest',+ inline: 'nearest',+ behavior: animationsEnabled.value ? 'smooth' : 'auto'+ })+ },+ { flush: 'post' }+)+const modelValue = computed({get: () => activeTabId.value,set: (id: string) => switchTab(id)@@ -45,52 +64,85 @@ function onClose(e: MouseEvent, tabId: string) {<TabsRootv-if="tabs.length > 0"v-model="modelValue"+ data-slot="tab-bar"activation-mode="automatic":class="baseStyles.root()">- <TabsList :class="baseStyles.list()">- <div- v-for="tab in tabs"- :key="tab.id"- data-slot="tab-item"- :class="tabBarStyles({ active: tab.isActive }).item()"- @mousedown="onMiddleClick($event, tab.id, tab.isHome)"- >- <TabsTrigger- :value="tab.id"- data-test-id="tabbar-tab"- :class="tabBarStyles({ active: tab.isActive }).trigger()"+ <div+ ref="scroller"+ :class="baseStyles.scroller()"+ :data-overflow-start="overflowStart || undefined"+ :data-overflow-end="overflowEnd || undefined"+ >+ <TabsList :class="baseStyles.list()">+ <div+ v-for="tab in tabs"+ :key="tab.id"+ data-slot="tab-item":data-active="tab.isActive || undefined"- :data-dirty="tab.isDirty || undefined"- >- <icon-lucide-house v-if="tab.isHome" :class="baseStyles.icon()" />- <PreparationIndicator v-else-if="tab.isPreparing" :progress="tab.preparationProgress" />- <icon-lucide-file v-else :class="baseStyles.icon()" />- <span :class="baseStyles.label()">{{ tab.isHome ? files.newTab : tab.name }}</span>- <Tip v-if="tab.isDirty" :label="files.unsavedChanges">- <span role="img" :aria-label="files.unsavedChanges" :class="baseStyles.dirtyDot()" />- </Tip>- </TabsTrigger>- <Tip- v-if="!tab.isHome || tabs.length > 1"- :label="files.closeTab({ name: tab.isHome ? files.newTab : tab.name })"+ :class="tabBarStyles({ active: tab.isActive }).item()"+ @mousedown="onMiddleClick($event, tab.id, tab.isHome)">- <button- type="button"- data-test-id="tabbar-close"- :class="tabBarStyles({ active: tab.isActive }).close()"+ <TabsTrigger+ :value="tab.id"+ data-test-id="tabbar-tab"+ :class="tabBarStyles({ active: tab.isActive }).trigger()":data-active="tab.isActive || undefined"- :aria-label="files.closeTab({ name: tab.isHome ? files.newTab : tab.name })"- tabindex="-1"- @click="onClose($event, tab.id)"+ :data-dirty="tab.isDirty || undefined"+ >+ <icon-lucide-house v-if="tab.isHome" :class="baseStyles.icon()" />+ <PreparationIndicator v-else-if="tab.isPreparing" :progress="tab.preparationProgress" />+ <icon-lucide-file v-else :class="baseStyles.icon()" />+ <span :class="baseStyles.label()">{{ tab.isHome ? files.newTab : tab.name }}</span>+ <Tip v-if="tab.isDirty" :label="files.unsavedChanges" side="bottom">+ <span role="img" :aria-label="files.unsavedChanges" :class="baseStyles.dirtyDot()" />+ </Tip>+ </TabsTrigger>+ <Tip+ v-if="!tab.isHome || tabs.length > 1"+ :label="files.closeTab({ name: tab.isHome ? files.newTab : tab.name })"+ side="bottom">- <icon-lucide-x :class="baseStyles.closeIcon()" />- </button>- </Tip>- </div>- </TabsList>- <IconButton :label="files.newTab" size="md" data-test-id="tabbar-new" @click="createNewTab">- <icon-lucide-plus :class="baseStyles.newIcon()" />- </IconButton>+ <button+ type="button"+ data-test-id="tabbar-close"+ :class="tabBarStyles({ active: tab.isActive }).close()"+ :data-active="tab.isActive || undefined"+ :aria-label="files.closeTab({ name: tab.isHome ? files.newTab : tab.name })"+ tabindex="-1"+ @click="onClose($event, tab.id)"+ >+ <icon-lucide-x :class="baseStyles.closeIcon()" />+ </button>+ </Tip>+ </div>+ </TabsList>+ </div>+ <!-- Sits beside the hidden tabs; it keeps its width when it switches sides, so the overflow never flips back.+ Pointer affordance only: arrow keys move between tabs and the active tab scrolls into view. -->+ <button+ v-if="overflowStart || overflowEnd"+ type="button"+ tabindex="-1"+ aria-hidden="true"+ data-test-id="tabbar-scroll"+ :class="baseStyles.scroll()"+ :data-start="!overflowEnd || undefined"+ @click="scrollTowardHidden"+ >+ <icon-lucide-chevron-right v-if="overflowEnd" :class="baseStyles.scrollIcon()" />+ <icon-lucide-chevron-left v-else :class="baseStyles.scrollIcon()" />+ </button>+ <Tip :label="files.newTab" side="bottom">+ <button+ type="button"+ data-test-id="tabbar-new"+ :aria-label="files.newTab"+ :class="baseStyles.newTab()"+ @click="createNewTab"+ >+ <icon-lucide-plus :class="baseStyles.newIcon()" />+ </button>+ </Tip></TabsRoot></template>diff --git a/src/components/navigation/examples/States.vue b/src/components/navigation/examples/States.vueindex 157e8b4ed..19b26b8e9 100644--- a/src/components/navigation/examples/States.vue+++ b/src/components/navigation/examples/States.vue@@ -88,6 +88,9 @@ const pageStates = [</span></button></div>+ <button aria-label="New tab" :class="tabStyles().newTab()">+ <icon-lucide-plus :class="tabStyles().newIcon()" />+ </button></div></section></div>diff --git a/src/components/shell/AppMenu.vue b/src/components/shell/AppMenu.vueindex 9f38a69b1..8ff481731 100644--- a/src/components/shell/AppMenu.vue+++ b/src/components/shell/AppMenu.vue@@ -1,5 +1,5 @@<script setup lang="ts">-import { templateRef, useResizeObserver, useScroll } from '@vueuse/core'+import { templateRef } from '@vueuse/core'import {MenubarCheckboxItem,MenubarContent,@@ -14,7 +14,7 @@ import {MenubarSubTrigger,MenubarTrigger} from 'reka-ui'-import { computed, useTemplateRef, watch } from 'vue'+import { useTemplateRef, watch } from 'vue'import IconChevronRight from '~icons/lucide/chevron-right'import { vTestId, useI18n } from '@open-pencil/vue'@@ -36,9 +36,9 @@ import {updateMenuChecked} from '@/app/shell/menu/entry'import { appMenuShortcutLabel } from '@/app/shell/menu/shortcut'-import { animationsEnabled } from '@/app/shell/motion'import { resolvedAppTheme } from '@/app/shell/theme'import BrandMark from '@/components/brand/BrandMark.vue'+import { useScrollOverflow } from '@/components/shell/useScrollOverflow'import IconButton from '@/components/ui/button/IconButton.vue'import AppShortcutText from '@/components/ui/menu/AppShortcutText.vue'import { useMenuUI } from '@/components/ui/menu/menu'@@ -62,24 +62,7 @@ const subMenuCls = useMenuUI({ content: 'min-w-44' })// A narrow panel clips the menubar; fade the clipped side and offer a chevron so it reads as scrollable.const menubar = useTemplateRef<HTMLElement>('menubar')-const { arrivedState, measure } = useScroll(menubar)-// Labels change width with the language, so watch the menus as well as the space they get.-useResizeObserver(menubar, measure)-useResizeObserver(() => {- const content = menubar.value?.firstElementChild- return content instanceof HTMLElement ? content : null-}, measure)-const overflowStart = computed(() => !arrivedState.left)-const overflowEnd = computed(() => !arrivedState.right)--function scrollMenubar() {- const element = menubar.value- if (!element) return- element.scrollBy({- left: overflowEnd.value ? element.clientWidth : -element.scrollWidth,- behavior: animationsEnabled.value ? 'smooth' : 'auto'- })-}+const { overflowStart, overflowEnd, scrollTowardHidden } = useScrollOverflow(menubar)</script><template>@@ -218,7 +201,7 @@ function scrollMenubar() {data-test-id="app-menubar-scroll"class="flex h-6 w-5 shrink-0 cursor-pointer items-center justify-center rounded text-muted hover:bg-hover hover:text-surface data-start:order-first":data-start="!overflowEnd || undefined"- @click="scrollMenubar"+ @click="scrollTowardHidden"><icon-lucide-chevron-right v-if="overflowEnd" class="size-3.5" /><icon-lucide-chevron-left v-else class="size-3.5" />diff --git a/src/components/shell/useScrollOverflow.ts b/src/components/shell/useScrollOverflow.tsnew file mode 100644index 000000000..92ffbd99a--- /dev/null+++ b/src/components/shell/useScrollOverflow.ts@@ -0,0 +1,43 @@+import { useMutationObserver, useResizeObserver, useScroll } from '@vueuse/core'+import { computed, ref, type ShallowRef } from 'vue'++import { animationsEnabled } from '@/app/shell/motion'++/**+ * Tracks which ends of a horizontally scrolling row hide content, for an edge fade and a chevron+ * that scrolls toward the hidden side. The row's first child is watched as well as the row, and+ * its items are watched as they come and go, so content that changes width without resizing a+ * box (another language, a tab opened or closed while the row overflows) is remeasured.+ */+export function useScrollOverflow(scroller: Readonly<ShallowRef<HTMLElement | null>>) {+ const { arrivedState, measure } = useScroll(scroller)+ /** Whether the row is wider than its box, from sizes alone, so scrolling never changes it. */+ const overflowing = ref(false)+ function remeasure() {+ measure()+ const element = scroller.value+ overflowing.value = !!element && element.scrollWidth > element.clientWidth+ }+ useResizeObserver(scroller, remeasure)+ const content = () => {+ const element = scroller.value?.firstElementChild+ return element instanceof HTMLElement ? element : null+ }+ useResizeObserver(content, remeasure)+ useMutationObserver(content, remeasure, { childList: true })++ const overflowStart = computed(() => !arrivedState.left)+ const overflowEnd = computed(() => !arrivedState.right)++ /** Scroll a page toward the hidden end, or back to the start once the end is reached. */+ function scrollTowardHidden() {+ const element = scroller.value+ if (!element) return+ element.scrollBy({+ left: overflowEnd.value ? element.clientWidth : -element.scrollWidth,+ behavior: animationsEnabled.value ? 'smooth' : 'auto'+ })+ }++ return { overflowing, overflowStart, overflowEnd, scrollTowardHidden }+}diff --git a/src/theme/tab-bar.ts b/src/theme/tab-bar.tsindex d2354ac1c..4d358c9d8 100644--- a/src/theme/tab-bar.ts+++ b/src/theme/tab-bar.ts@@ -1,8 +1,11 @@const tabBarTheme = {slots: {- root: 'scrollbar-none flex h-9 shrink-0 items-end overflow-x-auto border-b border-border bg-canvas',+ root: 'flex h-9 shrink-0 items-end border-b border-border bg-canvas',+ // Sized by its tabs until the bar runs out of room; then it scrolls and fades the clipped side.+ scroller:+ 'scrollbar-none h-full min-w-0 scroll-px-8 overflow-x-auto data-overflow-end:mask-r-from-[calc(100%-2rem)] data-overflow-start:mask-l-from-[calc(100%-2rem)]',list: 'flex h-full items-end',- item: 'group/tab flex h-full max-w-48 min-w-0 items-center border-r border-border pr-3',+ item: 'group/tab flex h-full max-w-48 min-w-24 items-center border-r border-border pr-3',trigger:'flex h-full min-w-0 cursor-pointer touch-manipulation items-center gap-1.5 px-3 text-[11px] transition-colors outline-none select-none focus-visible:ring-1 focus-visible:ring-panel-focus',icon: 'size-3 shrink-0 opacity-50',@@ -11,7 +14,12 @@ const tabBarTheme = {'flex size-6 shrink-0 cursor-pointer touch-manipulation items-center justify-center rounded transition-opacity group-hover/tab:opacity-100 hover:bg-hover focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-panel-focus sm:size-4',closeIcon: 'size-3',dirtyDot: 'size-1.5 shrink-0 rounded-full bg-accent',- newIcon: 'size-3.5'+ newTab:+ 'flex size-6 shrink-0 cursor-pointer touch-manipulation items-center justify-center self-center rounded text-muted transition-colors outline-none hover:bg-hover hover:text-surface focus-visible:ring-1 focus-visible:ring-panel-focus ms-1.75',+ newIcon: 'size-4',+ scroll:+ 'flex size-6 shrink-0 cursor-pointer touch-manipulation items-center justify-center self-center rounded text-muted transition-colors outline-none hover:bg-hover hover:text-surface focus-visible:ring-1 focus-visible:ring-panel-focus mx-0.5 data-start:order-first',+ scrollIcon: 'size-3.5'},variants: {active: {diff --git a/tests/e2e/app/tab-close.spec.ts b/tests/e2e/app/tabs/close.spec.tssimilarity index 100%rename from tests/e2e/app/tab-close.spec.tsrename to tests/e2e/app/tabs/close.spec.tsdiff --git a/tests/e2e/app/tabs/overflow.spec.ts b/tests/e2e/app/tabs/overflow.spec.tsnew file mode 100644index 000000000..ca2daaec9--- /dev/null+++ b/tests/e2e/app/tabs/overflow.spec.ts@@ -0,0 +1,69 @@+import { expect, test, type Page } from '@playwright/test'++import { CanvasHelper } from '#tests/helpers/canvas'++const TAB_COUNT = 20++/** Horizontal position of the tab row and whether the active tab and new-tab button are fully shown. */+function tabRowState(page: Page) {+ return page.evaluate(() => {+ const tabBar = document.querySelector('[data-slot="tab-bar"]')+ const scroller = tabBar?.querySelector('[role="tablist"]')?.parentElement+ const active = tabBar?.querySelector('[data-slot="tab-item"][data-active]')+ const newTab = tabBar?.querySelector('[data-test-id="tabbar-new"]')+ if (!scroller || !active || !newTab) throw new Error('Tab bar is missing')+ const row = scroller.getBoundingClientRect()+ const tab = active.getBoundingClientRect()+ const button = newTab.getBoundingClientRect()+ return {+ scrollLeft: Math.round(scroller.scrollLeft),+ maxScrollLeft: scroller.scrollWidth - scroller.clientWidth,+ activeVisible: tab.left >= row.left - 1 && tab.right <= row.right + 1,+ newTabVisible: button.right <= window.innerWidth+ }+ })+}++/** Scroll the tab row to its start and report where it is two frames later. */+function scrollTabsToStart(page: Page) {+ return page.evaluate(async () => {+ const scroller = document+ .querySelector('[data-slot="tab-bar"]')+ ?.querySelector('[role="tablist"]')?.parentElement+ if (!scroller) throw new Error('Tab bar is missing')+ scroller.scrollLeft = 0+ for (let frame = 0; frame < 2; frame++) {+ await new Promise<void>((resolve) => {+ requestAnimationFrame(() => resolve())+ })+ }+ return Math.round(scroller.scrollLeft)+ })+}++test('overflowing tabs keep the new-tab button and active tab in view', async ({ page }) => {+ await page.emulateMedia({ reducedMotion: 'reduce' })+ await page.goto('/?test')+ await new CanvasHelper(page).waitForInit()+ const newTab = page.getByTestId('tabbar-new')+ for (let count = 1; count < TAB_COUNT; count++) await newTab.click()+ await expect(page.getByTestId('tabbar-tab')).toHaveCount(TAB_COUNT)++ await expect+ .poll(() => tabRowState(page))+ .toMatchObject({+ activeVisible: true,+ newTabVisible: true+ })+ const scrolled = await tabRowState(page)+ expect(scrolled.maxScrollLeft).toBeGreaterThan(0)++ // Reaching the first tab must not pull the row back to the active tab.+ expect(await scrollTabsToStart(page)).toBe(0)++ // At the start the chevron points to the hidden tabs on the right and scrolls there.+ const chevron = page.getByTestId('tabbar-scroll')+ await expect(chevron).not.toHaveAttribute('data-start')+ await chevron.click()+ await expect.poll(async () => (await tabRowState(page)).scrollLeft).toBeGreaterThan(0)+})diff --git a/tests/e2e/app/tab-preparation.spec.ts b/tests/e2e/app/tabs/preparation.spec.tssimilarity index 100%rename from tests/e2e/app/tab-preparation.spec.tsrename to tests/e2e/app/tabs/preparation.spec.ts