Skip to content
Open
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
14 changes: 13 additions & 1 deletion src/components/FAB/Shell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
Expand Down
9 changes: 8 additions & 1 deletion src/components/FAB/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 ??
Expand Down
25 changes: 24 additions & 1 deletion src/components/IconButton/IconButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
Expand Down
24 changes: 19 additions & 5 deletions src/components/IconButton/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand All @@ -88,7 +95,7 @@ export const getIconButtonColor = ({
};
}

const set = colorSetFor(mode, selected);
const set = colorSetFor(resolvedMode, selected);

return {
iconColor: disabled
Expand Down Expand Up @@ -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',
Expand All @@ -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;
Expand All @@ -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,
Expand Down
17 changes: 17 additions & 0 deletions src/components/__tests__/FAB.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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(
<FAB
icon="plus"
variant={'tonalPrimary' as unknown as Variant}
onPress={() => {}}
/>
);

expect(warnSpy).toHaveBeenCalledWith(
expect.stringContaining('variant "tonalPrimary" is not valid')
);
});

it('renders FAB with aria-label', async () => {
const tree = (
await render(<FAB icon="plus" aria-label="Add item" />)
Expand Down
14 changes: 14 additions & 0 deletions src/components/__tests__/FABUtils.test.tsx
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -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', () => {
Expand Down
45 changes: 45 additions & 0 deletions src/components/__tests__/IconButton.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(
<IconButton
icon="camera"
mode={'contained-tonal' as unknown as Mode}
onPress={() => {}}
/>
);

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(
<IconButton
icon="camera"
size={24 as unknown as Size}
onPress={() => {}}
/>
);

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(
Expand Down
Loading