Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

toppy

lokesh-coder292MIT2.3.4TypeScript support: included

Overlay library for Angular 7+

angular, dropdown, modal, overlay, popover, popup, sidebar, tooltip

readme


Toppy

Tiny Angular library to create overlays for tooltips, modals, dropdowns, alerts, toastr, popovers, menus, and more

Github Release Licence Downloads


Demo and documentation

Installation

step 1: Install from npm or yarn

npm install toppy // or
yarn add toppy

step 2: Import ToppyModule in your main module

import { ToppyModule } from 'toppy';

@NgModule({
  declarations: [AppComponent],
  imports: [BrowserModule, ToppyModule], // <==
  bootstrap: [AppComponent]
})
export class AppModule {}

step 3: Import Toppy service in your component

import { Toppy } from 'toppy'; // <==

@Component({
  selector: 'app-root',
  template: '<div #el>Click me</div>'
})
export class AppComponent {
  @ViewChild('el', { read: ElementRef })
  el: ElementRef;

  constructor(private _toppy: Toppy) {}

  ngOnInit() {
    const position = new RelativePosition({
      placement: OutsidePlacement.BOTTOM_LEFT,
      src: this.el.nativeElement
    });

    this.overlay = this._toppy
      .position(position)
      .content('hello') // content
      .create();
  }

  open() {
    this.overlay.open();
  }

  close() {
    this.overlay.close();
  }
}

Content

Toppy allows to use string, html, TemplateRef, Component as overlay content.

Plain text

this.overlay = this._toppy
  .position(position)
  .content(`some plain text content`) // simple text
  .create();

HTML content

this.overlay = this._toppy
  .position(position)
  .content(`<div>any HTML content</div>`, { hasHTML: true }) // html
  .create();

Using TemplateRef

<ng-template #tpl>Hello world!</ng-template>
@ViewChild('tpl') tpl:TemplateRef<any>;

this.overlay = this._toppy
  .position(position)
  .content(this.tpl) // template ref
  .create();

Component

// host component
@Component({
  template: '<div>Hello</div>'
})
export class HelloComponent {}
this.overlay = this._toppy
  .position(position)
  .content(HelloComponent) // <==
  .create();

Dont forget to add host component in entryComponents in module

Positions

Position determines the location and size of the overlay. There are four positions:

Relative position

Overlay position that is relative to specific element. These are used in tooltips, popovers, dropdowns, menus

new RelativePosition({
  src: HTMLElement, // target element
  placement: OutsidePlacement, // location of the content
  width: string | number, // content width eg, `auto`, 150, `30%`
  height: string | number, // content height eg, `auto`, 150, `30%`
  autoUpdate: boolean // update position when window scroll/resize/drag
});

Relative position supports 12 placements:

OutsidePlacement.BOTTOM;
OutsidePlacement.BOTTOM_LEFT;
OutsidePlacement.BOTTOM_RIGHT;
OutsidePlacement.LEFT;
OutsidePlacement.LEFT_BOTTOM;
OutsidePlacement.LEFT_TOP;
OutsidePlacement.RIGHT;
OutsidePlacement.RIGHT_BOTTOM;
OutsidePlacement.RIGHT_TOP;
OutsidePlacement.TOP;
OutsidePlacement.TOP_LEFT;
OutsidePlacement.TOP_RIGHT;

Global position

Overlay position that is relative to window viewport. These are used in modals, alerts, toastr

new GlobalPosition({
  placement: InsidePlacement, // location of the content.
  width: string | number, // content width eg, `auto`, `150`, `30%`
  height: string | number, //content height eg, `auto`, 150, `30%`
  offset: number // oustide space of the content, in px
});

Global position supports 9 placements:

InsidePlacement.BOTTOM;
InsidePlacement.BOTTOM_LEFT;
InsidePlacement.BOTTOM_RIGHT;
InsidePlacement.LEFT;
InsidePlacement.RIGHT;
InsidePlacement.TOP;
InsidePlacement.TOP_LEFT;
InsidePlacement.TOP_RIGHT;
InsidePlacement.CENTER;

Slide position

Overlay position that is relative to window viewport. These are used in side panels, sidebars, blade

