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

ng-custom-modal

v1.1.0

Published

Angular Material's dialog like modal library

Downloads

36

Readme

Angular Custom Modal

This library has been built to help you with the modals creation in your angular applications.

The usage is very similar to the one you can find in Angular Material's Dialog.

How to use

In order to use this library, all you need to do is import NgCustomModal module into your application's module, like this:

@NgModule({
  ...,
  imports: [..., CustomModalModule]
  ...
})

This will automatically provide you with all you need in order to create dynamic components inside a dialog.

After that, you only need to open the component you will use to open your modal and inject ModalService imported from the library as well:

import { ModalService } from 'ng-custom-modal';

...

@Component({
    ...config
})
export class TestComponent {
    constructor(private modalService: ModalService) {}
}

And then, in the method you create you only need to call open from modalService with the next parameters:

openModal() {
    this.modalService.open<ComponentType, AdditionalDataType>(ComponentName, configObject);
}

Generic parameters:

You can pass both ComponentType and AdditionalDataType to your open method, this will automatically provide a type for the first parameter and also the AdditionalDataType for the data property in the configObject.

The configObject should look like this:

export class ModalConfig<DataType = unknown> {
  data?: DataType;
  width?: string;
  height?: string;
  disableBackdropClose?: boolean;
}

None of the properties are required and you can skip them, if you do so, the modal will be 50% width and 50% height and the default value for the disableBackdropClose property will be false, this means that when I click the modal's backdrop I will be able to close the modal, if I change it to true, then I would be unable to close the modal by clicking the backdrop.

After you've setup your openModal method all you need to do is go to the component that will be loaded inside the modal and accept some configurations.

Configure child component:

For this example the config object's data will be a string, which is defined in the ModalConfig injected in the constructor, and the second parameter is a reference to the modal.

import { Component, OnInit } from '@angular/core';
import { ModalConfig, ModalRef } from 'ng-custom-modal';

@Component({
  selector: 'app-test-modal',
  template: `{{ data }}`,
  styleUrls: ['./test-modal.component.scss'],
})
export class TestModalComponent implements OnInit {
  data = '';
  constructor(public config: ModalConfig<string>, public modal: ModalRef) {}

  ngOnInit(): void {
    if (this.config?.data) {
      this.data = this.config.data;
    }
  }
}

In order to close the modal you only need to call this.modal.close() and it should automatically close.

If you need any data to the parent component, you can optionally add any value to the close method.

Do actions on modal close:

I can also store the recently opened modal in a variable and then subscribe to afterClosed, thus, I will be able to trigger an action when the modal is closed. Here's an example:

// imports

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss'],
})
export class AppComponent {
  title = 'test-modal-package';
  constructor(private modalService: ModalService) {}

  ngOnInit(): void {
    const modalRef = this.modalService.open<TestModalComponent, string>(
      TestModalComponent,
      { data: 'this is a string', width: '400px', height: '250px' }
    );
    modalRef.afterClosed.subscribe((data) => {
      console.log(data);
      console.log('modal was closed');
    });
  }
}

For this to work, I will only need to pass a value to the close method in TestModalComponent:

  onClose() {
    this.modal.close({ confirmed: true });
  }

And that would be it, I will try to apply some other improvements to the package so if you have any ideas or would like to contribute in any way, it would be awesome.