Skip to content

Commit ab98851

Browse files
evancharltonnecolas
authored andcommitted
[fix] Text preserves white-space if numberOfLines={1}
Preserve meaningful white-space when `numberOfLines={1}` by using the `pre` value instead of `nowrap`. Source: https://developer.mozilla.org/en-US/docs/Web/CSS/white-space Close necolas#2058
1 parent d48f630 commit ab98851

File tree

4 files changed

+29
-5
lines changed

4 files changed

+29
-5
lines changed

packages/examples/pages/text/index.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -370,17 +370,30 @@ export default function TextPage() {
370370
</Text>
371371

372372
<View style={{ maxWidth: 320 }}>
373-
<Text numberOfLines={1}>
373+
<View style={{ borderColor: '#cecece', borderWidth: 1, marginVertical: 20 }}>
374+
<Text style={{ fontWeight: 700 }}>The next two lines should look identical:</Text>
375+
<View style={{ display: 'flex', flexDirection: 'row' }}>
376+
<Text numberOfLines={1}>Spaces </Text>
377+
<Text numberOfLines={1}>between</Text>
378+
<Text numberOfLines={1}> words</Text>
379+
</View>
380+
<View style={{ display: 'flex', flexDirection: 'row' }}>
381+
<Text>Spaces </Text>
382+
<Text>between</Text>
383+
<Text> words</Text>
384+
</View>
385+
</View>
386+
<Text numberOfLines={1} style={{ marginBottom: 20 }}>
374387
Maximum of one line, no matter how much I write here. If I keep writing, it
375388
{"'"}
376389
ll just truncate after one line.
377390
</Text>
378-
<Text numberOfLines={2} style={{ marginTop: 20 }}>
391+
<Text numberOfLines={2} style={{ marginBottom: 20 }}>
379392
Maximum of two lines, no matter how much I write here. If I keep writing, it
380393
{"'"}
381394
ll just truncate after two lines.
382395
</Text>
383-
<Text style={{ marginTop: 20 }}>
396+
<Text style={{ marginBottom: 20 }}>
384397
No maximum lines specified, no matter how much I write here. If I keep writing, it
385398
{"'"}
386399
ll just keep going and going.

packages/react-native-web/src/exports/Text/__tests__/__snapshots__/index-test.js.snap

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,13 @@ exports[`components/Text prop "numberOfLines" value is set 1`] = `
157157
/>
158158
`;
159159

160+
exports[`components/Text prop "numberOfLines" value is set to one 1`] = `
161+
<div
162+
class="css-text-901oao css-textOneLine-vcwn7f"
163+
dir="auto"
164+
/>
165+
`;
166+
160167
exports[`components/Text prop "selectable" value of false 1`] = `
161168
<div
162169
class="css-text-901oao r-userSelect-lrvibr"

packages/react-native-web/src/exports/Text/__tests__/index-test.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,11 @@ describe('components/Text', () => {
129129

130130
describe('prop "numberOfLines"', () => {
131131
test('value is set', () => {
132-
const { container } = render(<Text numberOfLines="3" />);
132+
const { container } = render(<Text numberOfLines={3} />);
133+
expect(container.firstChild).toMatchSnapshot();
134+
});
135+
test('value is set to one', () => {
136+
const { container } = render(<Text numberOfLines={1} />);
133137
expect(container.firstChild).toMatchSnapshot();
134138
});
135139
});

packages/react-native-web/src/exports/Text/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ const classes = css.create({
178178
maxWidth: '100%',
179179
overflow: 'hidden',
180180
textOverflow: 'ellipsis',
181-
whiteSpace: 'nowrap'
181+
whiteSpace: 'pre'
182182
},
183183
// See #13
184184
textMultiLine: {

0 commit comments

Comments
 (0)