Skip to content
Open
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
10 changes: 6 additions & 4 deletions packages/main/cypress/specs/Switch.cy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ describe("General accesibility attributes", () => {
.ui5SwitchCheckAttributeInShadowDomRoot("role", "switch");

cy.get("@switch")
.ui5SwitchCheckAttributeInShadowDomRoot("aria-label", `${geoLocationDescr} No`);
.ui5SwitchCheckAttributeInShadowDomRoot("aria-label", geoLocationDescr);

});

Expand All @@ -127,7 +127,7 @@ describe("General accesibility attributes", () => {
.ui5SwitchCheckAttributeInShadowDomRoot("role", "switch");

cy.get("@switch")
.ui5SwitchCheckAttributeInShadowDomRoot("aria-label", `${gpsLocationDescr} No`);
.ui5SwitchCheckAttributeInShadowDomRoot("aria-label", gpsLocationDescr);
});

it("Should set correct correct tooltip on the root", () => {
Expand All @@ -137,11 +137,13 @@ describe("General accesibility attributes", () => {
.ui5SwitchCheckAttributeInShadowDomRoot("title", gpsLocationDescr);
});

it("Should set correct attribute 'aria-label' when 'text-on' and 'text-off' attributes aren't set", () => {
it("Should not have 'aria-label' attribute when 'text-on' and 'text-off' attributes aren't set", () => {
cy.mount(<Switch></Switch>);

cy.get("[ui5-switch]")
.ui5SwitchCheckAttributeInShadowDomRoot("aria-label", "");
.shadow()
.find(".ui5-switch-root")
.should("not.have.attr", "aria-label");
});

it("Should propagate 'required' attribute properly on the root", () => {
Expand Down
34 changes: 20 additions & 14 deletions packages/main/src/Switch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ import "@ui5/webcomponents-icons/dist/accept.js";
import "@ui5/webcomponents-icons/dist/decline.js";
import "@ui5/webcomponents-icons/dist/less.js";
import SwitchDesign from "./types/SwitchDesign.js";
import { FORM_CHECKABLE_REQUIRED } from "./generated/i18n/i18n-defaults.js";
import {
FORM_CHECKABLE_REQUIRED,
SWITCH_ON,
SWITCH_OFF,
} from "./generated/i18n/i18n-defaults.js";

// Template
import SwitchTemplate from "./SwitchTemplate.js";
Expand Down Expand Up @@ -299,6 +303,20 @@ class Switch extends UI5Element implements IFormInputElement {
return this.graphical ? "" : this.textOff;
}

/**
* Determines if custom on/off texts duplicate the default role announcement.
* When textOn/textOff match the localized "On"/"Off" strings (case-insensitive),
* they duplicate what role="switch" with aria-checked already announces,
* so they should be aria-hidden to avoid duplicate screen reader announcements.
*/
get _textAriaHidden(): boolean | undefined {
const on = this.textOn?.toLowerCase();
const off = this.textOff?.toLowerCase();
const i18nOn = Switch.i18nBundle.getText(SWITCH_ON).toLowerCase();
const i18nOff = Switch.i18nBundle.getText(SWITCH_OFF).toLowerCase();
return (on === i18nOn && off === i18nOff) || undefined;
}

get effectiveTabIndex() {
return this.disabled ? undefined : 0;
}
Expand All @@ -307,20 +325,8 @@ class Switch extends UI5Element implements IFormInputElement {
return this.disabled ? "true" : undefined;
}

get accessibilityOnText() {
return this._textOn;
}

get accessibilityOffText() {
return this._textOff;
}

get hiddenText() {
return this.checked ? this.accessibilityOnText : this.accessibilityOffText;
}

get ariaLabelText() {
return [getEffectiveAriaLabelText(this) || getAssociatedLabelForTexts(this), this.hiddenText].join(" ").trim();
return getEffectiveAriaLabelText(this) || getAssociatedLabelForTexts(this) || undefined;
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/main/src/SwitchTemplate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ export default function SwitchTemplate(this: Switch) {
</>
:
<>
<span class="ui5-switch-text ui5-switch-text--on" part="text-on">{this._textOn}</span>
<span class="ui5-switch-text ui5-switch-text--off" part="text-off">{this._textOff}</span>
<span class="ui5-switch-text ui5-switch-text--on" part="text-on" aria-hidden={this._textAriaHidden}>{this._textOn}</span>
<span class="ui5-switch-text ui5-switch-text--off" part="text-off" aria-hidden={this._textAriaHidden}>{this._textOff}</span>
</>
}
</>
Expand Down
6 changes: 6 additions & 0 deletions packages/main/src/i18n/messagebundle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -1005,6 +1005,12 @@ DYNAMIC_DATE_RANGE_NEXT_COMBINED_TEXT=Next X {0} (included)
#XFLD: Suffix text for included date range options.
DYNAMIC_DATE_RANGE_INCLUDED_TEXT=(included)

#XACT: Default text for switch "on" state, used to detect duplicate screen reader announcements
SWITCH_ON=On

#XACT: Default text for switch "off" state, used to detect duplicate screen reader announcements
SWITCH_OFF=Off

#XACT: ARIA announcement for icon type image
ICON_ARIA_TYPE_IMAGE=Image

Expand Down
Loading