0

I have a component (for a visual view):

enter image description here

The current way I render this is like so:

const today = new Date()
const minimumDate = new Date(new Date().setDate(today.getDate() - 14))
const maximumDate = new Date(new Date().setDate(today.getDate() + 14))

const currentDate = minimumDate;
const dateRange = [];

while(currentDate <= maximumDate){
    const newDate = new Date(currentDate); newDate.setHours(0,0,0,0)
    dateRange.push(newDate)
    currentDate.setDate(currentDate.getDate() + 1)
}
const [itemDays, setItemDays] = useState(dateRange)
itemDays.map((date, i) => <CalendarDay key ={i} workout={{}} date={date}/>)

What i want is to render a set amount of days on the calendar, having the entire calendar would cause massive performace hits. The above code does this for +-14 days and the current day. What I need is to convert the scoll bar length into some for of index which corresponds to the dates in itemDays so that when it gets below or above a certain index I can then load the next or previous day continuously.

The trouble is, every method ive tried falls flat. Ive tried these methods which are not accurat enough:

  1. Getting the centre element and try to determine where we are in the array
  2. Using the scroll position and the itemDays length to generate an index (The math never checks out, could be to do with the way scrollLeft works)

Each method was either too far from being a feasible solution or would require working with a bunch of edge cases. I would also like to try to snap the scroll to the centre element which I cant do without getting some form on index like the above mentioned.

user13020816
  • 43
  • 2
  • 5
  • A scroll bar isn't a good UI control for infinite scrolling in two directions (it's already not ideal for dealing with infinite scroll in one direction) Have you considered just adding clickable arrows at either end of the calendar? – DBS Feb 02 '23 at 10:40
  • @DBS yeah, I'm starting to get at the bad UI thing. Arrows were a design consideration but took a swipe design over button click. – user13020816 Feb 02 '23 at 10:57
  • You could implement a swipe based control without relying on a native scrollbar, there are a few questions covering that sort of thing: [Detect a finger swipe through JavaScript on the iPhone and Android](https://stackoverflow.com/questions/2264072/detect-a-finger-swipe-through-javascript-on-the-iphone-and-android) – DBS Feb 02 '23 at 11:00

0 Answers0