diff --git a/docs/6.x/docs/guides/migration.md b/docs/6.x/docs/guides/migration.md index 0b0b13c650..2a47e40d95 100644 --- a/docs/6.x/docs/guides/migration.md +++ b/docs/6.x/docs/guides/migration.md @@ -104,9 +104,11 @@ Components no longer read values from the `style` prop to derive other styles, s - `Appbar` and `Appbar.Header`: `backgroundColor` and the border radius props (`borderRadius`, `borderTopLeftRadius` etc.) replace `style.backgroundColor` and the border radius in `style` - `Avatar.Icon`, `Avatar.Image` and `Avatar.Text`: `backgroundColor` replaces `style.backgroundColor` +- `Banner`: `backgroundColor` replaces `style.backgroundColor` - `Button`: `iconSize` replaces `labelStyle.fontSize` for the size of the icon and the loading indicator - `Button`: `textColor` replaces `labelStyle.color` for the color of the icon and the loading indicator - `Card`: `borderColor` replaces `style.borderColor` for the outline color +- `Menu`: `backgroundColor` replaces `contentStyle.backgroundColor` `Tooltip` no longer reads positioning styles from the wrapped element. Apply them to a parent `View` wrapping the `Tooltip`, see [Tooltip](#tooltip). @@ -558,6 +560,24 @@ e.g.: + ``` +### Banner + +The banner background is now `theme.colors.surfaceContainerLow` by default and no longer depends on the `elevation` prop, which only controls the shadow. If you customized `theme.colors.elevation.level1` to change the banner background, customize `theme.colors.surfaceContainerLow` instead. With `LightTheme` and `DarkTheme`, the rendered color at the default `elevation` of `1` is unchanged. A banner with `elevation={0}` is no longer transparent. With `DynamicLightTheme` and `DynamicDarkTheme` on Android 12 and newer, the banner now uses the system surface color. To change the background of a single banner, use the new `backgroundColor` prop. + +```diff +const theme = { + ...LightTheme, + colors: { + ...LightTheme.colors, +- elevation: { +- ...LightTheme.colors.elevation, +- level1: '#ffd8e4', +- }, ++ surfaceContainerLow: '#ffd8e4', + }, +}; +``` + ### Surface - The `elevation` prop no longer accepts a React Native `Animated.Value`. Any `elevation` changes are animated automatically. @@ -887,6 +907,22 @@ The icon is 24dp, in `theme.colors.secondary`. `Dialog.Icon`'s `color` and `size ### Menu +The menu background is now `theme.colors.surfaceContainer` by default, following the Material Design 3 spec, and no longer depends on the `elevation` prop, which only controls the shadow. If you customized `theme.colors.elevation.level2` to change the menu background, customize `theme.colors.surfaceContainer` instead. With `LightTheme` and `DarkTheme`, the rendered color at the default `elevation` of `2` is unchanged. A menu with `elevation={0}` is no longer transparent. With `DynamicLightTheme` and `DynamicDarkTheme` on Android 12 and newer, the menu now uses the system surface color. To change the background of a single menu, use the new `backgroundColor` prop. + +```diff +const theme = { + ...LightTheme, + colors: { + ...LightTheme.colors, +- elevation: { +- ...LightTheme.colors.elevation, +- level2: '#ffd8e4', +- }, ++ surfaceContainer: '#ffd8e4', + }, +}; +``` + The `overlayAccessibilityLabel` prop was renamed to `dismissAccessibilityLabel`: ```diff diff --git a/docs/6.x/docs/guides/theming.mdx b/docs/6.x/docs/guides/theming.mdx index d62fcfefef..2e4711b4db 100644 --- a/docs/6.x/docs/guides/theming.mdx +++ b/docs/6.x/docs/guides/theming.mdx @@ -107,7 +107,7 @@ A theme usually contains the following properties: - `errorContainer` - `onErrorContainer` - > Surfaces at elevation levels 0-5 are tinted via color overlays based on the primary color, such as app bars or menus. The addition of a grade from 0-5 introduces tonal variation to the surface baseline. + > Surfaces at elevation levels 1-5 use neutral surface tones, such as app bars. Menus and banners use the `surfaceContainer` and `surfaceContainerLow` roles instead. The addition of a grade from 0-5 introduces tonal variation to the surface baseline. - `elevation` (`object`) - `level0` - transparent - `level1` - 5% opacity diff --git a/docs/src/data/themeColors.ts b/docs/src/data/themeColors.ts index e22df5ed4e..eea45ec2a4 100644 --- a/docs/src/data/themeColors.ts +++ b/docs/src/data/themeColors.ts @@ -35,6 +35,7 @@ export const themeColors = { }, Banner: { '-': { + backgroundColor: 'theme.colors.surfaceContainerLow', textColor: 'theme.colors.onSurface', 'action textColor': 'theme.colors.primary', }, @@ -290,7 +291,7 @@ export const themeColors = { }, Menu: { '-': { - backgroundColor: 'theme.colors.elevation.level2', + backgroundColor: 'theme.colors.surfaceContainer', }, }, 'Menu.Item': { diff --git a/example/src/Examples/BannerExample.tsx b/example/src/Examples/BannerExample.tsx index 2a99b38866..ed226cf706 100644 --- a/example/src/Examples/BannerExample.tsx +++ b/example/src/Examples/BannerExample.tsx @@ -19,10 +19,7 @@ const BannerExample = () => { colors: { ...defaultTheme.colors, onSurface: Palette.tertiary100, - elevation: { - ...defaultTheme.colors.elevation, - level1: Palette.tertiary50, - }, + surfaceContainerLow: Palette.tertiary50, primary: Palette.tertiary10, }, }; diff --git a/src/components/Banner.tsx b/src/components/Banner.tsx index 4b51b5f953..21f9b50e71 100644 --- a/src/components/Banner.tsx +++ b/src/components/Banner.tsx @@ -1,6 +1,7 @@ import * as React from 'react'; import { StyleSheet, View } from 'react-native'; import type { + ColorValue, LayoutChangeEvent, StyleProp, ViewProps, @@ -67,9 +68,14 @@ export type Props = Omit & { contentStyle?: StyleProp; /** * @supported Available in v5.x with theme version 3 - * Changes Banner shadow and background on iOS and Android. + * Changes Banner shadow on iOS and Android. The banner background is + * `theme.colors.surfaceContainerLow` and is not affected by `elevation`. */ elevation?: Elevation; + /** + * Background color of the banner. Overrides `theme.colors.surfaceContainerLow`. + */ + backgroundColor?: ColorValue; /** * Specifies the largest possible scale a text font can reach. */ @@ -143,6 +149,7 @@ const Banner = ({ actions = [], contentStyle, elevation = 1, + backgroundColor, style, theme: themeOverrides, onShowAnimationFinished = () => {}, @@ -211,6 +218,7 @@ const Banner = ({ {...rest} style={[surfaceStyle, style]} theme={theme} + backgroundColor={backgroundColor ?? colors.surfaceContainerLow} elevation={elevation} > diff --git a/src/components/Menu/Menu.tsx b/src/components/Menu/Menu.tsx index f83cec3a47..8beabdf571 100644 --- a/src/components/Menu/Menu.tsx +++ b/src/components/Menu/Menu.tsx @@ -10,6 +10,7 @@ import { } from 'react-native'; import type { KeyboardEvent as RNKeyboardEvent } from 'react-native'; import type { + ColorValue, EmitterSubscription, LayoutRectangle, NativeEventSubscription, @@ -83,14 +84,18 @@ export type Props = { contentStyle?: StyleProp; style?: StyleProp; /** - * Elevation level of the menu's content. Shadow styles are calculated based on this value. Default `backgroundColor` is taken from the corresponding `theme.colors.elevation` property. By default equals `2`. + * Elevation level of the menu's content. Shadow styles are calculated based on this value. The menu background is `theme.colors.surfaceContainer` per the MD3 spec and is not affected by `elevation`. By default equals `2`. * @supported Available in v5.x with theme version 3 */ elevation?: Elevation; + /** + * Background color of the menu's content. Overrides `theme.colors.surfaceContainer`. + */ + backgroundColor?: ColorValue; /** * Mode of the menu's content. - * - `elevated` - Surface with a shadow and background color corresponding to set `elevation` value. - * - `flat` - Surface without a shadow, with the background color corresponding to set `elevation` value. + * - `elevated` - Surface with a shadow corresponding to set `elevation` value. + * - `flat` - Surface without a shadow. * * @supported Available in v5.x with theme version 3 */ @@ -203,6 +208,7 @@ const Menu = ({ contentStyle, style, elevation = DEFAULT_ELEVATION, + backgroundColor, mode = DEFAULT_MODE, children, theme: themeOverrides, @@ -699,6 +705,9 @@ const Menu = ({ > { @@ -119,6 +121,30 @@ it('renders visible banner, with action buttons and with image', async () => { expect(tree).toMatchSnapshot(); }); +it('renders banner on the theme surface container background', async () => { + await render( + + Text + + ); + + expect(screen.getByTestId('banner')).toHaveStyle({ + backgroundColor: LightTheme.colors.surfaceContainerLow, + }); +}); + +it('renders banner with a custom background color', async () => { + await render( + + Text + + ); + + expect(screen.getByTestId('banner')).toHaveStyle({ + backgroundColor: Palette.error60, + }); +}); + it('render visible banner, with custom theme', async () => { const tree = ( await render( diff --git a/src/components/__tests__/Menu.test.tsx b/src/components/__tests__/Menu.test.tsx index 7fced51b87..bf943621ef 100644 --- a/src/components/__tests__/Menu.test.tsx +++ b/src/components/__tests__/Menu.test.tsx @@ -15,6 +15,7 @@ import * as Reanimated from 'react-native-reanimated'; import PaperProvider from '../../core/PaperProvider'; import { render } from '../../test-utils'; import { LightTheme } from '../../theme/schemes'; +import { Palette } from '../../theme/tokens'; import type { Elevation } from '../../theme/types'; import Button from '../Button/Button'; import Menu from '../Menu/Menu'; @@ -172,7 +173,7 @@ const ViewPrototype: ComponentRef = View.prototype; const elevations: Elevation[] = [0, 1, 2, 3, 4, 5]; elevations.forEach((elevation) => - it(`renders menu with background color based on elevation value = ${elevation}`, async () => { + it(`keeps the same menu background at elevation ${elevation}`, async () => { const testID = 'menu-with-elevation'; await render( @@ -192,11 +193,34 @@ elevations.forEach((elevation) => ); expect(screen.getByTestId(testID)).toHaveStyle({ - backgroundColor: LightTheme.colors.elevation[`level${elevation}`], + backgroundColor: LightTheme.colors.surfaceContainer, }); }) ); +it('renders menu with a custom background color', async () => { + const testID = 'menu-with-background-color'; + + await render( + + Open menu} + mode="flat" + backgroundColor={Palette.error60} + testID={testID} + > + + + + ); + + expect(screen.getByTestId(testID)).toHaveStyle({ + backgroundColor: Palette.error60, + }); +}); + it('uses the default anchorPosition of top', async () => { const testID = 'top-positioned-menu'; const dimensionsSpy = jest.spyOn(Dimensions, 'get').mockReturnValue({