UPDATE [July 8, 2020]
LWC now supports showing hyperlinks in the toast message.
This seems to have been introduced in last couple of months as commented on the idea that I had created at the time of writing this answer first. The implementation details/example is documented, excerpt below.
Displaying Links in Toasts
To display a link in the message, use the messageData
attribute to pass in url
and label
values for the message
string.
const event = new ShowToastEvent({
"title": "Success!",
"message": "Record {0} created! See it {1}!",
"messageData": [
'Salesforce',
{
url: 'http://www.salesforce.com/',
label: 'here'
}
]
});
As of today, it doesn't seem you can use links in toast message in LWC directly. I have created this idea to allow the ability to show links in LWC toasts.
In Lighting Aura Components (LAC), the force:showToast
had additional attributes for this purpose - messageTemplate
and messageTemplateData
.
But, these attributes are not available for the corresponding LWC lightning-platform-show-toast-event
. Even if you try using it there, it does not have an impact.
Your option here is as Pranay mentioned in the comments, you can utilize a Custom Event in your LWC and then wrap your LWC in a LAC which handles the event and takes care of raising the toast message.
Here's a sample code how you can use it.
From LWC, raise a Custom Event:
showNotification() {
let myData = [];
myData[0] = "Title of Message";
myData[1] = "This message is from LWC";
myData[2] = "https://linktomyrecord.com";
const showToast = new CustomEvent('showtoast', {
detail: { myData },
});
this.dispatchEvent(showToast);
}
Then, in your Lightning Aura Component, include the LWC:
<c:lwcAuraComponent onshowtoast="{!c.handleShowToast}"/>
And then in handleShowToast()
, get the values and raise the toast message. (Note that, you will still need to keep the message
attribute in the event)
handleShowToast : function(component, event, helper) {
var myData = event.getParam('myData');
var title = myData[0];
var msg = myData[1];
var linkToRec = myData[2];
var toastEvent = $A.get("e.force:showToast");
toastEvent.setParams({
mode: 'pester', // or 'dismissible' -- see docs for details
title: title,
message: 'Sample Message', // you will need to keep this attribute
messageTemplate: 'Record {0} created! See it {1}!',
messageTemplateData: [
msg,
{
url: linkToRec, // here's your link
label: 'here',
}
]
});
toastEvent.fire();
}
You can't navigate outside your own template. That's one of the security features of LWC's shadow DOM. Instead, create a transparent, fixed div that covers the entire screen area, then react to that, instead:
<template>
<div class="backdrop" onclick={closeModal}></div>
<div class="content">... your content here ...</div>
</template>
.backdrop {
position: fixed;
z-index: 999;
top: 0;
left: 0;
right: 0;
bottom: 0;
}
.content {
z-index: 1000;
/* anything else you need here */
}
For a complete example, look at Lightning Strike's Lookup component (written in Aura, but still applicable).
Best Answer
If you look at the documentation, to programmatically register your event listener you need to do the following in your "parent" LWC (an ancestor of the LWC generating the show toast event):
I tried this in a playground but the playground doesn't appear to correctly support toasts. It therefore looks like an exercise for the OP to see if this works.
I would assume you would need to provide a component that wraps all the others (perhaps via a "slot") in the page in order to catch "standard" toasts from these components, but I don't think you can catch toasts from the "chrome" of the page outside the body content.