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.
<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.
<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"
.
<Notification position="bottom-end" />
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"
.
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'
, 'light'
, and 'gradient'
. The 'default'
theme follows the standard notification styling, the 'light'
theme provides a softer color palette for a more subtle look, and the 'gradient'
theme introduces a bold, eye-catching background with smooth color transitions.
Light Theme
With the 'light'
theme, notifications adopt a gentle color scheme, making them blend seamlessly into minimalistic interfaces.
notify( "Hello world!", "success", {theme: "light"} )
Gradient Theme
The 'gradient'
theme applies a lively blend of colors, giving notifications a more modern and attention-grabbing style.
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'
, 'barTop'
, 'barBottom'
, and 'barStart'
.
The default variant, 'card'
, follows a standard notification style without extra elements. The other three variants — 'barTop'
, 'barBottom'
, and 'barStart'
— add a thin line to the respective side of the notification. This subtle design change helps visually distinguish notifications without making them too complex.
For example, using barTop
will place a small line at the top of the notification, while barBottom
and barStart
will do the same for the bottom and start (left in LTR layouts, right in RTL layouts).
notify("Hello world!", "success", {theme: "light", variant: "borderTop"})
Notification Duration
The removeAfter
property controls how long a notification remains visible before closing automatically. It accepts an integer value in milliseconds, starting from 0.
- The default duration is 5000ms (5 seconds), meaning notifications disappear after this time.
- Setting it to 0 keeps the notification open indefinitely until manually closed.
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.
notify("Hello world!", "error", {removeOnClick: false})
Accessibility
The Alert component is built with accessibility in mind, handling all necessary ARIA attributes, including role="alert"
and aria-label="Close alert"
, to ensure compatibility with assistive technologies. 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 alert's text and background to ensure readability and compliance with accessibility standards.
notify() Function
The notify
function displays notifications with customizable options. It accepts three parameters:
- msg (type string) – The message displayed in the notification.
- 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.
Configuration Options
- removeAfter (type number) – Duration in milliseconds before the notification disappears. Set to
0
for persistent notifications.
- Default is5000
. - removeOnClick (type boolean) – Determines if the notification should close when clicked.
- Default istrue
. - rounded (type ROUNDED) – Controls the border radius.
- Default is'md'
. - theme (type string) – Sets the notification theme. Options:
'default'
,'light'
,'gradient'
.
- Default is'default'
. - variant (type string) – Defines the notification style. Options:
'card'
,'barTop'
,'barBottom'
,'barStart'
.
- Default is'card'
.