svelte-simpletoast

A lightweight, customizable toast notification library for Svelte applications.

Demo: https://mongodillo.github.io/svelte-simpletoast/

Installation

Use the package manager npm to install svelte-simpletoast:

$ npm i svelte-simpletoast

Usage

The following are exported:

To use svelte-simpletoast, you'll need to import and utilize both the ToastContainer component and the toasts controller.

Svelte

Place the ToastContainer in your root layout/page and optionally override various options for customization.

In the event you are using the optional toastConfig with the position/smPosition options, please import toasts as well as the options below are stored as values in the toasts.positionOptions object

+layout.svelte:

<script>
    import { ToastContainer, toasts } from 'svelte-simpletoast';

    // Optional configurations
    const toastConfig = {
        duration: 5000, // Override the default duration
        autoClose: true, // Override the default "autoClose" boolean
        position: toasts.positionOptions.TOP_RIGHT, // Override default positioning of toasts for screens larger than 640px
        smPosition: toasts.positionOptions.BOTTOM_MIDDLE, // Override default positioning of toasts for screens smaller than 640px
        maxToasts: 4, // Limit the number of toasts to avoid cluttering the screen
        // Customize the color scheme
        colorScheme: {
            neutral: { color: '#111827', bg: '#f3f4f6' },
            error: { color: '#dc2626', bg: '#fed7d7' },
            success: { color: '#059669', bg: '#c6f6d5' },
            info: { color: '#065d9d', bg: '#bfdbfe' },
            warning: { color: '#d97706', bg: '#fef3c7' }
        }
    };
</script>

...
<ToastContainer {toastConfig} />

The toasts controller can be used anywhere in your application to display toast notifications:

MyComponent.svelte:

<script>
    import { toasts } from 'svelte-simpletoast';

    function showAllToasts() {
        toasts.error('Error Title', 'Error message', 10000);
        toasts.success('Success Title', 'Success message');
        toasts.processing('Data Downloading', 'Processing message');
        toasts.neutral('Neutral Title', 'Neutral message', 500, true);
        toasts.info('Info Title', 'Info message', 7500);
        toasts.warning('Warning Title', 'Warning message', 1000, true);
    }
</script>

<button on:click="{showAllToasts}">Show Toasts</button>

Vanilla JS With Bundler / Other JS Frameworks

You can import the ToastContainer and toasts controller from the svelte-simpletoast/dist-js

import { ToastContainer, toasts } from 'svelte-simpletoast/dist-js';
const toastContainer = new ToastContainer({
    target: document.body,
    props: {
        toastConfig: {
            duration: 5000, // Duration (in milliseconds) for which a toast is displayed
            autoClose: true, // If true, the toast will automatically close after the duration
            position: toasts.positionOptions.TOP_RIGHT, // Positioning of toasts for screens larger than 640px
            smPosition: toasts.positionOptions.BOTTOM_RIGHT, // Positioning of toasts for screens smaller than 640px
            maxToasts: 7, // Maximum number of toasts that can be displayed at a time
            colorScheme: {
                neutral: { color: '#111827', bg: '#f3f4f6' },
                error: { color: '#dc2626', bg: '#fed7d7' },
                success: { color: '#059669', bg: '#c6f6d5' },
                info: { color: '#065d9d', bg: '#bfdbfe' },
                warning: { color: '#d97706', bg: '#fef3c7' }
            }
        }
    }
});

toasts.success('SUCCESS TITLE', 'MESSAGE');

CDN / Vanilla JS

<head>
    ...

    <script src="https://cdn.jsdelivr.net/npm/svelte-simpletoast"></script>
    ...
</head>
<body>
    <script>
        const toastContainer = new ToastContainer({
            target: document.body,
            props: {
                toastConfig: {
                    duration: 5000, // Duration (in milliseconds) for which a toast is displayed
                    autoClose: true, // If true, the toast will automatically close after the duration
                    position: toasts.positionOptions.TOP_RIGHT, // Positioning of toasts for screens larger than 640px
                    smPosition: toasts.positionOptions.BOTTOM_RIGHT, // Positioning of toasts for screens smaller than 640px
                    maxToasts: 7, // Maximum number of toasts that can be displayed at a time
                    colorScheme: {
                        neutral: { color: '#111827', bg: '#f3f4f6' },
                        error: { color: '#dc2626', bg: '#fed7d7' },
                        success: { color: '#059669', bg: '#c6f6d5' },
                        info: { color: '#065d9d', bg: '#bfdbfe' },
                        warning: { color: '#d97706', bg: '#fef3c7' }
                    }
                }
            }
        });

        toasts.success('SUCCESS TITLE', 'MESSAGE');
    </script>
</body>

Default Configuration

