Skip to content

Enhancement: Reusable ToastMessage #20

@sfdcschwabe

Description

@sfdcschwabe

Related to #19 I would like to suggest also a helper component created by me to make toastMessages reusable.
I've called the below component toastMessageHelper.js

With this approach you don't have the same import in different components. You only have one import in one component!

`import { ShowToastEvent } from 'lightning/platformShowToastEvent';
/**
* Toastmessage for user.
*
* @param {string} title -- The title of the toast, displayed as a heading.
* @param {string} message -- A string containing a message for the user.
* @param {string} variant -- The theme and icon displayed in the toast. Valid values are:
* info—(Default) A gray box with an info icon.
* success—A green box with a checkmark icon.
* warning—A yellow box with a warning icon.
* error—A red box with an error icon.
* @param {string} mode -- Determines how persistent the toast is. Valid values are:
* dismissable—(Default) Remains visible until the user clicks the close button or 3 seconds has elapsed, whichever comes first.
* pester—Remains visible for 3 seconds.
* sticky—Remains visible until the user clicks the close button.
*
* You can see the styling for each theme in the Lightning Design System (https://www.lightningdesignsystem.com/components/toast/) documentation.
*/
const notifyUser = (title, message, variant, mode) => {
const toastEvent = new ShowToastEvent({
title,
message,
variant,
mode
});
dispatchEvent(toastEvent);
}

 /**
 * Toastmessage for user.
 * 
 * @param {string} title -- The title of the toast, displayed as a heading.
 * @param {string} message -- A string containing a message for the user.
 * @param {string} variant -- The theme and icon displayed in the toast. Valid values are:
 *  info—(Default) A gray box with an info icon.
 *  success—A green box with a checkmark icon.
 *  warning—A yellow box with a warning icon.
 *  error—A red box with an error icon.
 * @param {string} mode -- Determines how persistent the toast is. Valid values are:
 * @param {string[] or object} messageData -- url and label values that replace the {index} placeholders in the message string.
 */
const notifyUserWithMessageData = (title, message, variant, mode, messageData) => {
    const event = new ShowToastEvent({
        title,
        message,
        variant,
        mode,
        messageData
    });
    dispatchEvent(event);
}

const createToastMessage = (title, message, variant, mode) => {
    return { title, message, variant, mode };
}

export { notifyUser, notifyUserWithMessageData, createToastMessage };`

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions