Skip to content
8 changes: 8 additions & 0 deletions .changeset/single-line-word-break.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'@astryxdesign/cli': patch
'@astryxdesign/core': patch
---

[feat] Change the single-line Text and Heading truncation default from `break-all` to `break-word`; pass `wordBreak="break-all"` to opt into splitting inside words

@bezabyte
4 changes: 2 additions & 2 deletions apps/storybook/stories/Text.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ export const WordBreakVariants: Story = {
<div style={{display: 'flex', gap: '16px', maxWidth: '600px'}}>
<div style={{flex: 1}}>
<Text type="label" display="block">
break-word (default for multi-line):
break-word (default for truncation):
</Text>
<div style={{width: '150px', border: '1px solid #ccc', padding: '8px'}}>
<Text type="body" maxLines={2} wordBreak="break-word">
Expand All @@ -420,7 +420,7 @@ export const WordBreakVariants: Story = {
</div>
<div style={{flex: 1}}>
<Text type="label" display="block">
break-all (default for single-line):
break-all (explicit opt-in):
</Text>
<div style={{width: '150px', border: '1px solid #ccc', padding: '8px'}}>
<Text type="body" maxLines={2} wordBreak="break-all">
Expand Down
53 changes: 28 additions & 25 deletions docs/specs/components/Heading/Heading.spec.md

Large diffs are not rendered by default.

31 changes: 17 additions & 14 deletions docs/specs/components/Text/Text.spec.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ export const doc = {
description: 'Compares break-word and break-all word break modes on a long unbreakable string.',
isReady: true,
aspectRatio: 16 / 9,
componentsUsed: ['Text'],
componentsUsed: ['Text', 'Card', 'Stack'],
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,29 @@

'use client';

import {Card} from '@astryxdesign/core/Card';
Comment thread
astracat-bot[bot] marked this conversation as resolved.
Resolved
import {VStack} from '@astryxdesign/core/Stack';
import {Text} from '@astryxdesign/core/Text';

export default function TextWordBreak() {
return (
<div
style={{
display: 'flex',
flexDirection: 'column',
gap: 16,
maxWidth: 400,
}}>
<div>
<Text type="label" display="block">
Break-word (default for multi-line)
</Text>
<div
style={{
width: 200,
border: '1px solid var(--color-border)',
padding: 8,
}}>
<VStack gap={4} maxWidth={400}>
<VStack gap={2}>
<Text type="label">Break-word (default for all truncation)</Text>
<Card width={200}>
<Text type="body" maxLines={2} wordBreak="break-word">
This is a verylongunbreakableword for a break-word example
</Text>
</div>
</div>
<div>
<Text type="label" display="block">
Break-all (default for single-line)
</Text>
<div
style={{
width: 200,
border: '1px solid var(--color-border)',
padding: 8,
}}>
</Card>
</VStack>
<VStack gap={2}>
<Text type="label">Break-all (explicit opt-in)</Text>
<Card width={200}>
<Text type="body" maxLines={2} wordBreak="break-all">
Breaks anywhere: abcdefghijklmnopqrstuvwxyz0123456789
</Text>
</div>
</div>
</div>
</Card>
</VStack>
</VStack>
);
}
30 changes: 28 additions & 2 deletions packages/core/src/Heading/Heading.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ describe('Heading long unbroken tokens', () => {
expect(wrapOf(screen.getByText(token))).toBe(DEFAULT_WRAP);
});

it('leaves single-line truncation clipping on one line', () => {
it('keeps single-line truncation on one line by default', () => {
render(
<Heading level={1} maxLines={1}>
{token}
Expand All @@ -331,7 +331,33 @@ describe('Heading long unbroken tokens', () => {
const style = getComputedStyle(screen.getByText(token));
expect(style.whiteSpace).toBe('nowrap');
expect(style.textOverflow).toBe('ellipsis');
expect(style.overflowWrap).not.toBe('break-word');
expect(style.wordBreak).toBe('normal');
expect(style.overflowWrap).toBe('break-word');
});

it('breaks between words when a caller allows the line to wrap', () => {
const sentence =
'Understanding accessibility requirements for design systems';
render(
<Heading level={1} maxLines={1} style={{whiteSpace: 'normal'}}>
{sentence}
</Heading>,
);
const style = getComputedStyle(screen.getByText(sentence));
expect(style.whiteSpace).toBe('normal');
expect(style.wordBreak).toBe('normal');
expect(style.overflowWrap).toBe('break-word');
});

it('splits inside words when a caller passes wordBreak="break-all"', () => {
render(
<Heading level={1} maxLines={1} wordBreak="break-all">
{token}
</Heading>,
);
expect(getComputedStyle(screen.getByText(token)).wordBreak).toBe(
'break-all',
);
});
});

Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/Heading/Heading.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ export interface HeadingProps extends Omit<

/**
* Word break behavior for truncated text.
* @default 'break-all' for maxLines=1, 'break-word' otherwise
* @default 'break-word'
*/
wordBreak?: WordBreak;

Expand Down Expand Up @@ -267,9 +267,9 @@ export function Heading({
? {'aria-level': accessibilityLevel}
: {};

// Resolve wordBreak with smart default
const resolvedWordBreak =
wordBreak ?? (maxLines === 1 ? 'break-all' : 'break-word');
// A single-line clamp does not wrap. If a caller restores wrapping,
// lines break where a multi-line clamp breaks them.
const resolvedWordBreak = wordBreak ?? 'break-word';
Comment thread
astracat-bot[bot] marked this conversation as resolved.
Resolved

// Resolve display - force block when maxLines > 0 or hasCapsize
const resolvedDisplay = maxLines > 0 || hasCapsize ? 'block' : display;
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/Text/Heading.doc.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export const docs = {
name: 'wordBreak',
type: "'break-word' | 'break-all'",
description:
"Word break behavior when truncating. Defaults to 'break-all' for single-line truncation, 'break-word' otherwise. Without truncation, a token longer than its line (a URL, an ID) wraps (`overflow-wrap: break-word`), so in ordinary flow text does not make the page scroll sideways. Inside a flex row, give the flex child `min-width: 0` so it can shrink. A container you style with `overflow-wrap: anywhere` does not reach this text; Astryx's own containers pass their wrap through.",
"Word break behavior when truncating. Defaults to 'break-word' for both single-line and multi-line truncation; pass 'break-all' when splitting inside words is intentional. Without truncation, a token longer than its line (a URL, an ID) wraps (`overflow-wrap: break-word`), so in ordinary flow text does not make the page scroll sideways. Inside a flex row, give the flex child `min-width: 0` so it can shrink. A container you style with `overflow-wrap: anywhere` does not reach this text; Astryx's own containers pass their wrap through.",
Comment thread
astracat-bot[bot] marked this conversation as resolved.
Resolved
},
{
name: 'textWrap',
Expand Down Expand Up @@ -206,7 +206,7 @@ export const docsZh = {
name: 'wordBreak',
type: "'break-word' | 'break-all'",
description:
"截断时的断词行为。单行截断默认为 'break-all',其他情况默认为 'break-word'。不截断时,超过一行宽度的长串(如 URL、ID)会换行(`overflow-wrap: break-word`),因此在普通文档流中文本不会让页面横向滚动。在 flex 行中,请为 flex 子元素设置 `min-width: 0` 以便其收缩。你自己设置了 `overflow-wrap: anywhere` 的容器不会作用于此文本;Astryx 自带的容器会传递其换行方式。",
"截断时的断词行为。单行和多行截断都默认为 'break-word';如果确实要在单词内部断开,请传入 'break-all'。不截断时,超过一行宽度的长串(如 URL、ID)会换行(`overflow-wrap: break-word`),因此在普通文档流中文本不会让页面横向滚动。在 flex 行中,请为 flex 子元素设置 `min-width: 0` 以便其收缩。你自己设置了 `overflow-wrap: anywhere` 的容器不会作用于此文本;Astryx 自带的容器会传递其换行方式。",
},
{
name: 'textWrap',
Expand Down Expand Up @@ -284,7 +284,7 @@ export const docsDense = {
hasTruncateTooltip:
"Tooltip for truncated text; true=default position, false=disabled, or a placement ('above' | 'below' | 'start' | 'end').",
wordBreak:
"Word break behavior when truncating; defaults 'break-all' for single-line, 'break-word' otherwise. Untruncated long tokens wrap (overflow-wrap: break-word); a flex child needs min-width: 0; your own container's overflow-wrap: anywhere does not reach this text (Astryx containers pass theirs through).",
"Word break behavior when truncating; defaults 'break-word' for both single-line and multi-line truncation. Pass 'break-all' when splitting inside words is intentional. Untruncated long tokens wrap (overflow-wrap: break-word); a flex child needs min-width: 0; your own container's overflow-wrap: anywhere does not reach this text (Astryx containers pass theirs through).",
textWrap: 'Text wrapping behavior.',
justify:
"Text alignment (justification); logical values 'start' | 'center' | 'end' for RTL.",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/Text/Text.doc.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export const docs = {
name: 'wordBreak',
type: "'break-word' | 'break-all'",
description:
"Word break behavior when truncating. Defaults to 'break-all' for single-line truncation, 'break-word' otherwise. Without truncation, a token longer than its line (a URL, an ID) wraps (`overflow-wrap: break-word`), so in ordinary flow text does not make the page scroll sideways. Inside a flex row, give the flex child `min-width: 0` so it can shrink. A container you style with `overflow-wrap: anywhere` does not reach this text; Astryx's own containers pass their wrap through.",
"Word break behavior when truncating. Defaults to 'break-word' for both single-line and multi-line truncation; pass 'break-all' when splitting inside words is intentional. Without truncation, a token longer than its line (a URL, an ID) wraps (`overflow-wrap: break-word`), so in ordinary flow text does not make the page scroll sideways. Inside a flex row, give the flex child `min-width: 0` so it can shrink. A container you style with `overflow-wrap: anywhere` does not reach this text; Astryx's own containers pass their wrap through.",
},
{
name: 'textWrap',
Expand Down
31 changes: 28 additions & 3 deletions packages/core/src/Text/Text.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -251,13 +251,38 @@ describe('Text', () => {
expect(wrapOf(screen.getByText(`${token}/block`))).toBe(DEFAULT_WRAP);
});

it('leaves single-line truncation clipping on one line', () => {
it('keeps single-line truncation on one line by default', () => {
render(<Text maxLines={1}>{token}</Text>);
const style = getComputedStyle(screen.getByText(token));
expect(style.whiteSpace).toBe('nowrap');
expect(style.textOverflow).toBe('ellipsis');
expect(style.wordBreak).toBe('break-all');
expect(style.overflowWrap).not.toBe('break-word');
expect(style.wordBreak).toBe('normal');
expect(style.overflowWrap).toBe('break-word');
});

it('breaks between words when a caller allows the line to wrap', () => {
Comment thread
astracat-bot[bot] marked this conversation as resolved.
Resolved
const sentence =
'Quarterly reliability review notes for the platform team';
render(
<Text maxLines={1} style={{whiteSpace: 'normal'}}>
{sentence}
</Text>,
);
const style = getComputedStyle(screen.getByText(sentence));
expect(style.whiteSpace).toBe('normal');
expect(style.wordBreak).toBe('normal');
expect(style.overflowWrap).toBe('break-word');
});

it('splits inside words when a caller passes wordBreak="break-all"', () => {
render(
<Text maxLines={1} wordBreak="break-all">
{token}
</Text>,
);
expect(getComputedStyle(screen.getByText(token)).wordBreak).toBe(
'break-all',
);
});
});

Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/Text/Text.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export interface TextProps extends Omit<BaseProps, 'children'> {

/**
* Word break behavior for truncated text.
* @default 'break-all' for maxLines=1, 'break-word' otherwise
* @default 'break-word'
*/
wordBreak?: WordBreak;

Expand Down Expand Up @@ -258,9 +258,9 @@ export function Text({
// class (see themeProps below).
const styleColor = resolveStyleColor(resolvedColor);

// Resolve wordBreak with smart default
const resolvedWordBreak =
wordBreak ?? (maxLines === 1 ? 'break-all' : 'break-word');
// A single-line clamp does not wrap. If a caller restores wrapping,
// lines break where a multi-line clamp breaks them.
const resolvedWordBreak = wordBreak ?? 'break-word';

// Resolve display - force block when maxLines > 0 or hasCapsize
const resolvedDisplay = maxLines > 0 || hasCapsize ? 'block' : display;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,16 @@ test('single-line truncation still clips the token on one line at 320 px', async
return {
whiteSpace: computed.whiteSpace,
textOverflow: computed.textOverflow,
wordBreak: computed.wordBreak,
overflowWrap: computed.overflowWrap,
};
});
expect(style, id).toEqual({whiteSpace: 'nowrap', textOverflow: 'ellipsis'});
expect(style, id).toEqual({
whiteSpace: 'nowrap',
textOverflow: 'ellipsis',
wordBreak: 'normal',
overflowWrap: 'break-word',
});
expect(await lineCount(target), id).toBe(1);
expect(
await target.evaluate(
Expand Down
Loading