-
-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Holding offset buttons adjusts by fine increments
- Loading branch information
1 parent
4300c9b
commit 06b686c
Showing
9 changed files
with
135 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { IconButton, IconButtonProps } from '@material-ui/core'; | ||
import React, { useEffect, useCallback, useState } from 'react'; | ||
import { isMobile } from 'react-device-detect'; | ||
|
||
interface Props extends IconButtonProps { | ||
onHold?: (repetition: number) => void; | ||
onClick: () => void; | ||
children: React.ReactNode; | ||
} | ||
|
||
const HoldableIconButton = ({ onHold, onClick, children, ...rest }: Props) => { | ||
const [startTime, setStartTime] = useState<number>(); | ||
|
||
const repetitions = useCallback(() => { | ||
if (startTime === undefined) { | ||
return undefined; | ||
} | ||
|
||
const holdTime = Date.now() - startTime; | ||
return holdTime / 250; | ||
}, [startTime]); | ||
|
||
const handleMouseUp = (event: any) => { | ||
const reps = repetitions(); | ||
|
||
if (reps !== undefined && reps < 1) { | ||
onClick?.(); | ||
} | ||
|
||
setStartTime(undefined); | ||
}; | ||
|
||
const handleMouseDown = (event: any) => { | ||
setStartTime(Date.now()); | ||
}; | ||
|
||
useEffect(() => { | ||
if (startTime === undefined) { | ||
return; | ||
} | ||
|
||
const interval = setInterval(() => { | ||
const reps = repetitions(); | ||
|
||
if (reps !== undefined && reps > 0) { | ||
onHold?.(reps); | ||
} | ||
}, 250); | ||
return () => clearInterval(interval); | ||
}, [startTime, onHold, repetitions]); | ||
return ( | ||
<> | ||
{isMobile && ( | ||
<IconButton onTouchStart={handleMouseDown} onTouchEnd={handleMouseUp} onClick={() => {}} {...rest}> | ||
{children} | ||
</IconButton> | ||
)} | ||
{!isMobile && ( | ||
<IconButton onMouseDown={handleMouseDown} onMouseUp={handleMouseUp} onClick={() => {}} {...rest}> | ||
{children} | ||
</IconButton> | ||
)} | ||
</> | ||
); | ||
}; | ||
|
||
export default HoldableIconButton; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters