-
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(usevibrate): add useVibrate hook
- Loading branch information
Showing
7 changed files
with
139 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 default { | ||
navigationBarTitleText: 'useVibrate', | ||
}; |
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,32 @@ | ||
import React from 'react'; | ||
import { AtButton } from 'taro-ui'; | ||
import DocPage from '@components/DocPage'; | ||
|
||
import { useVibrate } from 'taro-hooks'; | ||
|
||
export default () => { | ||
const [vibrateAction] = useVibrate(); | ||
const [vibrateIntervalAction, stopVibrateAction] = useVibrate(true); | ||
|
||
return ( | ||
<DocPage title="useVibrate 震动反馈" panelTitle="useVibrate"> | ||
<AtButton onClick={() => vibrateAction()}>短震动触感按钮</AtButton> | ||
<AtButton className="gap" onClick={() => vibrateAction(true)}> | ||
长震动触感按钮 | ||
</AtButton> | ||
<AtButton onClick={() => vibrateIntervalAction()}> | ||
持续短震动触感按钮 | ||
</AtButton> | ||
<AtButton | ||
className="gap" | ||
onClick={() => { | ||
stopVibrateAction(); | ||
vibrateIntervalAction(true); | ||
}} | ||
> | ||
持续长震动触感按钮 | ||
</AtButton> | ||
<AtButton onClick={() => stopVibrateAction()}>关闭持续震动</AtButton> | ||
</DocPage> | ||
); | ||
}; |
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,48 @@ | ||
--- | ||
title: useVibrate | ||
nav: | ||
title: Hooks | ||
path: /hooks | ||
order: 2 | ||
group: | ||
title: 设备 | ||
path: /device | ||
--- | ||
|
||
# useVibrate | ||
|
||
提供震动反馈 | ||
|
||
## 何时使用 | ||
|
||
当需要操作或者使用震动时 | ||
|
||
## API | ||
|
||
```jsx | pure | ||
const [vibrateAction, stopVibrateAction] = useVibrate(interval?: boolean, gap?: number); | ||
``` | ||
|
||
## 参数说明 | ||
|
||
| 参数 | 说明 | 类型 | 默认值 | | ||
| -------- | ------------ | --------- | ------ | | ||
| interval | 是否持续震动 | `boolean` | - | | ||
| gap | 持续震动间隔 | `number` | 200 | | ||
|
||
## 返回值说明 | ||
|
||
| 返回值 | 说明 | 类型 | | ||
| ----------------- | -------------------------------- | ----------------------------------------------------- | | ||
| vibrateAction | 操作函数, 可根据参数执行长短震动 | `(long?: boolean) => Promise<General.CallbackResult>` | | ||
| stopVibrateAction | 若为持续震动可取消震动 | `(long?: boolean) => Promise<General.CallbackResult>` | | ||
|
||
## 代码演示 | ||
|
||
<code src="@pages/useVibrate" /> | ||
|
||
## Hook 支持度 | ||
|
||
| 微信小程序 | H5 | ReactNative | | ||
| :--------: | :-: | :---------: | | ||
| ✔️ | ✔️ | ✔️ | |
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,49 @@ | ||
import { General, vibrateLong, vibrateShort } from '@tarojs/taro'; | ||
import { useCallback, useRef } from 'react'; | ||
|
||
export type VibrateAction = ( | ||
long?: boolean, | ||
) => Promise<General.CallbackResult | undefined>; | ||
|
||
export type StopVibrateAction = () => void; | ||
|
||
const SHORTINTERVAL = 15; | ||
const LONGINTERVAL = 400; | ||
const DEFAULTGAP = 200; | ||
|
||
function useVibrate( | ||
interval?: boolean, | ||
gap?: number, | ||
): [VibrateAction, StopVibrateAction] { | ||
const timer = useRef<NodeJS.Timeout>(); | ||
const vibrateAction = useCallback<VibrateAction>((long) => { | ||
return new Promise((resolve, reject) => { | ||
try { | ||
const vibrateMethod = long ? vibrateLong : vibrateShort; | ||
vibrateMethod({ | ||
success: (res) => { | ||
const computedGap = | ||
(gap || DEFAULTGAP) + (long ? LONGINTERVAL : SHORTINTERVAL); | ||
if (interval) { | ||
timer.current = setTimeout(() => { | ||
vibrateAction(long); | ||
}, computedGap); | ||
} | ||
resolve(res); | ||
}, | ||
fail: reject, | ||
}).catch(reject); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
}); | ||
}, []); | ||
|
||
const stopVibrateAction = useCallback(() => { | ||
timer.current && clearTimeout(timer.current); | ||
}, [timer]); | ||
|
||
return [vibrateAction, stopVibrateAction]; | ||
} | ||
|
||
export default useVibrate; |