@fluent-windows/hooks
Version:
Fluent-Windows React hooks.
29 lines (27 loc) • 669 B
JavaScript
/**
* Subscribe to touch events based on `onTouchStart`, `onTouchEnd`
*
* Demo
* import { useTouch } from '@fluent-windows/hooks'
*
* function handleChange() {
* // ...
* }
* const [status, bind] = useTouch(handleChange)
*
* <button {...bind}>{status}</button>
*/
import * as React from 'react';
function useTouch(statusHandler) {
const [isTouched, setTouched] = React.useState(false);
const bind = {
onTouchStart: () => {
statusHandler ? statusHandler(true) : setTouched(true);
},
onTouchEnd: () => {
statusHandler ? statusHandler(false) : setTouched(false);
}
};
return [isTouched, bind];
}
export default useTouch;