Files
react-native/Libraries/Components/Touchable/TouchableBounce.js
T
Diego Pasquali 7b05b091fd Integrated iOS-only accessibilityLanguage prop (#33090)
Summary:
This PR fixes https://github.com/facebook/react-native/issues/30891

This PR is going to add an `accessibilityLanguage` prop to all the available components. This props is currently working only on iOS and should follow the [guidelines of the relative configuration](https://developer.apple.com/documentation/objectivec/nsobject/1615192-accessibilitylanguage).

I'm in no way an expert on native programming (especially Objective-C) so I'm open to changes / improvements 🙂

## Changelog

<!-- Help reviewers and the release process by writing your own changelog entry. For an example, see:
https://github.com/facebook/react-native/wiki/Changelog
-->

[iOS] [Added] - Integrated the `accessibilityLanguage` prop to all the available components. The prop is available for any platform but it will work only on iOS.

Pull Request resolved: https://github.com/facebook/react-native/pull/33090

Test Plan:
This has been tested using both the Simulator, checking for the `Language` attribute, and using a physical device with the Voice Over enabled.

<img width="1083" alt="Screenshot 2022-02-11 at 13 17 32" src="https://user-images.githubusercontent.com/5963683/153590415-65fcb4ff-8f31-4a0f-90e5-8eb1aae6aa3d.png">

Reviewed By: philIip

Differential Revision: D34523608

Pulled By: rh389

fbshipit-source-id: b5d77fc0b3d76ea8ed8f30c8385459ba98122ff6
2022-03-07 09:43:30 -08:00

180 lines
5.6 KiB
JavaScript

/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @format
*/
import Pressability, {
type PressabilityConfig,
} from '../../Pressability/Pressability';
import {PressabilityDebugView} from '../../Pressability/PressabilityDebug';
import type {ViewStyleProp} from '../../StyleSheet/StyleSheet';
import typeof TouchableWithoutFeedback from './TouchableWithoutFeedback';
import {Animated, Platform} from 'react-native';
import * as React from 'react';
type Props = $ReadOnly<{|
...React.ElementConfig<TouchableWithoutFeedback>,
onPressAnimationComplete?: ?() => void,
onPressWithCompletion?: ?(callback: () => void) => void,
releaseBounciness?: ?number,
releaseVelocity?: ?number,
style?: ?ViewStyleProp,
hostRef: React.Ref<typeof Animated.View>,
|}>;
type State = $ReadOnly<{|
pressability: Pressability,
scale: Animated.Value,
|}>;
class TouchableBounce extends React.Component<Props, State> {
state: State = {
pressability: new Pressability(this._createPressabilityConfig()),
scale: new Animated.Value(1),
};
_createPressabilityConfig(): PressabilityConfig {
return {
cancelable: !this.props.rejectResponderTermination,
disabled: this.props.disabled,
hitSlop: this.props.hitSlop,
delayLongPress: this.props.delayLongPress,
delayPressIn: this.props.delayPressIn,
delayPressOut: this.props.delayPressOut,
minPressDuration: 0,
pressRectOffset: this.props.pressRetentionOffset,
android_disableSound: this.props.touchSoundDisabled,
onBlur: event => {
if (Platform.isTV) {
this._bounceTo(1, 0.4, 0);
}
if (this.props.onBlur != null) {
this.props.onBlur(event);
}
},
onFocus: event => {
if (Platform.isTV) {
this._bounceTo(0.93, 0.1, 0);
}
if (this.props.onFocus != null) {
this.props.onFocus(event);
}
},
onLongPress: this.props.onLongPress,
onPress: event => {
const {onPressAnimationComplete, onPressWithCompletion} = this.props;
const releaseBounciness = this.props.releaseBounciness ?? 10;
const releaseVelocity = this.props.releaseVelocity ?? 10;
if (onPressWithCompletion != null) {
onPressWithCompletion(() => {
this.state.scale.setValue(0.93);
this._bounceTo(
1,
releaseVelocity,
releaseBounciness,
onPressAnimationComplete,
);
});
return;
}
this._bounceTo(
1,
releaseVelocity,
releaseBounciness,
onPressAnimationComplete,
);
if (this.props.onPress != null) {
this.props.onPress(event);
}
},
onPressIn: event => {
this._bounceTo(0.93, 0.1, 0);
if (this.props.onPressIn != null) {
this.props.onPressIn(event);
}
},
onPressOut: event => {
this._bounceTo(1, 0.4, 0);
if (this.props.onPressOut != null) {
this.props.onPressOut(event);
}
},
};
}
_bounceTo(
toValue: number,
velocity: number,
bounciness: number,
callback?: ?() => void,
) {
Animated.spring(this.state.scale, {
toValue,
velocity,
bounciness,
useNativeDriver: true,
}).start(callback);
}
render(): React.Node {
// BACKWARD-COMPATIBILITY: Focus and blur events were never supported before
// adopting `Pressability`, so preserve that behavior.
const {onBlur, onFocus, ...eventHandlersWithoutBlurAndFocus} =
this.state.pressability.getEventHandlers();
return (
<Animated.View
style={[{transform: [{scale: this.state.scale}]}, this.props.style]}
accessible={this.props.accessible !== false}
accessibilityLabel={this.props.accessibilityLabel}
accessibilityHint={this.props.accessibilityHint}
accessibilityLanguage={this.props.accessibilityLanguage}
accessibilityRole={this.props.accessibilityRole}
accessibilityState={this.props.accessibilityState}
accessibilityActions={this.props.accessibilityActions}
onAccessibilityAction={this.props.onAccessibilityAction}
accessibilityValue={this.props.accessibilityValue}
importantForAccessibility={this.props.importantForAccessibility}
accessibilityLiveRegion={this.props.accessibilityLiveRegion}
accessibilityViewIsModal={this.props.accessibilityViewIsModal}
accessibilityElementsHidden={this.props.accessibilityElementsHidden}
nativeID={this.props.nativeID}
testID={this.props.testID}
hitSlop={this.props.hitSlop}
focusable={
this.props.focusable !== false &&
this.props.onPress !== undefined &&
!this.props.disabled
}
ref={this.props.hostRef}
{...eventHandlersWithoutBlurAndFocus}>
{this.props.children}
{__DEV__ ? (
<PressabilityDebugView color="orange" hitSlop={this.props.hitSlop} />
) : null}
</Animated.View>
);
}
componentDidUpdate(prevProps: Props, prevState: State) {
this.state.pressability.configure(this._createPressabilityConfig());
}
componentWillUnmount(): void {
this.state.pressability.reset();
}
}
module.exports = (React.forwardRef((props, hostRef) => (
<TouchableBounce {...props} hostRef={hostRef} />
)): React.AbstractComponent<$ReadOnly<$Diff<Props, {|hostRef: mixed|}>>>);