Skip to content

Commit e65f3d4

Browse files
author
Paweł Kata
committed
fix(appbar): use animated styles and fix positioning
1 parent d69a102 commit e65f3d4

3 files changed

Lines changed: 114 additions & 21 deletions

File tree

‎src/components/Appbar/Appbar.tsx‎

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ export type Props = AppbarBaseProps & {
212212
* | `safeAreaInsets` | Overrides detected top, left, or right safe-area insets. |
213213
* | `statusBarHeight` | Overrides only the automatic top inset. |
214214
* | `contentStyle` | Styles the headline and subtitle area. |
215-
* | `style` | Styles the app bar and can override its background color. |
215+
* | `style` | Styles the app bar container, including its safe-area padding, and can override its background color. Accepts Reanimated animated styles. |
216216
*
217217
* ## Migrating from the compound API
218218
*
@@ -278,17 +278,18 @@ const Appbar = ({
278278
}
279279

280280
const detectedInsets = useSafeAreaInsets();
281-
const { customBackground, restStyle, borderRadius } = React.useMemo(() => {
281+
// The flattened style is only read for the static overrides `Surface`
282+
// takes as props. `style` itself is forwarded unflattened, since Reanimated
283+
// only recognizes animated styles it receives as separate entries.
284+
const { customBackground, borderRadius } = React.useMemo(() => {
282285
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
283286
const resolvedStyle = (StyleSheet.flatten(style) || {}) as ViewStyle & {
284287
backgroundColor?: ColorValue;
285288
};
286-
const { backgroundColor, ...remainingStyle } = resolvedStyle;
287289

288290
return {
289-
customBackground: backgroundColor,
290-
restStyle: remainingStyle,
291-
borderRadius: getAppbarBorders(remainingStyle),
291+
customBackground: resolvedStyle.backgroundColor,
292+
borderRadius: getAppbarBorders(resolvedStyle),
292293
};
293294
}, [style]);
294295
const backgroundColor =
@@ -307,15 +308,18 @@ const Appbar = ({
307308
);
308309
const sideStyle = React.useMemo(() => ({ width: sideWidth }), [sideWidth]);
309310
const surfaceStyle = React.useMemo(
310-
() => ({
311-
paddingTop: topInset,
312-
paddingHorizontal: horizontalInset,
313-
}),
314-
[horizontalInset, topInset]
311+
() => [
312+
{
313+
paddingTop: topInset,
314+
paddingHorizontal: horizontalInset,
315+
},
316+
style,
317+
],
318+
[horizontalInset, style, topInset]
315319
);
316320
const appbarStyle = React.useMemo(
317-
() => [styles.appbar, { backgroundColor, minHeight }, restStyle],
318-
[backgroundColor, minHeight, restStyle]
321+
() => [styles.appbar, { minHeight }],
322+
[minHeight]
319323
);
320324
const resolvedSearchInputStyle = React.useMemo(
321325
() => [{ color: theme.colors.onSurface }, searchBar?.inputStyle],
@@ -474,14 +478,16 @@ const Appbar = ({
474478

475479
return (
476480
<Surface
481+
{...rest}
477482
ref={ref}
483+
testID={testID}
478484
elevation={0}
479485
backgroundColor={backgroundColor}
480486
theme={theme}
481487
style={surfaceStyle}
482488
{...borderRadius}
483489
>
484-
<View {...rest} testID={testID} style={appbarStyle}>
490+
<View style={appbarStyle}>
485491
{variant === 'search'
486492
? renderSearchAppbar()
487493
: variant === 'small'
@@ -494,6 +500,7 @@ const Appbar = ({
494500

495501
const styles = StyleSheet.create({
496502
appbar: {
503+
flexGrow: 1,
497504
paddingHorizontal: 4,
498505
},
499506
smallRow: {

‎src/components/Appbar/types.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,10 @@ export type AppbarBaseProps = Omit<
130130
left?: number;
131131
right?: number;
132132
};
133-
/** Style applied to the app bar container. */
133+
/**
134+
* Style applied to the app bar container, which includes the safe-area
135+
* padding. Accepts Reanimated animated styles.
136+
*/
134137
style?: StyleProp<AnimatedStyle<ViewStyle>>;
135138
/** Reference for the app bar container. */
136139
ref?: React.Ref<View>;

‎src/components/__tests__/Appbar/Appbar.test.tsx‎

Lines changed: 89 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
import * as React from 'react';
2-
import { Dimensions, Text, View } from 'react-native';
3-
import type { GestureResponderEvent } from 'react-native';
2+
import { Dimensions, Pressable, Text, View } from 'react-native';
3+
import type { GestureResponderEvent, ViewStyle } from 'react-native';
44

55
import { describe, expect, it, jest } from '@jest/globals';
6+
import {
7+
getAnimatedStyle,
8+
useAnimatedStyle,
9+
useSharedValue,
10+
} from 'react-native-reanimated';
611
import { SafeAreaProvider } from 'react-native-safe-area-context';
712

813
import { render, screen, userEvent, waitFor } from '../../../test-utils';
@@ -21,6 +26,28 @@ const writtenHeadlineVariants: Exclude<AppbarVariant, 'search'>[] = [
2126
'large-flexible',
2227
];
2328

29+
const AnimatedAppbar = ({ staticStyle }: { staticStyle?: ViewStyle }) => {
30+
const opacity = useSharedValue(0);
31+
const animatedStyle = useAnimatedStyle(() => ({ opacity: opacity.value }));
32+
33+
return (
34+
<>
35+
<Pressable
36+
testID="reveal-appbar"
37+
onPress={() => {
38+
opacity.value = 1;
39+
}}
40+
/>
41+
<Appbar
42+
variant="small"
43+
headline="Inbox"
44+
style={staticStyle ? [staticStyle, animatedStyle] : animatedStyle}
45+
testID={testIDPrefix}
46+
/>
47+
</>
48+
);
49+
};
50+
2451
const decorativeHeadlineImage = (
2552
<View>
2653
<View accessible role="img" accessibilityLabel="Brand artwork" />
@@ -229,7 +256,7 @@ describe('Appbar surface', () => {
229256
<Appbar variant="small" headline="Inbox" testID={testIDPrefix} />
230257
);
231258

232-
expect(screen.getByTestId(testIDPrefix).parent).toHaveStyle({
259+
expect(screen.getByTestId(testIDPrefix)).toHaveStyle({
233260
backgroundColor: LightTheme.colors.surface,
234261
});
235262

@@ -242,7 +269,7 @@ describe('Appbar surface', () => {
242269
/>
243270
);
244271

245-
expect(screen.getByTestId(testIDPrefix).parent).toHaveStyle({
272+
expect(screen.getByTestId(testIDPrefix)).toHaveStyle({
246273
backgroundColor: LightTheme.colors.surfaceContainer,
247274
});
248275

@@ -256,7 +283,7 @@ describe('Appbar surface', () => {
256283
/>
257284
);
258285

259-
expect(screen.getByTestId(testIDPrefix).parent).toHaveStyle({
286+
expect(screen.getByTestId(testIDPrefix)).toHaveStyle({
260287
backgroundColor: customBackground,
261288
});
262289
});
@@ -283,13 +310,69 @@ describe('Appbar surface', () => {
283310
</SafeAreaProvider>
284311
);
285312

286-
expect(screen.getByTestId(testIDPrefix).parent).toHaveStyle({
313+
expect(screen.getByTestId(testIDPrefix)).toHaveStyle({
287314
borderBottomLeftRadius: 16,
288315
borderBottomRightRadius: 16,
289316
paddingTop: 20,
290317
paddingHorizontal: 12,
291318
});
292319
});
320+
321+
it('animates the app bar with a Reanimated style', async () => {
322+
await render(<AnimatedAppbar />);
323+
324+
const appbar = screen.getByTestId(testIDPrefix);
325+
expect(getAnimatedStyle(appbar)).toMatchObject({ opacity: 0 });
326+
327+
await userEvent.press(screen.getByTestId('reveal-appbar'));
328+
await jest.runAllTimersAsync();
329+
330+
expect(getAnimatedStyle(appbar)).toMatchObject({ opacity: 1 });
331+
});
332+
333+
it('positions the whole app bar, including its safe-area padding', async () => {
334+
await render(
335+
<Appbar
336+
variant="small"
337+
headline="Inbox"
338+
statusBarHeight={20}
339+
style={{ position: 'absolute', top: 0, left: 0, right: 0, zIndex: 1 }}
340+
testID={testIDPrefix}
341+
/>
342+
);
343+
344+
expect(screen.getByTestId(testIDPrefix)).toHaveStyle({
345+
position: 'absolute',
346+
top: 0,
347+
zIndex: 1,
348+
paddingTop: 20,
349+
});
350+
});
351+
352+
it('keeps static styles combined with an animated style', async () => {
353+
const customBackground = 'rebeccapurple';
354+
await render(
355+
<AnimatedAppbar
356+
staticStyle={{
357+
position: 'absolute',
358+
top: 0,
359+
backgroundColor: customBackground,
360+
}}
361+
/>
362+
);
363+
364+
const appbar = screen.getByTestId(testIDPrefix);
365+
expect(appbar).toHaveStyle({
366+
position: 'absolute',
367+
top: 0,
368+
backgroundColor: customBackground,
369+
});
370+
371+
await userEvent.press(screen.getByTestId('reveal-appbar'));
372+
await jest.runAllTimersAsync();
373+
374+
expect(getAnimatedStyle(appbar)).toMatchObject({ opacity: 1 });
375+
});
293376
});
294377

295378
describe('Appbar actions', () => {

0 commit comments

Comments
 (0)