Skip to content

Commit

Permalink
Add support for touch events
Browse files Browse the repository at this point in the history
Fixes #4
  • Loading branch information
mnkhouri committed Oct 16, 2022
1 parent 8c33e6d commit c3a9bc6
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
2 changes: 1 addition & 1 deletion docs/main.bundle.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-circular-slider-svg",
"version": "0.1.5",
"version": "0.2.0",
"description": "A circular slider React component",
"keywords": [
"react",
Expand Down
23 changes: 23 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,25 @@ export class CircularSlider extends React.Component<
this.processSelection(x, y);
};

onTouch = (ev: React.TouchEvent<SVGSVGElement>) => {
/* This is a very simplistic touch handler. Some optimzations might be:
- Right now, the bounding box for a touch is the entire element. Having the bounding box
for touched be circular at a fixed distance around the slider would be more intuitive.
- Similarly, don't set `touchAction: 'none'` in CSS. Instead, call `ev.preventDefault()`
only when the touch is within X distance from the slider
*/
if (
ev.touches.length > 1 ||
(ev.type === "touchend" && ev.touches.length > 0)
)
return;

const touch = ev.changedTouches[0];
const x = touch.clientX;
const y = touch.clientY;
this.processSelection(x, y);
};

processSelection = (x: number, y: number) => {
const {
size,
Expand Down Expand Up @@ -213,6 +232,10 @@ export class CircularSlider extends React.Component<
if we propagate the click. */
(ev) => controllable && ev.stopPropagation()
}
onTouchStart={this.onTouch}
onTouchEnd={this.onTouch}
onTouchMove={this.onTouch}
style={{ touchAction: "none" }}
>
{
/* Shadow */
Expand Down

0 comments on commit c3a9bc6

Please sign in to comment.