new SlidePosition({
  placement: SlidePlacement, // rigth or left
  width: string // width eg, '300px' or '30%'
});

Slide position supports 2 placements:

SlidePlacement.LEFT;
SlidePlacement.RIGHT;

Fullscreen position

Overlay that occupies complete size of the viewport.

new FullScreenPosition();

Configuration

this.toppy
  .position(position: ToppyPosition)
  .config(configuration: ToppyConfig = {})
  .content('hello')
  .create();
property for
backdrop boolean · whether to show backdrop layer · default: false
closeOnEsc boolean · clicking Escape button will close overlay · default: false
closeOnDocClick boolean · dismiss on clicking outside of content · default: false
listenWindowEvents boolean · auto adjust the position on scroll/resize · default: true
containerClass string · overlay container class name · default: t-overlay
wrapperClass string · overlay wrapper class name · default: ''
backdropClass string · overlay backdrop class name · default: ''
bodyClass string · body class when overlay is open · default: t-open
windowResizeCallback function · triggered on window scroll
docClickCallback function · triggered on document click

Component communication

Component Data

When you host a component, you can control the overlay through ToppyOverlay service. Using this service you can access all properties that is provided in content. Also the properties comes with close.

this.overlay = this._toppy
  .position(position)
  .content(HelloComponent, { propName: 'toppy-test-prop' })
  .create();

this.overlay.listen('t_compins').subscribe(comp => {
  console.log('component is ready!', comp); // returns HelloComponent
});
// host component
@Component({
  template: '<div>Some text</div>'
})
export class HelloComponent {
  constructor(public overlay: ToppyOverlay) {
    console.log(this.overlay.props.propName); // will return 'toppy-test-prop'
  }

  close() {
    this.overlay.close();
  }
}

Template Data

This is very similar to above one. When you use template as a content, you can pass additional data to it.

this.overlay = this._toppy
  .position(position)
  .content(template, { name: 'Johny' })
  .create();

Then in your template you can refer the data like this,

<ng-template #tpl let-toppy>
  <div>Hello <span [innerText]="toppy.name"></span> !</div>
  <button (click)="toppy.close()">Close</button>
</ng-template>

Method close is automatically binded.

Plain text

When you use Plain text as a content, optionally you can able to set a class name to that div block.

this.overlay = this._toppy
  .position(position)
  .content('some content', { class: 'tooltip' })
  .create();

API


/* Toppy */

Toppy.position(position: ToppyPosition):Toppy

Toppy.config(config: ToppyConfig):Toppy

Toppy.content(data: ContentData, props: ContentProps = {}):Toppy

Toppy.create(key: string = ''):ToppyControl

Toppy.getCtrl(id: string):ToppyControl

Toppy.destroy():void

/* ToppyControl */

ToppyControl.open():void

ToppyControl.close():void

ToppyControl.toggle():void

ToppyControl.onDocumentClick():Observable<any>

ToppyControl.onWindowResize():Observable<any>

ToppyControl.changePosition(newPosition: ToppyPosition): void

ToppyControl.updateContent(content: ContentData, props: ContentProps = {}):void

ToppyControl.updatePosition(config:object):ToppyControl

ToppyControl.listen(eventName:string):Observable<any>
/* events */

`t_open`, `t_close`, `t_dynpos`, `t_detach`, `t_posupdate`, `t_compins`;

Contribution

Any kind of contributions ( Typo fix, documentation, code quality, performance, refactor, pipeline, etc., ) are welcome. :)

Credits

▶ Icons ━ icons8

▶ Illustrations ━ undraw

▶ Font icons ━ feathers

Issues

Found a bug? Have some idea? Or do you have questions? File it here

Licence

MIT

changelog

2.3.4 (2019-07-31)

Bug Fixes

2.3.3 (2019-03-13)

Bug Fixes

  • relative positon incorrect placement on autoUpdate (f3c0106)

2.3.2 (2019-02-12)

2.3.1 (2019-02-10)

2.3.0 (2019-01-05)

Features

  • add support to access host component instance (6cf180a)

2.2.0 (2018-12-31)

Features

  • attach component custom props on initialize without ToppyOverlay (e96a5f4)

2.1.0 (2018-12-25)

Bug Fixes

  • re-render when nested dynamic component (68f5844)

