npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ngx-snackbar-ease

v0.0.1

Published

ngx-snackbar-ease is a versatile Angular library providing a simple, performant, and responsive snackbar. This library supports data communication between components, opening of multiple snackbars, custom animations, and a range of options.

Downloads

5

Readme

ngx-snackbar-ease

Description

ngx-snackbar-ease is a versatile Angular library providing a simple, performant, and responsive snackbar. This library supports data communication between components, opening of multiple snackbars, custom animations, and a range of options.

Support Angular version starts at v17.

Demo

Live demonstration of the ngx-snackbar-ease library here.

Installation

You can install the library using the following command:

npm i ngx-snackbar-ease

Register the SnackbarModule in your app.config.ts and optionally provide general options:

export const appConfig: ApplicationConfig = {
  providers: [
    ...
    importProvidersFrom(
      SnackbarModule.forRoot({
        maximum: 3,
        closeOnNavigation: true,
      })
    ),
    ...
  ],
};

General options respect the Config interface:

Config {
  <!-- maximum number of snackbars on a page -->
  maximum?: number;

  <!-- close active snackbars on navigation (animations are disabled) -->
  closeOnNavigation?: boolean;
}

Simply pass SnackbarModule.forRoot() to leave it default.

Options

The snackbar supports a range of options:

Options {
  snackbar?: {
    enter?: string;
    leave?: string;
    top?: string;
    left?: string;
    position?: Position;
    duration?: number;
  };
  size?: {
    height?: string;
    maxHeight?: string;
    width?: string;
    maxWidth?: string;
    padding?: string;
  };
  data?: {
    [key: string]: unknown;
  };
}

Position =
  | 'bottom-left'
  | 'bottom'
  | 'bottom-right'
  | 'top-left'
  | 'top'
  | 'top-right';

| Name | Default | Description | | -------------------------------------------------------- | ---------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | enter | | Define the enter animation for the snackbar respecting the shorthand animation property. See predefined animations here. | | leave | | Define the leave animation for the snackbar respecting the shorthand animation property. See predefined animations here. | | top | | Top position of the snackbar in percent. Can be defined in any measure unit. | | left | | Left position of the snackbar in percent. Can be defined in any measure unit. | | position | | Generic positioning of the snackbar respecting the Position type (see above). | | duration | | Duration of the snackbar in ms. | | minHeight | | Minimum height of the snackbar. Can be defined in any measure unit. | | height | | Height of the snackbar. Can be defined in any measure unit. | | width | | Width of the snackbar. Can be defined in any measure unit. | | maxWidth | 100 | Max width of the snackbar in percent. Can be defined in any measure unit. | | padding | 0 0.5 | Padding to be applied on the snackbar in rem. Can be defined in any measure unit. | | data | | Data communication between components under the form of key-value pairs. Any type of data is supported. |

You have the choice for the snackbar positioning between a generic positioning or fine grained control with top and left values.

Complete Example

Inject the snackbarService through regular dependency injection in your component.

In the following example, snackbarContentComponent is the content of the snackbar and should be provided as first parameter. As second parameter, provide an object respecting the Options interface (see above).

  this.snackbarService
    .open(SnackbarContentComponent, {
      snackbar: {
        <!-- Generic positioning -->
        position: 'bottom-left',
        <!-- animations -->
        enter: 'going-right-enter 0.3s ease-out',
        leave: 'going-right-leave 0.3s ease-out',
        <!-- Auto close in 4 seconds -->
        duration: 4000,
      },
      data: {
        <!-- Data to send to SnackbarContentComponent -->
        color: 'blueviolet',
      },
    })
    .subscribe((dataFromSnackbarContentComponent) => {
      ...
  });

Any type of data can be provided between components. Create the corresponding property (here, color) in your component (here, SnackbarContentComponent) and the property will be assigned with the provided value.

In your SnackbarContentComponent: To pass information from the SnackbarContentComponent to your current component, inject the SnackbarService through regular dependency injection and call the close(this, data) method from the service with any data you wish to send back to your component. This method returns an RxJs subject, so subscribe to it as shown in the above example. It is not necessary to unsubscribe from the subscription since it will automatically complete() in the service.

