Skip to content

add ellipsizeMode prop for text truncation and improve accessibility #3542

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 7 commits into from
Mar 18, 2025
19 changes: 19 additions & 0 deletions demo/src/screens/__tests__/__snapshots__/AvatarScreen.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ exports[`AvatarScreen renders screen 1`] = `
"bottom": 0,
"justifyContent": "center",
"left": 0,
"overflow": "hidden",
"position": "absolute",
"right": 0,
"top": 0,
Expand Down Expand Up @@ -247,6 +248,7 @@ exports[`AvatarScreen renders screen 1`] = `
"bottom": 0,
"justifyContent": "center",
"left": 0,
"overflow": "hidden",
"position": "absolute",
"right": 0,
"top": 0,
Expand Down Expand Up @@ -434,6 +436,7 @@ exports[`AvatarScreen renders screen 1`] = `
"bottom": 0,
"justifyContent": "center",
"left": 0,
"overflow": "hidden",
"position": "absolute",
"right": 0,
"top": 0,
Expand All @@ -448,6 +451,7 @@ exports[`AvatarScreen renders screen 1`] = `
testID="undefined.container"
>
<Text
ellipsizeMode="clip"
numberOfLines={1}
style={
[
Expand Down Expand Up @@ -663,6 +667,7 @@ exports[`AvatarScreen renders screen 1`] = `
"bottom": 0,
"justifyContent": "center",
"left": 0,
"overflow": "hidden",
"position": "absolute",
"right": 0,
"top": 0,
Expand All @@ -677,6 +682,7 @@ exports[`AvatarScreen renders screen 1`] = `
testID="undefined.container"
>
<Text
ellipsizeMode="clip"
numberOfLines={1}
style={
[
Expand Down Expand Up @@ -898,6 +904,7 @@ exports[`AvatarScreen renders screen 1`] = `
"bottom": 0,
"justifyContent": "center",
"left": 0,
"overflow": "hidden",
"position": "absolute",
"right": 0,
"top": 0,
Expand Down Expand Up @@ -1155,6 +1162,7 @@ exports[`AvatarScreen renders screen 1`] = `
"bottom": 0,
"justifyContent": "center",
"left": 0,
"overflow": "hidden",
"position": "absolute",
"right": 0,
"top": 0,
Expand Down Expand Up @@ -1412,6 +1420,7 @@ exports[`AvatarScreen renders screen 1`] = `
"bottom": 0,
"justifyContent": "center",
"left": 0,
"overflow": "hidden",
"position": "absolute",
"right": 0,
"top": 0,
Expand Down Expand Up @@ -1620,6 +1629,7 @@ exports[`AvatarScreen renders screen 1`] = `
"bottom": 0,
"justifyContent": "center",
"left": 0,
"overflow": "hidden",
"position": "absolute",
"right": 0,
"top": 0,
Expand Down Expand Up @@ -1877,6 +1887,7 @@ exports[`AvatarScreen renders screen 1`] = `
"bottom": 0,
"justifyContent": "center",
"left": 0,
"overflow": "hidden",
"position": "absolute",
"right": 0,
"top": 0,
Expand Down Expand Up @@ -2179,6 +2190,7 @@ exports[`AvatarScreen renders screen 1`] = `
"bottom": 0,
"justifyContent": "center",
"left": 0,
"overflow": "hidden",
"position": "absolute",
"right": 0,
"top": 0,
Expand Down Expand Up @@ -2367,6 +2379,7 @@ exports[`AvatarScreen renders screen 1`] = `
"bottom": 0,
"justifyContent": "center",
"left": 0,
"overflow": "hidden",
"position": "absolute",
"right": 0,
"top": 0,
Expand All @@ -2386,6 +2399,7 @@ exports[`AvatarScreen renders screen 1`] = `
testID="undefined.container"
>
<Text
ellipsizeMode="clip"
numberOfLines={1}
style={
[
Expand Down Expand Up @@ -2597,6 +2611,7 @@ exports[`AvatarScreen renders screen 1`] = `
"bottom": 0,
"justifyContent": "center",
"left": 0,
"overflow": "hidden",
"position": "absolute",
"right": 0,
"top": 0,
Expand All @@ -2616,6 +2631,7 @@ exports[`AvatarScreen renders screen 1`] = `
testID="undefined.container"
>
<Text
ellipsizeMode="clip"
numberOfLines={1}
style={
[
Expand Down Expand Up @@ -2827,6 +2843,7 @@ exports[`AvatarScreen renders screen 1`] = `
"bottom": 0,
"justifyContent": "center",
"left": 0,
"overflow": "hidden",
"position": "absolute",
"right": 0,
"top": 0,
Expand Down Expand Up @@ -3015,6 +3032,7 @@ exports[`AvatarScreen renders screen 1`] = `
"bottom": 0,
"justifyContent": "center",
"left": 0,
"overflow": "hidden",
"position": "absolute",
"right": 0,
"top": 0,
Expand All @@ -3029,6 +3047,7 @@ exports[`AvatarScreen renders screen 1`] = `
testID="undefined.container"
>
<Text
ellipsizeMode="clip"
numberOfLines={1}
style={
[
Expand Down
11 changes: 9 additions & 2 deletions src/components/avatar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
ImagePropsBase,
ImageStyle,
TextStyle,
TextProps,
AccessibilityProps
} from 'react-native';
import {Colors, BorderRadiuses} from '../../style';
Expand Down Expand Up @@ -120,6 +121,10 @@ export type AvatarProps = Pick<AccessibilityProps, 'accessibilityLabel'> &
* The label color
*/
labelColor?: string;
/*
* The ellipsize mode for the label, default is clip
*/
labelEllipsizeMode?: TextProps['ellipsizeMode'];
/**
* ribbon label to display on the avatar
*/
Expand Down Expand Up @@ -185,6 +190,7 @@ const Avatar = forwardRef<any, AvatarProps>((props: AvatarProps, ref: React.Forw
useAutoColors,
autoColorsConfig,
containerStyle,
labelEllipsizeMode = 'clip',
onPress,
children
} = themeProps;
Expand Down Expand Up @@ -354,7 +360,7 @@ const Avatar = forwardRef<any, AvatarProps>((props: AvatarProps, ref: React.Forw
>
<View testID={`${testID}.container`} style={textContainerStyle}>
{!_.isUndefined(text) && (
<Text numberOfLines={1} style={textStyle} testID={`${testID}.label`}>
<Text numberOfLines={1} ellipsizeMode={labelEllipsizeMode} style={textStyle} testID={`${testID}.label`}>
{text}
</Text>
)}
Expand All @@ -373,7 +379,8 @@ const styles = StyleSheet.create({
...StyleSheet.absoluteFillObject,
alignItems: 'center',
justifyContent: 'center',
borderRadius: BorderRadiuses.br100
borderRadius: BorderRadiuses.br100,
overflow: 'hidden'
},
initialsContainerWithInset: {
top: 1,
Expand Down