Skip to content

Commit aa8593b

Browse files
committed
[change] Switch prop 'trackColor' support
Remove support for legacy React Native props and add support for the 'trackColor' object. Retains support for the web-only equivalents as I think this API is preferable to the one in React Native, both in terms of flexibility and performance (no inline objects). Fix #1382
1 parent 3c9cc66 commit aa8593b

File tree

2 files changed

+19
-39
lines changed

2 files changed

+19
-39
lines changed

packages/docs/src/components/Switch/Switch.stories.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ The color of the thumb grip when the switch is turned on. (Web-only)
2626

2727
### activeTrackColor
2828

29-
The color of the track when the switch is turned on. (Web-only, equivalent to React Native's `onTintColor`)
29+
The color of the track when the switch is turned on. (Web-only)
3030

3131
<Preview withSource='none'>
3232
<Story name="activeTrackColor">
@@ -56,7 +56,7 @@ Invoked with the new value when the value changes.
5656

5757
### thumbColor
5858

59-
The color of the thumb grip when the switch is turned off. (Web-only, equivalent to React Native's `thumbTintColor`)
59+
The color of the thumb grip when the switch is turned off.
6060

6161
<Preview withSource='none'>
6262
<Story name="thumbColor">
@@ -66,7 +66,7 @@ The color of the thumb grip when the switch is turned off. (Web-only, equivalent
6666

6767
### trackColor
6868

69-
The color of the track when the switch is turned off. (Web-only, equivalent to React Native's `tintColor`)
69+
The color of the track when the switch is turned off.
7070

7171
<Preview withSource='none'>
7272
<Story name="trackColor">

packages/react-native-web/src/exports/Switch/index.js

Lines changed: 16 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,8 @@ type SwitchProps = {
2525
disabled?: boolean,
2626
onValueChange?: (e: any) => void,
2727
thumbColor?: ColorValue,
28-
trackColor?: ColorValue,
29-
value?: boolean,
30-
// RN compat
31-
onTintColor?: ColorValue,
32-
thumbTintColor?: ColorValue,
33-
tintColor?: ColorValue
28+
trackColor?: ColorValue | {| false: ColorValue, true: ColorValue |},
29+
value?: boolean
3430
};
3531

3632
const emptyObject = {};
@@ -62,10 +58,6 @@ class Switch extends React.Component<SwitchProps> {
6258
thumbColor = '#FAFAFA',
6359
trackColor = '#939393',
6460
value = false,
65-
// React Native compatibility
66-
onTintColor,
67-
thumbTintColor,
68-
tintColor,
6961
...other
7062
} = this.props;
7163

@@ -74,30 +66,33 @@ class Switch extends React.Component<SwitchProps> {
7466
const minWidth = multiplyStyleLengthValue(height, 2);
7567
const width = styleWidth > minWidth ? styleWidth : minWidth;
7668
const trackBorderRadius = multiplyStyleLengthValue(height, 0.5);
77-
const trackCurrentColor = value ? onTintColor || activeTrackColor : tintColor || trackColor;
78-
const thumbCurrentColor = value ? activeThumbColor : thumbTintColor || thumbColor;
69+
const trackCurrentColor = value
70+
? (trackColor != null && typeof trackColor === 'object' && trackColor.true) ||
71+
activeTrackColor
72+
: (trackColor != null && typeof trackColor === 'object' && trackColor.false) || trackColor;
73+
const thumbCurrentColor = value ? activeThumbColor : thumbColor;
7974
const thumbHeight = height;
8075
const thumbWidth = thumbHeight;
8176

82-
const rootStyle = [styles.root, style, { height, width }, disabled && styles.cursorDefault];
77+
const rootStyle = [styles.root, style, disabled && styles.cursorDefault, { height, width }];
8378

8479
const trackStyle = [
8580
styles.track,
8681
{
87-
backgroundColor: trackCurrentColor,
82+
backgroundColor: disabled ? '#D5D5D5' : trackCurrentColor,
8883
borderRadius: trackBorderRadius
89-
},
90-
disabled && styles.disabledTrack
84+
}
9185
];
9286

9387
const thumbStyle = [
9488
styles.thumb,
89+
value && styles.thumbActive,
9590
{
96-
backgroundColor: thumbCurrentColor,
91+
backgroundColor: disabled ? '#BDBDBD' : thumbCurrentColor,
9792
height: thumbHeight,
93+
marginStart: value ? multiplyStyleLengthValue(thumbWidth, -1) : 0,
9894
width: thumbWidth
99-
},
100-
disabled && styles.disabledThumb
95+
}
10196
];
10297

10398
const nativeControl = createElement('input', {
@@ -115,16 +110,7 @@ class Switch extends React.Component<SwitchProps> {
115110
return (
116111
<View {...other} style={rootStyle}>
117112
<View style={trackStyle} />
118-
<View
119-
ref={this._setThumbRef}
120-
style={[
121-
thumbStyle,
122-
value && styles.thumbOn,
123-
{
124-
marginStart: value ? multiplyStyleLengthValue(thumbWidth, -1) : 0
125-
}
126-
]}
127-
/>
113+
<View ref={this._setThumbRef} style={thumbStyle} />
128114
{nativeControl}
129115
</View>
130116
);
@@ -170,9 +156,6 @@ const styles = StyleSheet.create({
170156
transitionDuration: '0.1s',
171157
width: '100%'
172158
},
173-
disabledTrack: {
174-
backgroundColor: '#D5D5D5'
175-
},
176159
thumb: {
177160
alignSelf: 'flex-start',
178161
borderRadius: '100%',
@@ -181,12 +164,9 @@ const styles = StyleSheet.create({
181164
transform: [{ translateZ: 0 }],
182165
transitionDuration: '0.1s'
183166
},
184-
thumbOn: {
167+
thumbActive: {
185168
start: '100%'
186169
},
187-
disabledThumb: {
188-
backgroundColor: '#BDBDBD'
189-
},
190170
nativeControl: {
191171
...StyleSheet.absoluteFillObject,
192172
height: '100%',

0 commit comments

Comments
 (0)