Search results

Getting started with Angular Tooltip component

01 Dec 2022 / 4 minutes to read

This section briefly explains how to create a simple Tooltip component and configure its available functionalities in angular.

Dependencies

The following list of dependencies are required to use Tooltip component in your application.

Copied to clipboard
|-- @syncfusion/ej2-angular-popups
    |-- @syncfusion/ej2-base
    |-- @syncfusion/ej2-angular-base
    |-- @syncfusion/ej2-popups
        |-- @syncfusion/ej2-buttons

Setup Angular Environment

You can use Angular CLI to setup your Angular applications. To install Angular CLI use the following command.

Copied to clipboard
npm install -g @angular/cli

Create an Angular Application

Start a new Angular application using below Angular CLI command.

Copied to clipboard
ng new my-app
cd my-app

Installing Syncfusion Tooltip package

Syncfusion packages are distributed in npm as @syncfusion scoped packages. You can get all the Angular Syncfusion package from npm link.

Currently, Syncfusion provides two types of package structures for Angular components,

  1. Ivy library distribution package format
  2. Angular compatibility compiler(Angular’s legacy compilation and rendering pipeline) package.

Ivy library distribution package

Syncfusion Angular packages(>=20.2.36) has been moved to the Ivy distribution to support the Angular Ivy rendering engine and the package are compatible with Angular version 12 and above. To download the package use the below command.

Add @syncfusion/ej2-angular-popups package to the application.

Copied to clipboard
npm install @syncfusion/ej2-angular-popups --save

Angular compatibility compiled package(ngcc)

For Angular version below 12, you can use the legacy (ngcc) package of the Syncfusion Angular components. To download the ngcc package use the below.

Add @syncfusion/ej2-angular-popups@ngcc package to the application.

Copied to clipboard
npm install @syncfusion/ej2-angular-popups@ngcc --save

To mention the ngcc package in the package.json file, add the suffix -ngcc with the package version as below.

Copied to clipboard
@syncfusion/ej2-angular-popups:"20.2.38-ngcc"

Note: If the ngcc tag is not specified while installing the package, the Ivy Library Package will be installed and this package will throw a warning.

Registering Tooltip Module

  • Import Tooltip module into Angular application(app.module.ts) from the package @syncfusion/ej2-angular-popups.
Copied to clipboard
import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';

//Syncfusion ej2-angular-popups module
import { TooltipModule } from '@syncfusion/ej2-angular-popups';

import { AppComponent }  from './app.component';

@NgModule({
  imports:      [ BrowserModule, TooltipModule ], //declaration of TooltipModule module into NgModule
  declarations: [ AppComponent ],
  bootstrap:    [ AppComponent ]
})
export class AppModule { }

Adding CSS Reference

  • Add Tooltip component’s styles as given below in styles.css.

[style.css]

Copied to clipboard
@import "../node_modules/@syncfusion/ej2-base/styles/material.css";
@import "../node_modules/@syncfusion/ej2-angular-buttons/styles/material.css";
@import "../node_modules/@syncfusion/ej2-angular-popups/styles/material.css";

We can also use CRG to generate combined component styles.

Add Tooltip component

Modify the template in app.component.ts file to render the Tooltip component. Add the Angular Tooltip by using <ejs-tooltip> selector in template section of the app.component.ts file.

Copied to clipboard
import { Component, ViewEncapsulation } from '@angular/core';

@Component({
  selector: 'my-app',
  template: `
    <ejs-tooltip id='tooltip' content='Tooltip content'>
        Hover Me
    </ejs-tooltip>`,
  encapsulation: ViewEncapsulation.None
})
export class AppComponent  { }

Run the application

  • Now, run the application in the browser using the following command.
Copied to clipboard
ng serve --open

The following code example depicts the way to initialize Tooltip on a single element.

Source
Preview
app.component.ts
app.module.ts
main.ts
Copied to clipboard
import { Component, ViewEncapsulation } from '@angular/core';

