17.2.2 • Published 25 days ago

ngx-tooltip-directives v17.2.2

Weekly downloads
-
License
MIT
Repository
github
Last release
25 days ago

ngx-tooltip-directives

npm version build status codecov

This library offers three different tooltip directives (string, html and template) and draws inspiration from the no longer maintained ng2-tooltip-directive.

It is compatible with Angular 17 and above.

Tooltips are informative pop-up tips that appear when you hover over or click on an item, providing helpful additional information or guidance.


Demo

https://mkeller1992.github.io/ngx-tooltip-directives/


Setup

To install the library, enter the following command in your console:

npm i ngx-tooltip-directives
    

Import NgxTooltipDirectivesModule:

import { NgxTooltipDirectivesModule } from 'ngx-tooltip-directives';
 
@NgModule({
    imports: [ NgxTooltipDirectivesModule ]
}) 

3 ways of setting the tooltip content

1 - Pass the tooltip text as a string via tooltipStr:

<div tooltipStr="Tooltip text">Show Tooltip</div>

2 - Pass the tooltip content as SafeHtml via tooltipHtml:

<div [tooltipHtml]="safeTooltipHtml" placement="right">Show Html Tooltip</div>
import { DomSanitizer, SafeHtml } from '@angular/platform-browser';
// Code skipped for brevity

export class AppComponent {
  rawHtml: string = '<div><p>This is a <strong>tooltip</strong> with HTML</p></div>';
  safeTooltipHtml: SafeHtml;

  constructor(private sanitizer: DomSanitizer){ 
    this.safeTooltipHtml = this.sanitizer.bypassSecurityTrustHtml(this.rawHtml);
  }
}

3 - Pass the tooltip content as template via tooltipTemplate:

<ng-template #myTemplate>
  <div style="color: blue; font-weight: bold;">
    Tooltip Template
  </div>
</ng-template>

<div [tooltipTemplate]="myTemplate" placement="right">Show Tooltip Template</div>

Trigger tooltip programmatically

<div tooltip [tooltipStr]="'Tooltip text'" #myTooltip="tooltipStr"></div>

<button class="btn btn-small btn-outline btn-rounded" (click)="show()">show() via component.ts</button>
<button class="btn btn-small btn-outline btn-rounded" (click)="hide()">hide() via component.ts</button>
@ViewChild('myTooltip')
tooltip!: TooltipStrDirective;

show() {
  this.tooltip.show();
}

hide() {
  this.tooltip.hide();
}

3 ways of setting tooltip options

1 - Options can be set via html-attributes, so they have the highest priority:

<div tooltipStr="Tooltip on the right" textAlign="left" placement="right">Show Tooltip</div>

2 - Options can be passed to the tooltips as TooltipOptions object:

<div tooltipStr="Tooltip on the right" [options]="myOptions">Show Tooltip</div>
myOptions: TooltipOptions = {
    'placement': 'right',
    'showDelay': 500
}

3 - Options can be set globally when importing the module:

import { TooltipModule, TooltipOptions } from 'ngx-tooltip-directives';

const myDefaultTooltipOptions: TooltipOptions = {
  'show-delay': 800
}

@NgModule({
    imports: [ 
      TooltipModule.forRoot(myDefaultTooltipOptions)
    ]
})

Properties

nametypedefaultdescription
idstring | number0A custom id that can be assigned to the tooltip.
placementPlacement'top'The position of the tooltip.
autoPlacementbooleantrueIf true, the tooltip will be placed so that it does not go beyond the borders of the browser window.
contentTypeContentType'string'The type of content passed to the tooltip.
textColorstring'black'The color of the tooltip text.
backgroundColorstring'white'The background color of the tooltip.
borderColorstring'blue'The border color of the tooltip.
textAlign"left" | "center" | "right"'center'The horizontal alignment of the tooltip text.
paddingstring'10px 13px 10px 13px'The padding around the tooltip text (top, right, bottom, left).
shadowbooleantrueIf true, the tooltip will have a shadow.
showDelaynumber0The delay in ms before the tooltip is shown.
hideDelaynumber0The delay in ms before the tooltip is removed.
hideDelayTouchscreennumber0The delay in ms before the tooltip is hidden on mobile devices.
zIndexnumber0The z-index of the tooltip.
animationDurationnumber100The duration in ms that the animation takes to run from start to finish.
trigger"hover" | "click"'hover'Specifies how the tooltip is triggered. The closing time is controlled with "hide-delay".
tooltipClassstring''Any additional classes to be passed to the tooltip (target them with ::ng-deep).
displaybooleantrueIf true, the tooltip is available for display.
displayTouchscreenbooleantrueIf true, the tooltip will be displayed on mobile devices.
offsetnumber8The offset of the tooltip relative to the item.
maxWidthstring'200px'The maximum width of the tooltip.
hideDelayAfterClicknumberundefinedThe delay in ms before hiding the tooltip when the "click" trigger is used.
pointerEvents"auto" | "none"'auto'Defines whether or not the tooltip reacts to pointer events.
position{top: number, left: number}undefinedThe coordinates of the tooltip relative to the browser window.

Events

Events are called in accordance with the delays specified in the options within the directive. By default, there is a 300-millisecond delay before the tooltip hides.

EventDescription
{type: "show", position: DOMRect}This event is fired prior to the tooltip's appearance.
{type: "shown", position: DOMRect}This event is fired following the tooltip's appearance animation.
{type: "hide", position: DOMRect}This event is fired prior to the tooltip being hidden.
{type: "hidden", position: DOMRect}This event is fired after the tooltip hiding animation completes.

Methods

If you have defined the directive options, these will be taken into consideration when calling the methods. This includes the delay before the tooltip appears and before it hides.

MethodDescription
show()Displays the tooltip.
hide()Hides the tooltip.

Testing with NgxTooltipDirectives

For easier unit testing of components that use NgxTooltipDirectives, we provide a set of mock directives and a mock module. You can use these mocks to bypass the actual directive behavior in your tests, focusing instead on the component logic.

Using the Mock Module

Import MockNgxTooltipDirectivesModule in your test suite's TestBed configuration:

import { TestBed } from '@angular/core/testing';
import { AppComponent } from './app.component';
import { MockNgxTooltipDirectivesModule } from '@ngx-tooltip-directives';

describe('AppComponent', () => {
  beforeEach(async () => {
    await TestBed.configureTestingModule({
      declarations: [AppComponent],
      imports: [MockNgxTooltipDirectivesModule]
    }).compileComponents();
  });

  // Your tests here
});
17.2.2

25 days ago

17.2.1

27 days ago

17.2.0

28 days ago

17.0.3

1 month ago

17.1.1

1 month ago

17.0.2

1 month ago

17.1.0

1 month ago

17.0.1

1 month ago

17.0.0

4 months ago

16.1.2

8 months ago

16.1.1

10 months ago

16.1.0

10 months ago

16.0.15

10 months ago

16.0.14

12 months ago

16.0.13

12 months ago

16.0.12

12 months ago

16.0.11

12 months ago

16.0.10

12 months ago

16.0.9

12 months ago

16.0.8

12 months ago

16.0.7

12 months ago

16.0.6

12 months ago

16.0.5

12 months ago

16.0.4

12 months ago

16.0.3

12 months ago

16.0.2

12 months ago

16.0.1

12 months ago

16.0.0

12 months ago