-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat:
useVibration
hook implementation and docs (#757)
* feat: `useVibration` hook implementation and docs #fix 683
- Loading branch information
Showing
10 changed files
with
181 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export const NavigatorVibrateSetup = () => { | ||
navigator.vibrate = (_: VibratePattern): boolean => true; | ||
}; |
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,7 @@ | ||
/** | ||
* Here goes any setup code that should be performed before actual tests and/or code imports. | ||
*/ | ||
|
||
import { NavigatorVibrateSetup } from './navigator.vibrate'; | ||
|
||
NavigatorVibrateSetup(); |
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,18 @@ | ||
import * as React from 'react'; | ||
import { useToggle, useVibrate } from '../..'; | ||
|
||
export const Example: React.FC = () => { | ||
const [doVibrate, setDoVibrate] = useToggle(false); | ||
|
||
useVibrate( | ||
doVibrate, | ||
[100, 30, 100, 30, 100, 30, 200, 30, 200, 30, 200, 30, 100, 30, 100, 30, 100], | ||
true | ||
); | ||
|
||
return ( | ||
<div> | ||
<button onClick={() => setDoVibrate()}>{doVibrate ? 'Stop' : 'Start'} vibration</button> | ||
</div> | ||
); | ||
}; |
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,38 @@ | ||
import { Canvas, Meta, Story } from '@storybook/addon-docs/blocks'; | ||
import { Example } from './example.stories'; | ||
import { ImportPath } from '../../storybookUtil/ImportPath'; | ||
|
||
<Meta title="Navigator/useVibrate" component={Example} /> | ||
|
||
# useVibrate | ||
|
||
Provides vibration feedback using the [Vibration API](https://developer.mozilla.org/en-US/docs/Web/API/Vibration_API). | ||
|
||
> Note: client may disable vibration for websites in browser settings. | ||
- SSR-friendly (do nothing during SSR). | ||
- Supports vibration loops. | ||
- Auto-cancel vibration on component unmount. | ||
|
||
#### Example | ||
|
||
<Canvas> | ||
<Story story={Example} inline /> | ||
</Canvas> | ||
|
||
## Reference | ||
|
||
```ts | ||
function useVibrate(enabled: boolean, pattern: VibratePattern, loop?: boolean): void; | ||
``` | ||
|
||
#### Importing | ||
|
||
<ImportPath /> | ||
|
||
#### Arguments | ||
|
||
- **enabled** _`boolean`_ - Whether to perform vibration or not. | ||
- **pattern** _`number | number[]`_ - VibrationPattern passed down to `navigator.vibrate`. | ||
- **loop** _`boolean | undefined`_ _(default: `undefined`)_ - If true - vibration will be looped | ||
using `setInterval`. |
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,57 @@ | ||
import { renderHook } from '@testing-library/react-hooks/dom'; | ||
import { useVibrate } from '../..'; | ||
|
||
describe('useVibrate', () => { | ||
const vibrateSpy = jest.spyOn(navigator, 'vibrate'); | ||
|
||
beforeEach(() => { | ||
vibrateSpy.mockReset(); | ||
}); | ||
|
||
afterAll(() => { | ||
vibrateSpy.mockRestore(); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(useVibrate).toBeDefined(); | ||
}); | ||
|
||
it('should render', () => { | ||
const { result } = renderHook(() => useVibrate(true, 100)); | ||
expect(result.error).toBeUndefined(); | ||
}); | ||
|
||
it('should call navigator.vibrate', () => { | ||
renderHook(() => useVibrate(true, [100, 200])); | ||
expect(vibrateSpy).toHaveBeenCalledTimes(1); | ||
expect(vibrateSpy.mock.calls[0][0]).toEqual([100, 200]); | ||
}); | ||
|
||
it('should call navigator.vibrate(0) on unmount', () => { | ||
const { unmount } = renderHook(() => useVibrate(true, [100, 200], true)); | ||
unmount(); | ||
|
||
expect(vibrateSpy.mock.calls[1][0]).toEqual(0); | ||
}); | ||
|
||
it('should vibrate constantly using interval', () => { | ||
jest.useFakeTimers(); | ||
renderHook(() => useVibrate(true, 300, true)); | ||
|
||
expect(vibrateSpy).toHaveBeenCalledTimes(1); | ||
expect(vibrateSpy.mock.calls[0][0]).toEqual(300); | ||
|
||
jest.advanceTimersByTime(299); | ||
expect(vibrateSpy).toHaveBeenCalledTimes(1); | ||
|
||
jest.advanceTimersByTime(1); | ||
expect(vibrateSpy).toHaveBeenCalledTimes(2); | ||
expect(vibrateSpy.mock.calls[1][0]).toEqual(300); | ||
|
||
jest.advanceTimersByTime(300); | ||
expect(vibrateSpy).toHaveBeenCalledTimes(3); | ||
expect(vibrateSpy.mock.calls[2][0]).toEqual(300); | ||
|
||
jest.useRealTimers(); | ||
}); | ||
}); |
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,13 @@ | ||
import { renderHook } from '@testing-library/react-hooks/server'; | ||
import { useVibrate } from '../..'; | ||
|
||
describe('useVibrate', () => { | ||
it('should be defined', () => { | ||
expect(useVibrate).toBeDefined(); | ||
}); | ||
|
||
it('should render', () => { | ||
const { result } = renderHook(() => useVibrate(true, 100)); | ||
expect(result.error).toBeUndefined(); | ||
}); | ||
}); |
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,39 @@ | ||
import { useEffect } from 'react'; | ||
import { isBrowser, noop } from '../util/const'; | ||
|
||
/** | ||
* Provides vibration feedback using the Vibration API. | ||
* | ||
* @param enabled Whether to perform vibration or not. | ||
* @param pattern VibrationPattern passed down to `navigator.vibrate`. | ||
* @param loop If true - vibration will be looped using `setInterval`. | ||
*/ | ||
export const useVibrate = | ||
!isBrowser || typeof navigator.vibrate === 'undefined' | ||
? noop | ||
: function useVibrate(enabled: boolean, pattern: VibratePattern, loop?: boolean): void { | ||
useEffect(() => { | ||
let interval: undefined | ReturnType<typeof setInterval>; | ||
|
||
if (enabled) { | ||
navigator.vibrate(pattern); | ||
|
||
if (loop) { | ||
interval = setInterval( | ||
() => { | ||
navigator.vibrate(pattern); | ||
}, | ||
Array.isArray(pattern) ? pattern.reduce((a, n) => a + n, 0) : pattern | ||
); | ||
} | ||
|
||
return () => { | ||
navigator.vibrate(0); | ||
|
||
if (interval) { | ||
clearInterval(interval); | ||
} | ||
}; | ||
} | ||
}, [loop, pattern, enabled]); | ||
}; |