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
5 changes: 5 additions & 0 deletions .changeset/fix-macos-accessibility-text-input.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'react-native-macos': patch
---

Fix macOS Fabric accessibility roles and states, keep read-only TextInputs focusable and selectable, honor disabled accessibility state, and give each Fabric TextInput a single Tab stop.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ @implementation RCTUITextView {
NSArray<NSString *> *_acceptDragAndDropTypes;
#if TARGET_OS_OSX // [macOS
BOOL _enableFocusRing;
BOOL _enabled;
NSArray<NSPasteboardType> *_readablePasteboardTypes;
#endif // macOS]
}
Expand Down Expand Up @@ -66,6 +67,7 @@ - (instancetype)initWithFrame:(CGRect)frame
self.canDrawSubviewsIntoLayer = YES;
self.allowsUndo = YES;
_enableFocusRing = YES;
_enabled = YES;
#endif // macOS]

_textInputDelegateAdapter = [[RCTBackedTextViewDelegateAdapter alloc] initWithTextView:self];
Expand Down Expand Up @@ -280,6 +282,38 @@ - (BOOL)enableFocusRing
{
return _enableFocusRing;
}

- (BOOL)isEnabled
{
return _enabled;
}

- (void)setEnabled:(BOOL)enabled
{
if (_enabled != enabled) {
_enabled = enabled;

// NSTextView has no enabled state. Behave like a disabled NSTextField: report AXEnabled = NO, refuse first
// responder (see -acceptsFirstResponder) and end editing if the view is focused.
self.accessibilityEnabled = enabled;
if (!enabled && self.window.firstResponder == self) {
[self.window makeFirstResponder:nil];
}
}
}

- (BOOL)acceptsFirstResponder
{
return _enabled && [super acceptsFirstResponder];
}

- (void)setAccessibilityState:(NSDictionary<NSString *, id> *)accessibilityState
{
// Paper routes `accessibilityState` to the backed text view through -[RCTBaseTextInputView reactAccessibilityElement].
[super setAccessibilityState:accessibilityState];
id disabled = accessibilityState[@"disabled"];
self.enabled = !([disabled isKindOfClass:[NSNumber class]] && [disabled boolValue]);
}
#endif // macOS]

- (void)setDefaultTextAttributes:(NSDictionary<NSAttributedStringKey, id> *)defaultTextAttributes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ - (NSResponder *)responder
return _forwardingTextView;
}

- (BOOL)canBecomeKeyView
{
// The forwarding text view is the Tab stop; this wrapper would otherwise add a second one.
return NO;
}

- (BOOL)acceptsFirstResponder
{
return _forwardingTextView.acceptsFirstResponder;
Expand All @@ -123,6 +129,16 @@ - (BOOL)resignFirstResponder
return [_forwardingTextView resignFirstResponder];
}

- (BOOL)isEnabled
{
return _forwardingTextView.isEnabled;
}

- (void)setEnabled:(BOOL)enabled
{
_forwardingTextView.enabled = enabled;
}

#pragma mark -
#pragma mark Text Input delegate forwarding

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic, assign) BOOL textWasPasted;
@property (nonatomic, readonly) NSResponder *responder;
@property (nonatomic, assign) BOOL enableFocusRing;
// Driven by `accessibilityState.disabled`; `editable` only controls whether the text can be changed.
@property (nonatomic, assign, getter=isEnabled) BOOL enabled;
#endif // macOS]
@property (nonatomic, assign, readonly) BOOL dictationRecognizing;
@property (nonatomic, assign) UIEdgeInsets textContainerInset;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,18 +391,42 @@ - (void)_updatePlaceholder

- (BOOL)isEditable
{
#if !TARGET_OS_OSX // [macOS]
return self.isEnabled;
#else // [macOS
return [super isEditable];
#endif // macOS]
}

- (void)setEditable:(BOOL)editable
{
#if TARGET_OS_OSX // [macOS
// on macos the super must be called otherwise its NSTextFieldCell editable property doesn't get set.
#if !TARGET_OS_OSX // [macOS]
self.enabled = editable;
#else // [macOS
// On macOS read-only text remains selectable and focusable, matching desktop behavior rather than iOS's disabled
// field. `accessibilityState.disabled` disables it; the super call sets NSTextFieldCell's editable state.
[super setEditable:editable];
self.selectable = YES;
#endif // macOS]
self.enabled = editable;
}

#if TARGET_OS_OSX // [macOS
- (BOOL)acceptsFirstResponder
{
// NSTextField refuses first responder when it is not editable, which would make read-only fields unreachable with
// Tab. Accept it while the field is enabled and selectable; disabled fields are skipped.
return self.isEnabled && self.isSelectable;
}

