diff --git a/src/components/FAB/Shell.tsx b/src/components/FAB/Shell.tsx index 01fe53496e..18e4fc7256 100644 --- a/src/components/FAB/Shell.tsx +++ b/src/components/FAB/Shell.tsx @@ -20,7 +20,7 @@ import type { AnimatedStyle } from 'react-native-reanimated'; import Content from './Content'; import { Tokens } from './tokens'; import type { Size, Variant } from './tokens'; -import { getDimensions, resolveColors } from './utils'; +import { getDimensions, resolveColors, resolveVariant } from './utils'; import { useInternalTheme } from '../../core/theming'; import { useReduceMotion } from '../../theme/accessibility/ReduceMotionContext'; import { toRawSpring } from '../../theme/tokens/sys/motion'; @@ -257,6 +257,18 @@ const Shell = ({ [theme, variant, containerColor, contentColor] ); + React.useEffect(() => { + if ( + process.env.NODE_ENV === 'production' || + resolveVariant(variant) === variant + ) { + return; + } + console.warn( + `FAB: variant "${variant}" is not valid. Falling back to the default "primaryContainer" variant.` + ); + }, [variant]); + const reduceMotion = useReduceMotion(); const scale = useSharedValue(visible ? 1 : 0); diff --git a/src/components/FAB/utils.ts b/src/components/FAB/utils.ts index 9c1a0eb72d..f91d7c0cfc 100644 --- a/src/components/FAB/utils.ts +++ b/src/components/FAB/utils.ts @@ -12,6 +12,13 @@ export type ResolvedColors = { content: ColorValue; }; +// A removed 6.0-alpha variant (`tonalPrimary`, `surface`, ...), or any other +// value that isn't a current token key, used to crash here instead of +// falling back. Pure: the component warns, in an effect, when this changes +// the value. +export const resolveVariant = (variant: Variant): Variant => + Tokens.variants[variant] ? variant : 'primaryContainer'; + /** * Resolve container + content colors. Explicit overrides win; when only * `containerColor` is set, the content color is derived via @@ -28,7 +35,7 @@ export const resolveColors = ({ containerColor?: ColorValue; contentColor?: ColorValue; }): ResolvedColors => { - const roles = Tokens.variants[variant]; + const roles = Tokens.variants[resolveVariant(variant)]; const container = containerColor ?? theme.colors[roles.container]; const content = contentColor ?? diff --git a/src/components/IconButton/IconButton.tsx b/src/components/IconButton/IconButton.tsx index a3553503c1..82e2095c68 100644 --- a/src/components/IconButton/IconButton.tsx +++ b/src/components/IconButton/IconButton.tsx @@ -12,7 +12,12 @@ import type { import Animated, { type AnimatedStyle } from 'react-native-reanimated'; import type { Mode, Shape, Size, Width } from './tokens'; -import { getDimensions, getIconButtonColor } from './utils'; +import { + getDimensions, + getIconButtonColor, + resolveMode, + resolveSize, +} from './utils'; import { useInternalTheme } from '../../core/theming'; import { useReduceMotion } from '../../theme/accessibility/ReduceMotionContext'; import type { ThemeProp } from '../../theme/types'; @@ -187,6 +192,24 @@ const IconButton = ({ const IconComponent = animated ? CrossFadeIcon : Icon; + React.useEffect(() => { + if (process.env.NODE_ENV === 'production' || resolveMode(mode) === mode) { + return; + } + console.warn( + `IconButton: mode "${mode}" is not valid. Falling back to the default "standard" mode.` + ); + }, [mode]); + + React.useEffect(() => { + if (process.env.NODE_ENV === 'production' || resolveSize(size) === size) { + return; + } + console.warn( + `IconButton: size "${size}" is not valid. Falling back to the default "small" size.` + ); + }, [size]); + const dimensions = getDimensions({ theme, size, diff --git a/src/components/IconButton/utils.ts b/src/components/IconButton/utils.ts index 5ba3cd6839..a32886c272 100644 --- a/src/components/IconButton/utils.ts +++ b/src/components/IconButton/utils.ts @@ -27,6 +27,12 @@ export type IconButtonDimensions = { pressedRadius: number; }; +// 5.x's `contained` / `contained-tonal` modes, or any other value that isn't +// a current token key, used to crash here instead of falling back. Pure: the +// component warns, in an effect, when this changes the value. +export const resolveMode = (mode: Mode): Mode => + IconButtonTokens.modes[mode] ? mode : 'standard'; + const colorSetFor = (mode: Mode, selected: boolean | undefined): ColorSet => { if (selected === true) { return IconButtonTokens.modes[mode].selected; @@ -73,9 +79,10 @@ export const getIconButtonColor = ({ customContainerColor?: ColorValue; outlineWidth?: number; }): IconButtonColors => { + const resolvedMode = resolveMode(mode); const iconOpacity = disabled ? stateOpacity.disabled : stateOpacity.enabled; const usesDisabledContainer = - Boolean(disabled) && hasDisabledContainer(mode, selected); + Boolean(disabled) && hasDisabledContainer(resolvedMode, selected); if (usesDisabledContainer) { return { @@ -88,7 +95,7 @@ export const getIconButtonColor = ({ }; } - const set = colorSetFor(mode, selected); + const set = colorSetFor(resolvedMode, selected); return { iconColor: disabled @@ -130,6 +137,12 @@ export const resolveShapeToken = ({ return inverted ? 'full' : spec.squareShape; }; +// A numeric 5.x `size`, or any other value that isn't a current token key, +// used to crash here instead of falling back. Pure: the component warns, in +// an effect, when this changes the value. +export const resolveSize = (size: Size): Size => + IconButtonTokens.sizes[size] ? size : 'small'; + export const getDimensions = ({ theme, size = 'small', @@ -145,7 +158,8 @@ export const getDimensions = ({ selected?: boolean; iconSize?: number; }): IconButtonDimensions => { - const spec = IconButtonTokens.sizes[size]; + const resolvedSize = resolveSize(size); + const spec = IconButtonTokens.sizes[resolvedSize]; const padding = spec.widths[width]; const resolvedIcon = iconSize ?? spec.icon; const height = spec.containerHeight; @@ -154,13 +168,13 @@ export const getDimensions = ({ const containerWidth = spec.icon + padding.leading + padding.trailing; const restingToken = resolveShapeToken({ - size, + size: resolvedSize, shape, selected, pressed: false, }); const pressedToken = resolveShapeToken({ - size, + size: resolvedSize, shape, selected, pressed: true, diff --git a/src/components/__tests__/FAB.test.tsx b/src/components/__tests__/FAB.test.tsx index 8eb91a1efa..6704d1929e 100644 --- a/src/components/__tests__/FAB.test.tsx +++ b/src/components/__tests__/FAB.test.tsx @@ -17,6 +17,7 @@ import { getAnimatedStyle } from 'react-native-reanimated'; import PaperProvider from '../../core/PaperProvider'; import { act, render, screen } from '../../test-utils'; import FAB from '../FAB'; +import type { Variant } from '../FAB/tokens'; import Tooltip from '../Tooltip/Tooltip'; jest.mock('react-native-reanimated', () => { @@ -70,6 +71,22 @@ it('renders FAB with tertiaryContainer variant', async () => { expect(tree).toMatchSnapshot(); }); +it('falls back instead of crashing on a removed variant value', async () => { + const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {}); + + await render( + {}} + /> + ); + + expect(warnSpy).toHaveBeenCalledWith( + expect.stringContaining('variant "tonalPrimary" is not valid') + ); +}); + it('renders FAB with aria-label', async () => { const tree = ( await render() diff --git a/src/components/__tests__/FABUtils.test.tsx b/src/components/__tests__/FABUtils.test.tsx index d5bb8b87f0..14c16ba588 100644 --- a/src/components/__tests__/FABUtils.test.tsx +++ b/src/components/__tests__/FABUtils.test.tsx @@ -1,6 +1,7 @@ import { describe, expect, it } from '@jest/globals'; import { DarkTheme, LightTheme } from '../../theme/schemes'; +import type { Variant } from '../FAB/tokens'; import { getDimensions, resolveColors } from '../FAB/utils'; describe('resolveColors', () => { @@ -126,6 +127,19 @@ describe('resolveColors', () => { expect(colors.container).toBe(LightTheme.colors.primary); expect(colors.content).toBe('#custom'); }); + + describe('removed 5.x / 6.0-alpha variant values', () => { + it('falls back instead of crashing on a removed variant value', () => { + expect( + resolveColors({ + theme: LightTheme, + variant: 'tonalPrimary' as unknown as Variant, + }) + ).toEqual( + resolveColors({ theme: LightTheme, variant: 'primaryContainer' }) + ); + }); + }); }); describe('getDimensions', () => { diff --git a/src/components/__tests__/IconButton.test.tsx b/src/components/__tests__/IconButton.test.tsx index 574dbb8598..b5127fa5a3 100644 --- a/src/components/__tests__/IconButton.test.tsx +++ b/src/components/__tests__/IconButton.test.tsx @@ -25,6 +25,7 @@ import { tokens } from '../../theme/tokens'; import { toSpringCurve } from '../../theme/tokens/sys/motion'; import IconButton from '../IconButton/IconButton'; import { IconButtonTokens } from '../IconButton/tokens'; +import type { Mode, Size } from '../IconButton/tokens'; import { getDimensions, getIconButtonColor, @@ -305,6 +306,50 @@ it('keeps handler-less content centered with custom container style', async () = }); }); +describe('removed 5.x prop values', () => { + afterEach(() => { + jest.restoreAllMocks(); + }); + + it('falls back instead of crashing on a removed mode value', async () => { + const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {}); + + await render( + {}} + /> + ); + + expect( + getIconButtonColor({ theme, mode: 'contained-tonal' as unknown as Mode }) + ).toEqual(getIconButtonColor({ theme, mode: 'standard' })); + expect(warnSpy).toHaveBeenCalledWith( + expect.stringContaining('mode "contained-tonal" is not valid') + ); + }); + + it('falls back instead of crashing on a numeric size value', async () => { + const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {}); + + await render( + {}} + /> + ); + + expect(getDimensions({ theme, size: 24 as unknown as Size })).toEqual( + getDimensions({ theme, size: 'small' }) + ); + expect(warnSpy).toHaveBeenCalledWith( + expect.stringContaining('size "24" is not valid') + ); + }); +}); + describe('getIconButtonColor - icon color', () => { it('should return custom icon color', () => { expect(