Features

  • add position classes to warpper element (79fc1a5)
  • add position classes to warpper element (b8ffb44)

2.0.5 (2018-12-24)

2.0.4 (2018-12-20)

Bug Fixes

  • allow custom props to be accessed in host component (9d8e4ba)

2.0.3 (2018-12-15)

2.0.2 (2018-12-15)

Bug Fixes

  • incorrect signature in API (c80658e)

2.0.1 (2018-12-14)

2.0.0 (2018-12-14)

Bug Fixes

  • remove unused components (10a3fd8)
  • ss class method [skip ci] (521c2d1)

Features

  • access custom props in Component, Template and Plain text contents (b8550c8)
  • add template ref context (e3e4ff1), closes #19
  • added support for custom key to reference later (3901878)
  • include context data in templateref (a865929)

BREAKING CHANGES

  • API has been changed. .overlay() to .position() , .host() to .content() , updateHost() to updateContent() , ToppyRef to ToppyControl. Introduced .config(). Performance improvement.

1.3.1 (2018-12-05)

Bug Fixes

  • global position: calculate proper left and top co-ordinates when hostWidth and hostHeight in percentage (59dd68b)

1.3.0 (2018-12-04)

Features

  • relative position: added support for content sticking with target element when autoUpdate is set to true (6fa37e7)

1.2.4 (2018-11-28)

Bug Fixes

  • replace codeclimate with codecov config in travis (af9d16f)

1.2.3 (2018-11-28)

Bug Fixes

  • docs: update font path (29834a5)
  • move artifacts after build (d7ff131)

1.2.2 (2018-11-28)

Bug Fixes

  • updated vulnerable dependencies (ace393d)
  • docs: update icon fonts path (eedca4c)

1.2.1 (2018-11-26)

Bug Fixes

  • docs: set font icons path properly (1d26809)
  • docs: set font icons path properly (0ae8424)

1.2.0 (2018-11-24)

Features

  • added new config prop 'bodyClassNameOnOpen' (0e047bf)
  • add new config 'closeOnEsc' (fc1577e)
  • added support to change content after create (02182b3)
  • added version selector in doc (7554f7b)
  • minor improvements and fixes (8152b1c)

1.1.1 (2018-11-22)

Bug Fixes

  • multiple toppy config override (#8) (ca0ce7e)

1.1.0 (2018-11-18)

Features

  • fade in content once position is updated (3520ef6)
  • fade in content only after final position is updated (abbfc8f)

1.0.18 (2018-11-16)

1.0.17 (2018-11-13)

1.0.16 (2018-11-09)

1.0.15 (2018-11-06)

1.0.14 (2018-11-03)

1.0.13 (2018-11-02)

Bug Fixes

  • override files on ghpages instead of replace (d282266)

1.0.12 (2018-11-02)

Bug Fixes

  • add and sort details in package file (6db7797)
  • merge conflict resolve (a412259)

1.0.11 (2018-11-01)

Bug Fixes

  • move archive logic to verfiyRelease stage (cdce3c1)

1.0.10 (2018-11-01)

Bug Fixes

  • archive version before publishing (6dd714a)

1.0.9 (2018-11-01)

Bug Fixes

  • publish gh-pages with achived versions (19a34c2)

1.0.8 (2018-11-01)

Bug Fixes

1.0.7 (2018-11-01)

Bug Fixes

  • set archive version properly (b01d0ab)

1.0.6 (2018-11-01)

Bug Fixes

  • archive doc build versions (06226c4)

1.0.5 (2018-11-01)

Bug Fixes

  • change gh pages publish package (a5ef149)

1.0.4 (2018-11-01)

Bug Fixes

  • update build command in package json (0ed8a10)

1.0.3 (2018-11-01)

Bug Fixes

  • remove assets release in github (8fb1ec8)

1.0.2 (2018-11-01)

Bug Fixes

  • release doc assets to github pages (87b460b)

1.0.1 (2018-11-01)

1.0.0 (2018-11-01)

Bug Fixes

  • package json scripts and travis (bc4f996)

Features

  • add support for ghpages auto publish (a8c8b7c)
  • initial commit (6464f1a)
  • docs: installation API update (a80fab0)