From 77035a070dca9134b6a14b909b94fc08cd346458 Mon Sep 17 00:00:00 2001 From: ShaneK Date: Mon, 23 Nov 2026 12:14:09 -0800 Subject: [PATCH] docs(haptics): add haptics guide documenting the ionHaptic event --- docs/developing/config.mdx | 82 ++++++++-------- docs/developing/haptics.mdx | 190 ++++++++++++++++++++++++++++++++++++ sidebars.js | 1 + 3 files changed, 232 insertions(+), 41 deletions(-) create mode 100644 docs/developing/haptics.mdx diff --git a/docs/developing/config.mdx b/docs/developing/config.mdx index 66d7b5eb0dc..bc825043507 100644 --- a/docs/developing/config.mdx +++ b/docs/developing/config.mdx @@ -170,44 +170,44 @@ class AppComponent { Below are the config options that Ionic uses. -| Config | Type | Description | -| --------------------------- | ---------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| `actionSheetEnter` | `AnimationBuilder` | Provides a custom enter animation for all `ion-action-sheet`, overriding the default "animation". | -| `actionSheetLeave` | `AnimationBuilder` | Provides a custom leave animation for all `ion-action-sheet`, overriding the default "animation". | -| `alertEnter` | `AnimationBuilder` | Provides a custom enter animation for all `ion-alert`, overriding the default "animation". | -| `alertLeave` | `AnimationBuilder` | Provides a custom leave animation for all `ion-alert`, overriding the default "animation". | -| `animated` | `boolean` | If `true`, Ionic will enable all animations and transitions across the app. | -| `backButtonDefaultHref` | `string` | Overrides the default value for the `defaultHref` property in all `` components. | -| `backButtonIcon` | `string` | Overrides the default icon in all `` components. | -| `backButtonText` | `string` | Overrides the default text in all `` components. | -| `innerHTMLTemplatesEnabled` | `boolean` | Relevant Components: `ion-alert`, `ion-infinite-scroll-content`, `ion-loading`, `ion-refresher-content`, `ion-select-option`, `ion-toast`. If `true`, content passed to the relevant components will be parsed as HTML instead of plaintext. Defaults to `false`. | -| `haptics` | `boolean` | Relevant Components: `ion-action-sheet`, `ion-alert`, `ion-picker-column`, `ion-refresher`, `ion-reorder-group`, `ion-toggle`. If `false`, Ionic components won't trigger haptics. Defaults to `true`. | -| `hardwareBackButton` | `boolean` | If `true`, Ionic will respond to the hardware back button in an Android device. | -| `infiniteLoadingSpinner` | `SpinnerTypes` | Overrides the default spinner type in all `` components. | -| `loadingEnter` | `AnimationBuilder` | Provides a custom enter animation for all `ion-loading`, overriding the default "animation". | -| `loadingLeave` | `AnimationBuilder` | Provides a custom leave animation for all `ion-loading`, overriding the default "animation". | -| `loadingSpinner` | `SpinnerTypes` | Overrides the default spinner for all `ion-loading` overlays. | -| `logLevel` | `'OFF' \| 'ERROR' \| 'WARN'` | Configures the logging level for Ionic Framework. If `'OFF'`, no errors or warnings are logged. If `'ERROR'`, only errors are logged. If `'WARN'`, errors and warnings are logged. | -| `menuIcon` | `string` | Overrides the default icon in all `` components. | -| `menuType` | `string` | Overrides the default menu type for all `` components. | -| `modalEnter` | `AnimationBuilder` | Provides a custom enter animation for all `ion-modal`, overriding the default "animation". | -| `modalLeave` | `AnimationBuilder` | Provides a custom leave animation for all `ion-modal`, overriding the default "animation". | -| `mode` | `Mode` | The mode determines which platform styles to use for the whole application. | -| `navAnimation` | `AnimationBuilder` | Overrides the default "animation" of all `ion-nav` and `ion-router-outlet` across the whole application. | -| `platform` | [`PlatformConfig`](/angular/platform.mdx#customizing-platform-detection-functions) | Overrides the default platform detection methods. | -| `popoverEnter` | `AnimationBuilder` | Provides a custom enter animation for all `ion-popover`, overriding the default "animation". | -| `popoverLeave` | `AnimationBuilder` | Provides a custom leave animation for all `ion-popover`, overriding the default "animation". | -| `refreshingIcon` | `string` | Overrides the default icon in all `` components. | -| `refreshingSpinner` | `SpinnerTypes` | Overrides the default spinner type in all `` components. | -| `rippleEffect` | `boolean` | If `true`, Material Design ripple effects will be enabled across the app. | -| `sanitizerEnabled` | `boolean` | If `true`, Ionic will enable a basic DOM sanitizer on component properties that accept custom HTML. | -| `spinner` | `SpinnerTypes` | Overrides the default spinner in all `` components. | -| `statusTap` | `boolean` | If `true`, clicking or tapping the status bar will cause the content to scroll to the top. | -| `swipeBackEnabled` | `boolean` | If `true`, Ionic will enable the "swipe-to-go-back" gesture across the application. | -| `tabButtonLayout` | `TabButtonLayout` | Overrides the default "layout" of all `ion-bar-button` across the whole application. | -| `toastDuration` | `number` | Overrides the default `duration` for all `ion-toast` components. | -| `toastEnter` | `AnimationBuilder` | Provides a custom enter animation for all `ion-toast`, overriding the default "animation". | -| `toastLeave` | `AnimationBuilder` | Provides a custom leave animation for all `ion-toast`, overriding the default "animation". | -| `toggleOnOffLabels` | `boolean` | Overrides the default `enableOnOffLabels` in all `ion-toggle` components. | -| `experimentalCloseWatcher` | `boolean` | **Experimental:** If `true`, the [CloseWatcher API](https://github.com/WICG/close-watcher) will be used to handle all Escape key and hardware back button presses to dismiss menus and overlays and to navigate. Note that the `hardwareBackButton` config option must also be `true`. | -| `focusManagerPriority` | [`FocusManagerPriority[]`](./managing-focus#types) | **Experimental:** When defined, Ionic will move focus to the appropriate element after each page transition. This ensures that users relying on assistive technology are informed when a page transition happens. Disabled by default. | +| Config | Type | Description | +| --------------------------- | ---------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | +| `actionSheetEnter` | `AnimationBuilder` | Provides a custom enter animation for all `ion-action-sheet`, overriding the default "animation". | +| `actionSheetLeave` | `AnimationBuilder` | Provides a custom leave animation for all `ion-action-sheet`, overriding the default "animation". | +| `alertEnter` | `AnimationBuilder` | Provides a custom enter animation for all `ion-alert`, overriding the default "animation". | +| `alertLeave` | `AnimationBuilder` | Provides a custom leave animation for all `ion-alert`, overriding the default "animation". | +| `animated` | `boolean` | If `true`, Ionic will enable all animations and transitions across the app. | +| `backButtonDefaultHref` | `string` | Overrides the default value for the `defaultHref` property in all `` components. | +| `backButtonIcon` | `string` | Overrides the default icon in all `` components. | +| `backButtonText` | `string` | Overrides the default text in all `` components. | +| `innerHTMLTemplatesEnabled` | `boolean` | Relevant Components: `ion-alert`, `ion-infinite-scroll-content`, `ion-loading`, `ion-refresher-content`, `ion-select-option`, `ion-toast`. If `true`, content passed to the relevant components will be parsed as HTML instead of plaintext. Defaults to `false`. | +| `haptics` | `boolean` | Relevant Components: `ion-action-sheet`, `ion-alert`, `ion-picker-column`, `ion-refresher`, `ion-reorder-group`, `ion-toggle`. If `false`, Ionic components won't trigger haptics. Defaults to `true`. Components still dispatch [`ionHaptic`](./haptics.mdx), so apps can play their own. | +| `hardwareBackButton` | `boolean` | If `true`, Ionic will respond to the hardware back button in an Android device. | +| `infiniteLoadingSpinner` | `SpinnerTypes` | Overrides the default spinner type in all `` components. | +| `loadingEnter` | `AnimationBuilder` | Provides a custom enter animation for all `ion-loading`, overriding the default "animation". | +| `loadingLeave` | `AnimationBuilder` | Provides a custom leave animation for all `ion-loading`, overriding the default "animation". | +| `loadingSpinner` | `SpinnerTypes` | Overrides the default spinner for all `ion-loading` overlays. | +| `logLevel` | `'OFF' \| 'ERROR' \| 'WARN'` | Configures the logging level for Ionic Framework. If `'OFF'`, no errors or warnings are logged. If `'ERROR'`, only errors are logged. If `'WARN'`, errors and warnings are logged. | +| `menuIcon` | `string` | Overrides the default icon in all `` components. | +| `menuType` | `string` | Overrides the default menu type for all `` components. | +| `modalEnter` | `AnimationBuilder` | Provides a custom enter animation for all `ion-modal`, overriding the default "animation". | +| `modalLeave` | `AnimationBuilder` | Provides a custom leave animation for all `ion-modal`, overriding the default "animation". | +| `mode` | `Mode` | The mode determines which platform styles to use for the whole application. | +| `navAnimation` | `AnimationBuilder` | Overrides the default "animation" of all `ion-nav` and `ion-router-outlet` across the whole application. | +| `platform` | [`PlatformConfig`](/angular/platform.mdx#customizing-platform-detection-functions) | Overrides the default platform detection methods. | +| `popoverEnter` | `AnimationBuilder` | Provides a custom enter animation for all `ion-popover`, overriding the default "animation". | +| `popoverLeave` | `AnimationBuilder` | Provides a custom leave animation for all `ion-popover`, overriding the default "animation". | +| `refreshingIcon` | `string` | Overrides the default icon in all `` components. | +| `refreshingSpinner` | `SpinnerTypes` | Overrides the default spinner type in all `` components. | +| `rippleEffect` | `boolean` | If `true`, Material Design ripple effects will be enabled across the app. | +| `sanitizerEnabled` | `boolean` | If `true`, Ionic will enable a basic DOM sanitizer on component properties that accept custom HTML. | +| `spinner` | `SpinnerTypes` | Overrides the default spinner in all `` components. | +| `statusTap` | `boolean` | If `true`, clicking or tapping the status bar will cause the content to scroll to the top. | +| `swipeBackEnabled` | `boolean` | If `true`, Ionic will enable the "swipe-to-go-back" gesture across the application. | +| `tabButtonLayout` | `TabButtonLayout` | Overrides the default "layout" of all `ion-bar-button` across the whole application. | +| `toastDuration` | `number` | Overrides the default `duration` for all `ion-toast` components. | +| `toastEnter` | `AnimationBuilder` | Provides a custom enter animation for all `ion-toast`, overriding the default "animation". | +| `toastLeave` | `AnimationBuilder` | Provides a custom leave animation for all `ion-toast`, overriding the default "animation". | +| `toggleOnOffLabels` | `boolean` | Overrides the default `enableOnOffLabels` in all `ion-toggle` components. | +| `experimentalCloseWatcher` | `boolean` | **Experimental:** If `true`, the [CloseWatcher API](https://github.com/WICG/close-watcher) will be used to handle all Escape key and hardware back button presses to dismiss menus and overlays and to navigate. Note that the `hardwareBackButton` config option must also be `true`. | +| `focusManagerPriority` | [`FocusManagerPriority[]`](./managing-focus#types) | **Experimental:** When defined, Ionic will move focus to the appropriate element after each page transition. This ensures that users relying on assistive technology are informed when a page transition happens. Disabled by default. | diff --git a/docs/developing/haptics.mdx b/docs/developing/haptics.mdx new file mode 100644 index 00000000000..ab1159697ed --- /dev/null +++ b/docs/developing/haptics.mdx @@ -0,0 +1,190 @@ +--- +title: Haptics +--- + +import Tabs from '@theme/Tabs'; +import TabItem from '@theme/TabItem'; + + + Haptics: Customize Touch Feedback in Ionic Components + + + +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 {/* #overview */} + +Ionic plays haptics through the [Capacitor Haptics plugin](../native/haptics.md) 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](./config.mdx). 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 {/* #components */} + +The table below shows which components dispatch `ionHaptic` and when. + +| Component | When | `detail.type` | +| ------------------- | ---------------------------------------------------------------------------------- | ---------------------------------------------------- | +| `ion-action-sheet` | Pressing and dragging across buttons in `ios` mode, when the buttons do not scroll | `selectionStart`, `selectionChanged`, `selectionEnd` | +| `ion-alert` | Pressing and dragging across buttons in `ios` mode | `selectionStart`, `selectionChanged`, `selectionEnd` | +| `ion-picker-column` | Scrolling the column on iOS | `selectionStart`, `selectionChanged`, `selectionEnd` | +| `ion-refresher` | Pulling far enough to refresh with the native iOS refresher | `impact` | +| `ion-reorder-group` | Dragging an item to reorder it | `selectionStart`, `selectionChanged`, `selectionEnd` | +| `ion-toggle` | Dragging the toggle, or tapping it on iOS | `selection` | + +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 {/* #event-detail */} + +The `detail` of the event describes the haptic Ionic would play. Its `type` matches the [Capacitor Haptics](../native/haptics.md) method Ionic would call, except `selection`, which has no matching method. + +| `detail.type` | Extra properties | Description | +| ------------------ | ---------------------------------------------------------- | ------------------------------------------------------------------------------------------------ | +| `selection` | | A one-time selection change. Ionic plays this as a light `impact`. | +| `selectionStart` | | A gesture that changes the selection has started. | +| `selectionChanged` | | The selection changed during a gesture. | +| `selectionEnd` | | The gesture has ended. | +| `impact` | `style`: `'LIGHT'`, `'MEDIUM'`, or `'HEAVY'` | A collision between user interface elements. | +| `notification` | `notificationType`: `'SUCCESS'`, `'WARNING'`, or `'ERROR'` | A task has succeeded, produced a warning, or failed. No built-in component dispatches this type. | + +### Gestures {/* #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 {/* #basic-usage */} + +The examples below listen for every `ionHaptic` event and log what Ionic would play. + +````mdx-code-block + + + +```javascript +document.addEventListener('ionHaptic', (event) => { + console.log(event.target.tagName, event.detail); +}); +``` + + + +```tsx +import { Component, HostListener } from '@angular/core'; +import type { HapticEvent } from '@ionic/angular/lazy'; + +@Component({ + ... +}) +export class AppComponent { + @HostListener('document:ionHaptic', ['$event']) + onHaptic(event: HapticEvent) { + console.log((event.target as HTMLElement).tagName, event.detail); + } +} +``` + + + +```tsx +import { Component, HostListener } from '@angular/core'; +import type { HapticEvent } from '@ionic/angular'; + +@Component({ + ... +}) +export class AppComponent { + @HostListener('document:ionHaptic', ['$event']) + onHaptic(event: HapticEvent) { + console.log((event.target as HTMLElement).tagName, event.detail); + } +} +``` + + + +```tsx +import { useEffect } from 'react'; +import type { HapticEvent } from '@ionic/react'; + +... + +useEffect(() => { + const onHaptic = (event: Event) => { + const { detail, target } = event as HapticEvent; + console.log((target as HTMLElement).tagName, detail); + }; + + document.addEventListener('ionHaptic', onHaptic); + return () => document.removeEventListener('ionHaptic', onHaptic); +}, []); +``` + + + +```typescript +import { onMounted, onUnmounted } from 'vue'; +import type { HapticEvent } from '@ionic/vue'; + +... + +const onHaptic = (event: Event) => { + const { detail, target } = event as HapticEvent; + console.log((target as HTMLElement).tagName, detail); +}; + +onMounted(() => document.addEventListener('ionHaptic', onHaptic)); +onUnmounted(() => document.removeEventListener('ionHaptic', onHaptic)); +``` + + +```` + +:::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](#basic-usage). + +::: + +## Replacing a Haptic {/* #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. + +```javascript +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 {/* #skipping-a-gesture */} + +Canceling `selectionStart` silences the whole gesture. The example below turns off haptics for reordering while keeping them everywhere else. + +```javascript +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](./config.mdx) instead. diff --git a/sidebars.js b/sidebars.js index 96c7adb61c5..a4c70541599 100644 --- a/sidebars.js +++ b/sidebars.js @@ -24,6 +24,7 @@ module.exports = { 'developing/android', 'developing/tips', 'developing/hardware-back-button', + 'developing/haptics', 'developing/keyboard', 'developing/config', 'developing/managing-focus',