Skip to content

Commit 251a1d1

Browse files
refactor: improving border radius logic
1 parent 9f158d5 commit 251a1d1

File tree

1 file changed

+41
-20
lines changed

1 file changed

+41
-20
lines changed

package/src/components/Message/MessageSimple/MessageContent.tsx

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -234,17 +234,47 @@ const MessageContentWithContext = <
234234

235235
const isBorderColor = isMyMessage && !error;
236236

237-
const isLastMessageInMessageGroup = (firstGroupStyle: string, secondGroupStyle: string) =>
238-
(groupStyle === firstGroupStyle || groupStyle === secondGroupStyle) &&
239-
(!hasThreadReplies || threadList);
237+
const getBorderRadius = () => {
238+
// enum('top', 'middle', 'bottom', 'single')
239+
const groupPosition = groupStyles?.[0];
240+
241+
const isBottomOrSingle = groupPosition === 'single' || groupPosition === 'bottom';
242+
let borderBottomLeftRadius = borderRadiusL;
243+
let borderBottomRightRadius = borderRadiusL;
244+
245+
if (isBottomOrSingle && (!hasThreadReplies || threadList)) {
246+
// add relevant sharp corner
247+
if (alignment === 'left') {
248+
borderBottomLeftRadius = borderRadiusS;
249+
} else {
250+
borderBottomRightRadius = borderRadiusS;
251+
}
252+
}
240253

241-
const calculateBorderRadius = (firstGroupStyle: string, secondGroupStyle: string) =>
242-
isLastMessageInMessageGroup(firstGroupStyle, secondGroupStyle) ? borderRadiusS : borderRadiusL;
254+
return {
255+
borderBottomLeftRadius,
256+
borderBottomRightRadius,
257+
};
258+
};
243259

244-
const getRadiusFromTheme = (
245-
radius: number | undefined,
246-
defaultValue: number | undefined = borderRadiusL,
247-
) => (radius !== undefined ? radius : defaultValue);
260+
const getBorderRadiusFromTheme = () => {
261+
const bordersFromTheme: Record<string, number | undefined> = {
262+
borderBottomLeftRadius,
263+
borderBottomRightRadius,
264+
borderRadius,
265+
borderTopLeftRadius,
266+
borderTopRightRadius,
267+
};
268+
269+
// filter out undefined values
270+
for (const key in bordersFromTheme) {
271+
if (bordersFromTheme[key] === undefined) {
272+
delete bordersFromTheme[key];
273+
}
274+
}
275+
276+
return bordersFromTheme;
277+
};
248278

249279
return (
250280
<TouchableOpacity
@@ -320,18 +350,9 @@ const MessageContentWithContext = <
320350
styles.containerInner,
321351
{
322352
backgroundColor,
323-
borderBottomLeftRadius: getRadiusFromTheme(
324-
borderBottomLeftRadius,
325-
calculateBorderRadius('left_bottom', 'left_single'),
326-
),
327-
borderBottomRightRadius: getRadiusFromTheme(
328-
borderBottomRightRadius,
329-
calculateBorderRadius('right_bottom', 'right_single'),
330-
),
331353
borderColor: isBorderColor ? backgroundColor : grey_whisper,
332-
borderRadius,
333-
borderTopLeftRadius: getRadiusFromTheme(borderTopLeftRadius),
334-
borderTopRightRadius: getRadiusFromTheme(borderTopRightRadius),
354+
...getBorderRadius(),
355+
...getBorderRadiusFromTheme(),
335356
},
336357
noBorder ? { borderWidth: 0 } : {},
337358
containerInner,

0 commit comments

Comments
 (0)