Svelte Toasts - Flowbite

Push notifications to your users using the toast component and choose from multiple sizes, colors, styles, and positions

The toast component can be used to enhance your website’s interactivity by pushing notifications to your visitors. You can choose from multiple styles, colors, sizes, and positions and even dismiss the component.

Setup #

  • Svelte
<script>
  import { Toast } from 'flowbite-svelte';
</script>

Default toast #

Use this simple toast component with an icon, message, and dismissable close button to show alert messages to your website visitors.

  • Svelte
<script>
  import { Toast } from 'flowbite-svelte';
  import { Icon } from 'flowbite-svelte-icons';
</script>

<Toast>
  <Icon name="fire-outline" slot="icon" class="w-5 h-5 text-primary-500 bg-primary-100 dark:bg-primary-800 dark:text-primary-200" />
  Set yourself free.
</Toast>

Colors #

Use the color prop to easily change the colors of the icons. Set the color prop to none and use the extraIconClass prop to customize your desired icon styles.

Usually, green, red, and orange are used to show success, danger, or warning alert messages to your users. See the first three examples below.

  • Svelte
<script>
  import { Toast } from 'flowbite-svelte';
  import { Icon } from 'flowbite-svelte-icons';
</script>

<Toast color="green">
  <svelte:fragment slot="icon">
    <Icon name="check-circle-solid" class="w-5 h-5" />
    <span class="sr-only">Check icon</span>
  </svelte:fragment>
  Item moved successfully.
</Toast>

<Toast color="red">
  <svelte:fragment slot="icon">
    <Icon name="close-circle-solid" class="w-5 h-5" />
    <span class="sr-only">Error icon</span>
  </svelte:fragment>
  Item has been deleted.
</Toast>

<Toast color="orange">
  <svelte:fragment slot="icon">
    <Icon name="exclamation-circle-solid" class="w-5 h-5" />
    <span class="sr-only">Warning icon</span>
  </svelte:fragment>
  Improve password difficulty.
</Toast>

<Toast color="gray">
  <Icon name="fire-outline" slot="icon" class="w-5 h-5" />
  Gray
</Toast>

<Toast color="yellow">
  <Icon name="fire-outline" slot="icon" class="w-5 h-5" />
  Yellow
</Toast>

<Toast color="blue">
  <Icon name="fire-outline" slot="icon" class="w-5 h-5" />
  Blue
</Toast>

<Toast color="indigo">
  <Icon name="fire-outline" slot="icon" class="w-5 h-5" />
  Indigo
</Toast>

<Toast color="purple">
  <Icon name="fire-outline" slot="icon" class="w-5 h-5" />
  Purple
</Toast>

<Toast color="none" defaultIconClass="w-8 h-8 text-pink-500 bg-pink-100 dark:bg-pink-800 dark:text-pink-200">
  <Icon name="fire-outline" slot="icon" class="w-5 h-5" />
  Customize your colors.
</Toast>

Simple toast #

This component can be used to show simple messages and notifications without the use of a close button.

  • Svelte
<script>
  import { Toast } from 'flowbite-svelte';
  import { Icon } from 'flowbite-svelte-icons';
</script>

<Toast simple contentClass="flex space-x-4 divide-x divide-gray-200 dark:divide-gray-700">
  <Icon name="papper-plane-outline" class="w-5 h-5 text-primary-600 dark:text-primary-500 rotate-45" />
  <div class="pl-4 text-sm font-normal">Message sent successfully.</div>
</Toast>

Icons #

For the right positioning of the icon use: slot="icon".

You can use any icon components.

  • Svelte
<script>
  import { Toast } from 'flowbite-svelte';
  import { Icon } from 'flowbite-svelte-icons';
</script>

<Toast>
  <Icon name="image-outline" slot="icon" class="w-6 h-6" />
  There is a box icon.
</Toast>

<Toast>No icon at all.</Toast>

Autohide example #

  • Svelte
<script>
  import { Toast, Button } from 'flowbite-svelte';
  import { slide } from 'svelte/transition';
  import { Icon } from 'flowbite-svelte-icons';

  let show = true;
  let counter = 6;

  function trigger() {
    show = true;
    counter = 6;
    timeout();
  }

  function timeout() {
    if (--counter > 0) return setTimeout(timeout, 1000);
    show = false;
  }
</script>

<div class="flex gap-10">
  <Button on:click={trigger} class="my-3">Restart</Button>
  <Toast simple transition={slide} bind:open={show}>
    <Icon name="check-circle-solid" slot="icon" class="w-4 h-4" />
    Autohide in {counter}s.
  </Toast>
</div>

Transitions #

You can use one of Svelte/easing .

  • Svelte
<script>
  import { Toast } from 'flowbite-svelte';
  import { slide } from 'svelte/transition';
  import { quintOut, elasticOut } from 'svelte/easing';
  import { Icon } from 'flowbite-svelte-icons';
</script>

<Toast transition={slide} class="mb-4">
  <Icon name="check-circle-solid" slot="icon" class="w-5 h-5" />
  Transition type: slide
</Toast>

<Toast transition={slide} params={{ delay: 250, duration: 300, easing: quintOut }} class="mb-4">
  <Icon name="check-circle-solid" slot="icon" class="w-5 h-5" />
  Transition type: slide, delay: 250, duration: 300, easing: quintOut
</Toast>

