-
Notifications
You must be signed in to change notification settings - Fork 13.5k
feat(toggle): add iOS 18 haptic feedback #29945
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
Merged
thetaPC
merged 8 commits into
ionic-team:feature-8.6
from
jedlikowski:29942-ios-18-toggle-haptic-feedback
Apr 4, 2025
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b29e007
feat(toggle): add ios 18 haptic feedback
jedlikowski 1822dc3
feat(toggle): make iOS haptic feedback configurable
jedlikowski 156578b
Merge branch 'main' into 29942-ios-18-toggle-haptic-feedback
jedlikowski 88de124
Merge branch 'feature-8.6' of github.com:ionic-team/ionic-framework i…
thetaPC 0c3eb4d
Merge branch 'feature-8.6' of github.com:ionic-team/ionic-framework i…
thetaPC cc1d892
refactor(toggle): use hapticSelection on onClick
thetaPC 5c32966
chore(toggle): revert changes
thetaPC c7e2f65
Update core/src/components/toggle/toggle.tsx
thetaPC File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'; | ||
|
@@ -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; | ||
|
@@ -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, | ||
|
@@ -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') { | ||
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 !== ''; | ||
} | ||
|
@@ -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), | ||
|
@@ -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}> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
{/* | ||
The native control must be rendered | ||
before the visible label text due to https://bugs.webkit.org/show_bug.cgi?id=251951 | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.