This method attaches a rich, flexible tooltip using Tippy.js on FullCalendar events in Angular.
Great question! You want to encapsulate your tooltip logic inside a dedicated Angular component — let’s call it EventTooltipComponent — and then use it with FullCalendar’s events, rendering each tooltip via Tippy.js.
Approach Overview
Create an EventTooltipComponent that receives the event data and renders the tooltip content.
In eventDidMount, instantiate a Tippy tooltip attached to the event element (info.el), and use the Angular component as the tooltip content — i.e., mount the Angular component dynamically inside a DOM element and pass that element to Tippy as the tooltip content.
This keeps all tooltip-related logic and template inside that reusable Angular component.
Step-by-step solution
1. Generate the tooltip component
bash
ng generate component event-tooltip
2. Implement the EventTooltipComponent
Make it accept event data as an Input and display tooltip content accordingly.
3. Update CalendarComponent to dynamically render EventTooltipComponent inside the tooltip
Because Tippy expects a DOM node as content, you can't pass the Angular component directly, but you can:
Use ViewContainerRef and ComponentFactoryResolver (or ViewEngine API / Angular 13+ dynamic component API) to programmatically create an instance of your component,
Attach it to a detached element,
Pass that element to Tippy.
Example:
typescript
// calendar.component.ts
import {
Component,
ViewContainerRef,
ComponentRef,
AfterViewInit,
OnDestroy,
} from '@angular/core';
import { CalendarOptions, EventApi, EventInput } from '@fullcalendar/angular';
import tippy, { Instance as TippyInstance } from 'tippy.js';
import 'tippy.js/dist/tippy.css';
import { EventTooltipComponent } from '../event-tooltip/event-tooltip.component'; // adjust path as needed
@Component({
selector: 'app-calendar',
template: `<full-calendar [options]="calendarOptions"></full-calendar>`,
})
export class CalendarComponent implements OnDestroy {
private tippyInstances: TippyInstance[] = [];
private componentRefs: ComponentRef<EventTooltipComponent>[] = [];
events: EventInput[] = [
{
id: '1',
title: 'Meeting',
date: '2024-06-10',
extendedProps: { description: 'Discuss project milestones' },
},
{ id: '2', title: 'Lunch', date: '2024-06-11', extendedProps: { description: 'Lunch with team' } },
];
calendarOptions: CalendarOptions = {
initialView: 'dayGridMonth',
events: this.events,
eventDidMount: this.handleEventDidMount.bind(this),
};
constructor(private vcr: ViewContainerRef) {}
handleEventDidMount(info: any) {
// Create wrapper element for tooltip content
const tooltipWrapper = document.createElement('div');
// Create and render the Angular component dynamically inside that wrapper
const compRef = this.vcr.createComponent(EventTooltipComponent);
compRef.instance.event = info.event;
compRef.changeDetectorRef.detectChanges();
// Append the rendered component root node to the wrapper div
tooltipWrapper.appendChild(compRef.location.nativeElement);
// Store reference to properly destroy later
this.componentRefs.push(compRef);
// Initialize Tippy with that wrapper as content
const tipInstance = tippy(info.el, {
content: tooltipWrapper,
allowHTML: true,
placement: 'top',
animation: 'shift-away',
theme: 'light-border',
interactive: true,
delay: [300, 200],
});
// Store tooltip instance so we can destroy on component destroy
this.tippyInstances.push(tipInstance[0]);
}
ngOnDestroy() {
// Clean up all Tippy instances and Angular components
this.tippyInstances.forEach(instance => instance.destroy());
this.componentRefs.forEach(compRef => compRef.destroy());
}
}
4. Brief explanation of key points
Dynamic creation: this.vcr.createComponent(EventTooltipComponent) creates a component instance dynamically.
Set Input: We pass the event to that component instance.
Append to a detached div: The component renders inside tooltipWrapper (a div), which is then passed to Tippy.
Tippy gets a DOM node: It can now display your Angular component as the tooltip content.
Maintain references: Keep the ComponentRefs and TippyInstances so you can destroy them properly when the calendar component is destroyed.
Interactive tooltip: Allows mouse interaction with tooltip content if needed.
5. Bonus: if you want to show/hide tooltip on hover only (default Tippy behavior) everything is already done here.
Summary
This structure cleanly separates tooltip UI logic into its own Angular component while leveraging Tippy.js’s rich tooltip capabilities for FullCalendar event elements.
If you want, I can also help with a full working example. Just let me know!