Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Material-UI Paper component does not recognize new line character #9189

Closed
alexanderwhatley opened this issue Nov 17, 2017 · 6 comments
Closed
Labels
support: question Community support but can be turned into an improvement

Comments

@alexanderwhatley
Copy link

alexanderwhatley commented Nov 17, 2017

I am passing to the Material-UI Paper component a string that contains a new line character. However, the Paper component seems to be ignoring it, as it is not starting a new line. Is this behavior intentional, or is this a bug. The code in question is below (comment.text is the string in question):

<Paper key={index} rounded={false} style={styles.paper}> 
<div>{comment.text}</div> 
<div style={{textAlign: 'right', color: grey500}}>{comment.user + ", " + comment.date}</div> 
</Paper>
@m2mathew
Copy link
Member

m2mathew commented Nov 17, 2017

@alexanderwhatley What is the value of comment.text? And which versions of everything are you using? The template that we provide is to help answer the questions more efficiently. 😁

@alexanderwhatley
Copy link
Author

Hey @m2mathew, sorry about that. The version of material-ui is 0.17.4, and the value of comment.text is "Hi how \n are you?"

@m2mathew m2mathew added the support: question Community support but can be turned into an improvement label Nov 17, 2017
@m2mathew
Copy link
Member

Well, think about how the DOM would handle this. I would expect \n to render if I put that between two <div>s in an HTML file.

image

Since this isn't related to MUI explicitly, I will close this issue. Be sure to head to Stack Overflow or similar for more help implementing content within the DOM.

@ericksprengel
Copy link

I used: style={{whiteSpace: 'pre-line'}}

<Typography
  variant="body1"
  style={{whiteSpace: 'pre-line'}}
>
  {body}
</Typography>

Reference:
https://stackoverflow.com/questions/39325414/line-break-in-html-with-n

@goodwin64
Copy link
Contributor

It seems like it worth additional prop on the "Typography" component.

@Stephane-Ag
Copy link

Stephane-Ag commented Jun 25, 2021

I used: style={{whiteSpace: 'pre-line'}}

<Typography
  variant="body1"
  style={{whiteSpace: 'pre-line'}}
>
  {body}
</Typography>

Reference:
https://stackoverflow.com/questions/39325414/line-break-in-html-with-n

Make sure to put your string in a separate 'body' variable. Writing it in-line does not work. And you do not need explicit '\n' characters.

const body = `Example line:
    Example1
  
  Example line2:
    Example2
  `;

To allow tabs as well, use style={{whiteSpace: 'pre-wrap'}} instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
support: question Community support but can be turned into an improvement
Projects
None yet
Development

No branches or pull requests

5 participants