diff --git a/README.md b/README.md index 57d96157..1c533371 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/docs/apis/Platform.md b/docs/apis/Platform.md index 69ac893c..e6e2e191 100644 --- a/docs/apis/Platform.md +++ b/docs/apis/Platform.md @@ -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. diff --git a/docs/apis/Vibration.md b/docs/apis/Vibration.md new file mode 100644 index 00000000..6640bf57 --- /dev/null +++ b/docs/apis/Vibration.md @@ -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]); +``` diff --git a/src/apis/Vibration/index.js b/src/apis/Vibration/index.js new file mode 100644 index 00000000..eb64cad4 --- /dev/null +++ b/src/apis/Vibration/index.js @@ -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 diff --git a/src/index.js b/src/index.js index 8631be7e..27ece9dc 100644 --- a/src/index.js +++ b/src/index.js @@ -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,