Skip to content

Commit 9f30143

Browse files
committed
main 🧊 rework use vibrate
1 parent 9bb3857 commit 9f30143

File tree

2 files changed

+21
-34
lines changed

2 files changed

+21
-34
lines changed

‎src/hooks/useVibrate/useVibrate.demo.tsx‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
import { useVibrate } from './useVibrate';
22

33
const Demo = () => {
4-
const { vibrating, vibrate, stop } = useVibrate({
5-
pattern: [300, 100, 200, 100, 1000, 300]
6-
});
4+
const { active, vibrate, stop } = useVibrate([300, 100, 200, 100, 1000, 300]);
75

86
return (
97
<>
10-
<h1>Vibration is <code>{vibrating ? 'vibrating' : 'not vibrating'}</code></h1>
8+
<h1>Vibration is <code>{active ? 'vibrating' : 'not vibrating'}</code></h1>
119
<p>Most modern mobile devices include vibration hardware, which lets software code provides physical feedback to the user by causing the device to shake.</p>
12-
13-
<button disabled={vibrating} type='button' onClick={() => vibrate()}>
10+
<button disabled={active} type='button' onClick={() => vibrate()}>
1411
Vibrate
1512
</button>
16-
<button type='button' onClick={stop}>
13+
<button disabled={!active} type='button' onClick={stop}>
1714
Stop
1815
</button>
1916
</>

‎src/hooks/useVibrate/useVibrate.ts‎

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,6 @@ import { useEffect, useRef, useState } from 'react';
33
/** The use vibrate pattern type */
44
export type UseVibratePattern = number | number[];
55

6-
/** The use vibrate params type */
7-
export interface UseVibrateParams {
8-
/** Time in milliseconds between vibrations */
9-
interval?: number;
10-
/** Pattern for vibration */
11-
pattern: UseVibratePattern;
12-
}
13-
146
/** The use vibrate return type */
157
export interface UseVibrateReturn {
168
/** The support indicator */
@@ -21,8 +13,8 @@ export interface UseVibrateReturn {
2113
pause: () => void;
2214
/** The resume function */
2315
resume: () => void;
24-
/** The stop function */
25-
stop: () => void;
16+
/** The start function */
17+
start: (interval: number) => void;
2618
/** The vibrate function */
2719
vibrate: (pattern?: UseVibratePattern) => void;
2820
}
@@ -38,48 +30,46 @@ export interface UseVibrateReturn {
3830
* @returns {UseVibrateReturn} An object containing support indicator, start vibration and stop vibration functions
3931
*
4032
* @example
41-
* const { supported, vibrating, vibrate, stop } = useVibrate(1000);
33+
* const { supported, active, vibrate, stop, pause, resume } = useVibrate(1000);
4234
*/
43-
export const useVibrate = (params: UseVibrateParams) => {
35+
export const useVibrate = (pattern: UseVibratePattern, interval: number = 0) => {
4436
const supported = typeof navigator !== 'undefined' && 'vibrate' in navigator;
4537

46-
const interval = params.interval ?? 0;
4738
const intervalIdRef = useRef<ReturnType<typeof setInterval>>();
48-
const [vibrating, setVibrating] = useState(false);
39+
const [active, setActive] = useState(false);
4940

50-
const vibrate = (pattern: UseVibratePattern = params.pattern) => {
41+
const vibrate = (internalPattern: UseVibratePattern = pattern) => {
5142
if (!supported) return;
52-
navigator.vibrate(pattern);
53-
setVibrating(true);
43+
navigator.vibrate(internalPattern);
5444
};
5545

5646
const stop = () => {
5747
if (!supported) return;
58-
59-
setVibrating(false);
6048
navigator.vibrate(0);
61-
49+
setActive(false);
6250
if (intervalIdRef.current) clearInterval(intervalIdRef.current);
6351
};
6452

6553
const pause = () => {
6654
if (!supported) return;
55+
setActive(false);
6756
if (intervalIdRef.current) clearInterval(intervalIdRef.current);
6857
};
6958

70-
const resume = () => {
59+
const resume = (intervalInterval: number = interval) => {
7160
if (!supported) return;
7261
if (intervalIdRef.current) clearInterval(intervalIdRef.current);
73-
intervalIdRef.current = setInterval(vibrate, interval);
62+
setActive(true);
63+
intervalIdRef.current = setInterval(vibrate, intervalInterval);
7464
};
7565

7666
useEffect(() => {
77-
if (!supported || typeof params.interval === 'undefined') return;
78-
intervalIdRef.current = setInterval(vibrate, interval);
67+
if (!supported || interval <= 0) return;
68+
resume(interval);
7969
return () => {
80-
clearInterval(intervalIdRef.current);
70+
stop();
8171
};
82-
}, [params.interval, params.pattern]);
72+
}, [interval, pattern]);
8373

84-
return { supported, vibrate, stop, vibrating, pause, resume };
74+
return { supported, vibrate, stop, active, pause, resume };
8575
};

0 commit comments

Comments
 (0)