Skip to content

fix(toggle): ensure proper visual selection when navigating via VoiceOver in Safari #30349

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
merged 3 commits into from
Apr 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 5 additions & 3 deletions core/src/components/toggle/toggle.scss
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@

max-width: 100%;

outline: none;

cursor: pointer;
user-select: none;
z-index: $z-index-item-input;
Expand Down Expand Up @@ -69,8 +67,12 @@
pointer-events: none;
}

/**
* The native input must be hidden with display instead of visibility or
* aria-hidden to avoid accessibility issues with nested interactive elements.
*/
input {
@include visually-hidden();
display: none;
}

// Toggle Wrapper
Expand Down
45 changes: 39 additions & 6 deletions core/src/components/toggle/toggle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import type { ToggleChangeEventDetail } from './toggle-interface';
})
export class Toggle implements ComponentInterface {
private inputId = `ion-tg-${toggleIds++}`;
private inputLabelId = `${this.inputId}-lbl`;
private helperTextId = `${this.inputId}-helper-text`;
private errorTextId = `${this.inputId}-error-text`;
private gesture?: Gesture;
Expand Down Expand Up @@ -246,6 +247,15 @@ export class Toggle implements ComponentInterface {
}
}

private onKeyDown = (ev: KeyboardEvent) => {
if (ev.key === ' ') {
ev.preventDefault();
if (!this.disabled) {
this.toggleChecked();
}
}
};

private onClick = (ev: MouseEvent) => {
if (this.disabled) {
return;
Expand Down Expand Up @@ -355,8 +365,23 @@ export class Toggle implements ComponentInterface {
}

render() {
const { activated, color, checked, disabled, el, justify, labelPlacement, inputId, name, alignment, required } =
this;
const {
activated,
alignment,
checked,
color,
disabled,
el,
errorTextId,
hasLabel,
inheritedAttributes,
inputId,
inputLabelId,
justify,
labelPlacement,
name,
required,
} = this;

const mode = getIonMode(this);
const value = this.getValue();
Expand All @@ -365,9 +390,16 @@ export class Toggle implements ComponentInterface {

return (
<Host
role="switch"
aria-checked={`${checked}`}
aria-describedby={this.getHintTextID()}
aria-invalid={this.getHintTextID() === this.errorTextId}
aria-invalid={this.getHintTextID() === errorTextId}
onClick={this.onClick}
aria-labelledby={hasLabel ? inputLabelId : null}
aria-label={inheritedAttributes['aria-label'] || null}
aria-disabled={disabled ? 'true' : null}
tabindex={disabled ? undefined : 0}
onKeyDown={this.onKeyDown}
class={createColorClasses(color, {
[mode]: true,
'in-item': hostContext('ion-item', el),
Expand All @@ -380,7 +412,7 @@ export class Toggle implements ComponentInterface {
[`toggle-${rtl}`]: true,
})}
>
<label class="toggle-wrapper">
<label class="toggle-wrapper" htmlFor={inputId}>
{/*
The native control must be rendered
before the visible label text due to https://bugs.webkit.org/show_bug.cgi?id=251951
Expand All @@ -396,14 +428,15 @@ export class Toggle implements ComponentInterface {
onBlur={() => this.onBlur()}
ref={(focusEl) => (this.focusEl = focusEl)}
required={required}
{...this.inheritedAttributes}
{...inheritedAttributes}
/>
<div
class={{
'label-text-wrapper': true,
'label-text-wrapper-hidden': !this.hasLabel,
'label-text-wrapper-hidden': !hasLabel,
}}
part="label"
id={inputLabelId}
>
<slot></slot>
{this.renderHintText()}
Expand Down
Loading