Skip to content
Merged
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
23 changes: 23 additions & 0 deletions Libraries/Pressability/Pressability.js
Original file line number Diff line number Diff line change
Expand Up @@ -628,12 +628,35 @@ export default class Pressability {
if (onKeyDown != null) {
onKeyDown(event);
}
// Pressables on macOS should respond to the enter/return and spacebar keys.
// The keyDown event triggers a press event as well as the pressIn effect mimicking a native control behavior.
if (
(event.nativeEvent.key === 'Enter' ||
event.nativeEvent.key === ' ') &&
event.defaultPrevented !== true
) {
const {onPress, onPressIn} = this._config;
// $FlowFixMe: PressEvents don't mesh with keyboarding APIs. Keep legacy behavior of passing KeyEvents instead
onPressIn && onPressIn(event);
// $FlowFixMe: PressEvents don't mesh with keyboarding APIs. Keep legacy behavior of passing KeyEvents instead
onPress && onPress(event);
}
},
onKeyUp: (event: KeyEvent): void => {
const {onKeyUp} = this._config;
if (onKeyUp != null) {
onKeyUp(event);
}
// The keyUp event triggers the pressOut effect.
if (
(event.nativeEvent.key === 'Enter' ||
event.nativeEvent.key === ' ') &&
event.defaultPrevented !== true
) {
const {onPressOut} = this._config;
// $FlowFixMe: PressEvents don't mesh with keyboarding APIs. Keep legacy behavior of passing KeyEvents instead
onPressOut && onPressOut(event);
}
},
};
// ]TODO(macOS GH#774)
Expand Down
6 changes: 6 additions & 0 deletions React/Views/RCTView.m
Original file line number Diff line number Diff line change
Expand Up @@ -1632,6 +1632,12 @@ - (RCTViewKeyboardEvent*)keyboardEvent:(NSEvent*)event {
NSArray<NSString *> *validKeys = keyDown ? self.validKeysDown : self.validKeysUp;
NSString *key = [RCTViewKeyboardEvent keyFromEvent:event];

// If the view is focusable and the component didn't explicity set the validKeysDown or Up,
// allow enter/return and spacebar key events to mimic the behavior of native controls.
if (self.focusable && validKeys == nil) {
validKeys = @[@"Enter", @" "];
}

// Only post events for keys we care about
if (![validKeys containsObject:key]) {
return nil;
Expand Down