Skip to content

Commit

Permalink
Update calendar resize handler
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeyandreevsky committed Oct 18, 2024
1 parent 72cebd2 commit 4a29597
Showing 1 changed file with 12 additions and 21 deletions.
33 changes: 12 additions & 21 deletions packages/frontend/src/components/calendar/DateRangePicker.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useState, useEffect, useRef } from 'react'
import useResizeObserver from '@react-hook/resize-observer'
import Calendar from 'react-calendar'
import 'react-calendar/dist/Calendar.css'
import './DateRangePicker.scss'
Expand All @@ -20,29 +21,19 @@ const DateRangePicker = ({
const [activeStartDate, setActiveStartDate] = useState(new Date(today.getFullYear(), today.getMonth() - 1, 1))
const [displayedMonths, setDisplayedMonths] = useState([null, null])

useEffect(() => {
const handleResize = () => {
setShowSingleCalendar(window.innerWidth < 600)

if (calendarRef.current) {
const containerWidth = calendarRef.current?.offsetWidth

if (containerWidth < 400) {
setMonthsToShow(4)
} else if (containerWidth < 500) {
setMonthsToShow(6)
} else {
setMonthsToShow(8)
}
}
}
useResizeObserver(calendarRef, (entry) => {
const containerWidth = entry.contentRect.width

setShowSingleCalendar(window.innerWidth < 600)

handleResize()
window.addEventListener('resize', handleResize)
return () => {
window.removeEventListener('resize', handleResize)
if (containerWidth < 400) {
setMonthsToShow(4)
} else if (containerWidth < 500) {
setMonthsToShow(6)
} else {
setMonthsToShow(8)
}
}, [])
})

useEffect(() => {
const generateMonthPairs = () => {
Expand Down

0 comments on commit 4a29597

Please sign in to comment.