Repository navigation
Allow Switch to use default UISwitch accessibility annoucements - #50796
Closed
joevilches wants to merge 1 commit into
Closed
joevilches wants to merge 1 commit into
joevilches wants to merge 1 commit into
Conversation
Contributor
|
This pull request was exported from Phabricator. Differential Revision: D73226500 |
joevilches
force-pushed
the
export-D73226500
branch
from
April 18, 2025 20:01
8cf92e8 to
2f6aeb7
Compare
…t#50796) Summary: Pull Request resolved: react#50796 When we render a `Switch` today we are never actually focusing on the native `UISwitch` that gets rendered. We instead focus on the parent `RCTViewComponentView` which houses the `UISwitch` as a `contentView`. That is because of [this logic](https://www.internalfb.com/code/fbsource/[b7e1547dab5a]/xplat/js/react-native-github/packages/react-native/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.mm?lines=1211-1213). This blocks the accessibility of any descendants, including the `contentView`. What we lose here is the announcement of "on" and "off" based on the toggle state that comes built into `UISwitch`. To do this today you need to add `accessibilityState={{checked: ...}}` which is very unintuitive. Android DOES announce "on" and "off". We can fix this with an override on the switch class. Changelog: [iOS][Fixed] - Fix "on" and "off" announcements on `Switch` Reviewed By: realsoelynn Differential Revision: D73226500
Contributor
|
This pull request was exported from Phabricator. Differential Revision: D73226500 |
joevilches
force-pushed
the
export-D73226500
branch
from
April 18, 2025 20:04
2f6aeb7 to
407b96c
Compare
Contributor
|
This pull request has been merged in db6e000. |
Collaborator
|
This pull request was successfully merged by @joevilches in db6e000 When will my fix make it into a release? | How to file a pick request? |
This was referenced Mar 24, 2026
meta-codesync Bot
pushed a commit
that referenced
this pull request
Oct 26, 2026
Summary: On iOS, `<Switch disabled />` is drawn greyed out and ignores taps, but VoiceOver and XCUITest still report it as enabled: the `UIAccessibilityTraitNotEnabled` trait is missing. Since #50796, the inner `UISwitch` is the Switch's accessibility element, so `RCTViewComponentView` writes the traits it derives from the role onto the `UISwitch`, replacing the ones UIKit sets, including `NotEnabled`. Since #57516, `accessibilityState.disabled` adds `NotEnabled` to those traits, but the iOS branch of `Switch.js` never sets it from `disabled`. The Android branch already does. This moves the existing Android logic above the platform check so iOS gets the same `accessibilityState`. As on Android, `disabled` wins over `accessibilityState.disabled` when both are set. It is done in `Switch.js` rather than `RCTSwitchComponentView` because the Android branch already does it here, and a Switch-specific native writer for the traits would bring back the second trait writer that #57516 removed. `disabled` now behaves exactly like `accessibilityState={{disabled: true}}` already does. That includes a Switch whose role maps to no trait, such as `accessibilityRole="none"`: it gets `NotEnabled` alone, without the switch trait. ## Changelog: [IOS] [FIXED] - Report a disabled `Switch` as disabled to VoiceOver and XCUITest Pull Request resolved: #59333 Test Plan: Local regression validation: ``` js1 test "(CosmoCollapsibleSelector|CosmoConsentSection|CosmoReportBugForm|TWUISwitch|TWUIListCell|OCDevModeToggleRow|CosmoGeneralAppPreferencesSection|CosmoImportWizard|CosmoSettingsRow|TWAUGroupedMenuListCell|CosmoStudioSettingsBox|TWUIPaymentListCell|TWUIPeopleListCell)-test\.js$" --maxWorkers=4 # 13 suites passed; 66 tests passed; 30 snapshots passed; exit 0 arc lint -a # No lint issues ``` The same targeted run before the snapshot update failed all 13 suites (16 snapshot failures). Updated only the expected disabled accessibility metadata in 16 snapshots. Removed one obsolete `TWUIPeopleListCell renders testSupportingText 2` entry: the helper invokes one snapshot assertion per exported test, so that second entry has no live producer; both retained snapshots are unchanged. Independent review found no blockers. Added two cases to `Switch-itest.js`: `disabled` overriding `accessibilityState.disabled`, and no `accessibilityState` when `disabled` is not set. Fantom renders the Android branch, so these cover the logic both platforms now share; the iOS side is covered by the manual check below. Each new case fails when its part of the logic is changed. ``` yarn fantom packages/react-native/Libraries/Components/Switch/__tests__/Switch-itest.js # 6 passed yarn flow-check # Found 0 errors yarn eslint --max-warnings 0 packages/react-native/Libraries/Components/Switch/Switch.js packages/react-native/Libraries/Components/Switch/__tests__/Switch-itest.js # no warnings yarn prettier --check packages/react-native/Libraries/Components/Switch/Switch.js packages/react-native/Libraries/Components/Switch/__tests__/Switch-itest.js # All matched files use Prettier code style! ``` Fantom was built locally on macOS with Xcode 27, whose clang needed `-Wno-deprecated-literal-operator` for the bundled `nlohmann/json.hpp`. Manual check: a new 0.88.0 app (prebuilt core; `Switch.js` is the same on 0.88.0 and `main`) on an iPhone 17 Pro simulator, iOS 27. For "After", this branch's `Switch.js` was copied into the app's `node_modules/react-native/Libraries/Components/Switch/`. The accessibility tree was read with [AXe](https://github.com/cameroncooke/AXe) `describe-ui`, which reports each element's `enabled` attribute. ```tsx <Switch testID="disabled-mount" value disabled /> <Switch testID="disabled-live" value disabled={liveDisabled} /> <Switch testID="disabled-state" value disabled accessibilityState={{disabled: true}} /> ``` | Switch | Before | After | | --- | --- | --- | | `disabled-mount` | enabled | not enabled | | `disabled-live`, after `disabled` is set to `true` | enabled | not enabled | | `disabled-live`, after `disabled` is set back to `false` | enabled | enabled | | `disabled-state` | not enabled | not enabled | The role (`switch`) and value (`1`) are unchanged. Not run: VoiceOver on a device. Reviewed By: rubennorte Differential Revision: D127141579 Pulled By: javache fbshipit-source-id: c68bc8353788bd850cc2e00a7e69a8a0cb10bc92
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary:
When we render a
Switchtoday we are never actually focusing on the nativeUISwitchthat gets rendered. We instead focus on the parentRCTViewComponentViewwhich houses theUISwitchas acontentView. That is because of this logic. This blocks the accessibility of any descendants, including thecontentView.What we lose here is the announcement of "on" and "off" based on the toggle state that comes built into
UISwitch. To do this today you need to addaccessibilityState={{checked: ...}}which is very unintuitive. Android DOES announce "on" and "off".We can fix this with an override on the switch class.
Changelog: [iOS][Fixed] - Fix "on" and "off" announcements on
SwitchDifferential Revision: D73226500