Skip to content

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

TypeScript

fix(Button): prevent text wrap and add ellipsis truncation (#892)

Cindy Zhang43dde514cb76
diff --git a/apps/storybook/stories/Button.stories.tsx b/apps/storybook/stories/Button.stories.tsx
index 55b84f402f..7e9c7953d0 100644
--- a/apps/storybook/stories/Button.stories.tsx
+++ b/apps/storybook/stories/Button.stories.tsx
@@ -240,3 +240,52 @@ export const AllVariants: Story = {
</div>
),
};
+
+/**
+ * Demonstrates button text truncation in constrained containers.
+ * When a button's container is narrower than the button's natural width,
+ * the label truncates with an ellipsis instead of wrapping to multiple lines.
+ */
+export const Truncation: Story = {
+ render: () => (
+ <div style={{display: 'flex', flexDirection: 'column', gap: '16px'}}>
+ <div>
+ <p style={{fontSize: 12, color: '#666', marginBottom: 8}}>
+ 200px container — label truncates with ellipsis
+ </p>
+ <div style={{width: 200, border: '1px dashed #ccc', padding: 4}}>
+ <XDSButton
+ label="A very long button label that overflows"
+ variant="primary"
+ icon={<Cog6ToothIcon style={{width: 16, height: 16}} />}
+ />
+ </div>
+ </div>
+ <div>
+ <p style={{fontSize: 12, color: '#666', marginBottom: 8}}>
+ Flex row with limited space — button shrinks gracefully
+ </p>
+ <div style={{display: 'flex', gap: 8, maxWidth: 320}}>
+ <div style={{flex: 1, minWidth: 0}}>
+ <XDSButton
+ label="Submit this extremely long form action"
+ variant="primary"
+ xstyle={{width: '100%'}}
+ />
+ </div>
+ <XDSButton label="Cancel" variant="secondary" />
+ </div>
+ </div>
+ <div>
+ <p style={{fontSize: 12, color: '#666', marginBottom: 8}}>
+ Unconstrained — renders at natural width
+ </p>
+ <XDSButton
+ label="A very long button label that shows fully"
+ variant="primary"
+ icon={<Cog6ToothIcon style={{width: 16, height: 16}} />}
+ />
+ </div>
+ </div>
+ ),
+};
diff --git a/packages/core/src/Button/XDSButton.tsx b/packages/core/src/Button/XDSButton.tsx
index a2584400a2..cd0207c70c 100644
--- a/packages/core/src/Button/XDSButton.tsx
+++ b/packages/core/src/Button/XDSButton.tsx
@@ -58,6 +58,7 @@ const styles = stylex.create({
fontSize: typeScaleVars['--text-label-size'],
lineHeight: lineHeightVars['--leading-base'],
fontWeight: fontWeightVars['--font-weight-medium'],
+ whiteSpace: 'nowrap',
cursor: 'pointer',
transitionProperty: 'background-image, transform',
transitionDuration: {
@@ -101,6 +102,11 @@ const styles = stylex.create({
contentWrapper: {
display: 'contents',
},
+ labelText: {
+ overflow: 'hidden',
+ textOverflow: 'ellipsis',
+ minWidth: 0,
+ },
visuallyHidden: {
position: 'absolute',
width: '1px',
@@ -489,7 +495,9 @@ export function XDSButton({
{...stylex.props(styles.contentWrapper)}
aria-hidden={isLoadingState || undefined}>
{icon}
- {children ?? (isIconOnly ? null : label)}
+ {isIconOnly ? null : (
+ <span {...stylex.props(styles.labelText)}>{children ?? label}</span>
+ )}
{!isIconOnly && endSlot && (
<span {...stylex.props(styles.endSlotWrapper)}>{endSlot}</span>
)}