Passing this as first argument is necessary to identify the current snackbar instance to close (there can be multiple snackbars).

  <!-- Inside SnackbarContentComponent -->
  onClose() {
    this.snackbarService.close(this, this.dataToSendBack);
  }

Publicly available methods have been exhaustively documented and typed, so you should get autocomplete and help from your code editor. Press on CTRL + space to get help on the available properties in the Options object.

SnackbarService

This library exposes a SnackbarService that contains the following API:

<!-- Opens a component inside the snackbar -->
open<C>(componentToCreate: Type<C>, options?: Options);

<!-- Close a snackbar with optional data to send back -->
close(instance: any, data?: unknown);

<!-- Close all opened snackbars -->
closeAll();

Ready-to-use animations keyframes

This library comes with predefined and ready-to-use animations keyframes. Just fill in the name, duration and easing function (more info on the animation CSS shorthand here). Those animations are position agnostic, ie. they are adaptable to any positioning. Of course, you can create your own keyframes too.

/* Recommended: 0.3s ease-out */
@keyframes going-right-enter {
  from {
    transform: translateX(-150%);
  }
  to {
    transform: translateX(0);
  }
}

/* Recommended: 0.3s ease-in */
@keyframes going-right-leave {
  from {
    transform: translateX(0);
  }
  to {
    transform: translateX(-150%);
  }
}

/* Recommended: 0.3s ease-out */
@keyframes going-left-enter {
  from {
    transform: translateX(150%);
  }
  to {
    transform: translateX(0%);
  }
}

/* Recommended: 0.3s ease-in */
@keyframes going-left-leave {
  from {
    transform: translateX(0%);
  }
  to {
    transform: translateX(150%);
  }
}

/* Recommended: 0.3s ease-out */
@keyframes going-down-enter {
  from {
    transform: translateY(-150%);
  }
  to {
    transform: translateY(0%);
  }
}

/* Recommended: 0.3s ease-in */
@keyframes going-down-leave {
  from {
    transform: translateY(0%);
  }
  to {
    transform: translateY(-150%);
  }
}

/* Recommended: 0.3s ease-out */
@keyframes going-up-enter {
  from {
    transform: translateY(150%);
  }
  to {
    transform: translateY(0);
  }
}

/* Recommended: 0.3s ease-in */
@keyframes going-up-leave {
  from {
    transform: translateY(0);
  }
  to {
    transform: translateY(150%);
  }
}

/* Recommended: 0.2s ease-out */
@keyframes scale-enter {
  from {
    transform: scale(0.9);
  }
  to {
    transform: scale(1);
  }
}

/* Recommended: 0.7s linear */
@keyframes bounce-in {
  0% {
    transform: translateY(-35%);
  }
  18% {
    transform: translateY(0);
  }
  60% {
    transform: translateY(-15%);
  }
  80% {
    transform: translateY(0);
  }
  90% {
    transform: translateY(-3%);
  }
  100% {
    transform: translateY(0);
  }
}

/* Recommended: 0.3s linear */
@keyframes fade-in {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

/* Recommended: 0.3s linear */
@keyframes fade-out {
  from {
    opacity: 1;
  }
  to {
    opacity: 0;
  }
}

If you create your own keyframes, I would recommend to create a new file snackbar-animations (.css or preprocessor), and @import it in your styles.css (or preprocessor) at the root of the application.

Responsive

Snackbars are responsive and will correctly render on smaller devices.

SSR (Server Side Rendering)

This library supports Server Side Rendering (SSR). The snackbar will not instantiate during server-side execution, as it requires access to the DOM API.

DX Friendly

This library has been documented and should provide autocomplete and help from your code editor. Tested on VS Code.

Performance

Emphasis has been placed on performance, adopting ChangeDetectionStrategy.OnPush strategy. This library respects Angular's mindset and use the Angular API to dynamically create components. Snackbar components will be removed from the DOM after closing and their respective RxJs Subject to emit data will be automatically closed. The resize event that add responsiveness is debounced and will be removed on close. SetTimeout (useful for auto-close if duration is set) will be cleared.

Change log

Report a Bug

Please provide a detailed description of the encountered bug, including your options and the steps/actions that led to the issue. An accurate description will help me to reproduce the issue.

Ngx-ease serie

You like this library? Discover the ngx-ease serie here.