Skip to main content
Version: v9

Haptics

Haptics are the small vibrations a device plays as touch feedback. Some Ionic components play a haptic during a gesture, such as when a toggle is switched or an item is reordered. This guide shows how to listen for those haptics so your app can replace them, skip them, or play its own.

Overview​

Ionic plays haptics through the Capacitor Haptics plugin when it is installed. Each time a component would play a haptic, it dispatches an ionHaptic event first. The event bubbles, so a single listener on document receives the events from every component.

The event is dispatched even when no haptic plays, such as when the @capacitor/haptics package is not installed, the app runs in a browser without vibration support, or haptics is false in the IonicConfig. Setting haptics: false and listening for ionHaptic lets an app replace every built-in haptic with its own.

Calling preventDefault() on the event skips the built-in haptic for that event only.

Components​

The table below shows which components dispatch ionHaptic and when.

ComponentWhendetail.type
ion-action-sheetPressing and dragging across buttons in ios mode, when the buttons do not scrollselectionStart, selectionChanged, selectionEnd
ion-alertPressing and dragging across buttons in ios modeselectionStart, selectionChanged, selectionEnd
ion-picker-columnScrolling the column on iOSselectionStart, selectionChanged, selectionEnd
ion-refresherPulling far enough to refresh with the native iOS refresherimpact
ion-reorder-groupDragging an item to reorder itselectionStart, selectionChanged, selectionEnd
ion-toggleDragging the toggle, or tapping it on iOSselection

Some components use these components internally. ion-datetime renders ion-picker-column inside its shadow root, so a listener outside the datetime receives ion-datetime as event.target. Use event.composedPath()[0] to get the inner component. ion-select opens an ion-alert or ion-action-sheet, so the event comes from that overlay instead of the select.

Event Detail​

The detail of the event describes the haptic Ionic would play. Its type matches the Capacitor Haptics method Ionic would call, except selection, which has no matching method.

detail.typeExtra propertiesDescription
selectionA one-time selection change. Ionic plays this as a light impact.
selectionStartA gesture that changes the selection has started.
selectionChangedThe selection changed during a gesture.
selectionEndThe gesture has ended.
impactstyle: 'LIGHT', 'MEDIUM', or 'HEAVY'A collision between user interface elements.
notificationnotificationType: 'SUCCESS', 'WARNING', or 'ERROR'A task has succeeded, produced a warning, or failed. No built-in component dispatches this type.

Gestures​

A gesture dispatches selectionStart, then one selectionChanged for each change, then selectionEnd. In ion-action-sheet and ion-alert, selectionStart is dispatched only when the press begins on a button. A press that begins elsewhere dispatches selectionChanged and selectionEnd without a selectionStart. The haptic engine only plays selectionChanged after a selectionStart, so canceling selectionStart also silences the rest of that gesture.

selectionEnd plays nothing. It releases the haptic engine for the next gesture, so Ionic always runs it, and canceling it has no effect.

Basic Usage​

The examples below listen for every ionHaptic event and log what Ionic would play.

document.addEventListener('ionHaptic', (event) => {
console.log(event.target.tagName, event.detail);
});
note

ionHaptic is not listed in the Events table of each component, so the framework event bindings do not know about it. In Vue, @ionHaptic on an Ionic component does not work. Add the listener with addEventListener instead, like the Vue example in Basic Usage.

Replacing a Haptic​

Call preventDefault() to skip the built-in haptic, then play your own. The example below plays a medium impact instead of the light one Ionic plays when a toggle is switched.

import { Haptics, ImpactStyle } from '@capacitor/haptics';

document.addEventListener('ionHaptic', (event) => {
if (event.target.tagName === 'ION-TOGGLE' && event.detail.type === 'selection') {
event.preventDefault();
Haptics.impact({ style: ImpactStyle.Medium });
}
});

Skipping a Gesture​

Canceling selectionStart silences the whole gesture. The example below turns off haptics for reordering while keeping them everywhere else.

document.addEventListener('ionHaptic', (event) => {
if (event.target.tagName === 'ION-REORDER-GROUP' && event.detail.type === 'selectionStart') {
event.preventDefault();
}
});

To turn off every built-in haptic, set haptics: false in the IonicConfig instead.