Skip to content

Fix PlatfromColor issue in TabController.TabBarItem #1955

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 10, 2022
Merged
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
19 changes: 9 additions & 10 deletions src/components/tabController/TabBarItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ interface Props extends TabControllerItemProps {
export default function TabBarItem({
index,
label,
labelColor,
selectedLabelColor,
labelColor = DEFAULT_LABEL_COLOR,
selectedLabelColor = DEFAULT_SELECTED_LABEL_COLOR,
labelStyle,
selectedLabelStyle,
icon,
Expand All @@ -131,6 +131,11 @@ export default function TabBarItem({
const sharedLabelStyle = useSharedValue(JSON.parse(JSON.stringify(labelStyle)));
const sharedSelectedLabelStyle = useSharedValue(JSON.parse(JSON.stringify(selectedLabelStyle)));

// NOTE: We clone these color values in refs because they might contain a PlatformColor value
// which throws an error (see https://github.com/software-mansion/react-native-reanimated/issues/3164)
const inactiveColor = useRef(_.cloneDeep(labelColor));
const activeColor = useRef(_.cloneDeep(!ignore ? selectedLabelColor : inactiveColor.current));
Copy link
Collaborator

Choose a reason for hiding this comment

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

I would give these consts a more specific name like 'animatedLabelColor' and 'animatedSelectedLabelColor' so it will be extra clear what this clone is for.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think the name is suffice, but I will add a note instead that explains the reason we keep it like this

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added a note


useEffect(() => {
if (props.width) {
props.onLayout?.({nativeEvent: {layout: {x: 0, y: 0, width: itemWidth.current, height: 0}}} as LayoutChangeEvent,
Expand Down Expand Up @@ -165,21 +170,15 @@ export default function TabBarItem({

const animatedLabelColorStyle = useAnimatedStyle(() => {
const isActive = currentPage.value === index;
const inactiveColor = labelColor || DEFAULT_LABEL_COLOR;
const activeColor = !ignore ? selectedLabelColor || DEFAULT_SELECTED_LABEL_COLOR : inactiveColor;

return {
color: isActive ? activeColor : inactiveColor
color: isActive ? activeColor.current : inactiveColor.current
};
});

const animatedIconStyle = useAnimatedStyle(() => {
const isActive = currentPage.value === index;
const inactiveColor = labelColor || DEFAULT_LABEL_COLOR;
const activeColor = !ignore ? selectedLabelColor || DEFAULT_SELECTED_LABEL_COLOR : inactiveColor;

return {
tintColor: isActive ? activeColor : inactiveColor
tintColor: isActive ? activeColor.current : inactiveColor.current
};
});

Expand Down