Primitives
Display additional information on hover.
import { Component } from "@angular/core";
import { NgpButton } from "ng-primitives/button";
import { NgpTooltip, NgpTooltipTrigger } from "ng-primitives/tooltip";
@Component({
selector: "app-tooltip",
imports: [NgpTooltipTrigger, NgpTooltip, NgpButton],
template: `
<button [ngpTooltipTrigger]="tooltip" ngpButton type="button">
Tooltip
</button>
<ng-template #tooltip>
<div ngpTooltip>
Hover over items to reveal additional context or details. Tooltips
provide quick insights without cluttering your screen.
</div>
</ng-template>
`,
styles: `
button {
padding-left: 1rem;
padding-right: 1rem;
border-radius: 0.5rem;
color: var(--ngp-text-primary);
outline: none;
height: 2.5rem;
font-weight: 500;
background-color: var(--ngp-background);
transition: background-color 300ms cubic-bezier(0.4, 0, 0.2, 1);
box-shadow: var(--ngp-button-shadow);
}
button[data-hover] {
background-color: var(--ngp-background-hover);
}
button[data-focus-visible] {
outline: 2px solid var(--ngp-focus-ring);
}
button[data-press] {
background-color: var(--ngp-background-active);
}
[ngpTooltip] {
position: absolute;
max-width: 16rem;
border-radius: 0.5rem;
background-color: var(--ngp-background-inverse);
padding: 0.5rem 0.75rem;
border: none;
font-size: 0.75rem;
font-weight: 500;
color: var(--ngp-text-inverse);
animation: tooltip-show 200ms ease-in-out;
transform-origin: var(--ngp-tooltip-transform-origin);
}
[ngpTooltip][data-exit] {
animation: tooltip-hide 200ms ease-in-out;
}
@keyframes tooltip-show {
0% {
opacity: 0;
transform: scale(0.9);
}
100% {
opacity: 1;
transform: scale(1);
}
}
@keyframes tooltip-hide {
0% {
opacity: 1;
transform: scale(1);
}
100% {
opacity: 0;
transform: scale(0.9);
}
}
`,
})
export default class TooltipExample {}
Import the Tooltip primitives from ng-primitives/tooltip
.
import { NgpTooltip, NgpTooltipTrigger } from 'ng-primitives/tooltip';
Assemble the tooltip directives in your template.
<button [ngpTooltipTrigger]="tooltip" ngpButton>Hover me</button>
<ng-template #tooltip>
<div ngpTooltip>Tooltip content</div>
</ng-template>
Create a tooltip component that uses the NgpTooltip
directive.
import { Component } from '@angular/core';
import { NgpButton } from 'ng-primitives/button';
import { TooltipTrigger } from './tooltip-trigger';
@Component({
selector: 'app-root',
imports: [TooltipTrigger, NgpButton],
template: `
<button ngpButton appTooltipTrigger="Tooltip content here">Show tooltip</button>
`,
styles: `
[ngpButton] {
padding-left: 1rem;
padding-right: 1rem;
border-radius: 0.5rem;
color: var(--ngp-text-primary);
outline: none;
height: 2.5rem;
font-weight: 500;
border: none;
background-color: var(--ngp-background);
transition: background-color 300ms cubic-bezier(0.4, 0, 0.2, 1);
box-shadow: var(--ngp-button-shadow);
}
[ngpButton][data-hover] {
background-color: var(--ngp-background-hover);
}
[ngpButton][data-focus-visible] {
outline: 2px solid var(--ngp-focus-ring);
}
[ngpButton][data-press] {
background-color: var(--ngp-background-active);
}
`,
})
export default class App {}
Generate a reusable tooltip component using the Angular CLI.
ng g ng-primitives:primitive tooltip
path
: The path at which to create the component file.prefix
: The prefix to apply to the generated component selector.componentSuffix
: The suffix to apply to the generated component class name.fileSuffix
: The suffix to apply to the generated component file name. Defaults to component
.exampleStyles
: Whether to include example styles in the generated component file. Defaults to true
.The following directives are available to import from the ng-primitives/tooltip
package:
Apply the `ngpTooltip` directive to an element that represents the tooltip. This typically would be a `div` inside an `ng-template`.[ngpTooltip]
ngpTooltip
Attribute | Description |
---|---|
data-enter |
Applied when the tooltip is being added to the DOM. This can be used to trigger animations. |
data-exit |
Applied when the tooltip is being removed from the DOM. This can be used to trigger animations. |
The following CSS custom properties are applied to the ngpTooltip
directive:
Property | Description |
---|---|
--ngp-tooltip-transform-origin |
The transform origin of the tooltip for animations. |
--ngp-tooltip-trigger-width |
The width of the trigger element. |
Apply the `ngpTooltipTrigger` directive to an element that triggers the tooltip to show. Access the tooltip template ref.
Define if the trigger should be disabled.
Define the placement of the tooltip relative to the trigger.
Define the offset of the tooltip relative to the trigger.
Define the delay before the tooltip is displayed.
Define the delay before the tooltip is hidden.
Define whether the tooltip should flip when there is not enough space for the tooltip.
Define the container in which the tooltip should be attached.
Provide context to the tooltip. This can be used to pass data to the tooltip content.
[ngpTooltipTrigger]
ngpTooltipTrigger
Attribute | Description |
---|---|
data-open |
Applied when the tooltip is open. |
data-disabled |
Applied when the tooltip is disabled. |
For the tooltip to be positioned correctly relative to the trigger element, it must use absolute or fixed positioning. For example, you can use the following CSS:
[ngpTooltip] {
position: absolute;
}
The ngpTooltip
primitive adds a CSS custom property --ngp-tooltip-transform-origin
to the element that can be used to animate the tooltip from the trigger element.
The ngpTooltip
will also add the data-enter
and data-exit
attributes to the element when it is being added or removed from the DOM. This can be used to trigger animations.
:host[data-enter] {
animation: fade-in 0.2s ease-in-out;
}
:host[data-exit] {
animation: fade-out 0.2s ease-in-out;
}
You can configure the default options for all tooltips in your application by using the provideTooltipConfig
function in a providers array.
import { provideTooltipConfig } from 'ng-primitives/tooltip';
bootstrapApplication(AppComponent, {
providers: [
provideTooltipConfig({
offset: 4,
placement: 'top',
showDelay: 0,
hideDelay: 0,
flip: true,
container: document.body,
}),
],
});
Define the offset from the trigger element.
Define the placement of the tooltip.
Define the delay before the tooltip shows.
Define the delay before the tooltip hides.
Define if the tooltip should flip when it reaches the edge of the viewport.
Define the container element for the tooltip. This is the document body by default.
Copyright © 2025 Angular Primitives