HotToast for Angular

repository·main·Indexed 42 days ago

https://github.com/ngxpert/hot-toast

A lightweight, customizable notification library for Angular inspired by react-hot-toast. It features an Observable-based API, built-in themes (Material, Minimal, Glassmorphism, iOS), and specialized integrations such as an HTTP error interceptor and Angular Form status tracking via the fromForm method.

Tokens
12.8K
Snippets
41
Records
55
Agent score
52%

What's inside HotToast

  1. Configure accessibility and aria-live settings

    main

    Hot-toast messages are announced via an aria-live region.

    • Default behavior: The polite setting is used by default.
    • Customization: You can change this by setting the ariaLive property within your ToastConfig or ToastOptions.

    Best Practices for Accessibility:

    • Focus Management: Do not move the application focus to the hot-toast element, as this is disruptive. Instead, provide alternative ways (like keyboard shortcuts or menus) to perform actions offered within a toast.
    • Actionable Toasts: If a toast contains an action, set autoClose: false. This allows screen-reader users enough time to navigate to the element and activate the action.
  2. Verify Hot Toast interceptors from an Angular app

    main

    To test the interceptor within your running Angular application:

    1. Start the test server (npm run sample:http-test-server).
    2. Start your Angular app (usually http://localhost:4200).
    3. Ensure your application's environment.sampleHttpTestServerUrl is set to the server URL (default is http://localhost:4000).
    4. Navigate to the sample page: http://localhost:4200/http-interceptor-sample.

    Alternatively, you can trigger an error directly from the browser DevTools console to see if a toast appears:

    fetch('http://localhost:4000/api/server-error').catch(() => {});

    Testing Header-based logic: To test skipRequest or shouldIgnore logic in your interceptor configuration, call the following endpoint with the X-Skip-Toast: 1 header: GET http://localhost:4000/api/skip-toast-header

  3. Add HotToast styles and themes

    main

    HotToast requires base styles to function. You can also import specific themes to change the visual appearance.

    1. Add Base Styles

    SCSS:

    @use '@ngxpert/hot-toast/styles';

    CSS (via angular.json):

    "styles": [
      "@ngxpert/hot-toast/styles.css"
    ]

    2. Add a Theme (Optional)

    Import one theme stylesheet on top of the base styles.

    SCSS:

    @use '@ngxpert/hot-toast/styles';
    @use '@ngxpert/hot-toast/themes/material'; // options: minimal | glassmorphism | ios

    CSS (via angular.json):

    "styles": [
      "@ngxpert/hot-toast/styles.css",
      "@ngxpert/hot-toast/themes/material.css"
    ]

    Tip: Running ng add @ngxpert/hot-toast handles these imports automatically and prompts you to pick a theme.

  4. Install @ngxpert/hot-toast

    main

    Depending on your Angular version, use one of the following methods to install the library.

    Angular 19+

    Use the Angular CLI to automatically handle installation and styling setup:

    ng add @ngxpert/hot-toast

    Other Angular Versions

    Install via npm or yarn. Note that for older versions, you may need to install @ngneat/overview alongside the library.

    npm install @ngneat/overview@7.0.0 @ngxpert/hot-toast
    # or
    yarn add @ngneat/overview@7.0.0 @ngxpert/hot-toast
  5. Enable dark mode for toasts

    main

    If your application uses a manual dark-mode toggle, you can force all themed toasts (Material, Minimal, Glassmorphism, iOS) to render in dark mode by adding the hot-toast-dark-theme class to a parent element, such as the <body> tag.

    <body class="hot-toast-dark-theme">
      <!-- All themed toasts inside here will use dark mode -->
    </body>
  6. Run the Express HTTP test server

    main

    The hot-toast-http-test-server is a small API used to manually verify hotToastHttpInterceptor and provideHotToastHttpInterceptor() configurations, such as status code ignores, toast messages, and headers.

    To run the server from the repository root:

    npm run sample:http-test-server:install
    npm run sample:http-test-server

    Alternatively, you can run it directly from the package folder:

    npm install
    npm start

    The server defaults to port 4000. You can change the port by setting the PORT environment variable:

    PORT=5000 npm start
  7. Configure HotToast providers

    main

    To use HotToast, you must provide its configuration in your application's provider list. You can use provideHotToastConfig() for both Standalone and Module-based applications.

    Standalone Setup (Angular 14+)

    Add provideHotToastConfig() to the providers array in your bootstrapApplication call.

    Module Setup

    Add provideHotToastConfig() to the providers array in your @NgModule.

    import { provideHotToastConfig } from '@ngxpert/hot-toast';
    
    // Standalone
    bootstrapApplication(AppComponent, {
      providers: [
        provideHotToastConfig(),
      ],
    });
    
    // Module-based
    @NgModule({
      providers: [provideHotToastConfig()],
    })
    class AppModule {}
  8. Configure the HTTP error interceptor

    main

    You can optionally register a functional interceptor to automatically show error toasts when HttpClient requests fail. The interceptor ensures failed requests are still rethrown to your existing subscribe or catchError handlers.

    To use it, add hotToastHttpInterceptor to your provideHttpClient call and use provideHotToastHttpInterceptor() to configure rules like ignoreStatuses (e.g., to skip 401 errors).

    import { hotToastHttpInterceptor, provideHotToastHttpInterceptor, provideHotToastConfig } from '@ngxpert/hot-toast';
    import { provideHttpClient, withFetch, withInterceptors } from '@angular/common/http';
    
    bootstrapApplication(AppComponent, {
      providers: [
        provideHotToastConfig(),
        provideHttpClient(withFetch(), withInterceptors([hotToastHttpInterceptor])),
        provideHotToastHttpInterceptor({
          ignoreStatuses: [401],
        }),
      ],
    });
  9. Choose a visual theme for toasts

    main

    Hot-toast supports several built-in themes. Note that some advanced themes require importing specific SCSS files in your project:

    • toast: The default look (no extra import needed).
    • snackbar: A dark snackbar variant (included in base styles).
    • material: Material Design elevation and typography. Requires: @use '@ngxpert/hot-toast/themes/material'.
    • minimal: A borderless, shadowless, clean look. Requires: @use '@ngxpert/hot-toast/themes/minimal'.
    • glassmorphism: A frosted-glass effect. Requires: @use '@ngxpert/hot-toast/themes/glassmorphism'.
    • ios: An iOS-style pill with blur effect. Requires: @use '@ngxpert/hot-toast/themes/ios'.
  10. Configure global `ToastConfig` options

    main

    In addition to standard ToastOptions, you can configure the following global settings for how toasts are stacked and displayed:

    NameTypeDescription
    reverseOrderbooleanSets the reverse order for hot-toast stacking. Default: false
    visibleToastsnumberSets the number of toasts visible. 0 sets no limit. Default: 5
    stacking"vertical"|"depth"Sets the type of stacking. Default: "vertical"
    usePopoverbooleanSets whether to use the popover API. Default: undefined