Skip to content

Commit 7eebdf1

Browse files
authored
Fix PlatfromColor issue in TabController.TabBarItem (#1955)
* Fix usage of colors in TabController.TabBarItem to fix PlatformColor issue * Add a note that explain what we are doing with colors in TabBarItem
1 parent 30a3ac6 commit 7eebdf1

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

src/components/tabController/TabBarItem.tsx

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ interface Props extends TabControllerItemProps {
108108
export default function TabBarItem({
109109
index,
110110
label,
111-
labelColor,
112-
selectedLabelColor,
111+
labelColor = DEFAULT_LABEL_COLOR,
112+
selectedLabelColor = DEFAULT_SELECTED_LABEL_COLOR,
113113
labelStyle,
114114
selectedLabelStyle,
115115
icon,
@@ -131,6 +131,11 @@ export default function TabBarItem({
131131
const sharedLabelStyle = useSharedValue(JSON.parse(JSON.stringify(labelStyle)));
132132
const sharedSelectedLabelStyle = useSharedValue(JSON.parse(JSON.stringify(selectedLabelStyle)));
133133

134+
// NOTE: We clone these color values in refs because they might contain a PlatformColor value
135+
// which throws an error (see https://github.com/software-mansion/react-native-reanimated/issues/3164)
136+
const inactiveColor = useRef(_.cloneDeep(labelColor));
137+
const activeColor = useRef(_.cloneDeep(!ignore ? selectedLabelColor : inactiveColor.current));
138+
134139
useEffect(() => {
135140
if (props.width) {
136141
props.onLayout?.({nativeEvent: {layout: {x: 0, y: 0, width: itemWidth.current, height: 0}}} as LayoutChangeEvent,
@@ -165,21 +170,15 @@ export default function TabBarItem({
165170

166171
const animatedLabelColorStyle = useAnimatedStyle(() => {
167172
const isActive = currentPage.value === index;
168-
const inactiveColor = labelColor || DEFAULT_LABEL_COLOR;
169-
const activeColor = !ignore ? selectedLabelColor || DEFAULT_SELECTED_LABEL_COLOR : inactiveColor;
170-
171173
return {
172-
color: isActive ? activeColor : inactiveColor
174+
color: isActive ? activeColor.current : inactiveColor.current
173175
};
174176
});
175177

176178
const animatedIconStyle = useAnimatedStyle(() => {
177179
const isActive = currentPage.value === index;
178-
const inactiveColor = labelColor || DEFAULT_LABEL_COLOR;
179-
const activeColor = !ignore ? selectedLabelColor || DEFAULT_SELECTED_LABEL_COLOR : inactiveColor;
180-
181180
return {
182-
tintColor: isActive ? activeColor : inactiveColor
181+
tintColor: isActive ? activeColor.current : inactiveColor.current
183182
};
184183
});
185184

0 commit comments

Comments
 (0)