@Component({
  selector: 'my-app',
  template: `
<div id='container' style="display: inline-block; position: relative; left: 50%;top: 100px;transform: translateX(-50%);">
    <ejs-tooltip id='tooltip' content='Tooltip content' target="#target">
        <button ejs-button id="target">Show Tooltip</button>
    </ejs-tooltip>
</div>`,
  encapsulation: ViewEncapsulation.None
})
export class AppComponent  { }
Copied to clipboard
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { TooltipModule } from '@syncfusion/ej2-angular-popups';
import { ButtonModule } from '@syncfusion/ej2-angular-buttons';
import { AppComponent } from './app.component';

/**
 * Module
 */
@NgModule({
    imports: [
        BrowserModule,
        TooltipModule,
        ButtonModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
export class AppModule { }
Copied to clipboard
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { AppModule } from './app.module';

enableProdMode();
platformBrowserDynamic().bootstrapModule(AppModule);

Initialize Tooltip within a container

It is possible to create Tooltip on multiple targets within a container. To do so, define the selector property with specific target elements - so that the tooltip will be initialized only on those matched targets within a container. In this case, the Tooltip content gets assigned from the title attribute of the target element.

Refer the following code example, to create a Tooltip on multiple targets within a container.

Source
Preview
app.component.ts
app.module.ts
main.ts
index.css
Copied to clipboard
import { Component } from '@angular/core';

@Component({
selector: 'my-app',
template: `
    <div id="tool">
      <ejs-tooltip target='.e-info' position='RightCenter'>
        <form id="details" role="form">
          <table>
            <tr>
                <td class="info">User Name</td>
                <td>
                    <input type="text" class="e-info" name="firstname" title="Please enter your name"> </td>
            </tr>
            <tr>
                <td class="info">Email Address</td>
                <td>
                    <input type="text" class="e-info" name="email" title="Enter a valid email address">
                </td>
            </tr>
            <tr>
                <td class="info">Password</td>
                <td>
                    <input type="password" class="e-info" name="password" title="Be at least 8 characters length">
                </td>
            </tr>
            <tr>
                <td class="info">Confirm Password</td>
                <td>
                    <input type="password" class="e-info" name="Cpwd" title="Re-enter your password">
                </td>
            </tr>
            <tr>
                <td><input ejs-button id="sample" class="center" type="submit" value="Submit"></td>
                <td><input ejs-button id="clear" class="center" type="reset" value="Reset"></td>
            </tr>
          </table>
        </form>
      </ejs-tooltip>
    </div>
    `,
})

export class AppComponent { }
Copied to clipboard
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { TooltipModule } from '@syncfusion/ej2-angular-popups';
import { ButtonModule } from '@syncfusion/ej2-angular-buttons';
import { AppComponent } from './app.component';

/**
 * Module
 */
@NgModule({
    imports: [
        BrowserModule,
        TooltipModule,
        ButtonModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
export class AppModule { }
Copied to clipboard
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { AppModule } from './app.module';

enableProdMode();
platformBrowserDynamic().bootstrapModule(AppModule);
Copied to clipboard
#loader {
  color: #008cff;
  font-family: 'Helvetica Neue', 'calibiri';
  font-size: 16px;
  height: 40px;
  left: 45%;
  position: absolute;
  top: 45%;
  width: 30%;
}
#tool {
  width: 500px;
  margin: auto;
 transform: translateX(10%);
}
#details table th,
#details table td {
  padding: 20px 10px;
  text-align: left;
}

#details .info {
  font-size: 14px;
  text-align: left;
  font-weight: 500;
}

.center {
  position: relative;
  left: 50%;
  transform: translateX(-50%);
}

In the above sample, details refers to the container’s id, and the target .e-info refers to the target elements available within that container.

See Also

Positioning Tooltip

Tooltip Open Mode

Customize the Tooltip