Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(TabList): a hugging tab keeps its label's width, so a narrow strip scrolls (#8730)

Merlin08e626f37894
diff --git a/.changeset/tab-keeps-its-width.md b/.changeset/tab-keeps-its-width.md
new file mode 100644
index 0000000000..31d78d1ff3
--- /dev/null
+++ b/.changeset/tab-keeps-its-width.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] In TabList's default `hug` layout, a `Tab` and a `TabMenu` trigger no longer shrink below their labels. On a page whose CSS lets flex items shrink below their content (a `min-width: 0` reset), a narrow strip squeezed its stops until the labels drew over one another, before overflow could scroll. Each hugging stop now keeps its width and the strip scrolls, as its overflow handling intends; `fill` tabs still share the strip.
+
+@light-merlin-dark
diff --git a/packages/core/src/TabList/Tab.tsx b/packages/core/src/TabList/Tab.tsx
index fb1d9a14bc..9b00b1108f 100644
--- a/packages/core/src/TabList/Tab.tsx
+++ b/packages/core/src/TabList/Tab.tsx
@@ -247,6 +247,12 @@ const hoverSizeStyles = stylex.create({
});
const layoutStyles = stylex.create({
+ // A hugging tab keeps its label's width. Without this the strip's flex
+ // shrinks every tab below its text before overflow can scroll, and on a
+ // narrow screen the labels draw over one another.
+ hug: {
+ flexShrink: 0,
+ },
fill: {
flex: 1,
justifyContent: 'center',
@@ -391,7 +397,7 @@ export function Tab({
styles.base,
sizeStyles[size],
isSelected && styles.selected,
- isFill && layoutStyles.fill,
+ isFill ? layoutStyles.fill : layoutStyles.hug,
!isDisabled && tabScope,
// The touch press's strength and release live on the tab the
// controller writes to; the hover layer inherits and paints it.
diff --git a/packages/core/src/TabList/TabMenu.tsx b/packages/core/src/TabList/TabMenu.tsx
index 166a735eae..8241bbc563 100644
--- a/packages/core/src/TabList/TabMenu.tsx
+++ b/packages/core/src/TabList/TabMenu.tsx
@@ -74,6 +74,8 @@ export interface TabMenuProps extends Omit<
const styles = stylex.create({
trigger: {
position: 'relative',
+ // Keeps its label's width in a strip that scrolls, as a hugging tab does.
+ flexShrink: 0,
display: 'inline-flex',
alignItems: 'center',
justifyContent: 'center',
diff --git a/packages/core/src/TabList/__tests__/TabListOverflow.interaction.browser.spec.ts b/packages/core/src/TabList/__tests__/TabListOverflow.interaction.browser.spec.ts
new file mode 100644
index 0000000000..4d877e6da1
--- /dev/null
+++ b/packages/core/src/TabList/__tests__/TabListOverflow.interaction.browser.spec.ts
@@ -0,0 +1,99 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+/**
+ * @file TabListOverflow.interaction.browser.spec.ts
+ * @input The built Overflow and WithMenu TabList stories, in a real browser,
+ * on a page whose reset lets flex items shrink below their content
+ * (`* { min-width: 0 }`)
+ * @output Proof that every stop in an overflowing strip, each hugging tab
+ * and a TabMenu trigger, keeps at least its content's width while the
+ * strip scrolls
+ * @position Layout evidence for Tab's and TabMenu's hug sizing in
+ * /packages/core/src/TabList
+ */
+
+import {expect, test, type Page} from '@playwright/test';
+import {
+ DEFAULT_STORYBOOK_DIR,
+ serveStorybook,
+ type StaticServer,
+} from '@astryxdesign/a11y-spec/storybook';
+
+let storybook: StaticServer;
+
+test.beforeAll(async () => {
+ storybook = await serveStorybook(
+ process.env.ASTRYX_STORYBOOK_DIR ?? DEFAULT_STORYBOOK_DIR,
+ );
+});
+
+test.afterAll(async () => {
+ await storybook?.close();
+});
+
+/** Opens a built story and waits for its tab strip. */
+async function openStory(page: Page, id: string): Promise<void> {
+ const params = new URLSearchParams({id, viewMode: 'story'});
+ await page.goto(`${storybook.origin}/iframe.html?${params}`);
+ await page.locator('.astryx-tab-strip').first().waitFor();
+}
+
+/** A common page reset that lets flex items shrink below their content. */
+async function allowShrinkBelowContent(page: Page): Promise<void> {
+ await page.addStyleTag({content: '* { min-width: 0; }'});
+}
+
+/** Each stop's rendered width against its content's, and the strip's overflow. */
+async function measureStrip(page: Page) {
+ return page.evaluate(() => {
+ const strip = document.querySelector('.astryx-tab-strip');
+ if (strip == null) {
+ throw new Error('no tab strip');
+ }
+ let scroller: Element | null = strip;
+ while (
+ scroller != null &&
+ scroller.scrollWidth <= scroller.clientWidth + 1
+ ) {
+ scroller = scroller.parentElement;
+ }
+ const stops = [
+ ...strip.querySelectorAll<HTMLElement>(
+ 'button:not(.astryx-tab-scroll-button)',
+ ),
+ ].map(stop => ({
+ label: stop.textContent?.trim() ?? '',
+ width: stop.getBoundingClientRect().width,
+ content: stop.scrollWidth,
+ }));
+ return {stops, scrolls: scroller != null};
+ });
+}
+
+test('in a narrow strip every hugging tab keeps its label width and the strip scrolls', async ({
+ page,
+}) => {
+ await page.setViewportSize({width: 362, height: 700});
+ await openStory(page, 'core-tablist--overflow');
+ await allowShrinkBelowContent(page);
+ const {stops, scrolls} = await measureStrip(page);
+ expect(stops.length).toBeGreaterThan(4);
+ for (const stop of stops) {
+ expect(stop.width, stop.label).toBeGreaterThanOrEqual(stop.content - 1);
+ }
+ expect(scrolls).toBe(true);
+});
+
+test('in a narrow strip with a TabMenu, the menu trigger keeps its width too', async ({
+ page,
+}) => {
+ await page.setViewportSize({width: 170, height: 700});
+ await openStory(page, 'core-tablist--with-menu');
+ await allowShrinkBelowContent(page);
+ const {stops} = await measureStrip(page);
+ const trigger = stops.find(stop => stop.label.startsWith('More'));
+ expect(trigger).toBeDefined();
+ for (const stop of stops) {
+ expect(stop.width, stop.label).toBeGreaterThanOrEqual(stop.content - 1);
+ }
+});