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

fix: correct changing Dialog backdrop color (#3259) #3260

Merged
merged 4 commits into from
Aug 8, 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
1 change: 1 addition & 0 deletions docs/pages/10.migration-guide-to-5.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ theme: {
inverseOnSurface,
inverseSurface,
inversePrimary,
backdrop,
elevation: {
level0,
level1,
Expand Down
1 change: 1 addition & 0 deletions docs/pages/2.theming.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ A theme usually contains the following properties:
- `inverseOnSurface`
- `inverseSurface`
- `inversePrimary`
- `backdrop`

- `typescale` (`object`): various fonts styling properties under the text variant key used in component.
- [`variant` e.g. `labelMedium`] (`object`):
Expand Down
1 change: 1 addition & 0 deletions src/components/Dialog/Dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ const Dialog = ({
styles.container,
style,
]}
theme={theme}
>
{React.Children.toArray(children)
.filter((child) => child != null && typeof child !== 'boolean')
Expand Down
12 changes: 9 additions & 3 deletions src/components/FAB/FABGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ type Props = {
* An action item should contain the following properties:
* - `icon`: icon to display (required)
* - `label`: optional label text
* - `accessibilityLabel`: accessibility label for the action, uses label by default if specified
* - `color`: custom icon color of the action item
* - `labelTextColor`: custom label text color of the action item
* - `accessibilityLabel`: accessibility label for the action, uses label by default if specified
* - `style`: pass additional styles for the fab item, for example, `backgroundColor`
* - `labelStyle`: pass additional styles for the fab item label, for example, `backgroundColor`
* - `size`: size of action item. Defaults to `small`. @supported Available in v5.x
* - `onPress`: callback that is called when `FAB` is pressed (required)
* - `size`: size of action item. Defaults to `small`. @supported Available in v5.x
* - `testID`: testID to be used on tests
*/
actions: Array<{
icon: IconSource;
Expand All @@ -55,6 +56,10 @@ type Props = {
* Custom color for the `FAB`.
*/
color?: string;
/**
* Custom backdrop color for opened speed dial background.
*/
backdropColor?: string;
/**
* Function to execute on pressing the `FAB`.
*/
Expand Down Expand Up @@ -170,6 +175,7 @@ const FABGroup = ({
onStateChange,
color: colorProp,
variant = 'primary',
backdropColor: customBackdropColor,
}: Props) => {
const { current: backdrop } = React.useRef<Animated.Value>(
new Animated.Value(0)
Expand Down Expand Up @@ -238,7 +244,7 @@ const FABGroup = ({
const toggle = () => onStateChange({ open: !open });

const { labelColor, backdropColor, stackedFABBackgroundColor } =
getFABGroupColors({ theme });
getFABGroupColors({ theme, customBackdropColor });

const backdropOpacity = open
? backdrop.interpolate({
Expand Down
21 changes: 18 additions & 3 deletions src/components/FAB/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,16 @@ const getLabelColor = ({ theme }: { theme: Theme }) => {
return color(theme.colors.text).fade(0.54).rgb().string();
};

const getBackdropColor = ({ theme }: { theme: Theme }) => {
const getBackdropColor = ({
theme,
customBackdropColor,
}: {
theme: Theme;
customBackdropColor?: string;
}) => {
if (customBackdropColor) {
return customBackdropColor;
}
if (theme.isV3) {
return color(theme.colors.background).alpha(0.95).rgb().string();
}
Expand All @@ -319,10 +328,16 @@ const getStackedFABBackgroundColor = ({ theme }: { theme: Theme }) => {
return theme.colors.surface;
};

export const getFABGroupColors = ({ theme }: { theme: Theme }) => {
export const getFABGroupColors = ({
theme,
customBackdropColor,
}: {
theme: Theme;
customBackdropColor?: string;
}) => {
return {
labelColor: getLabelColor({ theme }),
backdropColor: getBackdropColor({ theme }),
backdropColor: getBackdropColor({ theme, customBackdropColor }),
stackedFABBackgroundColor: getStackedFABBackgroundColor({ theme }),
};
};
Expand Down
26 changes: 17 additions & 9 deletions src/components/Modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,14 @@ import {
View,
NativeEventSubscription,
} from 'react-native';
import color from 'color';
import {
getStatusBarHeight,
getBottomSpace,
} from 'react-native-iphone-x-helper';
import Surface from './Surface';
import { useTheme } from '../core/theming';
import { withTheme } from '../core/theming';
import useAnimatedValue from '../utils/useAnimatedValue';
import { addEventListener } from '../utils/addEventListener';
import { MD3Colors } from '../styles/themes/v3/tokens';

type Props = {
/**
Expand Down Expand Up @@ -51,6 +49,14 @@ type Props = {
* Use this prop to change the default wrapper style or to override safe area insets with marginTop and marginBottom.
*/
style?: StyleProp<ViewStyle>;
/**
* @optional
*/
theme: ReactNativePaper.Theme;
/**
* testID to be used on tests.
*/
testID?: string;
lukewalczak marked this conversation as resolved.
Show resolved Hide resolved
};

const DEFAULT_DURATION = 220;
Expand Down Expand Up @@ -96,23 +102,23 @@ const BOTTOM_INSET = getBottomSpace();
* export default MyComponent;
* ```
*/
export default function Modal({
function Modal({
dismissable = true,
visible = false,
overlayAccessibilityLabel = 'Close modal',
onDismiss,
children,
contentContainerStyle,
style,
theme,
testID = 'modal',
}: Props) {
const visibleRef = React.useRef(visible);

React.useEffect(() => {
visibleRef.current = visible;
});

const theme = useTheme();

const { scale } = theme.animation;

const opacity = useAnimatedValue(visible ? 1 : 0);
Expand Down Expand Up @@ -209,6 +215,7 @@ export default function Modal({
accessibilityLiveRegion="polite"
style={StyleSheet.absoluteFill}
onAccessibilityEscape={hideModal}
testID={testID}
>
<TouchableWithoutFeedback
accessibilityLabel={overlayAccessibilityLabel}
Expand All @@ -218,12 +225,11 @@ export default function Modal({
importantForAccessibility="no"
>
<Animated.View
testID={`${testID}-backdrop`}
lukewalczak marked this conversation as resolved.
Show resolved Hide resolved
style={[
styles.backdrop,
{
backgroundColor: theme.isV3
? color(MD3Colors.neutralVariant20).alpha(0.4).rgb().string()
: theme.colors?.backdrop,
backgroundColor: theme.colors?.backdrop,
opacity,
},
]}
Expand Down Expand Up @@ -253,6 +259,8 @@ export default function Modal({
);
}

export default withTheme(Modal);

const styles = StyleSheet.create({
backdrop: {
flex: 1,
Expand Down
96 changes: 96 additions & 0 deletions src/components/__tests__/FABGroup.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import color from 'color';
import { getFABGroupColors } from '../FAB/utils';
import { getTheme } from '../../core/theming';

describe('getFABGroupColors - backdrop color', () => {
it('should return custom color', () => {
expect(
getFABGroupColors({
theme: getTheme(),
customBackdropColor: 'transparent',
})
).toMatchObject({
backdropColor: 'transparent',
});
});

it('should return correct backdrop color, for theme version 3', () => {
expect(
getFABGroupColors({
theme: getTheme(),
})
).toMatchObject({
backdropColor: color(getTheme().colors.background)
.alpha(0.95)
.rgb()
.string(),
});
});

it('should return correct backdrop color, for theme version 2', () => {
expect(
getFABGroupColors({
theme: getTheme(false, false),
})
).toMatchObject({
backdropColor: getTheme(false, false).colors.backdrop,
});
});
});

describe('getFABGroupColors - label color', () => {
it('should return correct theme color, for theme version 3', () => {
expect(
getFABGroupColors({
theme: getTheme(),
})
).toMatchObject({
labelColor: getTheme().colors.onSurface,
});
});

it('should return correct theme color, dark mode, for theme version 2', () => {
expect(
getFABGroupColors({
theme: getTheme(true, false),
})
).toMatchObject({
labelColor: getTheme(true, false).colors.text,
});
});

it('should return correct theme color, light mode, for theme version 2', () => {
expect(
getFABGroupColors({
theme: getTheme(false, false),
})
).toMatchObject({
labelColor: color(getTheme(false, false).colors.text)
.fade(0.54)
.rgb()
.string(),
});
});
});

describe('getFABGroupColors - stacked FAB background color', () => {
it('should return correct theme color, for theme version 3', () => {
expect(
getFABGroupColors({
theme: getTheme(),
})
).toMatchObject({
stackedFABBackgroundColor: getTheme().colors.elevation.level3,
});
});

it('should return correct theme color, dark mode, for theme version 2', () => {
expect(
getFABGroupColors({
theme: getTheme(false, false),
})
).toMatchObject({
stackedFABBackgroundColor: getTheme(false, false).colors.surface,
});
});
});
25 changes: 25 additions & 0 deletions src/components/__tests__/Modal.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import * as React from 'react';
import { render } from 'react-native-testing-library';
import Modal from '../Modal';

describe('Modal', () => {
it('should render custom backdrop color', () => {
const { getByTestId } = render(
<Modal
visible={true}
testID="modal"
theme={{
colors: {
backdrop: 'transparent',
},
}}
/>
);

expect(getByTestId('modal-backdrop').props.style).toEqual(
expect.arrayContaining([
expect.objectContaining({ backgroundColor: 'transparent' }),
])
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ exports[`renders list section with custom title style 1`] = `
"scale": 1,
},
"colors": Object {
"backdrop": "rgba(50, 47, 55, 0.4)",
"background": "rgba(255, 251, 254, 1)",
"elevation": Object {
"level0": "transparent",
Expand Down Expand Up @@ -47,7 +48,6 @@ exports[`renders list section with custom title style 1`] = `
"primaryContainer": "rgba(234, 221, 255, 1)",
"secondary": "rgba(98, 91, 113, 1)",
"secondaryContainer": "rgba(232, 222, 248, 1)",
"shadow": "rgba(0, 0, 0, 1)",
"surface": "rgba(255, 251, 254, 1)",
"surfaceDisabled": "rgba(28, 27, 31, 0.12)",
"surfaceVariant": "rgba(231, 224, 236, 1)",
Expand Down Expand Up @@ -456,6 +456,7 @@ exports[`renders list section with subheader 1`] = `
"scale": 1,
},
"colors": Object {
"backdrop": "rgba(50, 47, 55, 0.4)",
"background": "rgba(255, 251, 254, 1)",
"elevation": Object {
"level0": "transparent",
Expand Down Expand Up @@ -487,7 +488,6 @@ exports[`renders list section with subheader 1`] = `
"primaryContainer": "rgba(234, 221, 255, 1)",
"secondary": "rgba(98, 91, 113, 1)",
"secondaryContainer": "rgba(232, 222, 248, 1)",
"shadow": "rgba(0, 0, 0, 1)",
"surface": "rgba(255, 251, 254, 1)",
"surfaceDisabled": "rgba(28, 27, 31, 0.12)",
"surfaceVariant": "rgba(231, 224, 236, 1)",
Expand Down Expand Up @@ -894,6 +894,7 @@ exports[`renders list section without subheader 1`] = `
"scale": 1,
},
"colors": Object {
"backdrop": "rgba(50, 47, 55, 0.4)",
"background": "rgba(255, 251, 254, 1)",
"elevation": Object {
"level0": "transparent",
Expand Down Expand Up @@ -925,7 +926,6 @@ exports[`renders list section without subheader 1`] = `
"primaryContainer": "rgba(234, 221, 255, 1)",
"secondary": "rgba(98, 91, 113, 1)",
"secondaryContainer": "rgba(232, 222, 248, 1)",
"shadow": "rgba(0, 0, 0, 1)",
"surface": "rgba(255, 251, 254, 1)",
"surfaceDisabled": "rgba(28, 27, 31, 0.12)",
"surfaceVariant": "rgba(231, 224, 236, 1)",
Expand Down
4 changes: 2 additions & 2 deletions src/styles/themes/v3/DarkTheme.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import color from 'color';
import type { MD3Theme } from '../../../types';
import { MD3LightTheme } from './LightTheme';
import { tokens } from './tokens';
import { MD3Colors, tokens } from './tokens';

const { palette, opacity } = tokens.md.ref;

Expand Down Expand Up @@ -43,10 +43,10 @@ export const MD3DarkTheme: MD3Theme = {
onErrorContainer: palette.error80,
onBackground: palette.neutral90,
outline: palette.neutralVariant60,
shadow: palette.neutral0,
inverseSurface: palette.neutral90,
inverseOnSurface: palette.neutral20,
inversePrimary: palette.primary40,
backdrop: color(MD3Colors.neutralVariant20).alpha(0.4).rgb().string(),
elevation: {
level0: 'transparent',
// Note: Color values with transparency cause RN to transfer shadows to children nodes
Expand Down
Loading