From 911594f57800f90347f90934a44e9f87e543cbf0 Mon Sep 17 00:00:00 2001 From: ShaneK Date: Wed, 18 Nov 2026 13:57:39 -0800 Subject: [PATCH 1/5] feat(fab-list): add animated prop to disable the staggered button reveal --- core/api.txt | 1 + core/src/components.d.ts | 11 +++ core/src/components/fab-list/fab-list.tsx | 20 ++++- .../fab-list/test/animation/fab-list.e2e.ts | 77 +++++++++++++++++++ core/src/components/fab/fab.tsx | 4 + .../angular/src/lazy/directives/proxies.ts | 4 +- .../src/standalone/directives/ion-fab-list.ts | 4 +- packages/vue/src/proxies.ts | 3 +- 8 files changed, 117 insertions(+), 7 deletions(-) create mode 100644 core/src/components/fab-list/test/animation/fab-list.e2e.ts diff --git a/core/api.txt b/core/api.txt index 86a1d0251d7..7e04daee6c4 100644 --- a/core/api.txt +++ b/core/api.txt @@ -667,6 +667,7 @@ ion-fab-button,part,native ion-fab-list,shadow ion-fab-list,prop,activated,boolean,false,false,false +ion-fab-list,prop,animated,boolean,true,false,false ion-fab-list,prop,side,"bottom" | "end" | "start" | "top",'bottom',false,false ion-footer,none diff --git a/core/src/components.d.ts b/core/src/components.d.ts index af6fa7794f9..fd2671ed485 100644 --- a/core/src/components.d.ts +++ b/core/src/components.d.ts @@ -1207,6 +1207,11 @@ export namespace Components { * @default false */ "activated": boolean; + /** + * If `true`, the fab buttons in the list will show one at a time when the list opens. Inside an `ion-fab`, this is ignored and the main fab button's `animated` is used instead. + * @default true + */ + "animated": boolean; /** * The side the fab list will show on relative to the main fab button. * @default 'bottom' @@ -6364,6 +6369,11 @@ declare namespace LocalJSX { * @default false */ "activated"?: boolean; + /** + * If `true`, the fab buttons in the list will show one at a time when the list opens. Inside an `ion-fab`, this is ignored and the main fab button's `animated` is used instead. + * @default true + */ + "animated"?: boolean; /** * The side the fab list will show on relative to the main fab button. * @default 'bottom' @@ -9207,6 +9217,7 @@ declare namespace LocalJSX { interface IonFabListAttributes { "activated": boolean; "side": 'start' | 'end' | 'top' | 'bottom'; + "animated": boolean; } interface IonFooterAttributes { "collapse": 'fade'; diff --git a/core/src/components/fab-list/fab-list.tsx b/core/src/components/fab-list/fab-list.tsx index 1803f7f2c30..bebc13dad5a 100644 --- a/core/src/components/fab-list/fab-list.tsx +++ b/core/src/components/fab-list/fab-list.tsx @@ -1,6 +1,8 @@ import type { ComponentInterface } from '@stencil/core'; import { Component, Element, Host, Prop, Watch, h } from '@stencil/core'; +import { win } from '@utils/browser'; +import { config } from '../../global/config'; import { getIonMode } from '../../global/ionic-global'; @Component({ @@ -24,8 +26,7 @@ export class FabList implements ComponentInterface { const fabs = Array.from(this.el.querySelectorAll('ion-fab-button')); - // if showing the fabs add a timeout, else show immediately - const timeout = activated ? 30 : 0; + const timeout = activated && this.shouldAnimate() ? 30 : 0; fabs.forEach((fab, i) => { this.activateTimeouts.push(setTimeout(() => (fab.show = activated), i * timeout)); }); @@ -36,11 +37,26 @@ export class FabList implements ComponentInterface { */ @Prop() side: 'start' | 'end' | 'top' | 'bottom' = 'bottom'; + /** + * If `true`, the fab buttons in the list will show one at a time + * when the list opens. Inside an `ion-fab`, this is ignored and the + * main fab button's `animated` is used instead. + */ + @Prop() animated = true; + disconnectedCallback() { this.activateTimeouts.forEach(clearTimeout); this.activateTimeouts = []; } + private shouldAnimate() { + if (!this.animated || !config.getBoolean('animated', true)) { + return false; + } + + return !win?.matchMedia?.('(prefers-reduced-motion: reduce)').matches; + } + render() { const mode = getIonMode(this); return ( diff --git a/core/src/components/fab-list/test/animation/fab-list.e2e.ts b/core/src/components/fab-list/test/animation/fab-list.e2e.ts new file mode 100644 index 00000000000..4f3e8472d3e --- /dev/null +++ b/core/src/components/fab-list/test/animation/fab-list.e2e.ts @@ -0,0 +1,77 @@ +import { expect } from '@playwright/test'; +import type { Page } from '@playwright/test'; +import { configs, test } from '@utils/test/playwright'; + +/** + * Returns each list button's `show` right after the first reveal. A + * staggered list has only shown its first button by then. + */ +const activateAndGetShownButtons = (page: Page, selector: 'ion-fab' | 'ion-fab-list' = 'ion-fab') => + page.locator(selector).evaluate(async (el: HTMLIonFabElement | HTMLIonFabListElement) => { + el.activated = true; + await new Promise((resolve) => setTimeout(resolve, 0)); + return Array.from(el.querySelectorAll('ion-fab-list ion-fab-button')).map( + (button) => (button as HTMLIonFabButtonElement).show + ); + }); + +const listButtons = ` + + + +`; + +const fabMarkup = ({ fabButtonAttrs = '', fabListAttrs = '' } = {}) => ` + + + + + ${listButtons} + +`; + +/** + * This behavior does not vary across modes/directions. + */ +configs({ modes: ['md'], directions: ['ltr'] }).forEach(({ title, config }) => { + test.describe(title('fab-list: animation'), () => { + test('should show the list buttons one at a time by default', async ({ page }) => { + await page.setContent(fabMarkup(), config); + + expect(await activateAndGetShownButtons(page)).toEqual([true, false, false]); + await expect(page.locator('ion-fab-list ion-fab-button').last()).toHaveClass(/fab-button-show/); + }); + + test('should show all list buttons at once when animated is false', async ({ page }) => { + // Inside an `ion-fab` the list follows the main button, so this tests the list on its own. + await page.setContent(`${listButtons}`, config); + + expect(await activateAndGetShownButtons(page, 'ion-fab-list')).toEqual([true, true, true]); + }); + + test('should show all list buttons at once when the animated config is false', async ({ page }) => { + await page.setContent(`${fabMarkup()}`, config); + + expect(await activateAndGetShownButtons(page)).toEqual([true, true, true]); + }); + + test('should show all list buttons at once when the user prefers reduced motion', async ({ page }) => { + await page.emulateMedia({ reducedMotion: 'reduce' }); + await page.setContent(fabMarkup(), config); + + expect(await activateAndGetShownButtons(page)).toEqual([true, true, true]); + }); + + test('should show all list buttons at once when the main fab button is not animated', async ({ page }) => { + await page.setContent(fabMarkup({ fabButtonAttrs: 'animated="false"' }), config); + + expect(await activateAndGetShownButtons(page)).toEqual([true, true, true]); + }); + + test('should follow the main fab button over the animated property of the list', async ({ page }) => { + await page.setContent(fabMarkup({ fabListAttrs: 'animated="false"' }), config); + + expect(await activateAndGetShownButtons(page)).toEqual([true, false, false]); + }); + }); +}); diff --git a/core/src/components/fab/fab.tsx b/core/src/components/fab/fab.tsx index 221335062e5..b3f28c9a7b4 100644 --- a/core/src/components/fab/fab.tsx +++ b/core/src/components/fab/fab.tsx @@ -41,6 +41,10 @@ export class Fab implements ComponentInterface { fab.activated = activated; } Array.from(this.el.querySelectorAll('ion-fab-list')).forEach((list) => { + if (fab) { + // set before activated so the list reads it when it opens + list.animated = fab.animated; + } list.activated = activated; }); } diff --git a/packages/angular/src/lazy/directives/proxies.ts b/packages/angular/src/lazy/directives/proxies.ts index 95ffa9eaba9..4831a21cd03 100644 --- a/packages/angular/src/lazy/directives/proxies.ts +++ b/packages/angular/src/lazy/directives/proxies.ts @@ -831,14 +831,14 @@ export declare interface IonFabButton extends Components.IonFabButton { @ProxyCmp({ - inputs: ['activated', 'side'] + inputs: ['activated', 'animated', 'side'] }) @Component({ selector: 'ion-fab-list', changeDetection: ChangeDetectionStrategy.OnPush, template: '', // eslint-disable-next-line @angular-eslint/no-inputs-metadata-property - inputs: ['activated', 'side'], + inputs: ['activated', 'animated', 'side'], standalone: false }) export class IonFabList { diff --git a/packages/angular/src/standalone/directives/ion-fab-list.ts b/packages/angular/src/standalone/directives/ion-fab-list.ts index 2e2860c1a0a..baa24374936 100644 --- a/packages/angular/src/standalone/directives/ion-fab-list.ts +++ b/packages/angular/src/standalone/directives/ion-fab-list.ts @@ -10,14 +10,14 @@ import { defineCustomElement as defineIonFabList } from '@ionic/core/components/ @ProxyCmp({ defineCustomElementFn: defineIonFabList, - inputs: ['activated', 'side'] + inputs: ['activated', 'animated', 'side'] }) @Component({ selector: 'ion-fab-list', changeDetection: ChangeDetectionStrategy.OnPush, template: '', // eslint-disable-next-line @angular-eslint/no-inputs-metadata-property - inputs: ['activated', 'side'], + inputs: ['activated', 'animated', 'side'], }) export class IonFabList { protected el: HTMLIonFabListElement; diff --git a/packages/vue/src/proxies.ts b/packages/vue/src/proxies.ts index aaaedee7512..8fc0b1d78f0 100644 --- a/packages/vue/src/proxies.ts +++ b/packages/vue/src/proxies.ts @@ -397,7 +397,8 @@ export const IonFabButton: StencilVueComponent = /*@__PURE__*/ export const IonFabList: StencilVueComponent = /*@__PURE__*/ defineContainer('ion-fab-list', defineIonFabList, [ 'activated', - 'side' + 'side', + 'animated' ]); From 9fa60703e79d7c3f9a9bcd3007736f433ef2b0ed Mon Sep 17 00:00:00 2001 From: ShaneK Date: Fri, 20 Nov 2026 08:58:02 -0800 Subject: [PATCH 2/5] feat(fab-list): let the list animated prop override the main fab button --- core/api.txt | 2 +- core/src/components.d.ts | 12 ++++----- core/src/components/fab-button/fab-button.tsx | 4 ++- core/src/components/fab-list/fab-list.tsx | 11 +++++--- .../fab-list/test/animation/fab-list.e2e.ts | 25 +++++++++++++++---- core/src/components/fab/fab.tsx | 4 --- 6 files changed, 36 insertions(+), 22 deletions(-) diff --git a/core/api.txt b/core/api.txt index 7e04daee6c4..61d821e7cd0 100644 --- a/core/api.txt +++ b/core/api.txt @@ -667,7 +667,7 @@ ion-fab-button,part,native ion-fab-list,shadow ion-fab-list,prop,activated,boolean,false,false,false -ion-fab-list,prop,animated,boolean,true,false,false +ion-fab-list,prop,animated,boolean | undefined,undefined,false,false ion-fab-list,prop,side,"bottom" | "end" | "start" | "top",'bottom',false,false ion-footer,none diff --git a/core/src/components.d.ts b/core/src/components.d.ts index fd2671ed485..fdc0f00293b 100644 --- a/core/src/components.d.ts +++ b/core/src/components.d.ts @@ -1130,7 +1130,7 @@ export namespace Components { */ "activated": boolean; /** - * If `true`, the fab button will animate between states, such as swapping its content for the close icon. + * If `true`, the fab button will animate between states, such as swapping its content for the close icon. As the main button of an `ion-fab`, it also sets whether the `ion-fab-list` buttons show one at a time, unless the list sets its own `animated`. * @default true */ "animated": boolean; @@ -1208,10 +1208,9 @@ export namespace Components { */ "activated": boolean; /** - * If `true`, the fab buttons in the list will show one at a time when the list opens. Inside an `ion-fab`, this is ignored and the main fab button's `animated` is used instead. - * @default true + * If `true`, the fab buttons in the list will show one at a time when the list opens. If not set, it follows the main fab button's `animated`. */ - "animated": boolean; + "animated"?: boolean; /** * The side the fab list will show on relative to the main fab button. * @default 'bottom' @@ -6284,7 +6283,7 @@ declare namespace LocalJSX { */ "activated"?: boolean; /** - * If `true`, the fab button will animate between states, such as swapping its content for the close icon. + * If `true`, the fab button will animate between states, such as swapping its content for the close icon. As the main button of an `ion-fab`, it also sets whether the `ion-fab-list` buttons show one at a time, unless the list sets its own `animated`. * @default true */ "animated"?: boolean; @@ -6370,8 +6369,7 @@ declare namespace LocalJSX { */ "activated"?: boolean; /** - * If `true`, the fab buttons in the list will show one at a time when the list opens. Inside an `ion-fab`, this is ignored and the main fab button's `animated` is used instead. - * @default true + * If `true`, the fab buttons in the list will show one at a time when the list opens. If not set, it follows the main fab button's `animated`. */ "animated"?: boolean; /** diff --git a/core/src/components/fab-button/fab-button.tsx b/core/src/components/fab-button/fab-button.tsx index f3df7973043..5543659ad55 100755 --- a/core/src/components/fab-button/fab-button.tsx +++ b/core/src/components/fab-button/fab-button.tsx @@ -52,7 +52,9 @@ export class FabButton implements ComponentInterface, AnchorInterface, ButtonInt /** * If `true`, the fab button will animate between states, such as - * swapping its content for the close icon. + * swapping its content for the close icon. As the main button of an + * `ion-fab`, it also sets whether the `ion-fab-list` buttons show one + * at a time, unless the list sets its own `animated`. */ @Prop() animated = true; diff --git a/core/src/components/fab-list/fab-list.tsx b/core/src/components/fab-list/fab-list.tsx index bebc13dad5a..11bdab4f11e 100644 --- a/core/src/components/fab-list/fab-list.tsx +++ b/core/src/components/fab-list/fab-list.tsx @@ -39,10 +39,10 @@ export class FabList implements ComponentInterface { /** * If `true`, the fab buttons in the list will show one at a time - * when the list opens. Inside an `ion-fab`, this is ignored and the - * main fab button's `animated` is used instead. + * when the list opens. If not set, it follows the main fab button's + * `animated`. */ - @Prop() animated = true; + @Prop() animated?: boolean; disconnectedCallback() { this.activateTimeouts.forEach(clearTimeout); @@ -50,7 +50,10 @@ export class FabList implements ComponentInterface { } private shouldAnimate() { - if (!this.animated || !config.getBoolean('animated', true)) { + const fabButton = this.el.closest('ion-fab')?.querySelector(':scope > ion-fab-button'); + const animated = this.animated ?? fabButton?.animated ?? true; + + if (!animated || !config.getBoolean('animated', true)) { return false; } diff --git a/core/src/components/fab-list/test/animation/fab-list.e2e.ts b/core/src/components/fab-list/test/animation/fab-list.e2e.ts index 4f3e8472d3e..d72259a7019 100644 --- a/core/src/components/fab-list/test/animation/fab-list.e2e.ts +++ b/core/src/components/fab-list/test/animation/fab-list.e2e.ts @@ -3,8 +3,8 @@ import type { Page } from '@playwright/test'; import { configs, test } from '@utils/test/playwright'; /** - * Returns each list button's `show` right after the first reveal. A - * staggered list has only shown its first button by then. + * Returns each list button's `show` right after opening, when a + * staggered list has only shown its first button. */ const activateAndGetShownButtons = (page: Page, selector: 'ion-fab' | 'ion-fab-list' = 'ion-fab') => page.locator(selector).evaluate(async (el: HTMLIonFabElement | HTMLIonFabListElement) => { @@ -42,8 +42,13 @@ configs({ modes: ['md'], directions: ['ltr'] }).forEach(({ title, config }) => { await expect(page.locator('ion-fab-list ion-fab-button').last()).toHaveClass(/fab-button-show/); }); - test('should show all list buttons at once when animated is false', async ({ page }) => { - // Inside an `ion-fab` the list follows the main button, so this tests the list on its own. + test('should show the list buttons one at a time by default outside of a fab', async ({ page }) => { + await page.setContent(`${listButtons}`, config); + + expect(await activateAndGetShownButtons(page, 'ion-fab-list')).toEqual([true, false, false]); + }); + + test('should show all list buttons at once when animated is false outside of a fab', async ({ page }) => { await page.setContent(`${listButtons}`, config); expect(await activateAndGetShownButtons(page, 'ion-fab-list')).toEqual([true, true, true]); @@ -68,9 +73,19 @@ configs({ modes: ['md'], directions: ['ltr'] }).forEach(({ title, config }) => { expect(await activateAndGetShownButtons(page)).toEqual([true, true, true]); }); - test('should follow the main fab button over the animated property of the list', async ({ page }) => { + test('should show all list buttons at once when the list is not animated under an animated main fab button', async ({ + page, + }) => { await page.setContent(fabMarkup({ fabListAttrs: 'animated="false"' }), config); + expect(await activateAndGetShownButtons(page)).toEqual([true, true, true]); + }); + + test('should show the list buttons one at a time when the list is animated under a main fab button that is not', async ({ + page, + }) => { + await page.setContent(fabMarkup({ fabButtonAttrs: 'animated="false"', fabListAttrs: 'animated="true"' }), config); + expect(await activateAndGetShownButtons(page)).toEqual([true, false, false]); }); }); diff --git a/core/src/components/fab/fab.tsx b/core/src/components/fab/fab.tsx index b3f28c9a7b4..221335062e5 100644 --- a/core/src/components/fab/fab.tsx +++ b/core/src/components/fab/fab.tsx @@ -41,10 +41,6 @@ export class Fab implements ComponentInterface { fab.activated = activated; } Array.from(this.el.querySelectorAll('ion-fab-list')).forEach((list) => { - if (fab) { - // set before activated so the list reads it when it opens - list.animated = fab.animated; - } list.activated = activated; }); } From e40dee635efad316c1556f9cf529ef4e311bae0e Mon Sep 17 00:00:00 2001 From: ShaneK Date: Mon, 23 Nov 2026 06:42:12 -0800 Subject: [PATCH 3/5] feat(haptics): emit cancelable ionHaptic event when components trigger haptics --- .../components/action-sheet/action-sheet.tsx | 2 +- .../test/basic/action-sheet.e2e.ts | 29 +++++++ core/src/components/alert/alert.tsx | 2 +- .../picker-column/picker-column.tsx | 6 +- core/src/components/refresher/refresher.tsx | 2 +- .../reorder-group/reorder-group.tsx | 6 +- .../test/reorder-group-events.e2e.ts | 39 +++++++++ .../src/components/toggle/test/toggle.spec.ts | 21 ++++- core/src/components/toggle/toggle.tsx | 4 +- core/src/interface.d.ts | 1 + core/src/utils/config.ts | 1 + core/src/utils/gesture/button-active.ts | 17 ++-- core/src/utils/native/haptic.ts | 61 +++++++++++--- core/src/utils/native/test/haptic.spec.ts | 83 +++++++++++++++++-- packages/angular/src/lazy/index.ts | 2 + packages/angular/src/standalone/index.ts | 2 + packages/react/src/components/index.ts | 2 + packages/vue/src/index.ts | 2 + 18 files changed, 245 insertions(+), 37 deletions(-) diff --git a/core/src/components/action-sheet/action-sheet.tsx b/core/src/components/action-sheet/action-sheet.tsx index 8d63b81021d..0bbe4b146c3 100644 --- a/core/src/components/action-sheet/action-sheet.tsx +++ b/core/src/components/action-sheet/action-sheet.tsx @@ -510,7 +510,7 @@ export class ActionSheet implements ComponentInterface, OverlayInterface { if (this.gesture || !this.el.isConnected || groupEl.scrollHeight > groupEl.clientHeight) { return; } - this.gesture = createButtonActiveGesture(wrapperEl, (refEl: HTMLElement) => + this.gesture = createButtonActiveGesture(this.el, wrapperEl, (refEl: HTMLElement) => refEl.classList.contains('action-sheet-button') ); this.gesture.enable(true); diff --git a/core/src/components/action-sheet/test/basic/action-sheet.e2e.ts b/core/src/components/action-sheet/test/basic/action-sheet.e2e.ts index 19368dd652f..a039de48842 100644 --- a/core/src/components/action-sheet/test/basic/action-sheet.e2e.ts +++ b/core/src/components/action-sheet/test/basic/action-sheet.e2e.ts @@ -219,3 +219,32 @@ configs({ modes: ['ios'], directions: ['ltr'] }).forEach(({ config, title }) => }); }); }); + +/** + * The button press haptic only runs in iOS mode + */ +configs({ modes: ['ios'], directions: ['ltr'] }).forEach(({ config, title }) => { + test.describe(title('action sheet: haptics'), () => { + test('should emit ionHaptic from the action sheet when a button is pressed', async ({ page }) => { + await page.goto('/src/components/action-sheet/test/basic', config); + + const ionActionSheetDidPresent = await page.spyOnEvent('ionActionSheetDidPresent'); + const ionHaptic = await page.spyOnEvent('ionHaptic'); + + await page.click('#basic'); + await ionActionSheetDidPresent.next(); + + const button = page.locator('ion-action-sheet .action-sheet-button').first(); + const box = (await button.boundingBox())!; + + await page.mouse.move(box.x + box.width / 2, box.y + box.height / 2); + await page.mouse.down(); + await ionHaptic.next(); + + await expect(ionHaptic).toHaveReceivedEventDetail({ type: 'selectionStart' }); + expect((ionHaptic.events[0].target as HTMLElement).tagName.toLowerCase()).toBe('ion-action-sheet'); + + await page.mouse.up(); + }); + }); +}); diff --git a/core/src/components/alert/alert.tsx b/core/src/components/alert/alert.tsx index b8f121e57e0..e2071ed9be7 100644 --- a/core/src/components/alert/alert.tsx +++ b/core/src/components/alert/alert.tsx @@ -401,7 +401,7 @@ export class Alert implements ComponentInterface, OverlayInterface { */ private setupButtonActiveGesture() { if (!this.gesture && getIonMode(this) === 'ios' && this.wrapperEl) { - this.gesture = createButtonActiveGesture(this.wrapperEl, (refEl: HTMLElement) => + this.gesture = createButtonActiveGesture(this.el, this.wrapperEl, (refEl: HTMLElement) => refEl.classList.contains('alert-button') ); this.gesture.enable(true); diff --git a/core/src/components/picker-column/picker-column.tsx b/core/src/components/picker-column/picker-column.tsx index ea56f0711b2..3767c5af337 100644 --- a/core/src/components/picker-column/picker-column.tsx +++ b/core/src/components/picker-column/picker-column.tsx @@ -392,7 +392,7 @@ export class PickerColumn implements ComponentInterface { this.isScrolling = false; this.updateValueTextOnScroll = true; - this.enableHaptics && hapticSelectionEnd(); + this.enableHaptics && hapticSelectionEnd(this.el); /** * Certain tasks (such as those that @@ -503,7 +503,7 @@ export class PickerColumn implements ComponentInterface { this.resetScrollEndTimeout(); if (!this.isScrolling) { - this.enableHaptics && hapticSelectionStart(); + this.enableHaptics && hapticSelectionStart(this.el); this.isScrolling = true; } @@ -600,7 +600,7 @@ export class PickerColumn implements ComponentInterface { * we need to run haptics again. */ if (newActiveElement !== activeEl) { - this.enableHaptics && hapticSelectionChanged(); + this.enableHaptics && hapticSelectionChanged(this.el); if (this.canExitInputMode) { /** diff --git a/core/src/components/refresher/refresher.tsx b/core/src/components/refresher/refresher.tsx index 153f4cc6f76..e0f49bd57b8 100644 --- a/core/src/components/refresher/refresher.tsx +++ b/core/src/components/refresher/refresher.tsx @@ -272,7 +272,7 @@ export class Refresher implements ComponentInterface { if (!this.didRefresh) { this.beginRefresh(); this.didRefresh = true; - hapticImpact({ style: ImpactStyle.Light }); + hapticImpact(this.el, { style: ImpactStyle.Light }); /** * Clear focus from any active element to prevent scroll jumps diff --git a/core/src/components/reorder-group/reorder-group.tsx b/core/src/components/reorder-group/reorder-group.tsx index 918beebd99a..2b1d8cd8e86 100644 --- a/core/src/components/reorder-group/reorder-group.tsx +++ b/core/src/components/reorder-group/reorder-group.tsx @@ -187,7 +187,7 @@ export class ReorderGroup implements ComponentInterface { item.classList.add(ITEM_REORDER_SELECTED); - hapticSelectionStart(); + hapticSelectionStart(this.el); this.ionReorderStart.emit(); } @@ -212,7 +212,7 @@ export class ReorderGroup implements ComponentInterface { const fromIndex = indexForItem(selectedItem); this.lastToIndex = toIndex; - hapticSelectionChanged(); + hapticSelectionChanged(this.el); this.reorderMove(fromIndex, toIndex); } @@ -247,7 +247,7 @@ export class ReorderGroup implements ComponentInterface { }); } - hapticSelectionEnd(); + hapticSelectionEnd(this.el); this.ionReorderEnd.emit({ from: fromIndex, diff --git a/core/src/components/reorder-group/test/reorder-group-events.e2e.ts b/core/src/components/reorder-group/test/reorder-group-events.e2e.ts index d3324e7dbdf..7c9cca76431 100644 --- a/core/src/components/reorder-group/test/reorder-group-events.e2e.ts +++ b/core/src/components/reorder-group/test/reorder-group-events.e2e.ts @@ -285,5 +285,44 @@ configs({ modes: ['ios'], directions: ['ltr'] }).forEach(({ title, config }) => await expect(ionItemReorder).toHaveReceivedEventDetail({ from: 0, to: 2, complete: undefined }); }); }); + + test.describe('ionHaptic', () => { + test('should emit from the reorder group for each haptic during a reorder', async ({ page }) => { + await page.setContent( + ` + + + Item 1 + + + + Item 2 + + + + Item 3 + + + + `, + config + ); + + const reorderGroup = page.locator('ion-reorder-group'); + const ionHaptic = await page.spyOnEvent('ionHaptic'); + + await dragElementBy(reorderGroup.locator('ion-reorder').first(), page, 0, 300); + await page.waitForChanges(); + + const targets = ionHaptic.events.map((ev) => (ev.target as HTMLElement).tagName.toLowerCase()); + const types = ionHaptic.events.map((ev) => ev.detail.type); + + expect(targets.length).toBeGreaterThan(0); + expect(targets.every((target) => target === 'ion-reorder-group')).toBe(true); + expect(types[0]).toBe('selectionStart'); + expect(types).toContain('selectionChanged'); + expect(types[types.length - 1]).toBe('selectionEnd'); + }); + }); }); }); diff --git a/core/src/components/toggle/test/toggle.spec.ts b/core/src/components/toggle/test/toggle.spec.ts index 47f4717b684..cf3d87db4d1 100644 --- a/core/src/components/toggle/test/toggle.spec.ts +++ b/core/src/components/toggle/test/toggle.spec.ts @@ -119,11 +119,14 @@ describe('ion-toggle: haptics', () => { config.reset({}); }); - const clickToggleOnIOS = async () => { + const clickToggleOnIOS = async (onHaptic?: (ev: CustomEvent) => void) => { const page = await newSpecPage({ components: [Toggle], html: `Toggle`, }); + if (onHaptic) { + page.doc.addEventListener('ionHaptic', onHaptic as EventListener); + } const win = page.win as any; win.Ionic = { ...win.Ionic, platforms: ['ios'] }; // newSpecPage resets the window, so stub Capacitor after it @@ -150,4 +153,20 @@ describe('ion-toggle: haptics', () => { expect(impact).not.toHaveBeenCalled(); }); + + it('should emit ionHaptic from the toggle when clicked', async () => { + const events: CustomEvent[] = []; + + await clickToggleOnIOS((ev) => events.push(ev)); + + expect(events.length).toBe(1); + expect((events[0].target as HTMLElement).tagName.toLowerCase()).toBe('ion-toggle'); + expect(events[0].detail).toEqual({ type: 'selection' }); + }); + + it('should not trigger haptics when ionHaptic is canceled', async () => { + await clickToggleOnIOS((ev) => ev.preventDefault()); + + expect(impact).not.toHaveBeenCalled(); + }); }); diff --git a/core/src/components/toggle/toggle.tsx b/core/src/components/toggle/toggle.tsx index 041a200bead..27ddbe31c9b 100644 --- a/core/src/components/toggle/toggle.tsx +++ b/core/src/components/toggle/toggle.tsx @@ -294,7 +294,7 @@ export class Toggle implements ComponentInterface { private onMove(detail: GestureDetail) { if (shouldToggle(isRTL(this.el), this.checked, detail.deltaX, -10)) { this.toggleChecked(); - hapticSelection(); + hapticSelection(this.el); } } @@ -338,7 +338,7 @@ export class Toggle implements ComponentInterface { if (this.lastDrag + 300 < Date.now()) { this.toggleChecked(); - enableHaptics && hapticSelection(); + enableHaptics && hapticSelection(this.el); } }; diff --git a/core/src/interface.d.ts b/core/src/interface.d.ts index cfcdb68f828..b50cf412061 100644 --- a/core/src/interface.d.ts +++ b/core/src/interface.d.ts @@ -44,6 +44,7 @@ export { TextareaCustomEvent } from './components/textarea/textarea-interface'; export { ToastOptions } from './components/toast/toast-interface'; export { ToggleCustomEvent } from './components/toggle/toggle-interface'; export { BackButtonEvent, BackButtonEventDetail } from './utils/hardware-back-button'; +export { HapticEvent, HapticEventDetail } from './utils/native/haptic'; // Types from utils export { diff --git a/core/src/utils/config.ts b/core/src/utils/config.ts index 39cd5e2d62e..9b772f11887 100644 --- a/core/src/utils/config.ts +++ b/core/src/utils/config.ts @@ -194,6 +194,7 @@ export interface IonicConfig { * Relevant Components: ion-action-sheet, ion-alert, ion-picker-column, ion-refresher, ion-reorder-group, ion-toggle * Haptics are the small vibrations a device plays as touch feedback, such as when a toggle is switched. * If `false`, Ionic components won't trigger haptics. Defaults to `true`. + * Components still dispatch `ionHaptic`, so apps can play their own. */ haptics?: boolean; diff --git a/core/src/utils/gesture/button-active.ts b/core/src/utils/gesture/button-active.ts index 0ee930752dc..000430f1f3a 100644 --- a/core/src/utils/gesture/button-active.ts +++ b/core/src/utils/gesture/button-active.ts @@ -5,11 +5,18 @@ import { hapticSelectionChanged, hapticSelectionEnd, hapticSelectionStart } from import type { Gesture } from './index'; import { createGesture } from './index'; -export const createButtonActiveGesture = (el: HTMLElement, isButton: (refEl: HTMLElement) => boolean): Gesture => { +/** + * @param hostEl The component that owns the buttons. `ionHaptic` is dispatched from it. + */ +export const createButtonActiveGesture = ( + hostEl: HTMLElement, + el: HTMLElement, + isButton: (refEl: HTMLElement) => boolean +): Gesture => { let currentTouchedButton: HTMLElement | undefined; let initialTouchedButton: HTMLElement | undefined; - const activateButtonAtPoint = (x: number, y: number, hapticFeedbackFn: () => void) => { + const activateButtonAtPoint = (x: number, y: number, hapticFeedbackFn: (el: HTMLElement) => void) => { if (typeof (document as any) === 'undefined') { return; } @@ -25,7 +32,7 @@ export const createButtonActiveGesture = (el: HTMLElement, isButton: (refEl: HTM } }; - const setActiveButton = (button: HTMLElement, hapticFeedbackFn: () => void) => { + const setActiveButton = (button: HTMLElement, hapticFeedbackFn: (el: HTMLElement) => void) => { currentTouchedButton = button; if (!initialTouchedButton) { @@ -34,7 +41,7 @@ export const createButtonActiveGesture = (el: HTMLElement, isButton: (refEl: HTM const buttonToModify = currentTouchedButton; writeTask(() => buttonToModify.classList.add('ion-activated')); - hapticFeedbackFn(); + hapticFeedbackFn(hostEl); }; const clearActiveButton = (dispatchClick = false) => { @@ -68,7 +75,7 @@ export const createButtonActiveGesture = (el: HTMLElement, isButton: (refEl: HTM onMove: (ev) => activateButtonAtPoint(ev.currentX, ev.currentY, hapticSelectionChanged), onEnd: () => { clearActiveButton(true); - hapticSelectionEnd(); + hapticSelectionEnd(hostEl); initialTouchedButton = undefined; }, }); diff --git a/core/src/utils/native/haptic.ts b/core/src/utils/native/haptic.ts index 06d2d9fce14..fe583c5f437 100644 --- a/core/src/utils/native/haptic.ts +++ b/core/src/utils/native/haptic.ts @@ -144,48 +144,85 @@ export const hapticAvailable = (): boolean => { return HapticEngine.available(); }; +/** + * Detail of the `ionHaptic` event. `type` matches the Capacitor Haptics + * method Ionic would call, except `selection`, which Ionic plays as a + * light impact. The haptic engine only plays `selectionChanged` after a + * `selectionStart`, so canceling `selectionStart` also silences the rest + * of that gesture. + */ +export type HapticEventDetail = + | { type: 'selection' | 'selectionStart' | 'selectionChanged' | 'selectionEnd' } + | { type: 'impact'; style: 'HEAVY' | 'MEDIUM' | 'LIGHT' } + | { type: 'notification'; notificationType: 'SUCCESS' | 'WARNING' | 'ERROR' }; + +export type HapticEvent = CustomEvent; + +/** + * Dispatch `ionHaptic` from the component that triggered the haptic. + * The event fires even when haptics are disabled or no haptic engine is + * available, so apps can play their own haptics. Calling `preventDefault()` + * on the event skips Ionic's built-in haptic. + * + * @returns `true` if the built-in haptic should play. + */ +const emitHaptic = (el: HTMLElement, detail: HapticEventDetail): boolean => { + const ev: HapticEvent = new CustomEvent('ionHaptic', { + bubbles: true, + composed: true, + cancelable: true, + detail, + }); + el.dispatchEvent(ev); + /** + * Read defaultPrevented instead of the dispatchEvent return value: + * Stencil's mock-doc always returns true from dispatchEvent. + */ + return !ev.defaultPrevented && hapticAvailable(); +}; + /** * Trigger a selection changed haptic event. Good for one-time events * (not for gestures) */ -export const hapticSelection = () => { - hapticAvailable() && HapticEngine.selection(); +export const hapticSelection = (el: HTMLElement) => { + emitHaptic(el, { type: 'selection' }) && HapticEngine.selection(); }; /** * Tell the haptic engine that a gesture for a selection change is starting. */ -export const hapticSelectionStart = () => { - hapticAvailable() && HapticEngine.selectionStart(); +export const hapticSelectionStart = (el: HTMLElement) => { + emitHaptic(el, { type: 'selectionStart' }) && HapticEngine.selectionStart(); }; /** * Tell the haptic engine that a selection changed during a gesture. */ -export const hapticSelectionChanged = () => { - hapticAvailable() && HapticEngine.selectionChanged(); +export const hapticSelectionChanged = (el: HTMLElement) => { + emitHaptic(el, { type: 'selectionChanged' }) && HapticEngine.selectionChanged(); }; /** * Tell the haptic engine we are done with a gesture. This needs to be * called lest resources are not properly recycled. */ -export const hapticSelectionEnd = () => { - hapticAvailable() && HapticEngine.selectionEnd(); +export const hapticSelectionEnd = (el: HTMLElement) => { + emitHaptic(el, { type: 'selectionEnd' }) && HapticEngine.selectionEnd(); }; /** * Use this to indicate success/failure/warning to the user. * options should be of the type `{ type: NotificationType.SUCCESS }` (or `WARNING`/`ERROR`) */ -export const hapticNotification = (options: HapticNotificationOptions) => { - hapticAvailable() && HapticEngine.notification(options); +export const hapticNotification = (el: HTMLElement, options: HapticNotificationOptions) => { + emitHaptic(el, { type: 'notification', notificationType: options.type }) && HapticEngine.notification(options); }; /** * Use this to indicate success/failure/warning to the user. * options should be of the type `{ style: ImpactStyle.LIGHT }` (or `MEDIUM`/`HEAVY`) */ -export const hapticImpact = (options: HapticImpactOptions) => { - hapticAvailable() && HapticEngine.impact(options); +export const hapticImpact = (el: HTMLElement, options: HapticImpactOptions) => { + emitHaptic(el, { type: 'impact', style: options.style }) && HapticEngine.impact(options); }; diff --git a/core/src/utils/native/test/haptic.spec.ts b/core/src/utils/native/test/haptic.spec.ts index a90d0abcb16..e10fa9ec720 100644 --- a/core/src/utils/native/test/haptic.spec.ts +++ b/core/src/utils/native/test/haptic.spec.ts @@ -13,6 +13,7 @@ import { } from '../haptic'; describe('haptics', () => { + let el: HTMLElement; let haptics: { impact: jest.Mock; notification: jest.Mock; @@ -22,6 +23,8 @@ describe('haptics', () => { }; beforeEach(() => { + el = document.createElement('ion-toggle'); + document.body.appendChild(el); haptics = { impact: jest.fn(), notification: jest.fn(), @@ -38,12 +41,13 @@ describe('haptics', () => { }); afterEach(() => { + el.remove(); delete (window as any).Capacitor; config.reset({}); }); it('should trigger haptics by default', () => { - hapticSelection(); + hapticSelection(el); expect(haptics.impact).toHaveBeenCalledTimes(1); }); @@ -51,12 +55,12 @@ describe('haptics', () => { it('should not trigger haptics when the haptics config is false', () => { config.reset({ haptics: false }); - hapticSelection(); - hapticSelectionStart(); - hapticSelectionChanged(); - hapticSelectionEnd(); - hapticImpact({ style: ImpactStyle.Heavy }); - hapticNotification({ type: NotificationType.Success }); + hapticSelection(el); + hapticSelectionStart(el); + hapticSelectionChanged(el); + hapticSelectionEnd(el); + hapticImpact(el, { style: ImpactStyle.Heavy }); + hapticNotification(el, { type: NotificationType.Success }); Object.values(haptics).forEach((method) => expect(method).not.toHaveBeenCalled()); }); @@ -76,8 +80,71 @@ describe('haptics', () => { it('should trigger haptics when the haptics config is true', () => { config.reset({ haptics: true }); - hapticSelection(); + hapticSelection(el); expect(haptics.impact).toHaveBeenCalledTimes(1); }); + + describe('ionHaptic event', () => { + it('should dispatch ionHaptic when the haptics config is false', () => { + config.reset({ haptics: false }); + const listener = jest.fn(); + el.addEventListener('ionHaptic', listener); + + hapticSelection(el); + + expect(listener).toHaveBeenCalledTimes(1); + expect(haptics.impact).not.toHaveBeenCalled(); + }); + + it('should dispatch ionHaptic when no haptic engine is available', () => { + delete (window as any).Capacitor; + const listener = jest.fn(); + el.addEventListener('ionHaptic', listener); + + hapticSelection(el); + + expect(listener).toHaveBeenCalledTimes(1); + }); + + it('should describe the haptic in the event detail', () => { + const details: unknown[] = []; + el.addEventListener('ionHaptic', (ev) => details.push((ev as CustomEvent).detail)); + + hapticSelectionStart(el); + hapticSelectionChanged(el); + hapticSelectionEnd(el); + hapticImpact(el, { style: ImpactStyle.Heavy }); + hapticNotification(el, { type: NotificationType.Warning }); + + expect(details).toEqual([ + { type: 'selectionStart' }, + { type: 'selectionChanged' }, + { type: 'selectionEnd' }, + { type: 'impact', style: 'HEAVY' }, + { type: 'notification', notificationType: 'WARNING' }, + ]); + }); + + it('should skip the built-in haptic when the event is canceled', () => { + el.addEventListener('ionHaptic', (ev) => ev.preventDefault()); + + hapticSelection(el); + + expect(haptics.impact).not.toHaveBeenCalled(); + }); + + it('should dispatch a bubbling ionHaptic event from the source element', () => { + const listener = jest.fn(); + document.addEventListener('ionHaptic', listener); + + hapticSelection(el); + + document.removeEventListener('ionHaptic', listener); + expect(listener).toHaveBeenCalledTimes(1); + const ev = listener.mock.calls[0][0] as CustomEvent; + expect(ev.target).toBe(el); + expect(ev.detail).toEqual({ type: 'selection' }); + }); + }); }); diff --git a/packages/angular/src/lazy/index.ts b/packages/angular/src/lazy/index.ts index 21147495792..6e535b8b395 100644 --- a/packages/angular/src/lazy/index.ts +++ b/packages/angular/src/lazy/index.ts @@ -87,6 +87,8 @@ export { CheckboxChangeEventDetail, DatetimeCustomEvent, DatetimeChangeEventDetail, + HapticEvent, + HapticEventDetail, InfiniteScrollCustomEvent, InputCustomEvent, InputChangeEventDetail, diff --git a/packages/angular/src/standalone/index.ts b/packages/angular/src/standalone/index.ts index c0da0e63345..726afcad9c8 100644 --- a/packages/angular/src/standalone/index.ts +++ b/packages/angular/src/standalone/index.ts @@ -80,6 +80,8 @@ export type { CheckboxChangeEventDetail, DatetimeCustomEvent, DatetimeChangeEventDetail, + HapticEvent, + HapticEventDetail, InfiniteScrollCustomEvent, InputCustomEvent, InputChangeEventDetail, diff --git a/packages/react/src/components/index.ts b/packages/react/src/components/index.ts index 14bea3905f0..9cbbc8ccca1 100644 --- a/packages/react/src/components/index.ts +++ b/packages/react/src/components/index.ts @@ -40,6 +40,8 @@ export { CheckboxChangeEventDetail, DatetimeCustomEvent, DatetimeChangeEventDetail, + HapticEvent, + HapticEventDetail, InfiniteScrollCustomEvent, InputCustomEvent, InputChangeEventDetail, diff --git a/packages/vue/src/index.ts b/packages/vue/src/index.ts index 388a1cbc12f..a693c61c780 100644 --- a/packages/vue/src/index.ts +++ b/packages/vue/src/index.ts @@ -76,6 +76,8 @@ export { CheckboxChangeEventDetail, DatetimeCustomEvent, DatetimeChangeEventDetail, + HapticEvent, + HapticEventDetail, InfiniteScrollCustomEvent, InputCustomEvent, InputChangeEventDetail, From ca63331279817f0a69e244762463f402b8283c30 Mon Sep 17 00:00:00 2001 From: ShaneK Date: Mon, 23 Nov 2026 12:00:59 -0800 Subject: [PATCH 4/5] fix(haptics): always release the haptic engine when selectionEnd is canceled --- .../test/basic/action-sheet.e2e.ts | 5 ---- .../test/reorder-group-events.e2e.ts | 2 +- .../src/components/toggle/test/toggle.spec.ts | 6 ----- core/src/utils/config.ts | 2 +- core/src/utils/native/haptic.ts | 12 ++++----- core/src/utils/native/test/haptic.spec.ts | 27 +++++++++++++++++++ 6 files changed, 34 insertions(+), 20 deletions(-) diff --git a/core/src/components/action-sheet/test/basic/action-sheet.e2e.ts b/core/src/components/action-sheet/test/basic/action-sheet.e2e.ts index a039de48842..7f1bca265fd 100644 --- a/core/src/components/action-sheet/test/basic/action-sheet.e2e.ts +++ b/core/src/components/action-sheet/test/basic/action-sheet.e2e.ts @@ -218,12 +218,7 @@ configs({ modes: ['ios'], directions: ['ltr'] }).forEach(({ config, title }) => await page.mouse.up(); }); }); -}); -/** - * The button press haptic only runs in iOS mode - */ -configs({ modes: ['ios'], directions: ['ltr'] }).forEach(({ config, title }) => { test.describe(title('action sheet: haptics'), () => { test('should emit ionHaptic from the action sheet when a button is pressed', async ({ page }) => { await page.goto('/src/components/action-sheet/test/basic', config); diff --git a/core/src/components/reorder-group/test/reorder-group-events.e2e.ts b/core/src/components/reorder-group/test/reorder-group-events.e2e.ts index 7c9cca76431..9f38db7089d 100644 --- a/core/src/components/reorder-group/test/reorder-group-events.e2e.ts +++ b/core/src/components/reorder-group/test/reorder-group-events.e2e.ts @@ -318,7 +318,7 @@ configs({ modes: ['ios'], directions: ['ltr'] }).forEach(({ title, config }) => const types = ionHaptic.events.map((ev) => ev.detail.type); expect(targets.length).toBeGreaterThan(0); - expect(targets.every((target) => target === 'ion-reorder-group')).toBe(true); + expect(new Set(targets)).toEqual(new Set(['ion-reorder-group'])); expect(types[0]).toBe('selectionStart'); expect(types).toContain('selectionChanged'); expect(types[types.length - 1]).toBe('selectionEnd'); diff --git a/core/src/components/toggle/test/toggle.spec.ts b/core/src/components/toggle/test/toggle.spec.ts index cf3d87db4d1..e7717ecb3d6 100644 --- a/core/src/components/toggle/test/toggle.spec.ts +++ b/core/src/components/toggle/test/toggle.spec.ts @@ -163,10 +163,4 @@ describe('ion-toggle: haptics', () => { expect((events[0].target as HTMLElement).tagName.toLowerCase()).toBe('ion-toggle'); expect(events[0].detail).toEqual({ type: 'selection' }); }); - - it('should not trigger haptics when ionHaptic is canceled', async () => { - await clickToggleOnIOS((ev) => ev.preventDefault()); - - expect(impact).not.toHaveBeenCalled(); - }); }); diff --git a/core/src/utils/config.ts b/core/src/utils/config.ts index 9b772f11887..6d1053e7d16 100644 --- a/core/src/utils/config.ts +++ b/core/src/utils/config.ts @@ -194,7 +194,7 @@ export interface IonicConfig { * Relevant Components: ion-action-sheet, ion-alert, ion-picker-column, ion-refresher, ion-reorder-group, ion-toggle * Haptics are the small vibrations a device plays as touch feedback, such as when a toggle is switched. * If `false`, Ionic components won't trigger haptics. Defaults to `true`. - * Components still dispatch `ionHaptic`, so apps can play their own. + * Components still dispatch `ionHaptic` wherever they would trigger one, so apps can play their own. */ haptics?: boolean; diff --git a/core/src/utils/native/haptic.ts b/core/src/utils/native/haptic.ts index fe583c5f437..3c2950ecabb 100644 --- a/core/src/utils/native/haptic.ts +++ b/core/src/utils/native/haptic.ts @@ -149,7 +149,8 @@ export const hapticAvailable = (): boolean => { * method Ionic would call, except `selection`, which Ionic plays as a * light impact. The haptic engine only plays `selectionChanged` after a * `selectionStart`, so canceling `selectionStart` also silences the rest - * of that gesture. + * of that gesture. `selectionEnd` plays nothing and only releases the + * engine, so canceling it has no effect. */ export type HapticEventDetail = | { type: 'selection' | 'selectionStart' | 'selectionChanged' | 'selectionEnd' } @@ -159,11 +160,7 @@ export type HapticEventDetail = export type HapticEvent = CustomEvent; /** - * Dispatch `ionHaptic` from the component that triggered the haptic. - * The event fires even when haptics are disabled or no haptic engine is - * available, so apps can play their own haptics. Calling `preventDefault()` - * on the event skips Ionic's built-in haptic. - * + * Dispatch `ionHaptic` from `el`. * @returns `true` if the built-in haptic should play. */ const emitHaptic = (el: HTMLElement, detail: HapticEventDetail): boolean => { @@ -208,7 +205,8 @@ export const hapticSelectionChanged = (el: HTMLElement) => { * called lest resources are not properly recycled. */ export const hapticSelectionEnd = (el: HTMLElement) => { - emitHaptic(el, { type: 'selectionEnd' }) && HapticEngine.selectionEnd(); + emitHaptic(el, { type: 'selectionEnd' }); + hapticAvailable() && HapticEngine.selectionEnd(); }; /** diff --git a/core/src/utils/native/test/haptic.spec.ts b/core/src/utils/native/test/haptic.spec.ts index e10fa9ec720..62bb0883080 100644 --- a/core/src/utils/native/test/haptic.spec.ts +++ b/core/src/utils/native/test/haptic.spec.ts @@ -134,6 +134,33 @@ describe('haptics', () => { expect(haptics.impact).not.toHaveBeenCalled(); }); + it('should skip only selectionStart when only selectionStart is canceled', () => { + const types: string[] = []; + el.addEventListener('ionHaptic', (ev) => { + const { type } = (ev as CustomEvent).detail; + types.push(type); + if (type === 'selectionStart') { + ev.preventDefault(); + } + }); + + hapticSelectionStart(el); + hapticSelectionChanged(el); + hapticSelectionEnd(el); + + expect(types).toEqual(['selectionStart', 'selectionChanged', 'selectionEnd']); + expect(haptics.selectionStart).not.toHaveBeenCalled(); + expect(haptics.selectionChanged).toHaveBeenCalledTimes(1); + }); + + it('should still release the haptic engine when selectionEnd is canceled', () => { + el.addEventListener('ionHaptic', (ev) => ev.preventDefault()); + + hapticSelectionEnd(el); + + expect(haptics.selectionEnd).toHaveBeenCalledTimes(1); + }); + it('should dispatch a bubbling ionHaptic event from the source element', () => { const listener = jest.fn(); document.addEventListener('ionHaptic', listener); From 798b456957394faf7d451855a6256607710c565f Mon Sep 17 00:00:00 2001 From: ShaneK Date: Mon, 23 Nov 2026 12:52:13 -0800 Subject: [PATCH 5/5] fix(haptics): move public haptic types out of haptic.ts so the published types don't need @capacitor/haptics --- core/src/interface.d.ts | 2 +- core/src/utils/native/haptic-interface.ts | 20 ++++++++++++++++++++ core/src/utils/native/haptic.ts | 16 +--------------- 3 files changed, 22 insertions(+), 16 deletions(-) create mode 100644 core/src/utils/native/haptic-interface.ts diff --git a/core/src/interface.d.ts b/core/src/interface.d.ts index b50cf412061..2ce2193e922 100644 --- a/core/src/interface.d.ts +++ b/core/src/interface.d.ts @@ -44,7 +44,7 @@ export { TextareaCustomEvent } from './components/textarea/textarea-interface'; export { ToastOptions } from './components/toast/toast-interface'; export { ToggleCustomEvent } from './components/toggle/toggle-interface'; export { BackButtonEvent, BackButtonEventDetail } from './utils/hardware-back-button'; -export { HapticEvent, HapticEventDetail } from './utils/native/haptic'; +export { HapticEvent, HapticEventDetail } from './utils/native/haptic-interface'; // Types from utils export { diff --git a/core/src/utils/native/haptic-interface.ts b/core/src/utils/native/haptic-interface.ts new file mode 100644 index 00000000000..72e602af77a --- /dev/null +++ b/core/src/utils/native/haptic-interface.ts @@ -0,0 +1,20 @@ +/** + * These types are public, so they live apart from haptic.ts. Its + * `@capacitor/haptics` import would otherwise end up in the published + * types, and apps without that package would fail to compile. + */ + +/** + * Detail of the `ionHaptic` event. `type` matches the Capacitor Haptics + * method Ionic would call, except `selection`, which Ionic plays as a + * light impact. The haptic engine only plays `selectionChanged` after a + * `selectionStart`, so canceling `selectionStart` also silences the rest + * of that gesture. `selectionEnd` plays nothing and only releases the + * engine, so canceling it has no effect. + */ +export type HapticEventDetail = + | { type: 'selection' | 'selectionStart' | 'selectionChanged' | 'selectionEnd' } + | { type: 'impact'; style: 'HEAVY' | 'MEDIUM' | 'LIGHT' } + | { type: 'notification'; notificationType: 'SUCCESS' | 'WARNING' | 'ERROR' }; + +export type HapticEvent = CustomEvent; diff --git a/core/src/utils/native/haptic.ts b/core/src/utils/native/haptic.ts index 3c2950ecabb..cd180698552 100644 --- a/core/src/utils/native/haptic.ts +++ b/core/src/utils/native/haptic.ts @@ -6,6 +6,7 @@ import type { import { config } from '@global/config'; import { getCapacitor } from './capacitor'; +import type { HapticEvent, HapticEventDetail } from './haptic-interface'; export enum ImpactStyle { /** @@ -144,21 +145,6 @@ export const hapticAvailable = (): boolean => { return HapticEngine.available(); }; -/** - * Detail of the `ionHaptic` event. `type` matches the Capacitor Haptics - * method Ionic would call, except `selection`, which Ionic plays as a - * light impact. The haptic engine only plays `selectionChanged` after a - * `selectionStart`, so canceling `selectionStart` also silences the rest - * of that gesture. `selectionEnd` plays nothing and only releases the - * engine, so canceling it has no effect. - */ -export type HapticEventDetail = - | { type: 'selection' | 'selectionStart' | 'selectionChanged' | 'selectionEnd' } - | { type: 'impact'; style: 'HEAVY' | 'MEDIUM' | 'LIGHT' } - | { type: 'notification'; notificationType: 'SUCCESS' | 'WARNING' | 'ERROR' }; - -export type HapticEvent = CustomEvent; - /** * Dispatch `ionHaptic` from `el`. * @returns `true` if the built-in haptic should play.