From b63068191374088d69339d9498760fdb44350988 Mon Sep 17 00:00:00 2001 From: Hristo Totov Date: Mon, 14 Sep 2026 15:18:59 +0300 Subject: [PATCH 1/4] feat: add semantic container color prop to Surface --- docs/6.x/docs/guides/migration.md | 9 ++ docs/src/data/themeColors.ts | 6 +- example/src/Examples/SurfaceExample.tsx | 61 +++++++- src/components/Banner.tsx | 4 +- src/components/Menu/Menu.tsx | 7 +- src/components/Surface.tsx | 52 +++++-- src/components/__tests__/Banner.test.tsx | 15 +- src/components/__tests__/Menu.test.tsx | 4 +- src/components/__tests__/Surface.test.tsx | 168 ++++++++++++++++++++++ src/index.tsx | 7 +- src/theme/types/color.ts | 18 +++ 11 files changed, 331 insertions(+), 20 deletions(-) diff --git a/docs/6.x/docs/guides/migration.md b/docs/6.x/docs/guides/migration.md index c4874a10a9..10e2d7e494 100644 --- a/docs/6.x/docs/guides/migration.md +++ b/docs/6.x/docs/guides/migration.md @@ -115,6 +115,14 @@ The `style` props for `Appbar` and `Appbar.Header` no longer accept `Animated.Va The `style.elevation` property is no longer supported. Use the `elevated` prop to control Appbar elevation. +### Banner + +The banner background is now always `theme.colors.surfaceContainerLow` and no longer depends on the `elevation` prop, which only controls the shadow. At the default `elevation` of `1` the rendered color is unchanged. + +### Menu + +The menu background is now always `theme.colors.surfaceContainer`, following the Material Design 3 spec, and no longer depends on the `elevation` prop, which only controls the shadow. At the default `elevation` of `2` the rendered color is unchanged. + ### Surface - The `elevation` prop no longer accepts a React Native `Animated.Value`. Any `elevation` changes are animated automatically. @@ -138,6 +146,7 @@ The `style.elevation` property is no longer supported. Use the `elevated` prop t - The `pointerEvents` prop is no longer supported as it's deprecated in React Native Web. You can specify `pointerEvents` in the `style` prop instead. - The `overflow: 'hidden'` style is no longer supported in `style` as it can clip shadows. You can nest a `View` inside the `Surface` and apply `overflow: 'hidden'` to that instead. - The default `testID` for `Surface` was removed. You can specify a `testID` explicitly if you need it. +- A new `container` prop sets the background to a semantic surface-family color role from the theme (e.g. `container="surfaceContainerLow"`). When `container` is set, `elevation` only controls the shadow. Precedence: `backgroundColor` > `container` > the color derived from `elevation`. e.g.: diff --git a/docs/src/data/themeColors.ts b/docs/src/data/themeColors.ts index 118114da27..2964440296 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', }, @@ -246,7 +247,7 @@ export const themeColors = { }, Menu: { '-': { - backgroundColor: 'theme.colors.elevation.level2', + backgroundColor: 'theme.colors.surfaceContainer', }, }, 'Menu.Item': { @@ -314,6 +315,9 @@ export const themeColors = { elevated: { backgroundColor: 'theme.colors.elevation[elevation]', }, + 'with container': { + backgroundColor: 'theme.colors[container]', + }, }, Text: { '-': { diff --git a/example/src/Examples/SurfaceExample.tsx b/example/src/Examples/SurfaceExample.tsx index e941c7c843..3036dae607 100644 --- a/example/src/Examples/SurfaceExample.tsx +++ b/example/src/Examples/SurfaceExample.tsx @@ -1,13 +1,33 @@ import * as React from 'react'; import { ScrollView, StyleSheet, View } from 'react-native'; -import { Surface, Text, Palette, List, IconButton } from 'react-native-paper'; -import type { Elevation } from 'react-native-paper'; +import { + Surface, + Text, + Palette, + List, + IconButton, + useTheme, +} from 'react-native-paper'; +import type { Elevation, SurfaceRole } from 'react-native-paper'; import ScreenWrapper from '../ScreenWrapper'; const elevationLevels: Elevation[] = [0, 1, 2, 3, 4, 5]; +const containerRoles: SurfaceRole[] = [ + 'surface', + 'surfaceDim', + 'surfaceBright', + 'surfaceVariant', + 'surfaceContainerLowest', + 'surfaceContainerLow', + 'surfaceContainer', + 'surfaceContainerHigh', + 'surfaceContainerHighest', + 'inverseSurface', +]; + const AnimatedSurface = () => { const [index, setIndex] = React.useState(3); @@ -37,8 +57,20 @@ const AnimatedSurface = () => { }; const SurfaceExample = () => { + const theme = useTheme(); + const elevationValues: Elevation[] = [0, 1, 2, 3, 4, 5]; + const onColorFor = (role: SurfaceRole) => { + if (role === 'inverseSurface') { + return theme.colors.inverseOnSurface; + } + if (role === 'surfaceVariant') { + return theme.colors.onSurfaceVariant; + } + return theme.colors.onSurface; + }; + const renderSurface = (index: Elevation, mode: 'flat' | 'elevated') => ( { + + + {containerRoles.map((role) => ( + + + {role} + + + ))} + + + diff --git a/src/components/Banner.tsx b/src/components/Banner.tsx index 909f75bf3f..000ed067a7 100644 --- a/src/components/Banner.tsx +++ b/src/components/Banner.tsx @@ -66,7 +66,8 @@ 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; /** @@ -211,6 +212,7 @@ const Banner = ({ {...rest} style={[surfaceStyle, style]} theme={theme} + container="surfaceContainerLow" elevation={elevation} > diff --git a/src/components/Menu/Menu.tsx b/src/components/Menu/Menu.tsx index 96dd4096b7..12afb057f6 100644 --- a/src/components/Menu/Menu.tsx +++ b/src/components/Menu/Menu.tsx @@ -82,14 +82,14 @@ 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; /** * 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 */ @@ -704,6 +704,7 @@ const Menu = ({ > = Extract< AnimatedStyle>>, @@ -22,8 +22,8 @@ type BorderRadius = AnimatedStyleProp<'borderRadius'>; type SurfaceVisualProps = { /** - * Background color of the Surface. Overrides the color derived from - * `elevation`. + * Background color of the Surface. Overrides both `container` and the + * color derived from `elevation`. */ backgroundColor?: ColorValue; /** @@ -104,6 +104,24 @@ export type Props = Omit & * To achieve the same effect, wrap the content in a child View with the overflow style. */ style?: StyleProp; + /** + * Semantic color role of the Surface background, resolved from the + * theme, e.g. `container="surfaceContainerLow"` renders with + * `theme.colors.surfaceContainerLow`. + * + * When `container` is set, `elevation` only controls the shadow (and + * z-order on Android) and no longer affects the background color. + * + * Precedence: `backgroundColor` > `container` > + * `theme.colors.elevation[levelN]` derived from `elevation`. + * + * Prefer `container` when the fill is an MD3 surface-family role (as + * component specs define it, e.g. menus use `surfaceContainer`). Use + * `backgroundColor` only for raw, non-semantic colors. Components with + * variant-driven fills outside the surface family should keep resolving + * their own tokens and pass the result via `backgroundColor`. + */ + container?: SurfaceRole; /** * @supported Available in v5.x with theme version 3 * Changes shadows and background on iOS and Android. @@ -111,6 +129,9 @@ export type Props = Omit & * * Note: If `mode` is set to `flat`, Surface doesn't have a shadow. * + * Note: If `backgroundColor` or `container` is set, `elevation` only + * affects the shadow. + * * Note: In version 2 the `elevation` prop was accepted via `style` prop i.e. `style={{ elevation: 4 }}`. * It's no longer supported with theme version 3 and you should use `elevation` property instead. */ @@ -118,8 +139,8 @@ export type Props = Omit & /** * @supported Available in v5.x with theme version 3 * Mode of the Surface. - * - `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 and background color corresponding to set `elevation` value (unless `backgroundColor` or `container` is set). + * - `flat` - Surface without a shadow, with the background color corresponding to set `elevation` value (unless `backgroundColor` or `container` is set). */ mode?: 'flat' | 'elevated'; /** @@ -150,9 +171,19 @@ export type Props = Omit & * import { StyleSheet } from 'react-native'; * * const MyComponent = () => ( - * - * Surface - * + * <> + * + * Surface + * + * + * Semantic container color + * + * * ); * * export default MyComponent; @@ -170,6 +201,7 @@ export type Props = Omit & */ const Surface = ({ elevation = 1, + container, children, theme: overriddenTheme, style, @@ -199,7 +231,9 @@ const Surface = ({ const { colors } = theme; const backgroundColor = - customBackgroundColor ?? colors.elevation?.[`level${elevation}`]; + customBackgroundColor ?? + (container != null ? colors[container] : undefined) ?? + colors.elevation?.[`level${elevation}`]; const backgroundStyle = { backgroundColor }; diff --git a/src/components/__tests__/Banner.test.tsx b/src/components/__tests__/Banner.test.tsx index 5cbd07b387..2665e30001 100644 --- a/src/components/__tests__/Banner.test.tsx +++ b/src/components/__tests__/Banner.test.tsx @@ -11,7 +11,8 @@ import { } from '@jest/globals'; import { act } from '@testing-library/react-native'; -import { render } from '../../test-utils'; +import { render, screen } from '../../test-utils'; +import { LightTheme } from '../../theme/schemes'; import Banner from '../Banner'; it('renders hidden banner, without action buttons and without image', async () => { @@ -104,6 +105,18 @@ it('renders visible banner, with action buttons and with image', async () => { expect(tree).toMatchSnapshot(); }); +it('renders banner with surfaceContainerLow background', async () => { + await render( + + Text + + ); + + expect(screen.getByTestId('banner')).toHaveStyle({ + backgroundColor: LightTheme.colors.surfaceContainerLow, + }); +}); + 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 c35880b48f..6c491bbe16 100644 --- a/src/components/__tests__/Menu.test.tsx +++ b/src/components/__tests__/Menu.test.tsx @@ -51,7 +51,7 @@ it('renders not visible menu', async () => { 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(`renders menu with surfaceContainer background regardless of elevation value = ${elevation}`, async () => { const testID = 'menu-with-elevation'; await render( @@ -71,7 +71,7 @@ elevations.forEach((elevation) => ); expect(screen.getByTestId(testID)).toHaveStyle({ - backgroundColor: LightTheme.colors.elevation[`level${elevation}`], + backgroundColor: LightTheme.colors.surfaceContainer, }); }) ); diff --git a/src/components/__tests__/Surface.test.tsx b/src/components/__tests__/Surface.test.tsx index 96ed707ae1..764cd06f6a 100644 --- a/src/components/__tests__/Surface.test.tsx +++ b/src/components/__tests__/Surface.test.tsx @@ -173,6 +173,142 @@ describe('Surface', () => { transitionProperty: expect.not.arrayContaining(['backgroundColor']), }); }); + + it('resolves the container role from the theme', async () => { + await render( + + {null} + + ); + + expect(screen.getByTestId('surface-test')).toHaveStyle({ + backgroundColor: LightTheme.colors.surfaceContainerHigh, + }); + }); + + it('prefers backgroundColor over container', async () => { + const backgroundColor = 'rgba(1, 2, 3, 0.5)'; + await render( + + {null} + + ); + + expect(screen.getByTestId('surface-test')).toHaveStyle({ + backgroundColor, + }); + }); + + it('prefers container over the elevation-derived color', async () => { + await render( + + {null} + + ); + + expect(screen.getByTestId('surface-test')).toHaveStyle({ + backgroundColor: LightTheme.colors.surfaceContainerLowest, + }); + }); + + it('keeps the elevation-driven shadow when container is set', async () => { + await render( + + {null} + + ); + + expect(screen.getByTestId('surface-test')).toHaveStyle({ + shadowOpacity: SPOT_SHADOW_OPACITY, + }); + }); + + it('does not render a shadow in flat mode when container is set', async () => { + await render( + + {null} + + ); + + // @ts-expect-error + expect(screen.getByTestId('surface-test')).not.toHaveStyle({ + shadowOpacity: expect.any(Number), + }); + }); + + it('keeps the container background when elevation changes', async () => { + await render( + + {null} + + ); + + await screen.rerender( + + {null} + + ); + await jest.runAllTimersAsync(); + + expect(screen.getByTestId('surface-test')).toHaveStyle({ + backgroundColor: LightTheme.colors.surfaceContainer, + }); + }); + + it('does not transition a DynamicColorIOS container role', async () => { + await render( + + {null} + + ); + + expect( + getAnimatedStyle(screen.getByTestId('surface-test')) + ).toMatchObject({ + transitionProperty: expect.not.arrayContaining(['backgroundColor']), + }); + }); }); describe('on Android', () => { @@ -208,6 +344,38 @@ describe('Surface', () => { }); }); + it('resolves the container role from the theme', async () => { + await render( + + {null} + + ); + + expect(screen.getByTestId('surface-container')).toHaveStyle({ + backgroundColor: LightTheme.colors.surfaceContainerHigh, + }); + }); + + it('keeps the elevation-driven dp value when container is set', async () => { + await render( + + {null} + + ); + + expect(screen.getByTestId('surface-container')).toHaveStyle({ + elevation: 12, + }); + }); + it('does not transition a PlatformColor background', async () => { await render( ; + export type ThemeColors = { primary: ColorValue; primaryContainer: ColorValue; From a61aeb77ea596b475d6d2bc73d87bfd23cfe56af Mon Sep 17 00:00:00 2001 From: Hristo Totov Date: Sat, 24 Oct 2026 11:49:17 +0300 Subject: [PATCH 2/4] fix: cr comments --- docs/6.x/docs/guides/migration.md | 5 ++--- docs/6.x/docs/guides/theming.mdx | 2 +- example/src/Examples/BannerExample.tsx | 5 +---- example/src/Examples/SurfaceExample.tsx | 10 +++++++++- src/components/Searchbar.tsx | 2 +- src/components/Surface.tsx | 14 +++++--------- 6 files changed, 19 insertions(+), 19 deletions(-) diff --git a/docs/6.x/docs/guides/migration.md b/docs/6.x/docs/guides/migration.md index 2d5b5e286a..8355386412 100644 --- a/docs/6.x/docs/guides/migration.md +++ b/docs/6.x/docs/guides/migration.md @@ -116,11 +116,11 @@ The `style.elevation` property is no longer supported. Use the `elevated` prop t ### Banner -The banner background is now always `theme.colors.surfaceContainerLow` and no longer depends on the `elevation` prop, which only controls the shadow. At the default `elevation` of `1` the rendered color is unchanged. +The banner background is now always `theme.colors.surfaceContainerLow` 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. With `DynamicLightTheme` and `DynamicDarkTheme` on Android 12 and newer, the banner now uses the system surface color. ### Menu -The menu background is now always `theme.colors.surfaceContainer`, following the Material Design 3 spec, and no longer depends on the `elevation` prop, which only controls the shadow. At the default `elevation` of `2` the rendered color is unchanged. +The menu background is now always `theme.colors.surfaceContainer`, 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. With `DynamicLightTheme` and `DynamicDarkTheme` on Android 12 and newer, the menu now uses the system surface color. ### Surface @@ -145,7 +145,6 @@ The menu background is now always `theme.colors.surfaceContainer`, following the - The `pointerEvents` prop is no longer supported as it's deprecated in React Native Web. You can specify `pointerEvents` in the `style` prop instead. - The `overflow: 'hidden'` style is no longer supported in `style` as it can clip shadows. You can nest a `View` inside the `Surface` and apply `overflow: 'hidden'` to that instead. - The default `testID` for `Surface` was removed. You can specify a `testID` explicitly if you need it. -- A new `container` prop sets the background to a semantic surface-family color role from the theme (e.g. `container="surfaceContainerLow"`). When `container` is set, `elevation` only controls the shadow. Precedence: `backgroundColor` > `container` > the color derived from `elevation`. e.g.: diff --git a/docs/6.x/docs/guides/theming.mdx b/docs/6.x/docs/guides/theming.mdx index c7eed1703d..8b9c60cc22 100644 --- a/docs/6.x/docs/guides/theming.mdx +++ b/docs/6.x/docs/guides/theming.mdx @@ -106,7 +106,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 0-5 are tinted via color overlays based on the primary color, 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/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/example/src/Examples/SurfaceExample.tsx b/example/src/Examples/SurfaceExample.tsx index 3036dae607..26f3aad580 100644 --- a/example/src/Examples/SurfaceExample.tsx +++ b/example/src/Examples/SurfaceExample.tsx @@ -116,7 +116,11 @@ const SurfaceExample = () => { {containerRoles.map((role) => ( & /** * Semantic color role of the Surface background, resolved from the * theme, e.g. `container="surfaceContainerLow"` renders with - * `theme.colors.surfaceContainerLow`. + * `theme.colors.surfaceContainerLow`. Accepted roles: `surface`, + * `surfaceDim`, `surfaceBright`, `surfaceVariant`, + * `surfaceContainerLowest`, `surfaceContainerLow`, `surfaceContainer`, + * `surfaceContainerHigh`, `surfaceContainerHighest` and `inverseSurface`. * * When `container` is set, `elevation` only controls the shadow (and * z-order on Android) and no longer affects the background color. * * Precedence: `backgroundColor` > `container` > * `theme.colors.elevation[levelN]` derived from `elevation`. - * - * Prefer `container` when the fill is an MD3 surface-family role (as - * component specs define it, e.g. menus use `surfaceContainer`). Use - * `backgroundColor` only for raw, non-semantic colors. Components with - * variant-driven fills outside the surface family should keep resolving - * their own tokens and pass the result via `backgroundColor`. */ container?: SurfaceRole; /** @@ -232,8 +229,7 @@ const Surface = ({ const backgroundColor = customBackgroundColor ?? - (container != null ? colors[container] : undefined) ?? - colors.elevation?.[`level${elevation}`]; + (container ? colors[container] : colors.elevation?.[`level${elevation}`]); const backgroundStyle = { backgroundColor }; From ff1480f8a94d22f617ebdac2e9f792ffd547e9a3 Mon Sep 17 00:00:00 2001 From: lukewalczak Date: Mon, 23 Nov 2026 11:53:55 +0100 Subject: [PATCH 3/4] refactor: drop Surface container prop from menu and banner fix --- docs/src/data/themeColors.ts | 3 - example/src/Examples/SurfaceExample.tsx | 69 +-------- src/components/Banner.tsx | 2 +- src/components/Menu/Menu.tsx | 2 +- src/components/Searchbar.tsx | 2 +- src/components/Surface.tsx | 48 ++----- src/components/__tests__/Surface.test.tsx | 168 ---------------------- src/index.tsx | 7 +- src/theme/types/color.ts | 18 --- 9 files changed, 15 insertions(+), 304 deletions(-) diff --git a/docs/src/data/themeColors.ts b/docs/src/data/themeColors.ts index f13c7a1d2a..eea45ec2a4 100644 --- a/docs/src/data/themeColors.ts +++ b/docs/src/data/themeColors.ts @@ -359,9 +359,6 @@ export const themeColors = { elevated: { backgroundColor: 'theme.colors.elevation[elevation]', }, - 'with container': { - backgroundColor: 'theme.colors[container]', - }, }, Text: { '-': { diff --git a/example/src/Examples/SurfaceExample.tsx b/example/src/Examples/SurfaceExample.tsx index a9c8923918..19b2888fe5 100644 --- a/example/src/Examples/SurfaceExample.tsx +++ b/example/src/Examples/SurfaceExample.tsx @@ -1,33 +1,13 @@ import * as React from 'react'; import { ScrollView, StyleSheet, View } from 'react-native'; -import { - Surface, - Text, - Palette, - List, - IconButton, - useTheme, -} from 'react-native-paper'; -import type { Elevation, SurfaceRole } from 'react-native-paper'; +import { Surface, Text, Palette, List, IconButton } from 'react-native-paper'; +import type { Elevation } from 'react-native-paper'; import ScreenWrapper from '../ScreenWrapper'; const elevationLevels: Elevation[] = [0, 1, 2, 3, 4, 5]; -const containerRoles: SurfaceRole[] = [ - 'surface', - 'surfaceDim', - 'surfaceBright', - 'surfaceVariant', - 'surfaceContainerLowest', - 'surfaceContainerLow', - 'surfaceContainer', - 'surfaceContainerHigh', - 'surfaceContainerHighest', - 'inverseSurface', -]; - const AnimatedSurface = () => { const [index, setIndex] = React.useState(3); @@ -57,20 +37,8 @@ const AnimatedSurface = () => { }; const SurfaceExample = () => { - const theme = useTheme(); - const elevationValues: Elevation[] = [0, 1, 2, 3, 4, 5]; - const onColorFor = (role: SurfaceRole) => { - if (role === 'inverseSurface') { - return theme.colors.inverseOnSurface; - } - if (role === 'surfaceVariant') { - return theme.colors.onSurfaceVariant; - } - return theme.colors.onSurface; - }; - const renderSurface = (index: Elevation, mode: 'flat' | 'elevated') => ( { - - - {containerRoles.map((role) => ( - - - {role} - - - ))} - - - @@ -186,10 +125,6 @@ const styles = StyleSheet.create({ alignItems: 'center', justifyContent: 'center', }, - roleSurface: { - width: 180, - borderWidth: StyleSheet.hairlineWidth, - }, actions: { flexDirection: 'row', gap: 16, diff --git a/src/components/Banner.tsx b/src/components/Banner.tsx index 0971b9cc27..07c0614cdd 100644 --- a/src/components/Banner.tsx +++ b/src/components/Banner.tsx @@ -212,7 +212,7 @@ const Banner = ({ {...rest} style={[surfaceStyle, style]} theme={theme} - container="surfaceContainerLow" + backgroundColor={colors.surfaceContainerLow} elevation={elevation} > diff --git a/src/components/Menu/Menu.tsx b/src/components/Menu/Menu.tsx index 0318b0c6ec..1e6fcd037e 100644 --- a/src/components/Menu/Menu.tsx +++ b/src/components/Menu/Menu.tsx @@ -699,7 +699,7 @@ const Menu = ({ > = Extract< AnimatedStyle>>, @@ -24,8 +24,8 @@ type BorderRadius = AnimatedStyleProp<'borderRadius'>; export type SurfaceVisualProps = { /** - * Background color of the Surface. Overrides both `container` and the - * color derived from `elevation`. + * Background color of the Surface. Overrides the color derived from + * `elevation`. */ backgroundColor?: ColorValue; /** @@ -122,21 +122,6 @@ export type Props = Omit & * To achieve the same effect, wrap the content in a child View with the overflow style. */ style?: StyleProp; - /** - * Semantic color role of the Surface background, resolved from the - * theme, e.g. `container="surfaceContainerLow"` renders with - * `theme.colors.surfaceContainerLow`. Accepted roles: `surface`, - * `surfaceDim`, `surfaceBright`, `surfaceVariant`, - * `surfaceContainerLowest`, `surfaceContainerLow`, `surfaceContainer`, - * `surfaceContainerHigh`, `surfaceContainerHighest` and `inverseSurface`. - * - * When `container` is set, `elevation` only controls the shadow (and - * z-order on Android) and no longer affects the background color. - * - * Precedence: `backgroundColor` > `container` > - * `theme.colors.elevation[levelN]` derived from `elevation`. - */ - container?: SurfaceRole; /** * @supported Available in v5.x with theme version 3 * Changes shadows and background on iOS and Android. @@ -144,9 +129,6 @@ export type Props = Omit & * * Note: If `mode` is set to `flat`, Surface doesn't have a shadow. * - * Note: If `backgroundColor` or `container` is set, `elevation` only - * affects the shadow. - * * Note: In version 2 the `elevation` prop was accepted via `style` prop i.e. `style={{ elevation: 4 }}`. * It's no longer supported with theme version 3 and you should use `elevation` property instead. */ @@ -154,8 +136,8 @@ export type Props = Omit & /** * @supported Available in v5.x with theme version 3 * Mode of the Surface. - * - `elevated` - Surface with a shadow and background color corresponding to set `elevation` value (unless `backgroundColor` or `container` is set). - * - `flat` - Surface without a shadow, with the background color corresponding to set `elevation` value (unless `backgroundColor` or `container` is set). + * - `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. */ mode?: 'flat' | 'elevated'; /** @@ -186,19 +168,9 @@ export type Props = Omit & * import { StyleSheet } from 'react-native'; * * const MyComponent = () => ( - * <> - * - * Surface - * - * - * Semantic container color - * - * + * + * Surface + * * ); * * export default MyComponent; @@ -216,7 +188,6 @@ export type Props = Omit & */ const Surface = ({ elevation = 1, - container, children, theme: overriddenTheme, style, @@ -249,8 +220,7 @@ const Surface = ({ const { colors } = theme; const backgroundColor = - customBackgroundColor ?? - (container ? colors[container] : colors.elevation?.[`level${elevation}`]); + customBackgroundColor ?? colors.elevation?.[`level${elevation}`]; const backgroundStyle = { backgroundColor }; diff --git a/src/components/__tests__/Surface.test.tsx b/src/components/__tests__/Surface.test.tsx index 9fd42592b5..cb568ad767 100644 --- a/src/components/__tests__/Surface.test.tsx +++ b/src/components/__tests__/Surface.test.tsx @@ -194,142 +194,6 @@ describe('Surface', () => { transitionProperty: expect.not.arrayContaining(['backgroundColor']), }); }); - - it('resolves the container role from the theme', async () => { - await render( - - {null} - - ); - - expect(screen.getByTestId('surface-test')).toHaveStyle({ - backgroundColor: LightTheme.colors.surfaceContainerHigh, - }); - }); - - it('prefers backgroundColor over container', async () => { - const backgroundColor = 'rgba(1, 2, 3, 0.5)'; - await render( - - {null} - - ); - - expect(screen.getByTestId('surface-test')).toHaveStyle({ - backgroundColor, - }); - }); - - it('prefers container over the elevation-derived color', async () => { - await render( - - {null} - - ); - - expect(screen.getByTestId('surface-test')).toHaveStyle({ - backgroundColor: LightTheme.colors.surfaceContainerLowest, - }); - }); - - it('keeps the elevation-driven shadow when container is set', async () => { - await render( - - {null} - - ); - - expect(screen.getByTestId('surface-test')).toHaveStyle({ - shadowOpacity: SPOT_SHADOW_OPACITY, - }); - }); - - it('does not render a shadow in flat mode when container is set', async () => { - await render( - - {null} - - ); - - // @ts-expect-error - expect(screen.getByTestId('surface-test')).not.toHaveStyle({ - shadowOpacity: expect.any(Number), - }); - }); - - it('keeps the container background when elevation changes', async () => { - await render( - - {null} - - ); - - await screen.rerender( - - {null} - - ); - await jest.runAllTimersAsync(); - - expect(screen.getByTestId('surface-test')).toHaveStyle({ - backgroundColor: LightTheme.colors.surfaceContainer, - }); - }); - - it('does not transition a DynamicColorIOS container role', async () => { - await render( - - {null} - - ); - - expect( - getAnimatedStyle(screen.getByTestId('surface-test')) - ).toMatchObject({ - transitionProperty: expect.not.arrayContaining(['backgroundColor']), - }); - }); }); describe('on Android', () => { @@ -365,38 +229,6 @@ describe('Surface', () => { }); }); - it('resolves the container role from the theme', async () => { - await render( - - {null} - - ); - - expect(screen.getByTestId('surface-container')).toHaveStyle({ - backgroundColor: LightTheme.colors.surfaceContainerHigh, - }); - }); - - it('keeps the elevation-driven dp value when container is set', async () => { - await render( - - {null} - - ); - - expect(screen.getByTestId('surface-container')).toHaveStyle({ - elevation: 12, - }); - }); - it('does not transition a PlatformColor background', async () => { await render( ; - export type ThemeColors = { primary: ColorValue; primaryContainer: ColorValue; From d4d99d0516715ed4d8ad4a7093a9d2f4312b8c7a Mon Sep 17 00:00:00 2001 From: lukewalczak Date: Mon, 23 Nov 2026 12:20:20 +0100 Subject: [PATCH 4/4] feat(menu, banner): add backgroundColor prop BREAKING CHANGE: Menu and Banner backgrounds no longer follow `elevation`. Menu uses `theme.colors.surfaceContainer` and Banner `theme.colors.surfaceContainerLow`; customize those roles or pass `backgroundColor` instead of `theme.colors.elevation.level2` and `level1`. --- docs/6.x/docs/guides/migration.md | 36 +++++++++++++++++++++--- docs/6.x/docs/guides/theming.mdx | 2 +- src/components/Banner.tsx | 8 +++++- src/components/Menu/Menu.tsx | 10 ++++++- src/components/__tests__/Banner.test.tsx | 15 +++++++++- src/components/__tests__/Menu.test.tsx | 26 ++++++++++++++++- 6 files changed, 88 insertions(+), 9 deletions(-) diff --git a/docs/6.x/docs/guides/migration.md b/docs/6.x/docs/guides/migration.md index 63cf76fdbb..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). @@ -560,11 +562,21 @@ e.g.: ### Banner -The banner background is now always `theme.colors.surfaceContainerLow` 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. With `DynamicLightTheme` and `DynamicDarkTheme` on Android 12 and newer, the banner now uses the system surface color. +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. -### Menu - -The menu background is now always `theme.colors.surfaceContainer`, 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. With `DynamicLightTheme` and `DynamicDarkTheme` on Android 12 and newer, the menu now uses the system surface color. +```diff +const theme = { + ...LightTheme, + colors: { + ...LightTheme.colors, +- elevation: { +- ...LightTheme.colors.elevation, +- level1: '#ffd8e4', +- }, ++ surfaceContainerLow: '#ffd8e4', + }, +}; +``` ### Surface @@ -895,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 29eaa6bd58..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. 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. + > 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/src/components/Banner.tsx b/src/components/Banner.tsx index 07c0614cdd..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, @@ -71,6 +72,10 @@ export type Props = Omit & { * `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. */ @@ -144,6 +149,7 @@ const Banner = ({ actions = [], contentStyle, elevation = 1, + backgroundColor, style, theme: themeOverrides, onShowAnimationFinished = () => {}, @@ -212,7 +218,7 @@ const Banner = ({ {...rest} style={[surfaceStyle, style]} theme={theme} - backgroundColor={colors.surfaceContainerLow} + backgroundColor={backgroundColor ?? colors.surfaceContainerLow} elevation={elevation} > diff --git a/src/components/Menu/Menu.tsx b/src/components/Menu/Menu.tsx index 1e6fcd037e..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, @@ -87,6 +88,10 @@ export type Props = { * @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 corresponding to set `elevation` value. @@ -203,6 +208,7 @@ const Menu = ({ contentStyle, style, elevation = DEFAULT_ELEVATION, + backgroundColor, mode = DEFAULT_MODE, children, theme: themeOverrides, @@ -699,7 +705,9 @@ const Menu = ({ > { @@ -120,7 +121,7 @@ it('renders visible banner, with action buttons and with image', async () => { expect(tree).toMatchSnapshot(); }); -it('renders banner with surfaceContainerLow background', async () => { +it('renders banner on the theme surface container background', async () => { await render( Text @@ -132,6 +133,18 @@ it('renders banner with surfaceContainerLow background', async () => { }); }); +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 418e316ac1..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 surfaceContainer background regardless of elevation value = ${elevation}`, async () => { + it(`keeps the same menu background at elevation ${elevation}`, async () => { const testID = 'menu-with-elevation'; await render( @@ -197,6 +198,29 @@ elevations.forEach((elevation) => }) ); +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({