- Notifications
You must be signed in to change notification settings - Fork0
Angular 8+ wrapper for Tippy.js
License
xidedix/ngx-tippy-wrapper
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Angular 8+ wrapper forTippy.js
Example applicationhere
Code playgroundcodesandbox/ngx-tippy-wrapper
Install from npm:
npmingx-tippy-wrapper--save
ImportNgxTippyModule
:
import{NgxTippyModule}from'ngx-tippy-wrapper';
Then in your base module:
@NgModule({imports:[ ...,NgxTippyModule], ...})
Import base style file to your main style file:
@import'tippy.js/dist/tippy.css';
or angular.json:
"architect":{"build":{ ...,"options":{ ...,"styles":[...,"./node_modules/tippy.js/dist/tippy.css"]}
ApplyngxTippy
directive for element and pass content throughdata-tippy-content
attribute:
<buttonngxTippydata-tippy-content="Tooltip content"> Element with tooltip</button>
You can apply props withtippyProps
binding
In template:
<buttonngxTippydata-tippy-content="Tooltip content"class="t-demo__btn"[tippyProps]="{ arrow: false, placement: 'bottom' }"> Element with tooltip</button>
Or passprops
from component:
<spanngxTippydata-tippy-content="Tooltip content"[tippyProps]="tippyProps"> Element with tooltip</span>
...import{NgxTippyProps}from'ngx-tippy-wrapper';@Component({ ...})exportclassDemoComponentimplementsOnInit{tippyProps:NgxTippyProps={trigger:'click',allowHTML:true,}; ...}
Prop name | Type | Example |
---|---|---|
tippyProps | NgxTippyProps | [tippyProps]="{ arrow: false, placement: 'bottom' }" |
tippyName | string | tippyName="awesomeName" |
tippyClassName | string | tippyClassName="new-class" or tippyClassName="new-class another-class" |
tippyProps
-list of all props
tippyName
- name for tippy instance, required for accessing and control specific instance
tippyClassName
- add custom class to thetippy-box
element, support multiple classes passed as words separated by space
data
attribute:
<buttonngxTippydata-tippy-content="Tooltip content"> Element with tooltip</button>
...import{NgxTippyProps}from'ngx-tippy-wrapper';@Component({ ...})exportclassDemoComponentimplementsOnInit{bindedContent:string='Binded tooltip content'; ...}
Content binding works during componentinitialization, if new content should be set dynamic or reset again - usesetContent()
method
content
prop :
<buttonngxTippy[tippyProps]="{ allowHTML: true, content: '<p>Tooltip <strong>HTML</strong> content</p>' }"> Element with tooltip</button>
setContent()
method :
For this methodtippyName
prop should be setted
<buttonngxTippytippyName="content"> Element with tooltip</button>
...import{NgxTippyService}from'ngx-tippy-wrapper';@Component({ ...})exportclassDemoComponentimplementsOnInit,AfterViewInit{bindedContent:string='Binded tooltip content';constructor(privatetippyService:NgxTippyService){} ...ngAfterViewInit(){this.setContentForTooltip();}setContentForTooltip(){this.tippyService.setContent('content',this.bindedContent);}}
tippyProps
:
<buttonngxTippy[tippyProps]="tippyProps"> Element with tooltip</button>
...@Component({ ...})exportclassDemoComponentimplementsOnInit{tippyProps:NgxTippyProps={ ...} ...ngOnInit(){this.setContentForTooltip();}setContentForTooltip(){this.tippyProps.content='Initial tooltip content'}}
template
:
- Pass template reference directly
<button[ngxTippy]="tippyTemplate"tippyName="content"[tippyProps]="tippyContent"> Element with tooltip</button><div#tippyTemplate><h4>Caption</h4><p>Some content</p><button(click)="...">Action</button> ...</div>
- Pass
element
orelement.innerHTML
<div><buttonngxTippytippyName="content"[tippyProps]="tippyProps"> Element with tooltip</button><!-- If passing element itself --><div#tippyTemplate><h4>Caption</h4><p>Some content</p><button(click)="...">Action</button> ...</div><!-- If passing element innerHTML --><div#tippyTemplatestyle="display: none;"><h4>Caption</h4><p>Some content</p> ...</div></div>
...import{NgxTippyProps}from'ngx-tippy-wrapper';@Component({ ...})exportclassDemoComponentimplementsAfterViewInit{ @ViewChild('tippyTemplate',{read:ElementRef,static:false})tippyTemplate:ElementRef;tippyContent:NgxTippyProps={ ...};constructor(privatengxTippyService:NgxTippyService){}ngAfterViewInit(){this.setContentForTooltip();}setContentForTooltip(){consttemplate=this.tippyTemplate.nativeElement;// Pass element itselfthis.ngxTippyService.setContent('content',template);// or// Pass element innerHTMLthis.ngxTippyService.setContent('content',template.innerHTML);} ...}
For accessing and control specific tippy instance you need passtippyName
prop
Import and provideNgxTippyService
:
...import{NgxTippyService}from'ngx-tippy-wrapper';@Component({ ...})exportclassDemoComponentimplementsOnInit{constructor(privatetippyService:NgxTippyService){} ...}
Through service you can use all methods describedhere and some additional
Get instance(s)
Method name | Method parameter/parameters | Method short description |
---|---|---|
getInstance() | name : string | Get specific instance |
getInstances() | - | Get all tippy instances |
Instance methods
Method name | Method parameter/parameters | Method short description |
---|---|---|
show() | name : string | Programmatically show the tippy |
hide() | name : string | Programmatically hide the tippy |
hideWithInteractivity() | name : string,mouseEvent : MouseEvent | Will hide the tippy only if the cursor is outside of the tippy's interactive region |
disable() | name : string | Temporarily prevent a tippy from showing or hiding |
enable() | name : string | Re-enable a tippy |
setProps() | name : string,tippyProps : NgxTippyProps | Set/update any tippy props |
setContent() | name : string,tippyContent : NgxTippyContent | Set/update the content |
setTriggerTarget() | name : string,triggerTarget : Element | Element[] | Set/update the trigger source |
unmount() | name : string | Unmount the tippy from the DOM |
clearDelayTimeouts() | name : string | Clears the instances delay timeouts |
destroy() | name : string | Permanently destroy and clean up the tippy instance |
Static methods
Method name | Method parameter/parameters | Method short description |
---|---|---|
setDefaultProps() | tippyProps : NgxTippyProps | Set the default props for each new tippy instance |
showAll() | - | Show all tippies |
hideAll() | options? : NgxHideAllOptions | Hide all tippies or hide all except a particular one, additional hide them with duration |
It provides information in format:
{ name:string; reason:InstanceChangeReason; instance:NgxTippyInstance;}typeInstanceChangeReason=|'setInstance'|'show'|'hide'|'hideWithInteractivity'|'disable'|'enable'|'setProps'|'setContent'|'setTriggerTarget'|'unmount'|'clearDelayTimeouts'|'destroy';
...import{Subscription}from'rxjs';import{NgxTippyService}from'ngx-tippy-wrapper';@Component({ ...})exportclassDemoComponentimplementsOnInit,OnDestroy{privateinstancesChanges$:Subscription;constructor(privatetippyService:NgxTippyService){}ngOnInit(){this.subToInstancesChanges();}ngOnDestroy(){this.instancesChanges$&&this.instancesChanges$.unsubscribe();}subToInstancesChanges(){this.instancesChanges$=this.ngxTippyService.instancesChanges.subscribe((changes:InstancesChanges)=>{ ...});} ...}
If you want to give different tooltip content to many different elements, while only needing to initialize once with shared props usengx-tippy-group
component:
<ngx-tippy-group[tippyProps]="tippyProps"><buttondata-groupeddata-tippy-content="Tooltip content">Element with tooltip</button><buttondata-groupeddata-tippy-content="Tooltip content">Element with tooltip</button></ngx-tippy-group>
For each grouped tooltip you should passdata-grouped
attribute
Also content can be binded and shared props overridden (seecustomization):
<ngx-tippy-group[tippyProps]="tippyProps"><buttondata-grouped[attr.data-tippy-content]="bindedContent"> Element with tooltip</button><buttondata-grouped[attr.data-tippy-content]="bindedHTMLContent"data-tippy-allowHTML="true"> Element with tooltip</button><buttondata-groupeddata-tippy-content="Tooltip content"data-tippy-arrow="false"> Element with tooltip</button></ngx-tippy-group>
...import{NgxTippyProps}from'ngx-tippy-wrapper';@Component({ ...})exportclassDemoComponentimplementsOnInit{bindedContent:string='Binded tooltip content';bindedHTMLContent:string='<p>Binded <strong>HTML</strong> content</p>';tippyProps:NgxTippyProps={ ...}; ...}
For using multiple tooltips on a single element - nest elements with appliedngxTippy
directive:
<divngxTippydata-tippy-content="First tooltip content"[tippyProps]="{ ... }"><divngxTippydata-tippy-content="Second tooltip content"[tippyProps]="{ ... }"><buttonclass="t-demo__btn"ngxTippydata-tippy-content="Third tooltip content"[tippyProps]="{ ... }"> Element with tooltips</button></div></div>
Forsingleton using - put in tooltips insidengx-tippy-singleton
component:
<ngx-tippy-singleton[tippyProps]="{ ... }"><buttondata-singletondata-tippy-content="First tooltip content"> Element with tooltip</button><buttondata-singletondata-tippy-content="First tooltip content"> Element with tooltip</button></ngx-tippy-singleton>
To overrides generaltippyProps
by the individual tippyprops
:
<ngx-tippy-singleton[tippyProps]="{ ... }"><buttondata-singletondata-tippy-content="First tooltip content"data-tippy-placement="bottom"> Element with tooltip</button><buttondata-singletondata-tippy-content="First tooltip content"data-tippy-arrow="false"> Element with tooltip</button></ngx-tippy-singleton>
...import{NgxSingletonProps}from'ngx-tippy-wrapper';@Component({ ...})exportclassDemoComponentimplementsOnInit{tippyProps:NgxSingletonProps={ ...,overrides:['arrow','placement'],}; ...}
About
Angular 8+ wrapper for Tippy.js
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Languages
- TypeScript64.4%
- HTML19.6%
- JavaScript11.6%
- CSS4.4%