Skip to content

Commit

Permalink
(chore) Ignore web haptics unavailable error (#1662)
Browse files Browse the repository at this point in the history
  • Loading branch information
aeharding authored Nov 6, 2024
1 parent b4ce56e commit 26ede56
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/helpers/useHapticFeedback.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Capacitor, CapacitorException, ExceptionCode } from "@capacitor/core";
import {
Haptics,
ImpactOptions,
Expand All @@ -13,11 +14,23 @@ export default function useHapticFeedback() {
);

return useCallback(
(options: ImpactOptions | NotificationOptions) => {
async (options: ImpactOptions | NotificationOptions) => {
if (!enabled) return;

if ("style" in options) Haptics.impact(options);
else Haptics.notification(options);
try {
if ("style" in options) await Haptics.impact(options);
else await Haptics.notification(options);
} catch (e) {
// Ignore if the web browser doesn't support haptics
if (
Capacitor.getPlatform() === "web" &&
e instanceof CapacitorException &&
e.code === ExceptionCode.Unavailable
)
return;

throw e;
}
},
[enabled],
);
Expand Down

0 comments on commit 26ede56

Please sign in to comment.