2

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.

2 Answers 2

1

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 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. Sep 18, 2019 at 10:51
1

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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.