UNPKG

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