Skip to content

Commit 407b96c

Browse files
joevilchesfacebook-github-bot
authored andcommitted
Allow Switch to use default UISwitch accessibility annoucements (#50796)
Summary: Pull Request resolved: #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
1 parent 5fd5188 commit 407b96c

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

packages/react-native/React/Fabric/Mounting/ComponentViews/Switch/RCTSwitchComponentView.mm

+12
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,18 @@ - (void)onChange:(UISwitch *)sender
101101
.onChange(SwitchEventEmitter::OnChange{.value = static_cast<bool>(sender.on)});
102102
}
103103

104+
// UISwitch is the accessibility element not this view. If this is YES we block
105+
// accessibility on the switch itself
106+
- (BOOL)isAccessibilityElement
107+
{
108+
return NO;
109+
}
110+
111+
- (NSObject *)accessibilityElement
112+
{
113+
return _switchView;
114+
}
115+
104116
#pragma mark - Native Commands
105117

106118
- (void)handleCommand:(const NSString *)commandName args:(const NSArray *)args

0 commit comments

Comments
 (0)