Skip to main content

Shake

Apache Cordova / PhoneGap Plugin to detect when a physical device performs a shake gesture.

For iOS, the plugin uses the native shake detection. Fo all other platforms, it is based on a standalone JavaScript implementation.

Online documentation

Github documentation

Installation

Cordova

cordova plugin add cordova-plugin-shake
npm install @awesome-cordova-library/shake

Capacitor / Ionic

npm install cordova-plugin-shake
npm install @awesome-cordova-library/shake
npx cap sync

Vanilla

Declaration

export default class Shake {
static startWatch(
onShake: () => void,
sensitivity?: number,
onError?: () => void
): void;
static stopWatch(): void;
static warnPluginIsUnInstallOrIncompatible(): void;
}

Usages

import Shake from '@awesome-cordova-library/shake';

Shake.startWatch(() => console.log("deviceShaken success"), 40, () => console.err("deviceShaken error"));
setTimeout(() => {
Shake.stopWatch();
}, 2000);
.....

React

Declaration

declare const useShake: () => {
startWatch: (
onShake: () => void,
sensitivity?: number,
onError?: () => void
) => void;
stopWatch: () => void;
};

Usages

import { useEffect } from 'react';
import useShake from '@awesome-cordova-library/shake/lib/react';

function App() {
const { startWatch, stopWatch } = useShake();

useEffect(() => {
startWatch(
() => console.log('deviceShaken success'),
40,
() => console.err('deviceShaken error'),
);
return {
stopWatch();,
};
}, []);

return <div></div>;
}