Skip to content

Commit

Permalink
Trigger polite/assertive announcement with hint/label
Browse files Browse the repository at this point in the history
Add prop accessibilityLiveRegion to react-native iOS API
Trigger polite/assertive announcement if label/hint changesdd prop accessibilityLiveRegion to react-native iOS API
  • Loading branch information
fabOnReact committed Nov 22, 2022
1 parent 972a40a commit 53f7b76
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -292,24 +292,33 @@ - (void)updateProps:(Props::Shared const &)props oldProps:(Props::Shared const &

// `accessibilityLabel`
if (oldViewProps.accessibilityLabel != newViewProps.accessibilityLabel) {
self.accessibilityElement.accessibilityLabel = RCTNSStringFromStringNilIfEmpty(newViewProps.accessibilityLabel);
}

if (!newViewProps.accessibilityLabel.empty() && oldViewProps.accessibilityLabel != newViewProps.accessibilityLabel && newViewProps.accessibilityLiveRegion != AccessibilityLiveRegion::None) {
if ([self.nativeId isEqualToString:@"1"]) {
if (@available(iOS 11.0, *)) {
NSMutableDictionary<NSString *, NSNumber *> *attrsDictionary = [NSMutableDictionary new];
// Step 3 - add parameter accessibilityLiveRegion. Copy/Paste android settings.
// - retrieve parameter
// - trigger check if parameter is polite or assertive
NSLog(@"TESTING newViewProps.accessibilityLiveRegions is %d", newViewProps.accessibilityLiveRegion);
attrsDictionary[UIAccessibilitySpeechAttributeQueueAnnouncement] = @(newViewProps.accessibilityLiveRegion == AccessibilityLiveRegion::Polite ? YES : NO);
NSAttributedString *announcementWithAttrs = [[NSAttributedString alloc] initWithString:@"announcement"
NSAttributedString *announcementWithAttrs = [[NSAttributedString alloc] initWithString: RCTNSStringFromStringNilIfEmpty(newViewProps.accessibilityLabel)
attributes:attrsDictionary];
UIAccessibilityPostNotification(UIAccessibilityAnnouncementNotification, announcementWithAttrs);
}
// Step 4 - instead of triggering the announcement, mark an instance variable to trigger announcement
// - trigger only 1 time announcement once all props are set
}
self.accessibilityElement.accessibilityLabel = RCTNSStringFromStringNilIfEmpty(newViewProps.accessibilityLabel);
}


if (!newViewProps.accessibilityHint.empty() && oldViewProps.accessibilityHint != newViewProps.accessibilityHint && newViewProps.accessibilityLiveRegion != AccessibilityLiveRegion::None) {
if ([self.nativeId isEqualToString:@"1"]) {
if (@available(iOS 11.0, *)) {
NSMutableDictionary<NSString *, NSNumber *> *attrsDictionary = [NSMutableDictionary new];
attrsDictionary[UIAccessibilitySpeechAttributeQueueAnnouncement] = @(YES);
NSAttributedString *announcementWithAttrs = [[NSAttributedString alloc] initWithString: RCTNSStringFromStringNilIfEmpty(newViewProps.accessibilityHint)
attributes:attrsDictionary];
UIAccessibilityPostNotification(UIAccessibilityAnnouncementNotification, announcementWithAttrs);
}
}
}

// `accessibilityLanguage`
if (oldViewProps.accessibilityLanguage != newViewProps.accessibilityLanguage) {
self.accessibilityElement.accessibilityLanguage =
Expand Down

0 comments on commit 53f7b76

Please sign in to comment.