- (void)setAccessibilityState:(NSDictionary<NSString *, id> *)accessibilityState
{
// Paper routes `accessibilityState` to the backed text field through -[RCTBaseTextInputView reactAccessibilityElement].
[super setAccessibilityState:accessibilityState];
id disabled = accessibilityState[@"disabled"];
self.enabled = !([disabled isKindOfClass:[NSNumber class]] && [disabled boolValue]);
}
#endif // macOS]

#if !TARGET_OS_OSX // [macOS]
- (void)setSecureTextEntry:(BOOL)secureTextEntry
{
Expand Down
3 changes: 2 additions & 1 deletion packages/react-native/React/Base/RCTConvert.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1664,6 +1664,7 @@ + (NSString*)accessibilityRoleFromTrait:(NSString*)trait
@"tab": NSAccessibilityButtonRole, // no exact match on macOS
@"tablist": NSAccessibilityTabGroupRole,
@"text": NSAccessibilityStaticTextRole, // also a legacy iOS accessibilityTraits
@"textbox": NSAccessibilityTextFieldRole,
@"timer": NSAccessibilityStaticTextRole, // no exact match on macOS
@"toolbar": NSAccessibilityToolbarRole,
// Roles/traits that are macOS specific and are used by some of the core components (Lists):
Expand All @@ -1689,7 +1690,7 @@ + (NSString *)accessibilityRoleFromTraits:(id)json useAriaMappings:(BOOL)useAria
return useAriaMappings ? [RCTConvert accessibilityRoleFromAriaRole:json] : [RCTConvert accessibilityRoleFromTrait:json];
} else if ([json isKindOfClass:[NSArray class]]) {
for (NSString *trait in json) {
NSString *accessibilityRole = useAriaMappings ? [RCTConvert accessibilityRoleFromAriaRole:json] : [RCTConvert accessibilityRoleFromTrait:trait];
NSString *accessibilityRole = useAriaMappings ? [RCTConvert accessibilityRoleFromAriaRole:trait] : [RCTConvert accessibilityRoleFromTrait:trait];
if (![accessibilityRole isEqualToString:NSAccessibilityUnknownRole]) {
return accessibilityRole;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,20 @@ - (void)reactUpdateResponderOffsetForScrollView:(RCTScrollViewComponentView *)sc

#pragma mark - RCTViewComponentView overrides

#if TARGET_OS_OSX // [macOS
// The backed text input owns focus and accessibility; keep this host out of the key view loop to avoid a second Tab
// stop. `focus()` targets the backed view.
- (BOOL)canBecomeKeyView
{
return NO;
}

- (BOOL)acceptsFirstResponder
{
return NO;
}
#endif // macOS]

- (NSObject *)accessibilityElement
{
return _backedTextInputView;
Expand Down Expand Up @@ -305,6 +319,13 @@ - (void)updateProps:(const Props::Shared &)props oldProps:(const Props::Shared &
_backedTextInputView.editable = newTextInputProps.traits.editable;
}

#if TARGET_OS_OSX // [macOS
// `accessibilityState.disabled` disables the backed text input; `editable={false}` only makes it read-only.
if (newTextInputProps.accessibilityState != oldTextInputProps.accessibilityState) {
_backedTextInputView.enabled = !newTextInputProps.accessibilityState.value_or(AccessibilityState{}).disabled;
}
#endif // macOS]

#if !TARGET_OS_OSX // [macOS]
if (newTextInputProps.multiline &&
newTextInputProps.traits.dataDetectorTypes != oldTextInputProps.traits.dataDetectorTypes) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,12 @@ void RCTCopyBackedTextInput(
toTextInput.accessibilityHelp = fromTextInput.accessibilityHelp;
toTextInput.accessibilityIdentifier = fromTextInput.accessibilityIdentifier;
toTextInput.accessibilityLabel = fromTextInput.accessibilityLabel;
toTextInput.accessibilityRole = fromTextInput.accessibilityRole;
// An unset role reads back as NSAccessibilityUnknownRole; copying it would hide the new view's default role.
if (![fromTextInput.accessibilityRole isEqualToString:NSAccessibilityUnknownRole]) {
toTextInput.accessibilityRole = fromTextInput.accessibilityRole;
}
toTextInput.autoresizingMask = fromTextInput.autoresizingMask;
toTextInput.enabled = fromTextInput.enabled;
#endif // macOS]
#if TARGET_OS_IOS // [macOS] [visionOS]
toTextInput.inputAccessoryView = fromTextInput.inputAccessoryView;
Expand Down
Loading
Loading