ng-tooltip-directive is a versatile and customizable tooltip library designed for Angular and Ionic/Angular applications
- Dynamic positioning: Automatically adjusts when overflowing screen boundaries.
- Customizable styles: Modify tooltip styles using CSS variables or custom classes.
- Seamless integration: Easily integrates with Angular and Ionic frameworks (angular).
To install the library, run the following command:
npm install @duyvu-fsdev/ng-tooltip-directive
npm install @duyvu-fsdev/ng-tooltip-directive --legacy-peer-deps
1. Import the module
/* *.module.ts */
import { TooltipModule } from '@duyvu-fsdev/ng-tooltip-directive';
@NgModule({
imports: [..., TooltipModule],
})
export class YourModule {}
2. Add the tooltip to your template
/* *.html */
<host-element tooltip [tooltipOption]="tooltipOption">
<!-- content -->
</host-element>
// *.ts
import { Option } from '@duyvu-fsdev/ng-tooltip-directive';
...
export class YourComponent {
...
tooltipOption: Option = {
position: 'bottom',
text: 'Hello, this is a tooltip!',
class: 'custom-tooltip'
};
}
Property | Type | Default | Description | Example |
---|---|---|---|---|
position |
'top' | 'bottom' | 'left' | 'right'| undefined |
'bottom' |
Position of the tooltip relative to the host | 'bottom' |
text |
string |
'' |
Tooltip text content | 'Tooltip text content' |
class |
string |
'' |
CSS class for custom styling | 'custom-tooltip' |
/* global.scss */
.app-tooltip {
--tooltip-background: #fff;
--tooltip-color: #000;
--tooltip-border-radius: 4px;
--tooltip-padding: 8px;
--tooltip-max-width: 200px;
--tooltip-height: auto;
}
tooltipOption: Option = {
...
class: 'custom-tooltip' /* Custom class for styling */
};
/* global.scss */
.app-tooltip.custom-tooltip {
--tooltip-background: #fff;
--tooltip-color: #000;
--tooltip-border-radius: 4px;
--tooltip-padding: 8px;
--tooltip-max-width: 200px;
--tooltip-height: auto;
}
/* global.scss */
.custom-tooltip {
background: #f00 !important;
color: #fff !important;
padding: 10px !important;
border-radius: 8px !important;
}
Additionally, to avoid cases where the tooltip may not display correctly for elements like span, h1, etc., you should set box-sizing for the host-element.
.host-element-css {
box-sizing: border-box;
}
Or set box-sizing for all elements in your project (recommended):
/* global.scss */
* {
box-sizing: border-box;
}
Demo on stackblitz
1.0.1 - Update Usage
1.1.0 - Fix styles, Update Usage
1.1.1 - Update demo 1.1.1 - Update demo