Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(toggle): add iOS 18 haptic feedback #29945

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 35 additions & 3 deletions core/src/components/toggle/toggle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { ComponentInterface, EventEmitter } from '@stencil/core';
import { Component, Element, Event, Host, Prop, State, Watch, h } from '@stencil/core';
import { renderHiddenInput, inheritAriaAttributes } from '@utils/helpers';
import type { Attributes } from '@utils/helpers';
import { hapticSelection } from '@utils/native/haptic';
import { hapticAvailable, hapticSelection } from '@utils/native/haptic';
import { isRTL } from '@utils/rtl';
import { createColorClasses, hostContext } from '@utils/theme';
import { checkmarkOutline, removeOutline, ellipseOutline } from 'ionicons/icons';
Expand Down Expand Up @@ -34,6 +34,7 @@ export class Toggle implements ComponentInterface {
private inputId = `ion-tg-${toggleIds++}`;
private gesture?: Gesture;
private focusEl?: HTMLElement;
private hapticEl?: HTMLElement;
private lastDrag = 0;
private inheritedAttributes: Attributes = {};
private toggleTrack?: HTMLElement;
Expand Down Expand Up @@ -138,6 +139,10 @@ export class Toggle implements ComponentInterface {
const isNowChecked = !checked;
this.checked = isNowChecked;

if (this.hapticEl) {
this.hapticEl.click();
}

this.ionChange.emit({
checked: isNowChecked,
value,
Expand Down Expand Up @@ -285,6 +290,33 @@ export class Toggle implements ComponentInterface {
);
}

/**
* On Safari (iOS 18+) we can trigger haptic feedback programatically
* by rendering <input type="checkbox" switch> element
* with an associated <label> and triggering click()
* on the <label> element.
*/
private renderFallbackHapticElements() {
const { inputId } = this;
const mode = getIonMode(this);

if (hapticAvailable() || mode !== 'ios') {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand correctly, we don't need these elements if Vibration API is available, because then hapticSelection() will work.

return;
}

return (
<label aria-hidden="true" ref={(hapticEl) => (this.hapticEl = hapticEl)} style={{ display: 'none' }}>
<input
id={inputId + '-haptic'}
type="checkbox"
// @ts-expect-error safari-only custom attrrbute required for haptic feedback
switch
style={{ display: 'none' }}
/>
</label>
);
}

private get hasLabel() {
return this.el.textContent !== '';
}
Expand All @@ -299,7 +331,6 @@ export class Toggle implements ComponentInterface {

return (
<Host
onClick={this.onClick}
class={createColorClasses(color, {
[mode]: true,
'in-item': hostContext('ion-item', el),
Expand All @@ -312,7 +343,8 @@ export class Toggle implements ComponentInterface {
[`toggle-${rtl}`]: true,
})}
>
<label class="toggle-wrapper">
{this.renderFallbackHapticElements()}
<label class="toggle-wrapper" onClick={this.onClick}>
Copy link
Author

@jedlikowski jedlikowski Oct 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

onClick listener moved to label, without this change Host was capturing clicks on the haptic <label> too, not only on .toggle-wrapper

{/*
The native control must be rendered
before the visible label text due to https://bugs.webkit.org/show_bug.cgi?id=251951
Expand Down