UNPKG

2.7 kBJavaScriptView Raw
1'use strict';
2
3const HapticEngine = {
4 getEngine() {
5 const win = window;
6 return (win.TapticEngine) || (win.Capacitor && win.Capacitor.isPluginAvailable('Haptics') && win.Capacitor.Plugins.Haptics);
7 },
8 available() {
9 return !!this.getEngine();
10 },
11 isCordova() {
12 return !!window.TapticEngine;
13 },
14 isCapacitor() {
15 const win = window;
16 return !!win.Capacitor;
17 },
18 impact(options) {
19 const engine = this.getEngine();
20 if (!engine) {
21 return;
22 }
23 const style = this.isCapacitor() ? options.style.toUpperCase() : options.style;
24 engine.impact({ style });
25 },
26 notification(options) {
27 const engine = this.getEngine();
28 if (!engine) {
29 return;
30 }
31 const style = this.isCapacitor() ? options.style.toUpperCase() : options.style;
32 engine.notification({ style });
33 },
34 selection() {
35 this.impact({ style: 'light' });
36 },
37 selectionStart() {
38 const engine = this.getEngine();
39 if (!engine) {
40 return;
41 }
42 if (this.isCapacitor()) {
43 engine.selectionStart();
44 }
45 else {
46 engine.gestureSelectionStart();
47 }
48 },
49 selectionChanged() {
50 const engine = this.getEngine();
51 if (!engine) {
52 return;
53 }
54 if (this.isCapacitor()) {
55 engine.selectionChanged();
56 }
57 else {
58 engine.gestureSelectionChanged();
59 }
60 },
61 selectionEnd() {
62 const engine = this.getEngine();
63 if (!engine) {
64 return;
65 }
66 if (this.isCapacitor()) {
67 engine.selectionEnd();
68 }
69 else {
70 engine.gestureSelectionEnd();
71 }
72 }
73};
74/**
75 * Trigger a selection changed haptic event. Good for one-time events
76 * (not for gestures)
77 */
78const hapticSelection = () => {
79 HapticEngine.selection();
80};
81/**
82 * Tell the haptic engine that a gesture for a selection change is starting.
83 */
84const hapticSelectionStart = () => {
85 HapticEngine.selectionStart();
86};
87/**
88 * Tell the haptic engine that a selection changed during a gesture.
89 */
90const hapticSelectionChanged = () => {
91 HapticEngine.selectionChanged();
92};
93/**
94 * Tell the haptic engine we are done with a gesture. This needs to be
95 * called lest resources are not properly recycled.
96 */
97const hapticSelectionEnd = () => {
98 HapticEngine.selectionEnd();
99};
100/**
101 * Use this to indicate success/failure/warning to the user.
102 * options should be of the type `{ style: 'light' }` (or `medium`/`heavy`)
103 */
104const hapticImpact = (options) => {
105 HapticEngine.impact(options);
106};
107
108exports.hapticImpact = hapticImpact;
109exports.hapticSelection = hapticSelection;
110exports.hapticSelectionChanged = hapticSelectionChanged;
111exports.hapticSelectionEnd = hapticSelectionEnd;
112exports.hapticSelectionStart = hapticSelectionStart;