File tree Expand file tree Collapse file tree 3 files changed +27
-3
lines changed Expand file tree Collapse file tree 3 files changed +27
-3
lines changed Original file line number Diff line number Diff line change 1+ import { setMode } from '@stencil/core' ;
12import { newSpecPage } from '@stencil/core/testing' ;
23
34import { config } from '../../../global/config' ;
@@ -53,6 +54,21 @@ describe('toggle', () => {
5354 expect ( toggle ) . toHaveShadowPart ( 'handle' ) ;
5455 } ) ;
5556 } ) ;
57+
58+ describe ( 'iOS haptic feedback' , ( ) => {
59+ it ( 'should render additional elements when enabled in config' , async ( ) => {
60+ setMode ( ( ) => 'ios' ) ;
61+ const t = await newToggle ( ) ;
62+ expect ( t [ 'hapticEl' ] ) . toBeNull ( ) ;
63+
64+ config . reset ( {
65+ toggleIOSHapticFeedback : true ,
66+ } ) ;
67+
68+ const t2 = await newToggle ( ) ;
69+ expect ( t2 [ 'hapticEl' ] ) . not . toBeNull ( ) ;
70+ } ) ;
71+ } ) ;
5672} ) ;
5773
5874describe ( 'ion-toggle: disabled' , ( ) => {
Original file line number Diff line number Diff line change @@ -34,7 +34,8 @@ export class Toggle implements ComponentInterface {
3434 private inputId = `ion-tg-${ toggleIds ++ } ` ;
3535 private gesture ?: Gesture ;
3636 private focusEl ?: HTMLElement ;
37- private hapticEl ?: HTMLElement ;
37+ private enableIOSHapticFeedback = config . getBoolean ( 'toggleIOSHapticFeedback' , false ) ;
38+ private hapticEl ?: HTMLElement | null = null ;
3839 private lastDrag = 0 ;
3940 private inheritedAttributes : Attributes = { } ;
4041 private toggleTrack ?: HTMLElement ;
@@ -139,7 +140,7 @@ export class Toggle implements ComponentInterface {
139140 const isNowChecked = ! checked ;
140141 this . checked = isNowChecked ;
141142
142- if ( this . hapticEl ) {
143+ if ( this . enableIOSHapticFeedback && this . hapticEl ) {
143144 this . hapticEl . click ( ) ;
144145 }
145146
@@ -300,7 +301,7 @@ export class Toggle implements ComponentInterface {
300301 const { inputId } = this ;
301302 const mode = getIonMode ( this ) ;
302303
303- if ( hapticAvailable ( ) || mode !== 'ios' ) {
304+ if ( ! this . enableIOSHapticFeedback || hapticAvailable ( ) || mode !== 'ios' ) {
304305 return ;
305306 }
306307
Original file line number Diff line number Diff line change @@ -71,6 +71,13 @@ export interface IonicConfig {
7171 */
7272 toggleOnOffLabels ?: boolean ;
7373
74+ /**
75+ * Whenever changing the toggle state should produce a haptic feedback (slight vibration)
76+ * on iOS in browser and PWA environments. Requires iOS 18 or later.
77+ * Defaults to `false`.
78+ */
79+ toggleIOSHapticFeedback ?: boolean ;
80+
7481 /**
7582 * Overrides the default spinner for all `ion-loading` overlays, ie. the ones
7683 * created with `ion-loading-controller`.
You can’t perform that action at this time.
0 commit comments