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
84 changes: 32 additions & 52 deletions src/components/Switch.js
Original file line number Diff line number Diff line change
@@ -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 */
Expand All @@ -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 (
<PressableWithFeedback
style={[styles.switchTrack, !this.props.isOn && styles.switchInactive]}
onPress={this.toggleAction}
onLongPress={this.toggleAction}
accessibilityRole={CONST.ACCESSIBILITY_ROLE.SWITCH}
accessibilityState={{checked: this.props.isOn}}
aria-checked={this.props.isOn}
accessibilityLabel={this.props.accessibilityLabel}
// disable hover dim for switch
hoverDimmingValue={1}
pressDimmingValue={0.8}
>
<Animated.View style={[styles.switchThumb, switchTransform]} />
</PressableWithFeedback>
);
}
}, [props.isOn]);

return (
<PressableWithFeedback
style={[styles.switchTrack, !props.isOn && styles.switchInactive]}
onPress={() => 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}
>
<Animated.View style={[styles.switchThumb, styles.switchThumbTransformation(offsetX.current)]} />
</PressableWithFeedback>
);
}

Switch.propTypes = propTypes;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add displayName

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switch.displayName = 'Switch';

export default Switch;
4 changes: 4 additions & 0 deletions src/styles/styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -2504,6 +2504,10 @@ const styles = {
backgroundColor: themeColors.appBG,
},

switchThumbTransformation: (translateX) => ({
transform: [{translateX}],
}),

radioButtonContainer: {
backgroundColor: themeColors.componentBG,
borderRadius: 10,
Expand Down