Skip to content

Build your personal knowledge base with Trilium Notes

919 B
Raw Blame
import { useEffect, useLayoutEffect, useRef } from "preact/hooks";
import { CalendarOptions, Calendar as FullCalendar } from "fullcalendar";
import { RefObject } from "preact";

export interface CalendarProps extends CalendarOptions {
    calendarRef?: RefObject<FullCalendar | null>;
}

export default function Calendar({ calendarRef, ...options }: CalendarProps) {
    const containerRef = useRef<HTMLDivElement>(null);

    useLayoutEffect(() => {
        if (!containerRef.current) return;

        const calendar = new FullCalendar(containerRef.current, options);
        calendar.render();

        if (calendarRef) {
            calendarRef.current = calendar;
        }

        return () => calendar.destroy();
    }, [ ]);

    useEffect(() => {
        calendarRef?.current?.resetOptions(options);
    }, [ options ]);

    return (
        <div ref={containerRef} className="calendar-container" />
    );
}