Skip to content

Commit

Permalink
Add workaround for click throughs on mobile devices in CalendarCell a…
Browse files Browse the repository at this point in the history
…nd ListBox (#799)

## Background

We experience issues with click throughs in the DatePicker's calendar and in the ComboBox's list box on mobile devices. This was also noticed in React Aria's own examples.

## Solution

The issue was reported to react-spectrum, and they already linked to other related issues that had been reported before. In one of these, a workaround was suggested. 

The workaround is now tested locally in Spor, and seems to work great (and one can still scroll inside the list box). I believe we can release a new version and test this out in our app (otherwise I would have to copy the components to the app and test them there, which is a bit of a hassle, but not impossible, of course).
  • Loading branch information
heisand authored Aug 25, 2023
1 parent a8bc886 commit 1209e99
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/sweet-plants-live.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@vygruppen/spor-react": patch
---

Add workaround for click throughs on mobile devices in CalendarCell and ListBox
19 changes: 17 additions & 2 deletions packages/spor-react/src/datepicker/CalendarCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
isSameMonth,
isToday,
} from "@internationalized/date";
import React, { useRef } from "react";
import React, { useEffect, useRef } from "react";
import { useCalendarCell } from "react-aria";
import { CalendarState, RangeCalendarState } from "react-stately";

Expand Down Expand Up @@ -41,7 +41,22 @@ export function CalendarCell({ state, date, currentMonth }: CalendarCellProps) {
if (isOutsideMonth) {
stateProps["data-unavailable"] = true;
}


/*
Workaround to fix click througs on mobile devices
Related to https://github.com/adobe/react-spectrum/issues/4970
TODO: Follow up with react-spectrum to see if they can solve it on their end
*/
useEffect(() => {
(ref as any)?.current?.addEventListener(
"touchend",
(event: TouchEvent) => {
event.preventDefault();
},
{ passive: false, once: true }
);
}, []);

return (
<Box
as="td"
Expand Down
17 changes: 16 additions & 1 deletion packages/spor-react/src/input/ListBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
type BoxProps,
} from "@chakra-ui/react";
import type { Node } from "@react-types/shared";
import React, { useContext, useRef } from "react";
import React, { useContext, useEffect, useRef } from "react";
import {
AriaListBoxProps,
useListBox,
Expand Down Expand Up @@ -152,6 +152,21 @@ function Option({ item, state }: OptionProps) {
dataFields["data-focus"] = true;
}

/*
Workaround to fix click througs on mobile devices
Related to https://github.com/adobe/react-spectrum/issues/4970
TODO: Follow up with react-spectrum to see if they can solve it on their end
*/
useEffect(() => {
(ref as any)?.current?.addEventListener(
"touchend",
(event: TouchEvent) => {
event.preventDefault();
},
{ passive: false, once: true }
);
}, []);

return (
<OptionContext.Provider value={{ labelProps, descriptionProps }}>
<ListItem {...optionProps} {...dataFields} ref={ref} sx={styles.item}>
Expand Down

0 comments on commit 1209e99

Please sign in to comment.