Skip to content
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

Add animation to Segmented Button icons #4543

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
22 changes: 19 additions & 3 deletions src/components/SegmentedButtons/SegmentedButtonItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
getSegmentedButtonDensityPadding,
} from './utils';
import { useInternalTheme } from '../../core/theming';
import CrossFadeIcon from '../CrossFadeIcon';
import type { IconSource } from '../Icon';
import Icon from '../Icon';
import TouchableRipple from '../TouchableRipple/TouchableRipple';
Expand All @@ -34,6 +35,10 @@ export type Props = {
* Icon to display for the `SegmentedButtonItem`.
*/
icon?: IconSource;
/**
* Whether an icon change is animated.
*/
animated?: boolean;
/**
* @supported Available in v5.x with theme version 3
* Custom color for unchecked Text and Icon.
Expand Down Expand Up @@ -73,6 +78,10 @@ export type Props = {
* Label text of the button.
*/
label?: string;
/**
* Label text number of lines of the button.
*/
numberOfLines?: number;
/**
* Button segment.
*/
Expand Down Expand Up @@ -106,6 +115,7 @@ export type Props = {

const SegmentedButtonItem = ({
checked,
animated = false,
accessibilityLabel,
disabled,
style,
Expand All @@ -118,6 +128,7 @@ const SegmentedButtonItem = ({
icon,
testID,
label,
numberOfLines,
onPress,
segment,
density = 'regular',
Expand Down Expand Up @@ -202,7 +213,8 @@ const SegmentedButtonItem = ({
: theme.fonts.labelLarge),
color: textColor,
};

const IconComponent = animated ? CrossFadeIcon : Icon;
const NumberOfLines = numberOfLines ? numberOfLines : 1;
return (
<View style={[buttonStyle, styles.button, style]}>
<TouchableRipple
Expand All @@ -229,14 +241,18 @@ const SegmentedButtonItem = ({
) : null}
{showIcon ? (
<Animated.View testID={`${testID}-icon`} style={iconStyle}>
<Icon source={icon} size={iconSize} color={textColor} />
<IconComponent
color={textColor}
source={icon ? icon : 'progress-question'}
size={iconSize}
/>
</Animated.View>
) : null}
<Text
variant="labelLarge"
style={[styles.label, labelTextStyle, labelStyle]}
selectable={false}
numberOfLines={1}
numberOfLines={NumberOfLines}
maxFontSizeMultiplier={labelMaxFontSizeMultiplier}
testID={`${testID}-label`}
>
Expand Down
5 changes: 5 additions & 0 deletions src/components/SegmentedButtons/SegmentedButtons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export type Props = {
* - `icon`: icon to display for the item
* - `disabled`: whether the button is disabled
* - `accessibilityLabel`: acccessibility label for the button. This is read by the screen reader when the user taps the button.
* - `numberOfLines`: label text number of lines of the button
* - `checkedColor`: custom color for checked Text and Icon
* - `uncheckedColor`: custom color for unchecked Text and Icon
* - `onPress`: callback that is called when button is pressed
Expand All @@ -66,6 +67,7 @@ export type Props = {
icon?: IconSource;
disabled?: boolean;
accessibilityLabel?: string;
numberOfLines?: number;
checkedColor?: string;
uncheckedColor?: string;
onPress?: (event: GestureResponderEvent) => void;
Expand All @@ -79,6 +81,7 @@ export type Props = {
* Density is applied to the height, to allow usage in denser UIs
*/
density?: 'regular' | 'small' | 'medium' | 'high';
animated?: boolean;
style?: StyleProp<ViewStyle>;
theme?: ThemeProp;
} & ConditionalValue;
Expand Down Expand Up @@ -132,6 +135,7 @@ const SegmentedButtons = ({
buttons,
multiSelect,
density,
animated,
style,
theme: themeOverrides,
}: Props) => {
Expand Down Expand Up @@ -172,6 +176,7 @@ const SegmentedButtons = ({
{...item}
key={i}
checked={checked}
animated={animated}
segment={segment}
density={density}
onPress={onPress}
Expand Down