Notification

A lightweight way to display temporary messages without interrupting the user experience. It appears briefly, disappears automatically, and is fully customizable.

About

This component is ideal for showing temporary messages such as success alerts, error messages, or status updates. It supports customization options, including different colors, positions, icons, and auto-dismiss settings. Whether you need to notify users about form submissions, system errors, or background processes, the Notification (Toast) component offers a flexible and efficient solution.

Example

To use the Notification component, you need to add it once in your application. The recommended place is +layout.svelte to ensure it is available throughout your app, but you can place it anywhere suitable.

+layout.svelte

<script>
  import { Notification } from "theui-svelte";
</script>

<!-- Add Notification component once, ideally in +layout.svelte -->
<Notification />

Once added, you can trigger notifications anywhere in your app using the notify function.

routes/any/path/+page.svelte
<script>
  import { notify } from "theui-svelte";
</script>

<!-- Trigger the notifications -->
<Button
  onclick={
    () => notify("Hello world!")
  }
>Notify</Button>

This will display a simple notification with the message "Hello world!".

Notification Position

By default the notification will appears at the top-end corner of the display. But you can change it using the position prop in the Notification component. The available value of the position prop are "top-end", "top-center", "top-start", "bottom-end", "bottom-center", and "bottom-start". Default value is "top-end".

Svelte
<Notification position="bottom-end" />

Notification Animation

By default, notifications slide in when they appear and fade out when they are removed. Use the animationSpeed prop on the Notification component to control this. Unlike other components, it is a boolean: true (default) enables the animation, and false turns it off.

Svelte
<Notification animationSpeed={false} />

Notification Type

The notify function accept some parameters along with the message. You have to pass the message as the first parameter. The second parameter defines the type of the notification. Basically it changes the color based on the type. The available types are "error", "info", "success", and "warning". The default value is "error".

Svelte
notify("Hello world!", "success") // You can replace "success" with any message type!

Notification Theme

You can control the background color of the notifications with the theme prop. It supports three options: 'default', 'soft', and 'gradient'. The 'default' theme follows the standard notification styling, the 'soft' theme provides a softer color palette for a more subtle look, and the 'gradient' theme uses a two color gradient.

Soft Theme

The 'soft' theme uses a tinted background instead of a solid one.

Svelte
notify( "Hello world!", "success", {theme: "soft"} )

Gradient Theme

The 'gradient' theme fills the background with a two color gradient.

Svelte
notify( "Hello world!", "success", {theme: "gradient"} )

Notification Variant

The 'variant' property allows you to change the visual style of the notification while keeping it minimal. There are four available variants: 'card', 'borderTop', 'borderBottom', and 'borderStart'.

The default variant, 'card', follows a standard notification style without extra elements. The other three variants, 'borderTop', 'borderBottom' and 'borderStart', add a thin line to that side of the notification. It is a small change, but it tells one notification from another at a glance.

For example, using borderTop will place a small line at the top of the notification, while borderBottom and borderStart will do the same for the bottom and start (left in LTR layouts, right in RTL layouts).

Svelte
notify("Hello world!", "success", {theme: "soft", variant: "borderTop"})

Notification Duration

The removeAfter property controls how long a notification remains visible before closing automatically. It accepts an integer value in milliseconds, or false.

  • The default duration is 4000ms (4 seconds), meaning notifications disappear after this time.
  • Setting it to false keeps the notification open indefinitely until manually closed.
Svelte
notify("Hello world!", "error", {removeAfter: 3000})

Remove On Click

By default, notifications close when clicked. You can change this behavior using the removeOnClick property in the options object. When set to true (default), clicking the notification will dismiss it. If set to false, the notification will remain visible even when clicked.

Svelte
notify("Hello world!", "error", {removeOnClick: false})

notify() Function

The notify function displays notifications with customizable options. It accepts three parameters:

  • msg (type string) - The message displayed in the notification. HTML in the message is sanitized before rendering.
  • type (type NOTIFICATION_TYPE) - Defines the notification style. Options: 'error', 'info', 'success', and 'warning'.
    - Default is 'error'.
  • config (type NOTIFY_CONFIG) - An optional object to configure the notification's behavior and appearance.

Call notify in the browser, such as from an event handler, $effect or onMount. A call during server rendering does nothing, so one visitor's message never reaches another.

Configuration Options

  • removeAfter (type number | false) - Duration in milliseconds before the notification disappears. Set to false for persistent notifications.
    - Default is 4000.
  • removeOnClick (type boolean) - Determines if the notification should close when clicked.
    - Default is true.
  • rounded (type ROUNDED) - Controls the border radius.
    - Default is 'md'.
  • theme (type string) - Sets the notification theme. Options: 'default', 'soft', 'gradient'.
    - Default is 'default'.
  • variant (type string) - Defines the notification style. Options: 'card', 'borderTop', 'borderBottom', 'borderStart'.
    - Default is 'card'.

Accessibility

The Notification component is built with accessibility in mind, handling all necessary ARIA attributes, including role="alert", aria-live="assertive" and aria-atomic="true", so screen readers announce each notification as soon as it appears. These attributes are automatically applied, so you don't need to configure them manually.

The only consideration required is maintaining proper color contrast for the notification's text and background to ensure readability and compliance with accessibility standards.

Configuration