Skip to content

Commit

Permalink
fix: appbar header ignores border bottom radius (callstack#3767)
Browse files Browse the repository at this point in the history
  • Loading branch information
josemak25 committed Apr 3, 2023
1 parent b6cad7c commit 0a4cfb9
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/components/Appbar/AppbarHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
DEFAULT_APPBAR_HEIGHT,
getAppbarColor,
modeAppbarHeight,
getAppbarBorders,
} from './utils';

export type Props = React.ComponentProps<typeof Appbar> & {
Expand Down Expand Up @@ -115,6 +116,7 @@ const AppbarHeader = ({
mode = Platform.OS === 'ios' ? 'center-aligned' : 'small',
elevated = false,
theme: themeOverrides,
testID = 'appbar-header',
...rest
}: Props) => {
const theme = useInternalTheme(themeOverrides);
Expand All @@ -134,6 +136,8 @@ const AppbarHeader = ({
zIndex?: number;
};

const borderRadius = getAppbarBorders(restStyle);

const backgroundColor = getAppbarColor(
theme,
elevation,
Expand All @@ -145,6 +149,7 @@ const AppbarHeader = ({

return (
<View
testID={`${testID}-root-layer`}
style={[
{
backgroundColor,
Expand All @@ -153,10 +158,12 @@ const AppbarHeader = ({
paddingTop: statusBarHeight ?? top,
paddingHorizontal: Math.max(left, right),
},
borderRadius,
shadow(elevation) as ViewStyle,
]}
>
<Appbar
testID={testID}
style={[{ height, backgroundColor }, styles.appbar, restStyle]}
dark={dark}
{...(isV3 && {
Expand Down
28 changes: 27 additions & 1 deletion src/components/Appbar/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import type { ColorValue, StyleProp, ViewStyle } from 'react-native';
import { StyleSheet } from 'react-native';
import { StyleSheet, Animated } from 'react-native';

import overlay from '../../styles/overlay';
import { black, white } from '../../styles/themes/v2/colors';
Expand All @@ -12,6 +12,14 @@ import AppbarContent from './AppbarContent';

export type AppbarModes = 'small' | 'medium' | 'large' | 'center-aligned';

const borderStyleProperties = [
'borderRadius',
'borderTopLeftRadius',
'borderTopRightRadius',
'borderBottomRightRadius',
'borderBottomLeftRadius',
];

export const getAppbarColor = (
theme: InternalTheme,
elevation: number,
Expand Down Expand Up @@ -39,6 +47,24 @@ export const getAppbarColor = (
return colors.surface;
};

export const getAppbarBorders = (
style:
| Animated.Value
| Animated.AnimatedInterpolation<string | number>
| Animated.WithAnimatedObject<ViewStyle>
) => {
const borders: Record<string, number> = {};

for (const property of borderStyleProperties) {
const value = style[property as keyof typeof style];
if (value) {
borders[property] = value;
}
}

return borders;
};

type RenderAppbarContentProps = {
children: React.ReactNode;
isDark: boolean;
Expand Down
27 changes: 27 additions & 0 deletions src/components/__tests__/Appbar/Appbar.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import AppbarHeader from '../../Appbar/AppbarHeader';
import {
getAppbarColor,
modeTextVariant,
getAppbarBorders,
renderAppbarContent as utilRenderAppbarContent,
} from '../../Appbar/utils';
import Menu from '../../Menu/Menu';
Expand Down Expand Up @@ -488,4 +489,30 @@ describe('animated value changes correctly', () => {
transform: [{ scale: 1.5 }],
});
});

it('header bottom border radius applied correctly', () => {
const style = { borderBottomLeftRadius: 16, borderBottomRightRadius: 16 };

const { getByTestId } = render(
<mockSafeAreaContext.SafeAreaProvider>
<Appbar.Header style={style} testID="appbar-header">
{null}
</Appbar.Header>
</mockSafeAreaContext.SafeAreaProvider>
);
expect(getByTestId('appbar-header-root-layer')).toHaveStyle(style);
});

describe('getAppbarBorders', () => {
const style = { borderRadius: 10, height: 60, top: 13 };

it('should return only border radius styles', () => {
expect(getAppbarBorders(style)).toEqual({ borderRadius: 10 });
});

it('should return empty object if no borders are passed', () => {
const style = { height: 60, top: 13 };
expect(getAppbarBorders(style)).toEqual({});
});
});
});

0 comments on commit 0a4cfb9

Please sign in to comment.