<Toast transition={slide} params={{ delay: 250, duration: 2000, easing: elasticOut }}>
  <Icon name="check-circle-solid" slot="icon" class="w-5 h-5" />
  Transition type: slide, delay: 250, duration: 2000, easing: elasticOut
</Toast>

Blur examples #

  • Svelte
<script>
  import { Toast } from 'flowbite-svelte';
  import { blur } from 'svelte/transition';
  import { Icon } from 'flowbite-svelte-icons';
</script>

<Toast transition={blur} color="purple" params={{ amount: 10 }} class="mb-4">
  <Icon name="bell-outline" slot="icon" class="w-5 h-5" />
  Transition type: blur, amount: 10
</Toast>

<Toast transition={blur} color="purple" params={{ amount: 50, delay: 1000 }}>
  <Icon name="bell-outline" slot="icon" class="w-5 h-5" />
  Transition type: blur, amount: 50, delay 1000
</Toast>

Fly examples #

  • Svelte
<script>
  import { Toast } from 'flowbite-svelte';
  import { fly } from 'svelte/transition';
  import { Icon } from 'flowbite-svelte-icons';
</script>

<Toast transition={fly} params={{ x: 200 }} color="green" class="mb-4">
  <Icon name="download-outline" slot="icon" class="w-5 h-5" />
  Transition type: fly right
</Toast>

<Toast transition={fly} params={{ y: 200 }} color="green">
  <Icon name="download-outline" slot="icon" class="w-5 h-5" />
  Transition type: fly down
</Toast>

Undo button #

Use this toast component to also show an “undo” button to reverse the action of the user.

  • Svelte
<Toast contentClass="w-full text-sm font-normal flex items-center justify-between">
  Conversation archived.
  <a class="ml-auto font-medium text-primary-600 p-1.5 hover:bg-primary-100 rounded-lg dark:text-primary-500 dark:hover:bg-gray-700" href="/"> Undo </a>
</Toast>

Advanced examples #

If the toast content grows to multiple lines we need to switch off the items-center alignment. Notice how the examples below achieve that by setting align={flase} property.

Toast message #

This component can be used to show messages and a CTA button when receiving chat messages, comment notifications, and other use cases.

  • Svelte
<script>
  import { Toast, Avatar, Button } from 'flowbite-svelte';
</script>

<Toast align={false} color="none" defaultIconClass="">
  <Avatar slot="icon" src="/images/profile-picture-1.webp" />
  <div class="ml-3 text-sm font-normal">
    <span class="mb-1 text-sm font-semibold text-gray-900 dark:text-white">Jese Leos</span>
    <div class="mb-2 text-sm font-normal">Hi Neil, thanks for sharing your thoughts regarding Flowbite.</div>
    <Button size="xs">Reply</Button>
  </div>
</Toast>

Push notification #

This component can be used to show notifications for an action from another user such as posting a comment, receiving a like, being tagged. You can show an avatar, icon, message, and the time of the notification.

  • Svelte
<script>
  import { Toast, Avatar } from 'flowbite-svelte';
</script>

<Toast align={false}>
  <span class="font-semibold text-gray-900 dark:text-white">New notification</span>
  <div class="flex items-center mt-3">
    <Avatar src="/images/profile-picture-3.webp" />
    <div class="ml-3">
      <h4 class="text-sm font-semibold text-gray-900 dark:text-white">Bonnie Green</h4>
      <div class="text-sm font-normal">commented on your photo</div>
      <span class="text-xs font-medium text-primary-600 dark:text-primary-500">a few seconds ago</span>
    </div>
  </div>
</Toast>

Interactive toast #

Use this interactive toast component to encourage users to make a certain action such as updating to the latest software version. You can set an icon, message, and two CTA buttons.

This is an example with the icon and extra content at the same time.

  • Svelte
<script>
  import { Toast, Button } from 'flowbite-svelte';
  import { Icon } from 'flowbite-svelte-icons';
</script>

<Toast align={false}>
  <Icon name="camera-foto-outline" slot="icon" class="w-5 h-5" />

  <span class="font-semibold text-gray-900 dark:text-white">Update available</span>
  <div class="mt-3">
    <div class="mb-2 text-sm font-normal">A new software version is available for download.</div>
    <div class="grid grid-cols-2 gap-2">
      <Button size="xs" class="w-full">Update</Button>
      <Button size="xs" class="w-full" color="dark">Not now</Button>
    </div>
  </div>
</Toast>

Positioning #

Use the position property to position these toast components relative to the main content wrapper element from your document.

  • Svelte
<div class="relative h-56">
  <Toast simple position="top-left">Top left positioning.</Toast>
  <Toast simple position="top-right">Top right positioning.</Toast>
  <Toast simple position="bottom-left">Bottom left positioning.</Toast>
  <Toast simple position="bottom-right">Bottom right positioning.</Toast>
</div>

Props #

The component has the following props, type, and default values. See types page for type information.

  • Use the class prop to overwrite divClass.
Name Type Default
simple boolean false
color 'primary' | 'gray' | 'red' | 'yellow' | 'green' | 'blue' | 'indigo' | 'purple' | 'orange' | 'none' 'primary'
position 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right' | 'none' 'none'
open boolean true
divClass string 'w-full max-w-xs p-4 text-gray-500 bg-white shadow dark:text-gray-400 dark:bg-gray-800 gap-3'
defaultIconClass string 'w-8 h-8'
contentClass string 'w-full text-sm font-normal'
align boolean true

References #