Slot selection stops responding to the mouse after a touch (and the other way around)
Check that this is really a bug
- I confirm
Reproduction link
https://codesandbox.io/p/sandbox/react-big-calendar-example-forked-xkc6hf
Bug description
I am writing an app which uses react-big-calendar to record appointments. The user uses the mouse and often her Windows PC's touchscreen. She complains that the touchscreen often does not work on the calendar.
My investigation:
Selection commits to the first input kind it sees and ignores the other for the life
of the component. _addInitialEventListener (src/Selection.js) listens for mousedown
and touchstart. Whichever fires first removes both listeners and re-adds only its own:
after a mouse press the touchstart listener is gone, after a touch the mousedown
listener is gone, until the component is torn down.
On a laptop or PC with a touch screen, whichever the user reaches for first keeps working and the other silently does nothing. Reloading the page is the only way back.
Reproduction without the sandbox
Fork the issue template from CONTRIBUTING (https://codesandbox.io/s/react-big-calendar-example-v9wdyd), make sure react and react-dom are on the same version, add date-fns, and replace App.js with the file below. Open the preview on a device that has both a touch screen and a mouse, or in Chrome with the device toolbar (Ctrl+Shift+M) toggled on for the finger steps and off for the mouse steps, without reloading in between.
import { useState } from "react";
import { Calendar, dateFnsLocalizer, Views } from "react-big-calendar";
import { format, parse, startOfWeek, getDay } from "date-fns";
import { enUS } from "date-fns/locale";
import "react-big-calendar/lib/css/react-big-calendar.css";
const localizer = dateFnsLocalizer({
format, parse, startOfWeek, getDay, locales: { "en-US": enUS },
});
export default function App() {
const [log, setLog] = useState([]);
return (
<div style={{ height: "100vh", display: "flex", flexDirection: "column" }}>
<p style={{ margin: 8, fontFamily: "sans-serif" }}>
1. Long-press a slot with a finger, release. 2. Click a slot with the mouse.
The second one never logs. Reload and do it in the other order: the reverse.
</p>
<pre style={{ margin: 8, minHeight: "3em" }}>{log.join("\n") || "(nothing selected yet)"}</pre>
<div style={{ flex: 1 }}>
<Calendar
localizer={localizer}
events={[]}
defaultView={Views.WEEK}
selectable
onSelectSlot={({ action, start }) =>
setLog((l) => [`${new Date().toLocaleTimeString()} ${action} ${start.toLocaleTimeString()}`, ...l])
}
/>
</div>
</div>
);
}Where it happens
src/Selection.js, _addInitialEventListener. Both the mousedown and the touchstart handler start with this._removeInitialEventListener(), which removes both listeners, and then each re-adds only its own kind.
Expected Behavior
Selecting slots should work with the mouse and with the touchscreen in the same session without having to reload the page.
Actual Behavior
With selectable, in week view, on a fresh page load:
- Press and hold a slot with a finger, release.
onSelectSlotfires. - Click a slot with the mouse. Nothing happens.
Reverse order after reload:
- Click a slot with the mouse.
onSelectSlotfires. - Press and hold a slot with a finger and drag. Nothing happens.
Cause: _addInitialEventListener in src/Selection.js. Whichever of mousedown
and touchstart is called first removes both listeners and re-adds only its own kind.
react-big-calendar version
1.20.0
React version
19.3
Platform/Target and Browser Versions
Windows PC with a touch screen, Google Chrome
Validations
- Read the docs.
- Check that there isn't already an issue that request the same feature to avoid creating a duplicate.
- Make sure this is a react-big-calendar issue and not an implementation issue
Would you like to open a PR for this bug?
- I'm willing to open a PR
Source: bigcalendar/react-big-calendar