Skip to content

Commit

Permalink
fix: weird align of text when there is no whitespace with `textWrap: …
Browse files Browse the repository at this point in the history
…'balance'`
  • Loading branch information
dimaMachina committed May 30, 2023
1 parent 1c9e463 commit ffc2c9d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ export default async function* buildTextNodes(
// When doing `text-wrap: balance`, we reflow the text multiple times
// using binary search to find the best width.
// https://www.w3.org/TR/css-text-4/#valdef-text-wrap-balance
if (textWrap === 'balance') {
if (textWrap === 'balance' && content.includes(' ')) {
let l = width / 2
let r = width
let m: number = width
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 35 additions & 0 deletions test/text-wrap.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,39 @@ describe('text-wrap', () => {

expect(toImage(svg, 100)).toMatchImageSnapshot()
})

it('should properly align text when there is no whitespace with text-wrap: balance', async () => {
const svg = await satori(
<div
style={{
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
width: 100,
height: 100,
fontSize: 18,
color: 'red',
background: 'green',
}}
>
<span
style={{
background: 'blue',
// @ts-ignore: This isn't a valid CSS property supported by browsers yet.
textWrap: 'balance',
}}
>
Playground
</span>
<span style={{ background: 'yellow' }}>Playground</span>
</div>,
{
width: 100,
height: 100,
fonts,
}
)

expect(toImage(svg, 100)).toMatchImageSnapshot()
})
})

0 comments on commit ffc2c9d

Please sign in to comment.