The library comes with a set of default configurations for toast notifications. Here's what the default configuration looks like:

{
    duration: 5000, // Duration (in milliseconds) for which a toast is displayed
    autoClose: true, // If true, the toast will automatically close after the duration
    position: toasts.positionOptions.TOP_RIGHT, // Positioning of toasts for screens larger than 640px
    smPosition: toasts.positionOptions.BOTTOM_RIGHT, // Positioning of toasts for screens smaller than 640px
    maxToasts: 7, // Maximum number of toasts that can be displayed at a time
    colorScheme: {
        neutral: { color: '#111827', bg: '#f3f4f6' },
        error: { color: '#dc2626', bg: '#fed7d7' },
        success: { color: '#059669', bg: '#c6f6d5' },
        info: { color: '#065d9d', bg: '#bfdbfe' },
        warning: { color: '#d97706', bg: '#fef3c7' }
    }
};

You can override these defaults by passing your own configuration to the ToastContainer as shown in the Usage section.

Note: the default config for a processing toast is to not close automatically - i.e. you should be manually closing the toast via toasts.removeToast(id) given its meant to reflect a processing task such as "fetch"

Please note, the color scheme is used to define the background and text color for each type of toast. The keys are the toast types, and the values are objects containing the color (text color) and bg (background color) properties.

Remember, these default values are subject to change based on updates to the library. Always refer to the source code or official documentation for the most accurate information.

Position Options

The library provides a number of options for positioning toast notifications on the screen - toasts.positionOptions.[variable]. The positioning options are particularly useful for ensuring the toasts appear exactly where you want them to on different screen sizes.

Here are the available position options:

  • TOP_RIGHT: Positions the toasts at the top-right corner of the screen.
  • TOP_LEFT: Positions the toasts at the top-left corner of the screen.
  • TOP_MIDDLE: Positions the toasts at the top-center of the screen.
  • BOTTOM_RIGHT: Positions the toasts at the bottom-right corner of the screen.
  • BOTTOM_LEFT: Positions the toasts at the bottom-left corner of the screen.
  • BOTTOM_MIDDLE: Positions the toasts at the bottom-center of the screen.

To set the position of the toasts, you can use the position and smPosition properties in your configuration. position sets the position of the toasts for larger screens (screen width > 640px), while smPosition sets the position of the toasts for smaller screens (screen width < 640px).

Here's how you can set these properties in your configuration:

const toastConfig = {
    // ...
    position: toasts.positionOptions.TOP_RIGHT, // For larger screens
    smPosition: toasts.positionOptions.BOTTOM_MIDDLE // For smaller screens
    // ...
};

This configuration will position the toasts at the top-right corner of the screen for larger screens, and at the bottom-center of the screen for smaller screens.

You can use any combination of the position options based on your design preferences and requirements.

Toast Methods

The toasts controller provides several methods for displaying different types of toast notifications. Each method returns a toastId that can be used to manually remove the toast.

Note: duration (ms) and autoClose (boolean) are optional parameters.

// Success toast
toast.success(title, message, duration?, autoClose?);

// Error toast
toast.error(title, message, duration?, autoClose?);

// Info toast
toast.info(title, message, duration?, autoClose?);

// Warning toast
toast.warning(title, message, duration?, autoClose?);

// Processing toast
toast.processing(title, message, duration?, autoClose?);

// Neutral toast
toast.neutral(title, message, duration?, autoClose?);

// Remove a toast
toasts.removeToast(id);

Font Sizes

Font sizes are set to inherit. As such, you may tweak the font size of the toasts by wrapping the ToastContainer in a parent element with the stated font size.

The Toast title uses h6 and toast text uses p tags.

Svelte

For example, in Svelte

<div class="text-sm">
    <ToastContainer {toastConfig} />
</div>

other JS/Vanilla JS/CDN

You may have separate <div> element with the necessary class for text size, and have the target point to that specific element instead of document.body

Development

The package is bundled with SvelteKit. Contributions are welcomed following the standard Github contribution workflow.

License

svelte-simpletoast is MIT Licensed

Changelog

0.4.0

BREAKING CHANGE:

  • Updated to Svelte4
  • Implemented Typescript

0.3.4

  • Updated types for toasts so that autoClose and duration are optional as intended
  • Updated packages

0.3.3

  • Updated Readme to clarify toasts import when using optional config.
  • Updated Readme to clarify setting of font sizes as this package uses inherit for font size
  • Removed out:fade from toasts so that there is no lag.

0.3.2

  • Moved ToastContainer colour scheme set into a use:action function instead of onMount

0.3.1

  • Changed font-size to inherit. (previously was 1rem for body and 1.2rem for Title.)

Top categories

svelte logo

Need a Svelte website built?

Hire a professional Svelte developer today.
Loading Svelte Themes