mirror of
https://github.com/zoriya/react-native-web.git
synced 2026-06-01 01:59:10 +00:00
[add] Vibration API
This commit is contained in:
@@ -121,6 +121,7 @@ Exported modules:
|
|||||||
* [`PixelRatio`](docs/apis/PixelRatio.md)
|
* [`PixelRatio`](docs/apis/PixelRatio.md)
|
||||||
* [`Platform`](docs/apis/Platform.md)
|
* [`Platform`](docs/apis/Platform.md)
|
||||||
* [`StyleSheet`](docs/apis/StyleSheet.md)
|
* [`StyleSheet`](docs/apis/StyleSheet.md)
|
||||||
|
* [`Vibration`](docs/apis/Vibration.md)
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ const styles = StyleSheet.create({
|
|||||||
|
|
||||||
## Methods
|
## Methods
|
||||||
|
|
||||||
**select**: any
|
**select**(object): any
|
||||||
|
|
||||||
`Platform.select` takes an object containing `Platform.OS` as keys and returns
|
`Platform.select` takes an object containing `Platform.OS` as keys and returns
|
||||||
the value for the platform you are currently running on.
|
the value for the platform you are currently running on.
|
||||||
|
|||||||
@@ -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]);
|
||||||
|
```
|
||||||
@@ -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 Platform from './apis/Platform'
|
||||||
import StyleSheet from './apis/StyleSheet'
|
import StyleSheet from './apis/StyleSheet'
|
||||||
import UIManager from './apis/UIManager'
|
import UIManager from './apis/UIManager'
|
||||||
|
import Vibration from './apis/Vibration'
|
||||||
|
|
||||||
// components
|
// components
|
||||||
import ActivityIndicator from './components/ActivityIndicator'
|
import ActivityIndicator from './components/ActivityIndicator'
|
||||||
@@ -65,6 +66,7 @@ const ReactNative = {
|
|||||||
Platform,
|
Platform,
|
||||||
StyleSheet,
|
StyleSheet,
|
||||||
UIManager,
|
UIManager,
|
||||||
|
Vibration,
|
||||||
|
|
||||||
// components
|
// components
|
||||||
ActivityIndicator,
|
ActivityIndicator,
|
||||||
|
|||||||
Reference in New Issue
Block a user