svelte-french-toast

repository·master·Indexed 21 days ago

https://github.com/kbrgl/svelte-french-toast

A lightweight, customizable toast notification library for Svelte, ported from react-hot-toast. It provides a <Toaster /> component for global configuration and a toast API for triggering notifications like success and error messages. Supports flexible positioning via ToastPosition, custom IconThemes, and accessibility properties through ariaProps.

Tokens
1.4K
Snippets
6
Records
10
Agent score
77%

What's inside svelte-french-toast

  1. Basic usage of svelte-french-toast

    master

    To use svelte-french-toast, you must first mount the <Toaster /> component at the top level of your Svelte application. Once the toaster is mounted, you can trigger notifications using the toast API (e.g., toast.success(), toast.error(), etc.) from anywhere in your application.

    <script>
    	import toast, {Toaster} from 'svelte-french-toast'
    
    	function handleClick() {
    		toast.success('Hello, world!')
    	}
    </script>
    
    <Toaster />
    <button type="button" on:click={handleClick}>Toast</button>
  2. Customize Toast appearance and behavior

    master

    When triggering a toast, you can pass options to customize its look and feel. These options are defined by ToastOptions and can be applied individually to a single toast call.

    Available Options:

    • id: A unique identifier for the toast.
    • icon: A Renderable (Svelte component, string, or null) to display as an icon.
    • duration: How long the toast stays visible (in milliseconds).
    • ariaProps: Accessibility properties including role ('status' | 'alert') and 'aria-live' ('assertive' | 'off' | 'polite').
    • className: A CSS class for the toast.
    • style: Inline CSS styles for the toast.
    • position: Overrides the global Toaster position.
    • iconTheme: An IconTheme object specifying primary and secondary colors.
    • props: Custom properties passed to a custom Svelte component used as the toast message.
  3. Configure ToasterProps

    master

    The ToasterProps interface defines the configuration options for the <Toaster /> component. These settings apply globally to all toasts rendered by that instance.

    PropTypeDescription
    positionToastPositionThe default position for all toasts.
    toastOptionsDefaultToastOptionsGlobal default options for all toasts, including type-specific overrides (e.g., toastOptions.success).
    reverseOrderbooleanWhether to reverse the order in which toasts appear.
    gutternumberThe spacing between toasts.
    containerStylestringCSS styles applied to the toast container.
    containerClassNamestringCSS class applied to the toast container.
  4. Use the toast function to trigger notifications

    master

    The toast function is the primary way to trigger toast notifications in your application. It can be imported as a named export or as the default export. You can pass options to customize the appearance and behavior of each toast.

    import toast from 'svelte-french-toast';
    
    tost('Hello World');
    
    // With options
    tost('Success!', { 
      icon: '✅', 
      duration: 3000 
    });
  5. Add the Toaster component to your application

    master

    To display toasts, you must include the Toaster component in your Svelte component tree (usually at the root level, like App.svelte). The Toaster component manages the rendering and positioning of all active toasts.

    <script>
      import { Toaster } from 'svelte-french-toast';
    </script>
    
    <Toaster />
  6. Reference Toast types and options

    master

    The library exports several types to help with TypeScript integration when configuring toasts or building custom components:

    • ToastOptions: Configuration for an individual toast.
    • ToastPosition: Defines where toasts appear (e.g., top-center, bottom-right).
    • ToastType: The category of toast (e.g., success, error).
    • ToasterProps: Props available to the Toaster component.
    • Renderable: Types that can be rendered as toast content.
    • IconTheme: Configuration for toast icons.
  7. Configure ToastPosition for toasts and the Toaster

    master

    The ToastPosition type defines where toasts appear on the screen. It is recommended to use logical positions (start, end) instead of absolute positions (left, right) to ensure automatic adjustment for LTR (Left-to-Right) and RTL (Right-to-Left) text directions.

    Available positions:

    • Top: top-left, top-center, top-right, top-start, top-end
    • Bottom: bottom-left, bottom-center, bottom-right, bottom-start, bottom-end
    type ToastPosition =
    	| 'top-left'
    	| 'top-center'
    	| 'top-right'
    	| 'bottom-left'
    	| 'bottom-center'
    	| 'bottom-right'
    	| 'top-start'
    	| 'top-end'
    	| 'bottom-start'
    	| 'bottom-end';