diff --git a/src/components/Switch.js b/src/components/Switch.js
index 608471763e29..3b7f464b00e5 100644
--- a/src/components/Switch.js
+++ b/src/components/Switch.js
@@ -1,9 +1,9 @@
-import React, {Component} from 'react';
-import {Animated} from 'react-native';
import PropTypes from 'prop-types';
-import styles from '../styles/styles';
-import * as Pressables from './Pressable';
+import React, {useEffect, useRef} from 'react';
+import {Animated} from 'react-native';
import CONST from '../CONST';
+import styles from '../styles/styles';
+import PressableWithFeedback from './Pressable/PressableWithFeedback';
const propTypes = {
/** Whether the switch is toggled to the on position */
@@ -16,61 +16,41 @@ const propTypes = {
accessibilityLabel: PropTypes.string.isRequired,
};
-const PressableWithFeedback = Pressables.PressableWithFeedback;
-class Switch extends Component {
- constructor(props) {
- super(props);
- this.offPosition = 0;
- this.onPosition = 20;
- this.offsetX = new Animated.Value(props.isOn ? this.onPosition : this.offPosition);
-
- this.toggleSwitch = this.toggleSwitch.bind(this);
- this.toggleAction = this.toggleAction.bind(this);
- }
-
- componentDidUpdate(prevProps) {
- if (prevProps.isOn === this.props.isOn) {
- return;
- }
+const OFFSET_X = {
+ OFF: 0,
+ ON: 20,
+};
- this.toggleSwitch();
- }
+function Switch(props) {
+ const offsetX = useRef(new Animated.Value(props.isOn ? OFFSET_X.ON : OFFSET_X.OFF));
- // animates the switch to the on or off position
- toggleSwitch() {
- Animated.timing(this.offsetX, {
- toValue: this.props.isOn ? this.onPosition : this.offPosition,
+ useEffect(() => {
+ Animated.timing(offsetX.current, {
+ toValue: props.isOn ? OFFSET_X.ON : OFFSET_X.OFF,
duration: 300,
useNativeDriver: true,
}).start();
- }
-
- // executes the callback passed in as a prop
- toggleAction() {
- this.props.onToggle(!this.props.isOn);
- }
-
- render() {
- const switchTransform = {transform: [{translateX: this.offsetX}]};
- return (
-
-
-
- );
- }
+ }, [props.isOn]);
+
+ return (
+ props.onToggle(!props.isOn)}
+ onLongPress={() => props.onToggle(!props.isOn)}
+ accessibilityRole={CONST.ACCESSIBILITY_ROLE.SWITCH}
+ accessibilityState={{checked: props.isOn}}
+ aria-checked={props.isOn}
+ accessibilityLabel={props.accessibilityLabel}
+ // disable hover dim for switch
+ hoverDimmingValue={1}
+ pressDimmingValue={0.8}
+ >
+
+
+ );
}
Switch.propTypes = propTypes;
+Switch.displayName = 'Switch';
export default Switch;
diff --git a/src/styles/styles.js b/src/styles/styles.js
index 0fe77205e9ad..ce2ac50fd349 100644
--- a/src/styles/styles.js
+++ b/src/styles/styles.js
@@ -2504,6 +2504,10 @@ const styles = {
backgroundColor: themeColors.appBG,
},
+ switchThumbTransformation: (translateX) => ({
+ transform: [{translateX}],
+ }),
+
radioButtonContainer: {
backgroundColor: themeColors.componentBG,
borderRadius: 10,