4

I have a requirement where I need to display my toast message in multiple lines in my LWC. I tried using \n and
but they are not working. Please assist.

1

2 Answers 2

5

For Multiple Lines Toast in LWC

we have to override CSS :

  1. Create a CSS file and add in static resource

File Content :

.toastMessage{

    white-space: break-spaces !important;

}
  1. Import That static resource file in your component
import { loadStyle } from 'lightning/platformResourceLoader';

import CUSTOMCSS from '@salesforce/resourceUrl/{yourfileName}';

a) Define variable like : isCssLoaded = false;

  1. Call it in reRenderCallback
renderedCallback(){

    if(this.isCssLoaded) return
    this.isCssLoaded = true;
    loadStyle(this,CUSTOMCSS).then(()=>{
        console.log('loaded');
    })
    .catch(error=>{
        console.log('error to load');
    });

}

4)Create your toast message like this :

var msg = 'This is first-line \nThis is Second Line. \nThis is the third Line.';

    const evt = new ShowToastEvent({
                title: "Complete Required Fields :",
                message: msg,
                variant: "error",
            });
            this.dispatchEvent(evt);
2
  • why directly adding the mentioned css to the css file in lwc will not work? Commented Jul 22, 2022 at 3:09
  • 1
    for overriding LWC standard component CSS we need to import CSS file. Commented Jul 23, 2022 at 13:33
2

Have a read of this article on SFDC Monkey that covers this exact issue! (I've used this and it works great)

http://sfdcmonkey.com/2018/05/05/multiline-toast-message-lightning-component/

2
  • 1
    You cannot use it in LWC - cannot modify elements from shadow DOM Commented Sep 18, 2019 at 8:10
  • I am facing the same issue as I am using LWC here. @David can u suggest a way to make it work in LWC. Commented Sep 18, 2019 at 10:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .