0

I'm trying to apply the PerfectScrollbar on FullCalendar but I sadly got:

PerfectScrollbar is not a function

that's weird because in other place of my application the PerfectScrollbar is successful applied.

This is my implementation for FullCalendar:

 $calendar = $('#fullCalendar');

 $calendar.fullCalendar({
  viewRender: function(view, element) {
    // We make sure that we activate the perfect scrollbar when the view isn't on Month
    if (view.name != 'month') {
      $(element).find('.fc-scroller').perfectScrollbar();
    }
  }, ...

what I did wrong?

UPDATE:

This doesn't display the error:

var scroller = $(element).find('.fc-scroller')[0];
var ps = new PerfectScrollbar(scroller, {
            wheelSpeed: 2,
            wheelPropagation: true,
            minScrollbarLength: 20,
            suppressScrollX: true
          });

but doesn't show the PerfectScrollBar

sfarzoso
  • 1,356
  • 2
  • 24
  • 65

2 Answers2

2

The question is a bit old (9mo) but still relevant at least for others.

Problems were that 1, you were targeting the wrong ".fc-scroller" (there are indeed several) and 2, perfect-scrollbar's parent must have 'position:relative'.

FullCalendar v4:

import { Calendar } from '@fullcalendar/core';
import PerfectScrollbar from 'perfect-scrollbar'

const calendar = new Calendar(calendarEl, {
  viewSkeletonRender(){
    const el = document.querySelector('.fc-body .fc-time-area .fc-scroller')
     if (el) {
      el.style.overflow = 'hidden'
      el.style.position = 'relative'
      new PerfectScrollbar(el)
     }
  }
})

For v3, same snippet but using it inside the 'viewRender' method. just like @sfarzoso did.

bolognese
  • 36
  • 2
1

A little bit less elegant, but properly accounts for window resizing in v5.

    // Calendar attribute reset for perfectScrollbar
    const resetCalendarSizing = () => {
      const timeGrid = (<HTMLElement>document.querySelector('.fc-view-harness .fc-timegrid-body'));
      const dayGrid = (<HTMLElement>document.querySelector('.fc-view-harness .fc-daygrid-body-natural'));

      setTimeout(() => {
        if (timeGrid && dayGrid) {
          timeGrid.setAttribute('style', 'width: 100% !important');
          timeGrid.firstElementChild?.firstElementChild?.setAttribute('style', 'width: 100% !important');
          timeGrid.lastElementChild?.firstElementChild?.setAttribute('style', 'width: 100% !important');

          dayGrid.setAttribute('style', 'width: 100% !important');
          dayGrid.firstElementChild?.setAttribute('style', 'width: 100% !important');
        }
      }, 10);
    };

const calendar =  new Calendar(calendarEl.value!, {
        viewDidMount: () => {
          document.querySelectorAll('.fc-view-harness .fc-scroller')
            .forEach((element) => {
              if (element) {
                (<HTMLElement>element).style.overflow = 'hidden';
                const perfectScrollbar = new PerfectScrollbar(element);
              }
            });

          resetCalendarSizing();
        },
        windowResize: () => {
          resetCalendarSizing();
        },
});