mirror of
https://github.com/zhigang1992/react-native-web.git
synced 2026-03-27 01:34:17 +08:00
[add] Vibration API
This commit is contained in:
@@ -121,6 +121,7 @@ Exported modules:
|
||||
* [`PixelRatio`](docs/apis/PixelRatio.md)
|
||||
* [`Platform`](docs/apis/Platform.md)
|
||||
* [`StyleSheet`](docs/apis/StyleSheet.md)
|
||||
* [`Vibration`](docs/apis/Vibration.md)
|
||||
|
||||
## License
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ const styles = StyleSheet.create({
|
||||
|
||||
## Methods
|
||||
|
||||
**select**: any
|
||||
**select**(object): any
|
||||
|
||||
`Platform.select` takes an object containing `Platform.OS` as keys and returns
|
||||
the value for the platform you are currently running on.
|
||||
|
||||
35
docs/apis/Vibration.md
Normal file
35
docs/apis/Vibration.md
Normal file
@@ -0,0 +1,35 @@
|
||||
# Vibration
|
||||
|
||||
Vibration is described as a pattern of on-off pulses, which may be of varying
|
||||
lengths. The pattern may consist of either a single integer, describing the
|
||||
number of milliseconds to vibrate, or an array of integers describing a pattern
|
||||
of vibrations and pauses. Vibration is controlled with a single method:
|
||||
`Vibration.vibrate()`.
|
||||
|
||||
The vibration is asynchronous so this method will return immediately. There
|
||||
will be no effect on devices that do not support vibration.
|
||||
|
||||
## Methods
|
||||
|
||||
static **cancel**()
|
||||
|
||||
Stop the vibration.
|
||||
|
||||
static **vibrate**(pattern)
|
||||
|
||||
Start the vibration pattern.
|
||||
|
||||
## Examples
|
||||
|
||||
Vibrate once for 200ms:
|
||||
|
||||
```js
|
||||
Vibration.vibrate(200);
|
||||
Vibration.vibrate([200]);
|
||||
```
|
||||
|
||||
Vibrate for 200ms, pause for 100ms, vibrate for 200ms:
|
||||
|
||||
```js
|
||||
Vibration.vibrate([200, 100, 200]);
|
||||
```
|
||||
20
src/apis/Vibration/index.js
Normal file
20
src/apis/Vibration/index.js
Normal file
@@ -0,0 +1,20 @@
|
||||
const vibrate = (pattern) => {
|
||||
if ('vibrate' in window.navigator) {
|
||||
if (typeof pattern === 'number' || Array.isArray(pattern)) {
|
||||
window.navigator.vibrate(pattern)
|
||||
} else {
|
||||
throw new Error('Vibration pattern should be a number or array')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const Vibration = {
|
||||
cancel() {
|
||||
vibrate(0)
|
||||
},
|
||||
vibrate(pattern) {
|
||||
vibrate(pattern)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Vibration
|
||||
@@ -18,6 +18,7 @@ import PixelRatio from './apis/PixelRatio'
|
||||
import Platform from './apis/Platform'
|
||||
import StyleSheet from './apis/StyleSheet'
|
||||
import UIManager from './apis/UIManager'
|
||||
import Vibration from './apis/Vibration'
|
||||
|
||||
// components
|
||||
import ActivityIndicator from './components/ActivityIndicator'
|
||||
@@ -65,6 +66,7 @@ const ReactNative = {
|
||||
Platform,
|
||||
StyleSheet,
|
||||
UIManager,
|
||||
Vibration,
|
||||
|
||||
// components
|
||||
ActivityIndicator,
|
||||
|
||||
Reference in New Issue
Block a user