Skip to content

Commit e6ff0da

Browse files
ivan-hafacebook-github-bot
authored andcommitted
fix and enrich Vibration docs
Summary: The docs for [Vibration](https://facebook.github.io/react-native/docs/vibration.html) is broken and not informative now. Making people difficult to understand the usage without digging into the source code. ![screen shot 2017-08-23 at 12 44 50](https://user-images.githubusercontent.com/20895743/29599382-da4976b6-8801-11e7-96d2-b342f0bf36fb.jpg) Tested the page locally. ![fireshot capture 4 - vibration - http___localhost_8079_react-native_docs_vibration html vibrate](https://user-images.githubusercontent.com/20895743/29599338-976816e0-8801-11e7-8f53-a228363353f3.png) Closes #15614 Differential Revision: D5691254 Pulled By: hramos fbshipit-source-id: b84f0d0f9c69bd0983fbc32424c521108640fdd4
1 parent 28c1c88 commit e6ff0da

File tree

1 file changed

+38
-10
lines changed

1 file changed

+38
-10
lines changed

Libraries/Vibration/Vibration.js

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,54 @@
88
*
99
* @providesModule Vibration
1010
* @flow
11+
* @jsdoc
1112
*/
1213
'use strict';
1314

1415
var RCTVibration = require('NativeModules').Vibration;
1516
var Platform = require('Platform');
1617

1718
/**
19+
* @class
20+
* @description
1821
* The Vibration API is exposed at `Vibration.vibrate()`.
1922
* The vibration is asynchronous so this method will return immediately.
2023
*
2124
* There will be no effect on devices that do not support Vibration, eg. the simulator.
2225
*
23-
* **Note for android**
26+
* **Note for Android:**
2427
* add `<uses-permission android:name="android.permission.VIBRATE"/>` to `AndroidManifest.xml`
2528
*
26-
* **Android Usage:**
29+
* Since the **vibration duration in iOS is not configurable**, so there are some differences with Android.
30+
* In Android, if `pattern` is a number, it specified the vibration duration in ms. If `pattern`
31+
* is an array, those odd indices is the vibration duration, while the even one are the separation time.
2732
*
28-
* [0, 500, 200, 500]
29-
* V(0.5s) --wait(0.2s)--> V(0.5s)
33+
* In iOS, invoking `vibrate(duration)` will just ignore the duration and vibrate for a fixed time. While the
34+
* `pattern` array is used to define the duration between each vibration. See below example for more.
3035
*
31-
* [300, 500, 200, 500]
32-
* --wait(0.3s)--> V(0.5s) --wait(0.2s)--> V(0.5s)
36+
* Repeatable vibration is also supported, the vibration will repeat with defined pattern until `cancel()` is called.
3337
*
34-
* **iOS Usage:**
35-
* if first argument is 0, it will not be included in pattern array.
38+
* Example:
39+
* ```
40+
* const DURATION = 10000
41+
* const PATTERN = [1000, 2000, 3000]
3642
*
37-
* [0, 1000, 2000, 3000]
38-
* V(fixed) --wait(1s)--> V(fixed) --wait(2s)--> V(fixed) --wait(3s)--> V(fixed)
43+
* Vibration.vibrate(DURATION)
44+
* // Android: vibrate for 10s
45+
* // iOS: duration is not configurable, vibrate for fixed time (about 500ms)
46+
*
47+
* Vibration.vibrate(PATTERN)
48+
* // Android: wait 1s -> vibrate 2s -> wait 3s
49+
* // iOS: wait 1s -> vibrate -> wait 2s -> vibrate -> wait 3s -> vibrate
50+
*
51+
* Vibration.vibrate(PATTERN, true)
52+
* // Android: wait 1s -> vibrate 2s -> wait 3s -> wait 1s -> vibrate 2s -> wait 3s -> ...
53+
* // iOS: wait 1s -> vibrate -> wait 2s -> vibrate -> wait 3s -> vibrate -> wait 1s -> vibrate -> wait 2s -> vibrate -> wait 3s -> vibrate -> ...
54+
*
55+
* Vibration.cancel()
56+
* // Android: vibration stopped
57+
* // iOS: vibration stopped
58+
* ```
3959
*/
4060

4161
var _vibrating: boolean = false;
@@ -74,6 +94,11 @@ function vibrateScheduler(id, pattern: Array<number>, repeat: boolean, nextIndex
7494
}
7595

7696
var Vibration = {
97+
/**
98+
* Trigger a vibration with specified `pattern`.
99+
* @param pattern Vibration pattern, accept a number or an array of number. Default to 400ms.
100+
* @param repeat Optional. Repeat vibration pattern until cancel(), default to false.
101+
*/
77102
vibrate: function(pattern: number | Array<number> = 400, repeat: boolean = false) {
78103
if (Platform.OS === 'android') {
79104
if (typeof pattern === 'number') {
@@ -98,6 +123,9 @@ var Vibration = {
98123
},
99124
/**
100125
* Stop vibration
126+
* ```
127+
* Vibration.cancel()
128+
* ```
101129
*/
102130
cancel: function() {
103131
if (Platform.OS === 'ios') {

0 commit comments

Comments
 (0)