An open source design system that's fully customizable and agent ready
TypeScript
fix(Button): let labelled buttons shrink and truncate in narrow rows (#6812)
David Petersendb80a3ada543
diff --git a/.changeset/button-shrinks-in-narrow-rows.md b/.changeset/button-shrinks-in-narrow-rows.mdnew file mode 100644index 0000000000..8084d5056f--- /dev/null+++ b/.changeset/button-shrinks-in-narrow-rows.md@@ -0,0 +1,6 @@+---+'@astryxdesign/core': patch+---++[fix] Button no longer overflows narrow rows: a labelled button can shrink below its label width and truncates the label with an ellipsis, while icon-only buttons stay square+@thedjpetersendiff --git a/apps/storybook/stories/Button.stories.tsx b/apps/storybook/stories/Button.stories.tsxindex 9bd22871a8..a9617af6e2 100644--- a/apps/storybook/stories/Button.stories.tsx+++ b/apps/storybook/stories/Button.stories.tsx@@ -1,9 +1,18 @@// Copyright (c) Meta Platforms, Inc. and affiliates.+import {useState} from 'react';import type {Meta, StoryObj} from '@storybook/react';import * as stylex from '@stylexjs/stylex';import {Button} from '@astryxdesign/core/Button';import {Badge} from '@astryxdesign/core/Badge';+import {Card} from '@astryxdesign/core/Card';+import {Heading} from '@astryxdesign/core/Heading';+import {HStack} from '@astryxdesign/core/HStack';+import {Icon} from '@astryxdesign/core/Icon';+import {IconButton} from '@astryxdesign/core/IconButton';+import {StackItem} from '@astryxdesign/core/Stack';+import {TextInput} from '@astryxdesign/core/TextInput';+import {VStack} from '@astryxdesign/core/VStack';import {Cog6ToothIcon, TrashIcon} from '@heroicons/react/24/outline';const buttonStoryStyles = stylex.create({@@ -380,6 +389,78 @@ export const Truncation: Story = {),};+function ReviewersCard() {+ const [emails, setEmails] = useState('');+ return (+ <Card maxWidth={560}>+ <VStack gap={4}>+ <HStack gap={2} vAlign="center">+ <StackItem size="fill">+ <Heading level={3} maxLines={1}>+ Design review+ </Heading>+ </StackItem>+ <Button label="Request review from the design team" size="sm" />+ <IconButton+ label="More review options"+ icon={<Icon icon="moreHorizontal" />}+ variant="ghost"+ size="sm"+ />+ </HStack>+ <TextInput+ label="Reviewer emails"+ placeholder="[email protected]"+ value={emails}+ onChange={setEmails}+ />+ <HStack gap={2} hAlign="end">+ <StackItem size="static">+ <Button label="Cancel" variant="secondary" />+ </StackItem>+ <Button+ label="Send invitations to all 12 reviewers"+ variant="primary"+ />+ </HStack>+ </VStack>+ </Card>+ );+}++/**+ * The Button.doc.mjs "Actions in narrow rows" example at a 320px form width,+ * a 390px phone width, and a roomy 600px width. In the narrow frames the long+ * labels truncate with an ellipsis inside the card instead of overflowing or+ * being clipped; the icon-only button stays square; the roomy frame renders+ * every button at its natural width.+ */+export const NarrowRows: Story = {+ render: () => (+ <div+ style={{+ display: 'flex',+ gap: 24,+ flexWrap: 'wrap',+ alignItems: 'flex-start',+ }}>+ {[320, 390, 600].map(width => (+ <div key={width} style={{width}}>+ <p+ style={{+ fontSize: 12,+ color: 'var(--color-text-secondary)',+ marginBottom: 8,+ }}>+ {width}px+ </p>+ <ReviewersCard />+ </div>+ ))}+ </div>+ ),+};+export const Elevations: Story = {render: () => (<div style={{display: 'flex', gap: 24, alignItems: 'center'}}>diff --git a/packages/core/src/Button/Button.doc.mjs b/packages/core/src/Button/Button.doc.mjsindex 36f5e7a547..2b4612b49f 100644--- a/packages/core/src/Button/Button.doc.mjs+++ b/packages/core/src/Button/Button.doc.mjs@@ -171,7 +171,7 @@ export const docs = {name: 'width',type: 'SizeValue',description:- "Width of the button. Numbers are treated as pixels, strings are used as-is (e.g., '100%' for a full-width button). By default the button sizes to its content.",+ "Width of the button. Numbers are treated as pixels, strings are used as-is (e.g., '100%' for a full-width button). By default the button sizes to its content, never grows past its container, and truncates its label with an ellipsis when a row is too narrow; icon-only buttons stay square.",},{name: 'children',@@ -229,6 +229,65 @@ export const docs = {description: 'HTML rel attribute when rendered as a link (e.g. "noopener noreferrer").',},],+ examples: [+ {+ label: 'Actions in narrow rows',+ code: `+import {useState} from 'react';+import {Button} from '@astryxdesign/core/Button';+import {Card} from '@astryxdesign/core/Card';+import {Heading} from '@astryxdesign/core/Heading';+import {HStack} from '@astryxdesign/core/HStack';+import {Icon} from '@astryxdesign/core/Icon';+import {IconButton} from '@astryxdesign/core/IconButton';+import {StackItem} from '@astryxdesign/core/Stack';+import {TextInput} from '@astryxdesign/core/TextInput';+import {VStack} from '@astryxdesign/core/VStack';++// On a phone (or any card narrower than its actions), a labelled Button+// shrinks with its row and truncates its label with an ellipsis instead of+// overflowing or being clipped by the card. The full label stays the+// accessible name. Icon-only buttons keep their square size.+function ReviewersCard() {+ const [emails, setEmails] = useState('');+ return (+ <Card maxWidth={560}>+ <VStack gap={4}>+ <HStack gap={2} vAlign="center">+ <StackItem size="fill">+ <Heading level={3} maxLines={1}>+ Design review+ </Heading>+ </StackItem>+ <Button label="Request review from the design team" size="sm" />+ <IconButton+ label="More review options"+ icon={<Icon icon="moreHorizontal" />}+ variant="ghost"+ size="sm"+ />+ </HStack>+ <TextInput+ label="Reviewer emails"+ placeholder="[email protected]"+ value={emails}+ onChange={setEmails}+ />+ {/* In a 320px form footer the long primary action truncates.+ StackItem size="static" keeps the short Cancel label whole. */}+ <HStack gap={2} hAlign="end">+ <StackItem size="static">+ <Button label="Cancel" variant="secondary" />+ </StackItem>+ <Button label="Send invitations to all 12 reviewers" variant="primary" />+ </HStack>+ </VStack>+ </Card>+ );+}+`,+ },+ ],playground: {defaults: {label: 'Click me',@@ -295,7 +354,7 @@ export const docsZh = {default: 'false',},{name: 'icon', type: 'ReactNode', description: '图标元素。未显式指定尺寸的 Astryx Icon 在 sm/md 按钮中默认为 sm,在 lg 按钮中默认为 md。仅提供 icon 而不提供 children 时,按钮渲染为正方形的纯图标按钮。'},- {name: 'width', type: 'SizeValue', description: "按钮宽度。数字按像素处理,字符串按原样使用(如 '100%' 表示全宽按钮)。默认按内容自适应宽度。"},+ {name: 'width', type: 'SizeValue', description: "按钮宽度。数字按像素处理,字符串按原样使用(如 '100%' 表示全宽按钮)。默认按内容自适应宽度,不会超出容器;行宽不足时标签以省略号截断;纯图标按钮保持正方形。"},{name: 'children', type: 'ReactNode', description: '可选的可见内容覆盖;label 仍然是必需的(用于无障碍名称)。大多数情况使用 <Button label="Save" />。'},{name: 'endContent',@@ -363,7 +422,7 @@ export const docsDense = {isLoading: 'shows spinner+disables interaction; announces via live region',icon: 'icon element rendered before label text; unsized Astryx Icon defaults to sm for sm/md buttons and md for lg',isIconOnly: 'when true, renders square icon-only button; label becomes aria-label',- width: "Width of button. Numbers=pixels, strings=as-is (e.g. '100%' for full-width).",+ width: "Width of button. Numbers=pixels, strings=as-is (e.g. '100%' for full-width). Default: content width, capped at the container; label truncates w/ ellipsis in a narrow row; icon-only stays square.",children: 'optional visible override; label is still required for a11y. Prefer <Button label="Save" /> over using children',endContent: 'trailing icon/badge after label; ignored when isIconOnly; color inherited',tooltip: 'tooltip on hover',diff --git a/packages/core/src/Button/Button.test.tsx b/packages/core/src/Button/Button.test.tsxindex a334c8a57e..f52c7eafb9 100644--- a/packages/core/src/Button/Button.test.tsx+++ b/packages/core/src/Button/Button.test.tsx@@ -13,9 +13,16 @@ import {describe, it, expect, vi} from 'vitest';import {render, screen, fireEvent, act} from '@testing-library/react';import userEvent from '@testing-library/user-event';import {Button} from './Button';+import * as stylex from '@stylexjs/stylex';import {Badge} from '../Badge/Badge';+import {ButtonGroup} from '../ButtonGroup';+import {IconButton} from '../IconButton';import {InternationalizationProvider} from '../i18n';+const narrowRowProbe = stylex.create({+ rigid: {flexShrink: 0, minWidth: 'auto'},+});+describe('Button', () => {// Retained, narrowed: the shared contract proves the ROLE and the accessible// NAME in a real engine (button.role.exposed, button.name.exposed), which is@@ -493,6 +500,83 @@ describe('Button', () => {expect(liveRegion).toHaveTextContent('Chargement');});+ describe('in a narrow row', () => {+ // jsdom has no layout, so these assert the computed declarations that+ // produce the behavior. The real-browser widths are in the PR evidence.+ it('lets a labelled button shrink so its label truncates', () => {+ render(<Button label="A very long button label that should truncate" />);+ const style = getComputedStyle(screen.getByRole('button'));+ expect(style.minWidth).toBe('0');+ expect(style.maxWidth).toBe('100%');+ const label = screen.getByText(+ 'A very long button label that should truncate',+ );+ expect(getComputedStyle(label).textOverflow).toBe('ellipsis');+ expect(getComputedStyle(label).overflow).toBe('hidden');+ });++ it('keeps the default flex-shrink so a row can shrink the button', () => {+ render(<Button label="Save" icon={<span>+</span>} />);+ const style = getComputedStyle(screen.getByRole('button'));+ // No opt-out: a labelled button (with or without an icon) shrinks.+ expect(style.flexShrink).not.toBe('0');+ });++ it('keeps an icon-only button square instead of shrinking it', () => {+ render(<Button label="Settings" icon={<span>⚙</span>} isIconOnly />);+ const style = getComputedStyle(screen.getByRole('button'));+ expect(style.flexShrink).toBe('0');+ expect(style.maxWidth).toBe('none');+ expect(style.aspectRatio).toBe('var(--button-icon-only-aspect)');+ });++ it('keeps IconButton square too, since it renders the icon-only mode', () => {+ render(<IconButton label="More actions" icon={<span>⋯</span>} />);+ const style = getComputedStyle(+ screen.getByRole('button', {name: 'More actions'}),+ );+ expect(style.flexShrink).toBe('0');+ expect(style.maxWidth).toBe('none');+ });++ it('caps an explicit width at the container instead of dropping it', () => {+ render(<Button label="Sign in" width={240} />);+ const button = screen.getByRole('button');+ expect(button.getAttribute('style')).toContain('240');+ expect(getComputedStyle(button).maxWidth).toBe('100%');+ });++ it('applies the same cap in link mode', () => {+ render(<Button label="Read the full release notes" href="#notes" />);+ const style = getComputedStyle(+ screen.getByRole('link', {name: 'Read the full release notes'}),+ );+ expect(style.minWidth).toBe('0');+ expect(style.maxWidth).toBe('100%');+ });++ it('shrinks inside a ButtonGroup like a standalone button', () => {+ render(+ <ButtonGroup label="Draft actions">+ <Button label="Save changes to draft" />+ <Button label="Discard" />+ </ButtonGroup>,+ );+ const style = getComputedStyle(+ screen.getByRole('button', {name: 'Save changes to draft'}),+ );+ expect(style.minWidth).toBe('0');+ expect(style.maxWidth).toBe('100%');+ });++ it('lets xstyle restore a rigid button', () => {+ render(<Button label="Cancel" xstyle={narrowRowProbe.rigid} />);+ const style = getComputedStyle(screen.getByRole('button'));+ expect(style.flexShrink).toBe('0');+ expect(style.minWidth).toBe('auto');+ });+ });+describe('elevation', () => {it('reflects each elevation level as a theme attribute', () => {const attrFor = (elevation: 'none' | 'low' | 'med' | 'high') => {diff --git a/packages/core/src/Button/Button.tsx b/packages/core/src/Button/Button.tsxindex 357956b55c..33c53a07dd 100644--- a/packages/core/src/Button/Button.tsx+++ b/packages/core/src/Button/Button.tsx@@ -86,6 +86,13 @@ const styles = stylex.create({lineHeight: typeScaleVars['--text-label-leading'],fontWeight: fontWeightVars['--font-weight-medium'],whiteSpace: 'nowrap',+ // One line by construction, so a label wider than the space available+ // has to truncate. As a flex item the automatic minimum size would hold+ // the button at its full label width (and `nowrap` lets an inline button+ // run past its container), so the label's ellipsis never engaged. These+ // let a row shrink the button and cap it at its container.+ minWidth: 0,+ maxWidth: '100%',cursor: {default: 'pointer',':is(:disabled,[aria-disabled="true"])': 'default',@@ -129,6 +136,10 @@ const styles = stylex.create({aspectRatio: 'var(--button-icon-only-aspect)',paddingInline: 0,paddingBlock: 0,+ // An icon-only button has no label to truncate: keep it square rather+ // than letting a crowded row squeeze it.+ flexShrink: 0,+ maxWidth: 'none',},endContentWrapper: {display: 'inline-flex',