Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[@mantine/core] Rating: Prevent emulated mouse events in touch devices #4976

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 46 additions & 11 deletions src/mantine-core/src/components/Rating/Rating.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ export const Rating = factory<RatingFactory>((_props, ref) => {
onMouseMove,
onHover,
onMouseLeave,
onTouchStart,
onTouchEnd,
size,
variant,
getSymbolLabel,
Expand Down Expand Up @@ -174,6 +176,16 @@ export const Rating = factory<RatingFactory>((_props, ref) => {
const stableValueRounded = roundValueTo(_value, decimalUnit);
const finalValue = hovered !== -1 ? hovered : stableValueRounded;

const getRatingFromCoordinates = (x: number) => {
const { left, right, width } = rootRef.current!.getBoundingClientRect();
const symbolWidth = width / _count;

const hoverPosition = dir === 'rtl' ? right - x : x - left;
const hoverValue = hoverPosition / symbolWidth;

return clamp(roundValueTo(hoverValue + decimalUnit / 2, decimalUnit), decimalUnit, _count);
};

const handleMouseEnter = (event: React.MouseEvent<HTMLDivElement>) => {
onMouseEnter?.(event);
!readOnly && setOutside(false);
Expand All @@ -186,17 +198,7 @@ export const Rating = factory<RatingFactory>((_props, ref) => {
return;
}

const { left, right, width } = rootRef.current!.getBoundingClientRect();
const symbolWidth = width / _count;

const hoverPosition = dir === 'rtl' ? right - event.clientX : event.clientX - left;
const hoverValue = hoverPosition / symbolWidth;

const rounded = clamp(
roundValueTo(hoverValue + decimalUnit / 2, decimalUnit),
decimalUnit,
_count
);
const rounded = getRatingFromCoordinates(event.clientX);

setHovered(rounded);
rounded !== hovered && onHover?.(rounded);
Expand All @@ -214,8 +216,38 @@ export const Rating = factory<RatingFactory>((_props, ref) => {
hovered !== -1 && onHover?.(-1);
};

const handleTouchStart = (event: React.TouchEvent<HTMLDivElement>) => {
event.preventDefault();

const { touches } = event;
if (touches.length !== 1) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For multitouch devices it's a bit farfetched so we might be safe here? 🤔

return;
}

const touch = touches[0];
setValue(getRatingFromCoordinates(touch.clientX));

onTouchStart?.(event);
};

const handleTouchEnd = (event: React.TouchEvent<HTMLDivElement>) => {
event.preventDefault();

onTouchEnd?.(event);
};

const handleItemBlur = () => isOutside && setHovered(-1);

const handleInputChange = (event: React.ChangeEvent<HTMLInputElement> | number) => {
if (!readOnly) {
if (typeof event === 'number') {
setHovered(event);
} else {
setHovered(parseFloat(event.target.value));
}
}
};

const handleChange = (event: React.ChangeEvent<HTMLInputElement> | number) => {
if (!readOnly) {
if (typeof event === 'number') {
Expand Down Expand Up @@ -261,6 +293,7 @@ export const Rating = factory<RatingFactory>((_props, ref) => {
name={_name}
onChange={handleChange}
onBlur={handleItemBlur}
onInputChange={handleInputChange}
id={`${_id}-${index}-${fractionIndex}`}
/>
);
Expand All @@ -277,6 +310,8 @@ export const Rating = factory<RatingFactory>((_props, ref) => {
onMouseMove={handleMouseMove}
onMouseEnter={handleMouseEnter}
onMouseLeave={handleMouseLeave}
onTouchStart={handleTouchStart}
onTouchEnd={handleTouchEnd}
variant={variant}
size={size}
id={_id}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface RatingItemProps extends BoxProps, ElementProps<'input', 'value'
value: number;
id: string;
onChange(event: React.ChangeEvent<HTMLInputElement> | number): void;
onInputChange(event: React.ChangeEvent<HTMLInputElement> | number): void;
}

export function RatingItem({
Expand All @@ -28,7 +29,9 @@ export function RatingItem({
fractionValue,
color,
id,
onBlur,
onChange,
onInputChange,
style,
...others
}: RatingItemProps) {
Expand All @@ -48,7 +51,8 @@ export function RatingItem({
data-active={active || undefined}
aria-label={getSymbolLabel?.(value)}
value={value}
onChange={onChange}
onBlur={onBlur}
onChange={onInputChange}
{...others}
/>
)}
Expand Down