Skip to content

Commit dbc5533

Browse files
committed
fix: review findings
1 parent 48a0d39 commit dbc5533

8 files changed

Lines changed: 27 additions & 49 deletions

File tree

src/components/Appbar/Appbar.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ const Appbar = ({
167167
elevation = elevated ? 2 : 0,
168168
...restStyle
169169
} = (flattenedStyle || {}) as Exclude<typeof flattenedStyle, number> & {
170-
elevation?: number;
170+
elevation?: Elevation;
171171
backgroundColor?: ColorValue;
172172
};
173173

@@ -234,7 +234,7 @@ const Appbar = ({
234234
insets,
235235
restStyle,
236236
]}
237-
elevation={elevation as Elevation}
237+
elevation={elevation}
238238
container
239239
{...rest}
240240
>

src/components/Menu/Menu.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { useSafeAreaInsets } from 'react-native-safe-area-context';
2323

2424
import MenuItem from './MenuItem';
2525
import { useInternalTheme } from '../../core/theming';
26-
import type { Elevation, ElevationLevel, Theme, ThemeProp } from '../../types';
26+
import type { Elevation, Theme, ThemeProp } from '../../types';
2727
import { addEventListener } from '../../utils/addEventListener';
2828
import { BackHandler } from '../../utils/BackHandler/BackHandler';
2929
import Portal from '../Portal/Portal';
@@ -669,10 +669,7 @@ const Menu = ({
669669
styles.shadowMenuContainer,
670670
shadowMenuContainerStyle,
671671
{
672-
backgroundColor:
673-
md3Colors.elevation[
674-
`level${elevation}` as ElevationLevel
675-
],
672+
backgroundColor: md3Colors.elevation[`level${elevation}`],
676673
},
677674
contentStyle,
678675
]}

src/components/Surface.tsx

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,13 @@ function getStyleForShadowLayer(
9696
shadowOffset: {
9797
width: 0,
9898
height: elevation.interpolate({
99-
inputRange: [...elevationInputRange],
100-
outputRange: [...shadowLayers[layer].height],
99+
inputRange: elevationInputRange,
100+
outputRange: shadowLayers[layer].height,
101101
}),
102102
},
103103
shadowRadius: elevation.interpolate({
104-
inputRange: [...elevationInputRange],
105-
outputRange: [...shadowLayers[layer].shadowRadius],
104+
inputRange: elevationInputRange,
105+
outputRange: shadowLayers[layer].shadowRadius,
106106
}),
107107
};
108108
}
@@ -252,9 +252,9 @@ const Surface = forwardRef<View, Props>(
252252
const backgroundColor = (() => {
253253
if (isAnimatedValue(elevation)) {
254254
return elevation.interpolate({
255-
inputRange: [...elevationInputRange],
256-
outputRange: [...elevationInputRange].map((elevation) => {
257-
return colors.elevation?.[`level${elevation as Elevation}`];
255+
inputRange: elevationInputRange,
256+
outputRange: elevationInputRange.map((elevation) => {
257+
return colors.elevation?.[`level${elevation}`];
258258
}),
259259
});
260260
}
@@ -286,17 +286,15 @@ const Surface = forwardRef<View, Props>(
286286
}
287287

288288
if (Platform.OS === 'android') {
289-
const elevationLevel = [...androidElevationLevels];
290-
291289
const getElevationAndroid = () => {
292290
if (isAnimatedValue(elevation)) {
293291
return elevation.interpolate({
294-
inputRange: [...elevationInputRange],
295-
outputRange: elevationLevel,
292+
inputRange: elevationInputRange,
293+
outputRange: androidElevationLevels,
296294
});
297295
}
298296

299-
return elevationLevel[elevation];
297+
return androidElevationLevels[elevation];
300298
};
301299

302300
const { margin, padding, transform, borderRadius } = (StyleSheet.flatten(

src/components/__tests__/Menu.test.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Animated, Dimensions, StyleSheet, View } from 'react-native';
44
import { act, render, screen, waitFor } from '@testing-library/react-native';
55

66
import { getTheme } from '../../core/theming';
7-
import { Elevation, ElevationLevel } from '../../types';
7+
import { Elevation } from '../../types';
88
import Button from '../Button/Button';
99
import Menu from '../Menu/Menu';
1010
import Portal from '../Portal/Portal';
@@ -87,8 +87,7 @@ it('renders menu with content styles', () => {
8787
);
8888

8989
expect(getByTestId('menu-surface')).toHaveStyle({
90-
backgroundColor:
91-
theme.colors.elevation[`level${elevation}` as ElevationLevel],
90+
backgroundColor: theme.colors.elevation[`level${elevation}`],
9291
});
9392
})
9493
);

src/index.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ export * from './theme/schemes';
1212

1313
export { default as Provider } from './core/PaperProvider';
1414
export { default as PaperProvider } from './core/PaperProvider';
15-
export { default as shadow } from './theme/shadow';
1615
export { default as configureFonts } from './theme/fonts';
1716

1817
import * as Avatar from './components/Avatar/Avatar';

src/theme/shadow.tsx

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/theme/tokens/sys/elevation.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33

44
import { Animated } from 'react-native';
55

6-
import type { ThemeElevation } from '../../types';
6+
import { isAnimatedValue } from '../../../utils/animations';
7+
import type { Elevation, ThemeElevation } from '../../types';
78

89
export const defaultElevation: ThemeElevation = {
910
level0: 0,
@@ -14,9 +15,9 @@ export const defaultElevation: ThemeElevation = {
1415
level5: 5,
1516
};
1617

17-
export const elevationInputRange = [0, 1, 2, 3, 4, 5] as const;
18+
export const elevationInputRange: Elevation[] = Object.values(defaultElevation);
1819

19-
export const androidElevationLevels = [0, 1, 3, 6, 8, 12] as const;
20+
export const androidElevationLevels = [0, 1, 3, 6, 8, 12];
2021

2122
export const shadowLayers = [
2223
{
@@ -29,20 +30,20 @@ export const shadowLayers = [
2930
height: [0, 1, 1, 1, 2, 4],
3031
shadowRadius: [0, 2, 2, 3, 3, 4],
3132
},
32-
] as const;
33+
];
3334

3435
export function shadow(
3536
elevation: number | Animated.Value = 0,
3637
shadowColor: string
3738
) {
38-
if (elevation instanceof Animated.Value) {
39+
if (isAnimatedValue(elevation)) {
3940
return {
4041
shadowColor,
4142
shadowOffset: {
4243
width: new Animated.Value(0),
4344
height: elevation.interpolate({
44-
inputRange: [...elevationInputRange],
45-
outputRange: [...shadowLayers[0].height],
45+
inputRange: elevationInputRange,
46+
outputRange: shadowLayers[0].height,
4647
}),
4748
},
4849
shadowOpacity: elevation.interpolate({
@@ -51,8 +52,8 @@ export function shadow(
5152
extrapolate: 'clamp',
5253
}),
5354
shadowRadius: elevation.interpolate({
54-
inputRange: [...elevationInputRange],
55-
outputRange: [...shadowLayers[0].shadowRadius],
55+
inputRange: elevationInputRange,
56+
outputRange: shadowLayers[0].shadowRadius,
5657
}),
5758
};
5859
}

src/theme/types/elevation.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
export type Elevation = 0 | 1 | 2 | 3 | 4 | 5;
22

3-
export type ElevationLevel =
4-
| 'level0'
5-
| 'level1'
6-
| 'level2'
7-
| 'level3'
8-
| 'level4'
9-
| 'level5';
3+
export type ElevationLevel = `level${Elevation}`;
104

115
export type ElevationColors = Record<ElevationLevel, string>;
126

0 commit comments

Comments
 (0)