Skip to content
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

feat: adjust elevation usage in components based on Surface #3154

Merged
merged 2 commits into from
Apr 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions src/components/Banner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import Icon, { IconSource } from './Icon';
import { withTheme } from '../core/theming';
import type { $RemoveChildren, Theme } from '../types';

const ELEVATION = 1;
const DEFAULT_MAX_WIDTH = 960;

type Props = $RemoveChildren<typeof Surface> & {
Expand Down Expand Up @@ -42,6 +41,11 @@ type Props = $RemoveChildren<typeof Surface> & {
* Use this prop to apply custom width for wide layouts.
*/
contentStyle?: StyleProp<ViewStyle>;
/**
* @supported Available in v3.x with theme version 3
* Changes Banner shadow and background on iOS and Android.
*/
elevation?: 0 | 1 | 2 | 3 | 4 | 5 | Animated.Value;
style?: StyleProp<ViewStyle>;
ref?: React.RefObject<View>;
/**
Expand Down Expand Up @@ -125,6 +129,7 @@ const Banner = ({
children,
actions,
contentStyle,
elevation = 1,
style,
theme,
onShowAnimationFinished = () => {},
Expand Down Expand Up @@ -183,14 +188,9 @@ const Banner = ({
return (
<Surface
{...rest}
style={[
style,
!theme.isV3 && {
elevation: ELEVATION,
},
]}
style={[!theme.isV3 && styles.elevation, style]}
theme={theme}
{...(theme.isV3 && { elevation: ELEVATION })}
{...(theme.isV3 && { elevation })}
>
<View style={[styles.wrapper, contentStyle]}>
<Animated.View style={{ height }} />
Expand Down Expand Up @@ -285,6 +285,9 @@ const styles = StyleSheet.create({
button: {
margin: 4,
},
elevation: {
elevation: 1,
},
});

export default withTheme(Banner);
15 changes: 9 additions & 6 deletions src/components/Card/Card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ type HandlePressType = 'in' | 'out';

type Props = React.ComponentProps<typeof Surface> & {
/**
* Resting elevation of the card which controls the drop shadow.
* Changes Card shadow and background on iOS and Android.
*/
elevation?: never | number;
elevation?: 0 | 1 | 2 | 3 | 4 | 5 | Animated.Value;
/**
* Function to execute on long press.
*/
Expand Down Expand Up @@ -123,7 +123,7 @@ const Card = ({
const { current: elevationDarkAdaptive } = React.useRef<Animated.Value>(
new Animated.Value(cardElevation)
);
const { animation, dark, mode, roundness } = theme;
const { animation, dark, mode, roundness, isV3 } = theme;

const prevDarkRef = React.useRef<boolean>(dark);
React.useEffect(() => {
Expand Down Expand Up @@ -158,13 +158,13 @@ const Card = ({
const isPressTypeIn = pressType === 'in';
if (dark && isAdaptiveMode) {
Animated.timing(elevationDarkAdaptive, {
toValue: isPressTypeIn ? 8 : cardElevation,
toValue: isPressTypeIn ? (isV3 ? 2 : 8) : cardElevation,
duration: animationDuration,
useNativeDriver: false,
}).start();
} else {
Animated.timing(elevation, {
toValue: isPressTypeIn ? 8 : cardElevation,
toValue: isPressTypeIn ? (isV3 ? 2 : 8) : cardElevation,
duration: animationDuration,
useNativeDriver: false,
}).start();
Expand Down Expand Up @@ -197,13 +197,16 @@ const Card = ({
style={[
{
borderRadius: roundness,
elevation: computedElevation as unknown as number,
borderColor,
},
!isV3 && {
elevation: computedElevation as unknown as number,
},
cardMode === 'outlined' ? styles.outlined : {},
style,
]}
theme={theme}
{...(isV3 && { elevation: computedElevation })}
{...rest}
>
<TouchableWithoutFeedback
Expand Down
3 changes: 2 additions & 1 deletion src/components/Menu/Menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,7 @@ class Menu extends React.Component<Props, State> {
opacity: opacityAnimation,
transform: scaleTransforms,
borderRadius: theme.roundness,
...(!theme.isV3 && { elevation: 8 }),
...(scrollableMenuHeight ? { height: scrollableMenuHeight } : {}),
};

Expand Down Expand Up @@ -574,6 +575,7 @@ class Menu extends React.Component<Props, State> {
contentStyle,
] as StyleProp<ViewStyle>
}
{...(theme.isV3 && { elevation: 2 })}
>
{(scrollableMenuHeight && (
<ScrollView>{children}</ScrollView>
Expand All @@ -595,7 +597,6 @@ const styles = StyleSheet.create({
shadowMenuContainer: {
opacity: 0,
paddingVertical: 8,
elevation: 8,
},
});

Expand Down
19 changes: 15 additions & 4 deletions src/components/Searchbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
TextInputProps,
ViewStyle,
TextStyle,
Animated,
} from 'react-native';

import color from 'color';
Expand Down Expand Up @@ -47,12 +48,16 @@ type Props = React.ComponentPropsWithRef<typeof TextInput> & {
* Callback to execute if we want the left icon to act as button.
*/
onIconPress?: () => void;
/**
* @supported Available in v3.x with theme version 3
* Changes Searchbar shadow and background on iOS and Android.
*/
elevation?: 0 | 1 | 2 | 3 | 4 | 5 | Animated.Value;
/**
* Set style of the TextInput component inside the searchbar
*/
inputStyle?: StyleProp<TextStyle>;
style?: StyleProp<ViewStyle>;

/**
* @optional
*/
Expand Down Expand Up @@ -113,6 +118,7 @@ const Searchbar = React.forwardRef<TextInputHandles, Props>(
onIconPress,
placeholder,
searchAccessibilityLabel = 'search',
elevation = 1,
style,
theme,
value,
Expand Down Expand Up @@ -153,8 +159,8 @@ const Searchbar = React.forwardRef<TextInputHandles, Props>(
rest.onChangeText?.('');
};

const { colors, roundness, dark, fonts } = theme;
const textColor = theme.isV3 ? theme.colors.onSurface : theme.colors.text;
const { colors, roundness, dark, fonts, isV3 } = theme;
const textColor = isV3 ? theme.colors.onSurface : theme.colors.text;
const font = fonts.regular;
const iconColor =
customIconColor ||
Expand All @@ -164,10 +170,12 @@ const Searchbar = React.forwardRef<TextInputHandles, Props>(
return (
<Surface
style={[
{ borderRadius: roundness, elevation: 4 },
{ borderRadius: roundness },
!isV3 && styles.elevation,
styles.container,
style,
]}
{...(theme.isV3 && { elevation })}
>
<IconButton
// @ts-expect-error We keep old a11y props for backwards compat with old RN versions
Expand Down Expand Up @@ -257,6 +265,9 @@ const styles = StyleSheet.create({
textAlign: I18nManager.isRTL ? 'right' : 'left',
minWidth: 0,
},
elevation: {
elevation: 4,
},
});

export default withTheme(Searchbar);
15 changes: 12 additions & 3 deletions src/components/Snackbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ export type SnackbarProps = React.ComponentProps<typeof Surface> & {
/**
* Style for the wrapper of the snackbar
*/
/**
* @supported Available in v3.x with theme version 3
* Changes Snackbar shadow and background on iOS and Android.
*/
elevation?: 0 | 1 | 2 | 3 | 4 | 5 | Animated.Value;
wrapperStyle?: StyleProp<ViewStyle>;
style?: StyleProp<ViewStyle>;
ref?: React.RefObject<View>;
Expand Down Expand Up @@ -110,6 +115,7 @@ const Snackbar = ({
duration = DURATION_MEDIUM,
onDismiss,
children,
elevation = 2,
wrapperStyle,
style,
theme,
Expand Down Expand Up @@ -168,7 +174,7 @@ const Snackbar = ({
}
}, [visible, duration, opacity, scale, onDismiss]);

const { colors, roundness } = theme;
const { colors, roundness, isV3 } = theme;

if (hidden) return null;

Expand All @@ -189,7 +195,7 @@ const Snackbar = ({
accessibilityLiveRegion="polite"
style={
[
!theme.isV3 && { elevation: 6 },
!isV3 && styles.elevation,
styles.container,
{
borderRadius: roundness,
Expand All @@ -209,7 +215,7 @@ const Snackbar = ({
style,
] as StyleProp<ViewStyle>
}
{...(theme.isV3 && { elevation: 2 })}
{...(isV3 && { elevation })}
{...rest}
>
<Text
Expand Down Expand Up @@ -278,6 +284,9 @@ const styles = StyleSheet.create({
marginHorizontal: 8,
marginVertical: 6,
},
elevation: {
elevation: 6,
},
});

export default withTheme(Snackbar);
12 changes: 9 additions & 3 deletions src/components/Surface.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,14 @@ const Surface = ({
<Animated.View
style={[
{
elevation: getElevationAndroid(),
backgroundColor,
transform,
},
outerLayerStyles,
sharedStyle,
{
elevation: getElevationAndroid(),
},
]}
>
{children}
Expand Down Expand Up @@ -194,7 +196,11 @@ const Surface = ({
const getStyleForAnimatedShadowLayer = (layer: 0 | 1) => {
return {
shadowColor,
shadowOpacity: iOSShadowOutputRanges[layer].shadowOpacity,
shadowOpacity: elevation.interpolate({
inputRange: [0, 1],
outputRange: [0, iOSShadowOutputRanges[layer].shadowOpacity],
extrapolate: 'clamp',
}),
shadowOffset: {
width: 0,
height: elevation.interpolate({
Expand Down Expand Up @@ -223,7 +229,7 @@ const Surface = ({
const getStyleForShadowLayer = (layer: 0 | 1) => {
return {
shadowColor,
shadowOpacity: iOSShadowOutputRanges[layer].shadowOpacity,
shadowOpacity: elevation ? iOSShadowOutputRanges[layer].shadowOpacity : 0,
shadowOffset: {
width: 0,
height: iOSShadowOutputRanges[layer].height[elevation],
Expand Down