Skip to content

Commit cc1d892

Browse files
committed
refactor(toggle): use hapticSelection on onClick
1 parent 0c3eb4d commit cc1d892

File tree

1 file changed

+13
-36
lines changed

1 file changed

+13
-36
lines changed

core/src/components/toggle/toggle.tsx

Lines changed: 13 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import type { ComponentInterface, EventEmitter } from '@stencil/core';
22
import { Component, Element, Event, Host, Prop, State, Watch, h } from '@stencil/core';
33
import { renderHiddenInput, inheritAriaAttributes } from '@utils/helpers';
44
import type { Attributes } from '@utils/helpers';
5-
import { hapticAvailable, hapticSelection } from '@utils/native/haptic';
5+
import { hapticSelection } from '@utils/native/haptic';
6+
import { isPlatform } from '@utils/platform';
67
import { isRTL } from '@utils/rtl';
78
import { createColorClasses, hostContext } from '@utils/theme';
89
import { checkmarkOutline, removeOutline, ellipseOutline } from 'ionicons/icons';
@@ -39,8 +40,6 @@ export class Toggle implements ComponentInterface {
3940
private errorTextId = `${this.inputId}-error-text`;
4041
private gesture?: Gesture;
4142
private focusEl?: HTMLElement;
42-
private enableIOSHapticFeedback = config.getBoolean('toggleIOSHapticFeedback', false);
43-
private hapticEl?: HTMLElement | null = null;
4443
private lastDrag = 0;
4544
private inheritedAttributes: Attributes = {};
4645
private toggleTrack?: HTMLElement;
@@ -162,10 +161,6 @@ export class Toggle implements ComponentInterface {
162161
const isNowChecked = !checked;
163162
this.checked = isNowChecked;
164163

165-
if (this.enableIOSHapticFeedback && this.hapticEl) {
166-
this.hapticEl.click();
167-
}
168-
169164
this.setFocus();
170165
this.ionChange.emit({
171166
checked: isNowChecked,
@@ -253,6 +248,14 @@ export class Toggle implements ComponentInterface {
253248
}
254249

255250
private onClick = (ev: MouseEvent) => {
251+
/**
252+
* The haptics for the toggle is
253+
* an iOS-only feature when tapped.
254+
* As a result, it should be
255+
* disabled on Android.
256+
*/
257+
const enableHaptics = isPlatform('ios');
258+
256259
if (this.disabled) {
257260
return;
258261
}
@@ -261,6 +264,7 @@ export class Toggle implements ComponentInterface {
261264

262265
if (this.lastDrag + 300 < Date.now()) {
263266
this.toggleChecked();
267+
enableHaptics && hapticSelection();
264268
}
265269
};
266270

@@ -314,33 +318,6 @@ export class Toggle implements ComponentInterface {
314318
);
315319
}
316320

317-
/**
318-
* On Safari (iOS 18+) we can trigger haptic feedback programatically
319-
* by rendering <input type="checkbox" switch> element
320-
* with an associated <label> and triggering click()
321-
* on the <label> element.
322-
*/
323-
private renderFallbackHapticElements() {
324-
const { inputId } = this;
325-
const mode = getIonMode(this);
326-
327-
if (!this.enableIOSHapticFeedback || hapticAvailable() || mode !== 'ios') {
328-
return;
329-
}
330-
331-
return (
332-
<label aria-hidden="true" ref={(hapticEl) => (this.hapticEl = hapticEl)} style={{ display: 'none' }}>
333-
<input
334-
id={inputId + '-haptic'}
335-
type="checkbox"
336-
// @ts-expect-error safari-only custom attrrbute required for haptic feedback
337-
switch
338-
style={{ display: 'none' }}
339-
/>
340-
</label>
341-
);
342-
}
343-
344321
private get hasLabel() {
345322
return this.el.textContent !== '';
346323
}
@@ -400,6 +377,7 @@ export class Toggle implements ComponentInterface {
400377
<Host
401378
aria-describedby={this.getHintTextID()}
402379
aria-invalid={this.getHintTextID() === this.errorTextId}
380+
onClick={this.onClick}
403381
class={createColorClasses(color, {
404382
[mode]: true,
405383
'in-item': hostContext('ion-item', el),
@@ -412,8 +390,7 @@ export class Toggle implements ComponentInterface {
412390
[`toggle-${rtl}`]: true,
413391
})}
414392
>
415-
{this.renderFallbackHapticElements()}
416-
<label class="toggle-wrapper" onClick={this.onClick}>
393+
<label class="toggle-wrapper">
417394
{/*
418395
The native control must be rendered
419396
before the visible label text due to https://bugs.webkit.org/show_bug.cgi?id=251951

0 commit comments

Comments
 (0)