Skip to content

Commit

Permalink
feat: Exposes iOS prefersCrossFadeTransitions
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrieldonadel committed Feb 17, 2022
1 parent c1db41f commit be79058
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 0 deletions.
25 changes: 25 additions & 0 deletions Libraries/Components/AccessibilityInfo/AccessibilityInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,31 @@ const AccessibilityInfo = {
});
},

/**
* Query whether reduce motion and prefer cross-fade transitions settings are currently enabled.
*
* Returns a promise which resolves to a boolean.
* The result is `true` when prefer cross-fade transitions is enabled and `false` otherwise.
*
* See https://reactnative.dev/docs/accessibilityinfo#prefersCrossFadeTransitions
*/
prefersCrossFadeTransitions(): Promise<boolean> {
return new Promise((resolve, reject) => {
if (Platform.OS === 'android') {
return Promise.resolve(false);
} else {
if (NativeAccessibilityManagerIOS != null) {
NativeAccessibilityManagerIOS.getCurrentPrefersCrossFadeTransitionsState(
resolve,
reject,
);
} else {
reject(null);
}
}
});
},

/**
* Query whether reduced transparency is currently enabled.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ export interface Spec extends TurboModule {
onSuccess: (isReduceMotionEnabled: boolean) => void,
onError: (error: Object) => void,
) => void;
+getCurrentPrefersCrossFadeTransitionsState: (
onSuccess: (prefersCrossFadeTransitions: boolean) => void,
onError: (error: Object) => void,
) => void;
+getCurrentReduceTransparencyState: (
onSuccess: (isReduceTransparencyEnabled: boolean) => void,
onError: (error: Object) => void,
Expand Down
1 change: 1 addition & 0 deletions React/CoreModules/RCTAccessibilityManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ extern NSString *const RCTAccessibilityManagerDidUpdateMultiplierNotification; /
@property (nonatomic, assign) BOOL isGrayscaleEnabled;
@property (nonatomic, assign) BOOL isInvertColorsEnabled;
@property (nonatomic, assign) BOOL isReduceMotionEnabled;
@property (nonatomic, assign) BOOL prefersCrossFadeTransitions;
@property (nonatomic, assign) BOOL isReduceTransparencyEnabled;
@property (nonatomic, assign) BOOL isVoiceOverEnabled;

Expand Down
28 changes: 28 additions & 0 deletions React/CoreModules/RCTAccessibilityManager.mm
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ - (instancetype)init
name:UIAccessibilityReduceMotionStatusDidChangeNotification
object:nil];

if (@available(iOS 14.0, *)) {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(prefersCrossFadeTransitionsStatusDidChange:)
name:UIAccessibilityPrefersCrossFadeTransitionsStatusDidChangeNotification
object:nil];
}

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(reduceTransparencyStatusDidChange:)
name:UIAccessibilityReduceTransparencyStatusDidChangeNotification
Expand All @@ -91,6 +98,7 @@ - (instancetype)init
_isGrayscaleEnabled = UIAccessibilityIsGrayscaleEnabled();
_isInvertColorsEnabled = UIAccessibilityIsInvertColorsEnabled();
_isReduceMotionEnabled = UIAccessibilityIsReduceMotionEnabled();
_prefersCrossFadeTransitions = UIAccessibilityPrefersCrossFadeTransitions();
_isReduceTransparencyEnabled = UIAccessibilityIsReduceTransparencyEnabled();
_isVoiceOverEnabled = UIAccessibilityIsVoiceOverRunning();
}
Expand Down Expand Up @@ -169,6 +177,19 @@ - (void)reduceMotionStatusDidChange:(__unused NSNotification *)notification
}
}

- (void)prefersCrossFadeTransitionsStatusDidChange:(__unused NSNotification *)notification
{
BOOL newPrefersCrossFadeTransitionsEnabled = UIAccessibilityPrefersCrossFadeTransitions();
if (_prefersCrossFadeTransitions != newPrefersCrossFadeTransitionsEnabled) {
_prefersCrossFadeTransitions = newPrefersCrossFadeTransitionsEnabled;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
[[_moduleRegistry moduleForName:"EventDispatcher"] sendDeviceEventWithName:@"prefersCrossFadeTransitionsChanged"
body:@(_prefersCrossFadeTransitions)];
#pragma clang diagnostic pop
}
}

- (void)reduceTransparencyStatusDidChange:(__unused NSNotification *)notification
{
BOOL newReduceTransparencyEnabled = UIAccessibilityIsReduceTransparencyEnabled();
Expand Down Expand Up @@ -358,6 +379,13 @@ static void setMultipliers(
onSuccess(@[ @(_isReduceMotionEnabled) ]);
}

RCT_EXPORT_METHOD(getCurrentPrefersCrossFadeTransitionsState
: (RCTResponseSenderBlock)onSuccess onError
: (__unused RCTResponseSenderBlock)onError)
{
onSuccess(@[ @(_prefersCrossFadeTransitions) ]);
}

RCT_EXPORT_METHOD(getCurrentReduceTransparencyState
: (RCTResponseSenderBlock)onSuccess onError
: (__unused RCTResponseSenderBlock)onError)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1127,6 +1127,11 @@ class DisplayOptionsStatusExample extends React.Component<{}> {
optionChecker={AccessibilityInfo.isReduceMotionEnabled}
notification={'reduceMotionChanged'}
/>
<DisplayOptionStatusExample
optionName={'Prefer Cross'}
optionChecker={AccessibilityInfo.prefersCrossFadeTransitions}
notification={'prefersCrossFadeTransitionsChanged'}
/>
<DisplayOptionStatusExample
optionName={'Screen Reader'}
optionChecker={AccessibilityInfo.isScreenReaderEnabled}
Expand Down

0 comments on commit be79058

Please sign in to comment.