haptic.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*!
  2. * (C) Ionic http://ionicframework.com - MIT License
  3. */
  4. import { g as getCapacitor } from './capacitor.js';
  5. var ImpactStyle;
  6. (function (ImpactStyle) {
  7. /**
  8. * A collision between large, heavy user interface elements
  9. *
  10. * @since 1.0.0
  11. */
  12. ImpactStyle["Heavy"] = "HEAVY";
  13. /**
  14. * A collision between moderately sized user interface elements
  15. *
  16. * @since 1.0.0
  17. */
  18. ImpactStyle["Medium"] = "MEDIUM";
  19. /**
  20. * A collision between small, light user interface elements
  21. *
  22. * @since 1.0.0
  23. */
  24. ImpactStyle["Light"] = "LIGHT";
  25. })(ImpactStyle || (ImpactStyle = {}));
  26. var NotificationType;
  27. (function (NotificationType) {
  28. /**
  29. * A notification feedback type indicating that a task has completed successfully
  30. *
  31. * @since 1.0.0
  32. */
  33. NotificationType["Success"] = "SUCCESS";
  34. /**
  35. * A notification feedback type indicating that a task has produced a warning
  36. *
  37. * @since 1.0.0
  38. */
  39. NotificationType["Warning"] = "WARNING";
  40. /**
  41. * A notification feedback type indicating that a task has failed
  42. *
  43. * @since 1.0.0
  44. */
  45. NotificationType["Error"] = "ERROR";
  46. })(NotificationType || (NotificationType = {}));
  47. const HapticEngine = {
  48. getEngine() {
  49. const capacitor = getCapacitor();
  50. if (capacitor === null || capacitor === void 0 ? void 0 : capacitor.isPluginAvailable('Haptics')) {
  51. // Capacitor
  52. return capacitor.Plugins.Haptics;
  53. }
  54. return undefined;
  55. },
  56. available() {
  57. const engine = this.getEngine();
  58. if (!engine) {
  59. return false;
  60. }
  61. const capacitor = getCapacitor();
  62. /**
  63. * Developers can manually import the
  64. * Haptics plugin in their app which will cause
  65. * getEngine to return the Haptics engine. However,
  66. * the Haptics engine will throw an error if
  67. * used in a web browser that does not support
  68. * the Vibrate API. This check avoids that error
  69. * if the browser does not support the Vibrate API.
  70. */
  71. if ((capacitor === null || capacitor === void 0 ? void 0 : capacitor.getPlatform()) === 'web') {
  72. // eslint-disable-next-line @typescript-eslint/prefer-optional-chain
  73. return typeof navigator !== 'undefined' && navigator.vibrate !== undefined;
  74. }
  75. return true;
  76. },
  77. impact(options) {
  78. const engine = this.getEngine();
  79. if (!engine) {
  80. return;
  81. }
  82. engine.impact({ style: options.style });
  83. },
  84. notification(options) {
  85. const engine = this.getEngine();
  86. if (!engine) {
  87. return;
  88. }
  89. engine.notification({ type: options.type });
  90. },
  91. selection() {
  92. this.impact({ style: ImpactStyle.Light });
  93. },
  94. selectionStart() {
  95. const engine = this.getEngine();
  96. if (!engine) {
  97. return;
  98. }
  99. engine.selectionStart();
  100. },
  101. selectionChanged() {
  102. const engine = this.getEngine();
  103. if (!engine) {
  104. return;
  105. }
  106. engine.selectionChanged();
  107. },
  108. selectionEnd() {
  109. const engine = this.getEngine();
  110. if (!engine) {
  111. return;
  112. }
  113. engine.selectionEnd();
  114. },
  115. };
  116. /**
  117. * Check to see if the Haptic Plugin is available
  118. * @return Returns `true` or false if the plugin is available
  119. */
  120. const hapticAvailable = () => {
  121. return HapticEngine.available();
  122. };
  123. /**
  124. * Trigger a selection changed haptic event. Good for one-time events
  125. * (not for gestures)
  126. */
  127. const hapticSelection = () => {
  128. hapticAvailable() && HapticEngine.selection();
  129. };
  130. /**
  131. * Tell the haptic engine that a gesture for a selection change is starting.
  132. */
  133. const hapticSelectionStart = () => {
  134. hapticAvailable() && HapticEngine.selectionStart();
  135. };
  136. /**
  137. * Tell the haptic engine that a selection changed during a gesture.
  138. */
  139. const hapticSelectionChanged = () => {
  140. hapticAvailable() && HapticEngine.selectionChanged();
  141. };
  142. /**
  143. * Tell the haptic engine we are done with a gesture. This needs to be
  144. * called lest resources are not properly recycled.
  145. */
  146. const hapticSelectionEnd = () => {
  147. hapticAvailable() && HapticEngine.selectionEnd();
  148. };
  149. /**
  150. * Use this to indicate success/failure/warning to the user.
  151. * options should be of the type `{ style: ImpactStyle.LIGHT }` (or `MEDIUM`/`HEAVY`)
  152. */
  153. const hapticImpact = (options) => {
  154. hapticAvailable() && HapticEngine.impact(options);
  155. };
  156. export { ImpactStyle as I, hapticSelectionStart as a, hapticSelectionChanged as b, hapticImpact as c, hapticSelection as d, hapticSelectionEnd as h };