diff --git a/docs/6.x/docs/guides/migration.md b/docs/6.x/docs/guides/migration.md index 30cbc45a6a..9e6c193d0d 100644 --- a/docs/6.x/docs/guides/migration.md +++ b/docs/6.x/docs/guides/migration.md @@ -65,6 +65,7 @@ The following component style props no longer support overriding their backgroun - `Card` - `Chip` - `Dialog` +- `IconButton` - `Menu`: `contentStyle` - `Searchbar` - `Snackbar` @@ -693,10 +694,6 @@ The misspelled `traileringIcon` props have been renamed: The close button (`onClose`) now fills the trailing 34dp column reserved for it, instead of only the 26x18 area around its icon, and its touch target extends to 48dp in height. Taps in that column that used to activate the chip's `onPress` now activate `onClose`. -### IconButton - -Corner radii set through `style` no longer shape the press, hover and disabled feedback. - ### TouchableRipple `borderless` has no effect on web. The web ripple is always clipped to the touchable's shape, including when `centered` is set, and the touchable no longer clips its own content. A child that needs a clipped or rounded shape should carry that shape itself. @@ -1151,6 +1148,137 @@ An accordion with a `left` element now indents only its `List.Item` children. An `theme` set on `List.Accordion` no longer reaches its children either. Pass it to the child that needs the override. +### IconButton + +`IconButton` now follows the Material 3 Expressive spec. The color modes, size API, and toggle `selected` behavior have changed. Passing the old numeric `size` (for example `size={24}`) crashes at runtime, because `size` is now a named token and `IconButtonTokens.sizes[24]` is `undefined`. + +#### Mode names + +- **`contained`** → **`filled`** +- **`contained-tonal`** → **`tonal`** +- Unspecified mode is now **`standard`** (no container fill), which matches the previous default. + +```tsx +// Before (v5) + + + +// After (v6) + + +``` + +#### Size + +`size` is a named scale: `'extraSmall' | 'small' | 'medium' | 'large' | 'extraLarge'`. It defaults to `'small'`, which still uses a 24dp icon. + +To set the glyph size in dp without changing the container scale, use `iconSize`. + +```tsx +// Before (v5) + + + +// After (v6) + + +``` + +```tsx +// After (v6) — named container sizes + + + + + +``` + +#### Shape + +`style.borderRadius` no longer overrides the container corner or shapes the press, hover and disabled feedback. Use `shape`: `'round'` (default) or `'square'`. Toggle-ON inverts the resting shape; press uses a shared corner for both. Arbitrary radii from `style` are not supported. + +```tsx +// Before (v5) + + +// After (v6) + +``` + +#### Touch target + +An `IconButton` with a touch handler reaches the 48dp minimum target through hit slop, without changing what is drawn. This applies to `extraSmall` and `small`, whose containers are smaller than 48dp. Pass `hitSlop` to override it, or `hitSlop={0}` to opt out. A disabled button, or one with no touch handler, gets no extra target. + +#### `Appbar.Action` and `Appbar.BackAction` + +`size` is no longer a numeric prop on `Appbar.Action` or `Appbar.BackAction`. They inherit `IconButton`'s named `size` (default `'small'`). Drop `size={24}`. For a custom glyph size, pass `iconSize`. + +```tsx +// Before (v5) + + + +// After (v6) + + +``` + +#### `selected={false}` uses toggle-off colors + +`selected` is tri-state. Omit it for the default (non-toggle) colors. Pass `true` / `false` only for a toggle. + +For `filled`, the default and toggle-on containers use `primary`. Toggle-off (`selected={false}`) uses `surfaceContainer` instead of `primary`. + +```tsx +// Before (v5) — selected={false} still used the default (non-toggle) colors + + +// After (v6) — omit selected unless the button is a toggle + + +// After (v6) — explicit toggle off / on + +``` + +#### `Card.Title` `right` props + +`Card.Title` still calls `right({ size: 24 })`. Do not spread those props onto `IconButton`: `size` is no longer a number. Ignore the callback argument, or pass it as `iconSize`. + +```tsx +// Before (v5) + ( + + )} +/> + +// After (v6) + } +/> +``` + +```tsx +// After (v6) — keep the 24dp glyph from Card.Title + ( + + )} +/> +``` + ### Typography components The Material Design 2 typography components were removed. Use `Text` with the matching `variant` instead: diff --git a/docs/public/screenshots/icon-button-1.png b/docs/public/screenshots/icon-button-1.png deleted file mode 100644 index acfa6e7097..0000000000 Binary files a/docs/public/screenshots/icon-button-1.png and /dev/null differ diff --git a/docs/public/screenshots/icon-button-2.png b/docs/public/screenshots/icon-button-2.png deleted file mode 100644 index d555823405..0000000000 Binary files a/docs/public/screenshots/icon-button-2.png and /dev/null differ diff --git a/docs/public/screenshots/icon-button-3.png b/docs/public/screenshots/icon-button-3.png deleted file mode 100644 index 367f7d0c3c..0000000000 Binary files a/docs/public/screenshots/icon-button-3.png and /dev/null differ diff --git a/docs/public/screenshots/icon-button-4.png b/docs/public/screenshots/icon-button-4.png deleted file mode 100644 index 57c723ade5..0000000000 Binary files a/docs/public/screenshots/icon-button-4.png and /dev/null differ diff --git a/docs/public/screenshots/icon-button-filled.android.png b/docs/public/screenshots/icon-button-filled.android.png new file mode 100644 index 0000000000..155d794a81 Binary files /dev/null and b/docs/public/screenshots/icon-button-filled.android.png differ diff --git a/docs/public/screenshots/icon-button-filled.ios.png b/docs/public/screenshots/icon-button-filled.ios.png new file mode 100644 index 0000000000..77bffeab12 Binary files /dev/null and b/docs/public/screenshots/icon-button-filled.ios.png differ diff --git a/docs/public/screenshots/icon-button-outlined.android.png b/docs/public/screenshots/icon-button-outlined.android.png new file mode 100644 index 0000000000..ea80b50dea Binary files /dev/null and b/docs/public/screenshots/icon-button-outlined.android.png differ diff --git a/docs/public/screenshots/icon-button-outlined.ios.png b/docs/public/screenshots/icon-button-outlined.ios.png new file mode 100644 index 0000000000..ff6e66f7f4 Binary files /dev/null and b/docs/public/screenshots/icon-button-outlined.ios.png differ diff --git a/docs/public/screenshots/icon-button-standard.android.png b/docs/public/screenshots/icon-button-standard.android.png new file mode 100644 index 0000000000..24632d599d Binary files /dev/null and b/docs/public/screenshots/icon-button-standard.android.png differ diff --git a/docs/public/screenshots/icon-button-standard.ios.png b/docs/public/screenshots/icon-button-standard.ios.png new file mode 100644 index 0000000000..889a007181 Binary files /dev/null and b/docs/public/screenshots/icon-button-standard.ios.png differ diff --git a/docs/public/screenshots/icon-button-tonal.android.png b/docs/public/screenshots/icon-button-tonal.android.png new file mode 100644 index 0000000000..4345feef9c Binary files /dev/null and b/docs/public/screenshots/icon-button-tonal.android.png differ diff --git a/docs/public/screenshots/icon-button-tonal.ios.png b/docs/public/screenshots/icon-button-tonal.ios.png new file mode 100644 index 0000000000..0ad4bdce8c Binary files /dev/null and b/docs/public/screenshots/icon-button-tonal.ios.png differ diff --git a/docs/src/components/ScreenshotTabs.tsx b/docs/src/components/ScreenshotTabs.tsx index ee45872920..124539a3cd 100644 --- a/docs/src/components/ScreenshotTabs.tsx +++ b/docs/src/components/ScreenshotTabs.tsx @@ -26,7 +26,11 @@ const ScreenshotTabs = ({ screenshotData }: ScreenshotTabsProps) => { const screenshots = Object.entries(screenshotData).map(([key, value]) => ( - {typeof value === 'string' ? renderScreenshot(value) : null} + {typeof value === 'string' ? ( + renderScreenshot(value) + ) : ( + + )} )); diff --git a/docs/src/data/screenshots.ts b/docs/src/data/screenshots.ts index 50abc5bf10..df5abcd807 100644 --- a/docs/src/data/screenshots.ts +++ b/docs/src/data/screenshots.ts @@ -81,10 +81,22 @@ export const screenshots = { 'FAB.Group': 'screenshots/fab-group.gif', Icon: 'screenshots/icon.png', IconButton: { - default: 'screenshots/icon-button-1.png', - outlined: 'screenshots/icon-button-4.png', - contained: 'screenshots/icon-button-2.png', - 'contained-tonal': 'screenshots/icon-button-3.png', + standard: { + Android: 'screenshots/icon-button-standard.android.png', + iOS: 'screenshots/icon-button-standard.ios.png', + }, + filled: { + Android: 'screenshots/icon-button-filled.android.png', + iOS: 'screenshots/icon-button-filled.ios.png', + }, + tonal: { + Android: 'screenshots/icon-button-tonal.android.png', + iOS: 'screenshots/icon-button-tonal.ios.png', + }, + outlined: { + Android: 'screenshots/icon-button-outlined.android.png', + iOS: 'screenshots/icon-button-outlined.ios.png', + }, }, 'List.Accordion': { 'with left icon': 'screenshots/list-accordion-1.png', diff --git a/docs/src/data/themeColors.ts b/docs/src/data/themeColors.ts index cac194c50d..3969022508 100644 --- a/docs/src/data/themeColors.ts +++ b/docs/src/data/themeColors.ts @@ -227,56 +227,72 @@ export const themeColors = { }, }, IconButton: { + default: { + standard: { + iconColor: 'theme.colors.onSurfaceVariant', + }, + outlined: { + iconColor: 'theme.colors.onSurfaceVariant', + borderColor: 'theme.colors.outlineVariant', + }, + filled: { + backgroundColor: 'theme.colors.primary', + iconColor: 'theme.colors.onPrimary', + }, + tonal: { + backgroundColor: 'theme.colors.secondaryContainer', + iconColor: 'theme.colors.onSecondaryContainer', + }, + }, selected: { - default: { + standard: { iconColor: 'theme.colors.primary', }, outlined: { backgroundColor: 'theme.colors.inverseSurface', iconColor: 'theme.colors.inverseOnSurface', }, - contained: { + filled: { backgroundColor: 'theme.colors.primary', iconColor: 'theme.colors.onPrimary', }, - 'contained-tonal': { - backgroundColor: 'theme.colors.secondaryContainer', - iconColor: 'theme.colors.onSecondaryContainer', + tonal: { + backgroundColor: 'theme.colors.secondary', + iconColor: 'theme.colors.onSecondary', }, }, unselected: { - default: { + standard: { iconColor: 'theme.colors.onSurfaceVariant', }, outlined: { iconColor: 'theme.colors.onSurfaceVariant', - borderColor: 'theme.colors.outline', - }, - contained: { - backgroundColor: 'theme.colors.surfaceVariant', - iconColor: 'theme.colors.primary', + borderColor: 'theme.colors.outlineVariant', }, - 'contained-tonal': { - backgroundColor: 'theme.colors.surfaceVariant', + filled: { + backgroundColor: 'theme.colors.surfaceContainer', iconColor: 'theme.colors.onSurfaceVariant', }, + tonal: { + backgroundColor: 'theme.colors.secondaryContainer', + iconColor: 'theme.colors.onSecondaryContainer', + }, }, disabled: { - default: { - iconColor: 'theme.colors.onSurfaceDisabled', + standard: { + iconColor: 'theme.colors.onSurface', }, outlined: { - backgroundColor: 'theme.colors.surfaceDisabled', - iconColor: 'theme.colors.onSurfaceDisabled', - borderColor: 'theme.colors.surfaceDisabled', + iconColor: 'theme.colors.onSurface', + borderColor: 'theme.colors.outlineVariant', }, - contained: { - backgroundColor: 'theme.colors.surfaceDisabled', - iconColor: 'theme.colors.onSurfaceDisabled', + filled: { + backgroundColor: 'theme.colors.onSurface', + iconColor: 'theme.colors.onSurface', }, - 'contained-tonal': { - backgroundColor: 'theme.colors.surfaceDisabled', - iconColor: 'theme.colors.onSurfaceDisabled', + tonal: { + backgroundColor: 'theme.colors.onSurface', + iconColor: 'theme.colors.onSurface', }, }, }, diff --git a/example/src/Examples/BadgeExample.tsx b/example/src/Examples/BadgeExample.tsx index 7ca1a43f9c..41fabfc9ed 100644 --- a/example/src/Examples/BadgeExample.tsx +++ b/example/src/Examples/BadgeExample.tsx @@ -29,13 +29,17 @@ const BadgeExample = () => { - + 12 - + { - + - + diff --git a/example/src/Examples/CardExample.tsx b/example/src/Examples/CardExample.tsx index 09b732d9d0..69c85841a6 100644 --- a/example/src/Examples/CardExample.tsx +++ b/example/src/Examples/CardExample.tsx @@ -87,9 +87,7 @@ const CardExample = () => { title="Berries that are trimmed at the end" subtitle="Omega Ruby" left={(props: any) => } - right={(props: any) => ( - {}} /> - )} + right={() => {}} />} /> @@ -146,10 +144,10 @@ const CardExample = () => { ( + right={() => ( setIsSelected(!isSelected)} /> )} diff --git a/example/src/Examples/IconButtonExample.tsx b/example/src/Examples/IconButtonExample.tsx index 5fb463880b..dee40ab2f8 100644 --- a/example/src/Examples/IconButtonExample.tsx +++ b/example/src/Examples/IconButtonExample.tsx @@ -1,203 +1,298 @@ -import { StyleSheet, View } from 'react-native'; +import * as React from 'react'; +import { ScrollView, StyleSheet, View } from 'react-native'; -import { IconButton, List, Palette } from 'react-native-paper'; +import { + Chip, + IconButton, + List, + Palette, + Text, + useTheme, +} from 'react-native-paper'; +import type { + IconButtonMode, + IconButtonShape, + IconButtonSize, + IconButtonWidth, +} from 'react-native-paper'; import ScreenWrapper from '../ScreenWrapper'; -const ButtonExample = () => { - return ( - - - - {}} /> - {}} /> - {}} /> - {}} loading /> - - +const modes: IconButtonMode[] = ['standard', 'filled', 'tonal', 'outlined']; +const sizes: IconButtonSize[] = [ + 'extraSmall', + 'small', + 'medium', + 'large', + 'extraLarge', +]; +const widths: IconButtonWidth[] = ['narrow', 'default', 'wide']; +const shapes: IconButtonShape[] = ['round', 'square']; - - - {}} - /> +type ChipRowProps = { + label: string; + options: readonly T[]; + value: T; + onChange: (value: T) => void; +}; + +const ChipRow = ({ + label, + options, + value, + onChange, +}: ChipRowProps) => ( + + + {label} + + + {options.map((option) => ( + onChange(option)} + > + {option} + + ))} + + +); + +const IconButtonExample = () => { + const { colors } = useTheme(); + const [mode, setMode] = React.useState('filled'); + const [size, setSize] = React.useState('small'); + const [width, setWidth] = React.useState('default'); + const [shape, setShape] = React.useState('round'); + const [selected, setSelected] = React.useState(false); + const [toggles, setToggles] = React.useState>({}); + + const toggle = (key: string) => { + setToggles((current) => ({ ...current, [key]: !current[key] })); + }; + + return ( + + + + + + + {}} + icon={selected ? 'camera' : 'camera-outline'} + mode={mode} + size={size} + width={width} + shape={shape} + selected={selected} + onPress={() => setSelected((value) => !value)} + aria-label="Playground icon button" /> {}} + aria-label="Disabled icon button" /> {}} + aria-label="Loading icon button" /> + + Press the first button to toggle. Pressed and selected states morph + the container shape. + + + + + {modes.map((colorMode) => ( + + + {colorMode} + + {}} + /> + {}} + /> + {}} + /> + {}} + /> + + ))} + + Default, toggle off, toggle on, disabled. + - + - {}} - /> - {}} - /> - {}} - /> - {}} - loading - /> + {sizes.map((item) => ( + {}} + aria-label={`${item} icon button`} + /> + ))} - + + + {widths.map((item) => ( + {}} + aria-label={`${item} width icon button`} + /> + ))} + + + + {}} - /> - {}} - /> - {}} + icon={toggles.round ? 'heart' : 'heart-outline'} + mode="filled" + shape="round" + selected={!!toggles.round} + onPress={() => toggle('round')} /> {}} - loading + icon={toggles.square ? 'heart' : 'heart-outline'} + mode="filled" + shape="square" + selected={!!toggles.square} + onPress={() => toggle('square')} /> + + Round (default) and square. Toggle inverts the resting shape. + {}} - /> - {}} - /> - {}} /> {}} /> {}} - /> - {}} /> - {}} - containerColor={Palette.tertiary60} /> - {}} /> - {}} loading /> ); }; -ButtonExample.title = 'Icon Button'; +IconButtonExample.title = 'Icon Button'; const styles = StyleSheet.create({ - v3Container: { + container: { flexDirection: 'column', + paddingHorizontal: 12, + paddingBottom: 24, }, - row: { + chipRow: { flexDirection: 'row', - paddingHorizontal: 12, + alignItems: 'center', + marginBottom: 8, }, - square: { - borderRadius: 0, + chipRowLabel: { + width: 56, + marginRight: 8, }, - slightlyRounded: { - borderRadius: 4, - width: 48, - height: 48, + chipRowContent: { + gap: 8, + paddingVertical: 4, }, - contentPadding: { - padding: 8, + playgroundRow: { + flexDirection: 'row', + alignItems: 'center', + flexWrap: 'wrap', + gap: 8, + paddingVertical: 12, + }, + row: { + flexDirection: 'row', + flexWrap: 'wrap', + alignItems: 'center', + gap: 8, + }, + colorRow: { + flexDirection: 'row', + alignItems: 'center', + gap: 8, }, - differentBorderRadius: { - borderTopLeftRadius: 2, - borderTopRightRadius: 4, - borderBottomLeftRadius: 8, - borderBottomRightRadius: 6, + colorLabel: { + width: 88, }, }); -export default ButtonExample; +export default IconButtonExample; diff --git a/example/src/Examples/SurfaceExample.tsx b/example/src/Examples/SurfaceExample.tsx index e941c7c843..19b2888fe5 100644 --- a/example/src/Examples/SurfaceExample.tsx +++ b/example/src/Examples/SurfaceExample.tsx @@ -20,13 +20,13 @@ const AnimatedSurface = () => { setIndex(index - 1)} /> setIndex(index + 1)} diff --git a/example/src/Examples/TooltipExample.tsx b/example/src/Examples/TooltipExample.tsx index 4ecbdfe544..aebff5d51c 100644 --- a/example/src/Examples/TooltipExample.tsx +++ b/example/src/Examples/TooltipExample.tsx @@ -83,7 +83,6 @@ const TooltipExample = () => { > {}} /> @@ -95,7 +94,7 @@ const TooltipExample = () => { setTextAlign('left')} /> @@ -103,7 +102,7 @@ const TooltipExample = () => { setTextAlign('center')} /> @@ -111,7 +110,7 @@ const TooltipExample = () => { setTextAlign('right')} diff --git a/src/components/Appbar/AppbarAction.tsx b/src/components/Appbar/AppbarAction.tsx index bd0296c8a3..98694dbaa5 100644 --- a/src/components/Appbar/AppbarAction.tsx +++ b/src/components/Appbar/AppbarAction.tsx @@ -18,10 +18,6 @@ export type Props = React.PropsWithoutRef & { * Name of the icon to show. */ icon: IconSource; - /** - * Optional icon size. - */ - size?: number; /** * Whether the button is disabled. A disabled button is greyed out and `onPress` is not called on touch. */ @@ -71,7 +67,6 @@ export type Props = React.PropsWithoutRef & { * ``` */ const AppbarAction = ({ - size = 24, color: iconColor, icon, disabled, @@ -93,7 +88,6 @@ const AppbarAction = ({ return ( , 'icon'> & { * Custom color for back icon. */ color?: ColorValue; - /** - * Optional icon size. - */ - size?: number; /** * Whether the button is disabled. A disabled button is greyed out and `onPress` is not called on touch. */ diff --git a/src/components/Card/CardTitle.tsx b/src/components/Card/CardTitle.tsx index ed8289cde5..e347d5f4db 100644 --- a/src/components/Card/CardTitle.tsx +++ b/src/components/Card/CardTitle.tsx @@ -115,7 +115,7 @@ const LEFT_SIZE = 40; * title="Card Title" * subtitle="Card Subtitle" * left={(props) => } - * right={(props) => {}} />} + * right={() => {}} />} * /> * ); * diff --git a/src/components/IconButton/IconButton.tsx b/src/components/IconButton/IconButton.tsx index 4bf54c324d..85766b9f3a 100644 --- a/src/components/IconButton/IconButton.tsx +++ b/src/components/IconButton/IconButton.tsx @@ -7,15 +7,22 @@ import type { ViewStyle, } from 'react-native'; -import Animated, { type AnimatedStyle } from 'react-native-reanimated'; +import Animated, { + type AnimatedStyle, + cubicBezier, + type CSSStyle, +} from 'react-native-reanimated'; -import { getIconButtonColor } from './utils'; +import type { Mode, Shape, Size, Width } from './tokens'; +import { getDimensions, getIconButtonColor } from './utils'; import { useInternalTheme } from '../../core/theming'; +import { useReduceMotion } from '../../theme/accessibility/ReduceMotionContext'; import type { ThemeProp } from '../../theme/types'; import getMinInteractiveSizeHitSlop from '../../utils/getMinInteractiveSizeHitSlop'; import hasTouchHandler, { carriesToggleState, } from '../../utils/hasTouchHandler'; +import { useFocusRing } from '../../utils/useFocusRing'; import ActivityIndicator from '../ActivityIndicator'; import CrossFadeIcon from '../CrossFadeIcon'; import Icon from '../Icon'; @@ -23,23 +30,23 @@ import type { IconSource } from '../Icon'; import TouchableRipple from '../TouchableRipple/TouchableRipple'; import type { Props as TouchableRippleProps } from '../TouchableRipple/TouchableRipple'; -const PADDING = 8; - -type IconButtonMode = 'outlined' | 'contained' | 'contained-tonal'; - export type Props = Omit< React.PropsWithoutRef, - 'children' | 'style' + 'children' | 'style' | 'onPress' > & { /** * Icon to display. */ icon: IconSource; /** - * @supported Available in v5.x with theme version 3 - * Mode of the icon button. By default there is no specified mode - only pressable icon will be rendered. + * Color style of the icon button. + * + * - `standard` — no container fill (default) + * - `filled` — primary container + * - `tonal` — secondary-container fill + * - `outlined` — outline, no fill (selected uses inverse surface) */ - mode?: IconButtonMode; + mode?: Mode; /** * @renamed Renamed from 'color' to 'iconColor' in v5.x * Color of the icon. @@ -50,13 +57,29 @@ export type Props = Omit< */ containerColor?: ColorValue; /** - * Whether icon button is selected. A selected button receives alternative combination of icon and container colors. + * Toggle state. Omit for the default (non-toggle) color set; + * `true` / `false` use the toggle-ON / toggle-OFF color sets and invert + * the resting shape. */ selected?: boolean; /** - * Size of the icon. + * Named size on the MD3 Expressive scale. Defaults to `small`. + */ + size?: Size; + /** + * Horizontal padding around the icon: `narrow`, `default`, or `wide`. + * Uniform (`default`) sizes are square. + */ + width?: Width; + /** + * Resting container shape. Toggle-ON inverts round ↔ square. Pressed + * uses a shared corner for both shapes. + */ + shape?: Shape; + /** + * Icon size override in dp. Defaults to the size token. */ - size?: number; + iconSize?: number; /** * Whether the button is disabled. A disabled button is greyed out and `onPress` is not called on touch. */ @@ -71,7 +94,7 @@ export type Props = Omit< 'aria-label'?: string; /** * Style of button's inner content. - * Use this prop to apply custom height and width or to set a custom padding`. + * Use this prop to apply custom height and width or to set a custom padding. */ contentStyle?: StyleProp; /** @@ -106,7 +129,7 @@ export type Props = Omit< * console.log('Pressed')} * /> * ); @@ -120,13 +143,16 @@ const IconButton = ({ icon, iconColor: customIconColor, containerColor: customContainerColor, - size = 24, + size = 'small', + width = 'default', + shape = 'round', + iconSize, 'aria-label': ariaLabel, disabled, onPress, - selected = false, + selected, animated = false, - mode, + mode = 'standard', style, theme: themeOverrides, testID, @@ -135,18 +161,34 @@ const IconButton = ({ hitSlop, role = 'button', ref, + onPressIn: onPressInProp, + onPressOut: onPressOutProp, + onFocus: onFocusProp, + onBlur: onBlurProp, ...rest }: Props) => { const theme = useInternalTheme(themeOverrides); + const reduceMotion = useReduceMotion(); + const [pressed, setPressed] = React.useState(false); const IconComponent = animated ? CrossFadeIcon : Icon; + const dimensions = getDimensions({ + theme, + size, + width, + shape, + selected, + iconSize, + }); + const { iconColor, iconOpacity, backgroundColor, borderColor, backgroundOpacity, + borderWidth, } = getIconButtonColor({ theme, disabled, @@ -154,27 +196,68 @@ const IconButton = ({ mode, customIconColor, customContainerColor, + outlineWidth: dimensions.outlineWidth, }); - const buttonSize = size + 2 * PADDING; - const borderWidth = mode === 'outlined' && !selected ? 1 : 0; + const borderRadius = pressed + ? dimensions.pressedRadius + : dimensions.restingRadius; + const radiusStyle: CSSStyle = { + borderRadius, + transitionProperty: ['borderRadius'], + transitionDuration: reduceMotion ? 0 : theme.motion.duration.short4, + transitionTimingFunction: cubicBezier(...theme.motion.easing.standard), + }; - const shapeStyle = { borderRadius: buttonSize / 2 }; + const handlePressIn = (e: GestureResponderEvent) => { + setPressed(true); + onPressInProp?.(e); + }; - const borderStyles = { - borderWidth, - borderColor, - ...shapeStyle, + const handlePressOut = (e: GestureResponderEvent) => { + setPressed(false); + onPressOutProp?.(e); + }; + + const hasPassedTouchHandler = hasTouchHandler({ + onPress, + onLongPress: rest.onLongPress, + onPressIn: onPressInProp, + onPressOut: onPressOutProp, + }); + + // The ring goes on the clip view, around the outline, rather than on the + // touchable inside it. Off whenever the button can't be pressed, as on + // TouchableRipple. + const { target: focusTarget, ring: focusRing } = useFocusRing( + disabled || !hasPassedTouchHandler, + theme.colors.secondary, + 'outward', + 'within' + ); + + const handleFocus = (e: Parameters>[0]) => { + onFocusProp?.(e); + focusTarget.onFocus?.(e); + }; + + const handleBlur = (e: Parameters>[0]) => { + onBlurProp?.(e); + focusTarget.onBlur?.(); }; - const touchableSize = buttonSize - 2 * borderWidth; + // The touchable sits inside the outline, so it is smaller than the container. + const touchableWidth = dimensions.width - 2 * borderWidth; + const touchableHeight = dimensions.height - 2 * borderWidth; - const defaultHitSlop = disabled - ? undefined - : getMinInteractiveSizeHitSlop({ - width: touchableSize, - height: touchableSize, - }); + // Disabled and handler-less buttons get no extra target. + const defaultHitSlop = + disabled || !hasPassedTouchHandler + ? undefined + : getMinInteractiveSizeHitSlop({ + width: touchableWidth, + height: touchableHeight, + }); // Without a handler the touchable drops the button role, and on web that // renders role="presentation", which can't carry a name. A labelled icon @@ -185,67 +268,89 @@ const IconButton = ({ role === 'button' && !disabled && ariaLabel !== undefined && - !hasTouchHandler({ - onPress, - onLongPress: rest.onLongPress, - onPressIn: rest.onPressIn, - onPressOut: rest.onPressOut, - }) && + !hasPassedTouchHandler && !carriesToggleState(rest); + // The touchable clips its own ripple to the container shape. Not on web, + // where `overflow: 'hidden'` would also clip the hit slop target. + const touchableShapeStyle = { borderRadius }; + return ( - {backgroundOpacity < 1 && ( - - )} - - - {loading ? ( - - ) : ( - - )} - - + {backgroundOpacity < 1 && ( + + )} + + + {loading ? ( + + ) : ( + + )} + + + ); }; @@ -253,12 +358,22 @@ const IconButton = ({ const styles = StyleSheet.create({ container: { margin: 6, + overflow: 'visible', + }, + // No `overflow: 'hidden'`: it would clip the touchable's hit slop. The + // background and outline follow the radius on their own. + clip: { + width: '100%', + height: '100%', }, touchable: { flex: 1, justifyContent: 'center', alignItems: 'center', }, + overlay: { + pointerEvents: 'none', + }, clipToShape: { overflow: 'hidden', }, diff --git a/src/components/IconButton/tokens.ts b/src/components/IconButton/tokens.ts new file mode 100644 index 0000000000..7fee7b3862 --- /dev/null +++ b/src/components/IconButton/tokens.ts @@ -0,0 +1,152 @@ +import type { ColorRole } from '../../theme/types'; +import type { ShapeToken } from '../../theme/utils/shape'; + +export type Size = 'extraSmall' | 'small' | 'medium' | 'large' | 'extraLarge'; + +export type Width = 'narrow' | 'default' | 'wide'; + +export type Shape = 'round' | 'square'; + +export type Mode = 'standard' | 'filled' | 'tonal' | 'outlined'; + +type WidthSpec = { + leading: number; + trailing: number; +}; + +type SizeSpec = { + containerHeight: number; + icon: number; + outlineWidth: number; + squareShape: ShapeToken; + pressedShape: ShapeToken; + widths: Record; +}; + +export type ColorSet = { + container?: ColorRole; + icon: ColorRole; + outline?: ColorRole; +}; + +/** + * MD3 Expressive Icon Button size scale. + * @see https://m3.material.io/components/icon-buttons/specs + * @see androidx `{XSmall|Small|Medium|Large|XLarge}IconButtonTokens` 14_1_0 + */ +const sizes = { + extraSmall: { + containerHeight: 32, + icon: 20, + outlineWidth: 1, + squareShape: 'medium', + pressedShape: 'small', + widths: { + narrow: { leading: 4, trailing: 4 }, + default: { leading: 6, trailing: 6 }, + wide: { leading: 10, trailing: 10 }, + }, + }, + small: { + containerHeight: 40, + icon: 24, + outlineWidth: 1, + squareShape: 'medium', + pressedShape: 'small', + widths: { + narrow: { leading: 4, trailing: 4 }, + default: { leading: 8, trailing: 8 }, + wide: { leading: 14, trailing: 14 }, + }, + }, + medium: { + containerHeight: 56, + icon: 24, + outlineWidth: 1, + squareShape: 'large', + pressedShape: 'medium', + widths: { + narrow: { leading: 12, trailing: 12 }, + default: { leading: 16, trailing: 16 }, + wide: { leading: 24, trailing: 24 }, + }, + }, + large: { + containerHeight: 96, + icon: 32, + outlineWidth: 2, + squareShape: 'extraLarge', + pressedShape: 'large', + widths: { + narrow: { leading: 16, trailing: 16 }, + default: { leading: 32, trailing: 32 }, + wide: { leading: 48, trailing: 48 }, + }, + }, + extraLarge: { + containerHeight: 136, + icon: 40, + outlineWidth: 3, + squareShape: 'extraLarge', + pressedShape: 'large', + widths: { + narrow: { leading: 32, trailing: 32 }, + default: { leading: 48, trailing: 48 }, + wide: { leading: 72, trailing: 72 }, + }, + }, +} as const satisfies Record; + +/** + * Default (non-toggle) vs toggle selected/unselected color roles. + * Filled default uses `primary`; toggle-OFF uses `surfaceContainer`. + * @see androidx `FilledIconButtonTokens` / `FilledTonalIconButtonTokens` / + * `OutlinedIconButtonTokens` / `StandardIconButtonTokens` 14_1_0 + */ +const modes = { + filled: { + default: { container: 'primary', icon: 'onPrimary' }, + selected: { container: 'primary', icon: 'onPrimary' }, + unselected: { container: 'surfaceContainer', icon: 'onSurfaceVariant' }, + }, + tonal: { + default: { container: 'secondaryContainer', icon: 'onSecondaryContainer' }, + selected: { container: 'secondary', icon: 'onSecondary' }, + unselected: { + container: 'secondaryContainer', + icon: 'onSecondaryContainer', + }, + }, + outlined: { + default: { icon: 'onSurfaceVariant', outline: 'outlineVariant' }, + selected: { container: 'inverseSurface', icon: 'inverseOnSurface' }, + unselected: { icon: 'onSurfaceVariant', outline: 'outlineVariant' }, + }, + standard: { + default: { icon: 'onSurfaceVariant' }, + selected: { icon: 'primary' }, + unselected: { icon: 'onSurfaceVariant' }, + }, +} satisfies Record< + Mode, + { default: ColorSet; selected: ColorSet; unselected: ColorSet } +>; + +const disabled = { + container: 'onSurface', + icon: 'onSurface', + outline: 'outlineVariant', + /** Filled/tonal (and outlined-selected) disabled container opacity. */ + containerOpacity: 0.1, +} as const satisfies { + container: ColorRole; + icon: ColorRole; + outline: ColorRole; + containerOpacity: number; +}; + +export const IconButtonTokens = { + sizes, + modes, + disabled, +}; diff --git a/src/components/IconButton/utils.ts b/src/components/IconButton/utils.ts index d35700a499..008b3098c9 100644 --- a/src/components/IconButton/utils.ts +++ b/src/components/IconButton/utils.ts @@ -1,155 +1,191 @@ import type { ColorValue } from 'react-native'; +import { IconButtonTokens } from './tokens'; +import type { ColorSet, Mode, Shape, Size, Width } from './tokens'; import { tokens } from '../../theme/tokens'; import type { InternalTheme } from '../../theme/types'; +import { resolveCornerRadius } from '../../theme/utils/shape'; +import type { ShapeToken } from '../../theme/utils/shape'; const stateOpacity = tokens.md.sys.state.opacity; -type IconButtonMode = 'outlined' | 'contained' | 'contained-tonal'; - -type BaseProps = { - theme: InternalTheme; - isMode: (mode: IconButtonMode) => boolean; - disabled?: boolean; - selected?: boolean; +export type IconButtonColors = { + iconColor: ColorValue; + iconOpacity: number; + backgroundColor: ColorValue | undefined; + backgroundOpacity: number; + borderColor: ColorValue; + borderWidth: number; }; -const getBackgroundColor = ({ - theme, - isMode, - disabled, - selected, - customContainerColor, -}: BaseProps & { customContainerColor?: ColorValue }) => { - if (disabled) { - if (isMode('contained') || isMode('contained-tonal')) { - return theme.colors.onSurface; - } - } - - if (typeof customContainerColor !== 'undefined') { - return customContainerColor; - } +export type IconButtonDimensions = { + width: number; + height: number; + iconSize: number; + outlineWidth: number; + restingRadius: number; + pressedRadius: number; +}; - if (isMode('contained')) { - if (selected) { - return theme.colors.primary; - } - return theme.colors.surfaceVariant; +const colorSetFor = (mode: Mode, selected: boolean | undefined): ColorSet => { + if (selected === true) { + return IconButtonTokens.modes[mode].selected; } - - if (isMode('contained-tonal')) { - if (selected) { - return theme.colors.secondaryContainer; - } - return theme.colors.surfaceVariant; + if (selected === false) { + return IconButtonTokens.modes[mode].unselected; } + return IconButtonTokens.modes[mode].default; +}; - if (isMode('outlined')) { - if (selected) { - return theme.colors.inverseSurface; - } +const hasDisabledContainer = ( + mode: Mode, + selected: boolean | undefined +): boolean => { + if (mode === 'filled' || mode === 'tonal') { + return true; } - - return undefined; + return mode === 'outlined' && selected === true; }; -const getIconColor = ({ +/** + * Resolve container, icon, and outline colors for an icon button. + * + * A disabled button always takes the disabled icon color, whatever + * `customIconColor` says. + * + * `selected` is a tri-state: `undefined` is the default (non-toggle) + * appearance; `true`/`false` are the toggle-ON / toggle-OFF color sets. + */ +export const getIconButtonColor = ({ theme, - isMode, disabled, + mode = 'standard', selected, customIconColor, -}: BaseProps & { customIconColor?: ColorValue }) => { - if (disabled) { - return theme.colors.onSurface; + customContainerColor, + outlineWidth = 0, +}: { + theme: InternalTheme; + disabled?: boolean; + mode?: Mode; + selected?: boolean; + customIconColor?: ColorValue; + customContainerColor?: ColorValue; + outlineWidth?: number; +}): IconButtonColors => { + const iconOpacity = disabled ? stateOpacity.disabled : stateOpacity.enabled; + const usesDisabledContainer = + Boolean(disabled) && hasDisabledContainer(mode, selected); + + if (usesDisabledContainer) { + return { + iconColor: theme.colors[IconButtonTokens.disabled.icon], + iconOpacity, + backgroundColor: theme.colors[IconButtonTokens.disabled.container], + backgroundOpacity: IconButtonTokens.disabled.containerOpacity, + borderColor: theme.colors[IconButtonTokens.disabled.outline], + borderWidth: 0, + }; } - if (typeof customIconColor !== 'undefined') { - return customIconColor; + if (disabled) { + const set = colorSetFor(mode, selected); + return { + iconColor: theme.colors[IconButtonTokens.disabled.icon], + iconOpacity, + backgroundColor: customContainerColor, + backgroundOpacity: stateOpacity.enabled, + borderColor: + theme.colors[set.outline ?? IconButtonTokens.disabled.outline], + borderWidth: set.outline ? outlineWidth : 0, + }; } - if (isMode('contained')) { - if (selected) { - return theme.colors.onPrimary; - } - return theme.colors.primary; - } + const set = colorSetFor(mode, selected); + const backgroundColor = + customContainerColor ?? + (set.container ? theme.colors[set.container] : undefined); - if (isMode('contained-tonal')) { - if (selected) { - return theme.colors.onSecondaryContainer; - } - return theme.colors.onSurfaceVariant; - } + return { + iconColor: customIconColor ?? theme.colors[set.icon], + iconOpacity, + backgroundColor, + backgroundOpacity: stateOpacity.enabled, + borderColor: theme.colors[set.outline ?? 'outlineVariant'], + borderWidth: set.outline ? outlineWidth : 0, + }; +}; - if (isMode('outlined')) { - if (selected) { - return theme.colors.inverseOnSurface; - } - return theme.colors.onSurfaceVariant; +/** + * Resting container shape. Toggle-ON inverts round↔square; pressed uses + * the shared pressed shape (same for round and square). + */ +export const resolveShapeToken = ({ + size = 'small', + shape = 'round', + selected, + pressed = false, +}: { + size?: Size; + shape?: Shape; + selected?: boolean; + pressed?: boolean; +}): ShapeToken => { + const spec = IconButtonTokens.sizes[size]; + if (pressed) { + return spec.pressedShape; } - - if (selected) { - return theme.colors.primary; + const inverted = selected === true; + if (shape === 'round') { + return inverted ? spec.squareShape : 'full'; } - return theme.colors.onSurfaceVariant; + return inverted ? 'full' : spec.squareShape; }; -export const getIconButtonColor = ({ +export const getDimensions = ({ theme, - disabled, - mode, + size = 'small', + width = 'default', + shape = 'round', selected, - customIconColor, - customContainerColor, + iconSize, }: { theme: InternalTheme; - disabled?: boolean; + size?: Size; + width?: Width; + shape?: Shape; selected?: boolean; - mode?: IconButtonMode; - customIconColor?: ColorValue; - customContainerColor?: ColorValue; -}): { - iconColor: ColorValue; - iconOpacity: number; - backgroundColor: ColorValue | undefined; - borderColor: ColorValue; - backgroundOpacity: number; -} => { - const isMode = (modeToCompare: IconButtonMode) => { - return mode === modeToCompare; - }; - - const baseIconColorProps = { - theme, - isMode, - disabled, + iconSize?: number; +}): IconButtonDimensions => { + const spec = IconButtonTokens.sizes[size]; + const padding = spec.widths[width]; + const resolvedIcon = iconSize ?? spec.icon; + const height = spec.containerHeight; + const containerWidth = resolvedIcon + padding.leading + padding.trailing; + + const restingToken = resolveShapeToken({ + size, + shape, selected, - }; - - const iconColor = getIconColor({ - ...baseIconColorProps, - customIconColor, + pressed: false, }); - - const iconOpacity = disabled ? stateOpacity.disabled : stateOpacity.enabled; - - const backgroundColor = getBackgroundColor({ - ...baseIconColorProps, - customContainerColor, + const pressedToken = resolveShapeToken({ + size, + shape, + selected, + pressed: true, }); - const backgroundOpacity = - disabled && (isMode('contained') || isMode('contained-tonal')) - ? stateOpacity.disabled - : stateOpacity.enabled; + const roundRadius = height / 2; + const resolveRadius = (token: ShapeToken) => + token === 'full' ? roundRadius : resolveCornerRadius(theme, token); return { - iconColor, - iconOpacity, - backgroundColor, - borderColor: theme.colors.outlineVariant, - backgroundOpacity, + width: containerWidth, + height, + iconSize: resolvedIcon, + outlineWidth: spec.outlineWidth, + restingRadius: resolveRadius(restingToken), + pressedRadius: resolveRadius(pressedToken), }; }; diff --git a/src/components/TextInput/TextInputIcon.tsx b/src/components/TextInput/TextInputIcon.tsx index b75b5dd658..5553536363 100644 --- a/src/components/TextInput/TextInputIcon.tsx +++ b/src/components/TextInput/TextInputIcon.tsx @@ -25,7 +25,12 @@ export type TextInputAccessoryProps = { }; export type TextInputIconProps = TextInputAccessoryProps & - Omit; + Omit & { + /** + * Size of the icon. + */ + size?: number; + }; /** * A component to render a leading / trailing icon in the TextInput @@ -109,7 +114,7 @@ const TextInputIcon = ({ hitSlop={hitSlop !== undefined ? hitSlop : ACCESSORY_HIT_SLOP} icon={icon} iconColor={color} - size={iconSize} + iconSize={iconSize} theme={themeOverride} style={[ styles.icon, diff --git a/src/components/Tooltip/Tooltip.tsx b/src/components/Tooltip/Tooltip.tsx index 52243f5d97..cfdc52bf96 100644 --- a/src/components/Tooltip/Tooltip.tsx +++ b/src/components/Tooltip/Tooltip.tsx @@ -57,7 +57,7 @@ export type Props = { * * const MyComponent = () => ( * - * {}} /> + * {}} /> * * ); * diff --git a/src/components/__tests__/Appbar/__snapshots__/Appbar.test.tsx.snap b/src/components/__tests__/Appbar/__snapshots__/Appbar.test.tsx.snap index 3280897b93..3b03204ebb 100644 --- a/src/components/__tests__/Appbar/__snapshots__/Appbar.test.tsx.snap +++ b/src/components/__tests__/Appbar/__snapshots__/Appbar.test.tsx.snap @@ -194,84 +194,104 @@ exports[`Appbar does not pass any additional props to Searchbar 1`] = ` [ { "margin": 6, + "overflow": "visible", }, { - "backgroundColor": undefined, "height": 40, "width": 40, }, + undefined, { - "borderColor": "rgba(202, 196, 208, 1)", "borderRadius": 20, - "borderWidth": 0, }, - undefined, ] } > - + + - magnify - + [ + { + "lineHeight": 24, + "transform": [ + { + "scaleX": 1, + }, + ], + }, + { + "backgroundColor": "transparent", + }, + ], + ] + } + > + magnify + + @@ -329,120 +349,138 @@ exports[`Appbar does not pass any additional props to Searchbar 1`] = ` [ { "margin": 6, + "overflow": "visible", }, { - "backgroundColor": undefined, "height": 40, "width": 40, }, + undefined, { - "borderColor": "rgba(202, 196, 208, 1)", "borderRadius": 20, - "borderWidth": 0, }, - undefined, ] } > - + + - close - + [ + { + "lineHeight": 24, + "transform": [ + { + "scaleX": 1, + }, + ], + }, + { + "backgroundColor": "transparent", + }, + ], + ] + } + > + close + + @@ -556,162 +594,180 @@ exports[`Appbar passes additional props to AppbarBackAction, AppbarContent and A [ { "margin": 6, + "overflow": "visible", }, { - "backgroundColor": undefined, "height": 40, "width": 40, }, + undefined, { - "borderColor": "rgba(202, 196, 208, 1)", "borderRadius": 20, - "borderWidth": 0, }, - undefined, ] } > - + - + > + + @@ -784,155 +840,173 @@ exports[`Appbar passes additional props to AppbarBackAction, AppbarContent and A [ { "margin": 6, + "overflow": "visible", }, { - "backgroundColor": undefined, "height": 40, "width": 40, }, + undefined, { - "borderColor": "rgba(202, 196, 208, 1)", "borderRadius": 20, - "borderWidth": 0, }, - undefined, ] } > - + - - menu - + + menu + + @@ -1046,120 +1120,142 @@ exports[`AppbarAction should be rendered with custom color 1`] = ` [ { "margin": 6, + "overflow": "visible", }, { - "backgroundColor": undefined, "height": 40, "width": 40, }, + undefined, { - "borderColor": "rgba(202, 196, 208, 1)", "borderRadius": 20, - "borderWidth": 0, }, - undefined, ] } + testID="appbar-action-container" > - + - - menu - + + menu + + @@ -1273,120 +1369,142 @@ exports[`AppbarAction should be rendered with default theme color 1`] = ` [ { "margin": 6, + "overflow": "visible", }, { - "backgroundColor": undefined, "height": 40, "width": 40, }, + undefined, { - "borderColor": "rgba(202, 196, 208, 1)", "borderRadius": 20, - "borderWidth": 0, }, - undefined, ] } + testID="appbar-action-container" > - + - - menu - + + menu + + @@ -1500,120 +1618,142 @@ exports[`AppbarAction should be rendered with specific theme color if is leading [ { "margin": 6, + "overflow": "visible", }, { - "backgroundColor": undefined, "height": 40, "width": 40, }, + undefined, { - "borderColor": "rgba(202, 196, 208, 1)", "borderRadius": 20, - "borderWidth": 0, }, - undefined, ] } + testID="appbar-action-container" > - + - - menu - + + menu + + @@ -1727,127 +1867,149 @@ exports[`AppbarAction should render AppbarBackAction with custom color 1`] = ` [ { "margin": 6, + "overflow": "visible", }, { - "backgroundColor": undefined, "height": 40, "width": 40, }, + undefined, { - "borderColor": "rgba(202, 196, 208, 1)", "borderRadius": 20, - "borderWidth": 0, }, - undefined, ] } + testID="appbar-action-container" > - + - + > + + diff --git a/src/components/__tests__/IconButton.test.tsx b/src/components/__tests__/IconButton.test.tsx index a1dc4418ff..5b498fbae2 100644 --- a/src/components/__tests__/IconButton.test.tsx +++ b/src/components/__tests__/IconButton.test.tsx @@ -1,3 +1,4 @@ +import { useState } from 'react'; import { Platform, StyleSheet } from 'react-native'; import { @@ -9,21 +10,23 @@ import { jest, } from '@jest/globals'; -import { render, screen } from '../../test-utils'; +import { fireEvent, render, screen, userEvent } from '../../test-utils'; import { pink500 } from '../../theme/colors'; import { LightTheme } from '../../theme/schemes'; import { tokens } from '../../theme/tokens'; import IconButton from '../IconButton/IconButton'; -import { getIconButtonColor } from '../IconButton/utils'; +import { + getDimensions, + getIconButtonColor, + resolveShapeToken, +} from '../IconButton/utils'; const stateOpacity = tokens.md.sys.state.opacity; +const theme = LightTheme; const styles = StyleSheet.create({ - square: { - borderRadius: 0, - }, - slightlyRounded: { - borderRadius: 4, + scaled: { + transform: [{ scale: 1 }], }, customBackground: { backgroundColor: pink500, @@ -36,16 +39,42 @@ it('renders icon button by default', async () => { expect(tree).toMatchSnapshot(); }); -it('renders icon button with color', async () => { +it('renders filled icon button', async () => { const tree = ( - await render() + await render() ).toJSON(); expect(tree).toMatchSnapshot(); }); -it('renders icon button with size', async () => { - const tree = (await render()).toJSON(); +it('renders tonal icon button', async () => { + const tree = ( + await render() + ).toJSON(); + + expect(tree).toMatchSnapshot(); +}); + +it('renders outlined icon button', async () => { + const tree = ( + await render() + ).toJSON(); + + expect(tree).toMatchSnapshot(); +}); + +it('renders extraLarge icon button', async () => { + const tree = ( + await render() + ).toJSON(); + + expect(tree).toMatchSnapshot(); +}); + +it('renders icon button with color', async () => { + const tree = ( + await render() + ).toJSON(); expect(tree).toMatchSnapshot(); }); @@ -78,32 +107,127 @@ it('renders icon change animated', async () => { expect(tree).toMatchSnapshot(); }); -it('renders icon button with custom border radius', async () => { - const { toJSON } = await render( - {}} - style={styles.square} - /> +it('renders a square icon button', async () => { + const tree = ( + await render( + {}} + /> + ) + ).toJSON(); + + expect(tree).toMatchSnapshot(); +}); + +it('applies a static transform from style', async () => { + const tree = ( + await render( + + ) + ).toJSON(); + + expect(tree).toMatchSnapshot(); +}); + +it('calls onPress', async () => { + const onPress = jest.fn(); + const user = userEvent.setup(); + + await render( + ); + await user.press(screen.getByTestId('icon-button')); - expect(toJSON()).toMatchSnapshot(); + expect(onPress).toHaveBeenCalledTimes(1); }); -it('renders icon button with small border radius', async () => { - const { toJSON } = await render( +it('exposes a selected toggle to assistive technology', async () => { + await render( {}} />); + + expect(screen.getByRole('button', { selected: true })).toBeOnTheScreen(); +}); + +it('exposes an unselected toggle to assistive technology', async () => { + await render( + {}} /> + ); + + expect(screen.getByRole('button', { selected: false })).toBeOnTheScreen(); +}); + +it('does not expose a selected state when it is not a toggle', async () => { + await render( {}} />); + + expect(screen.getByRole('button')).toHaveProp( + 'accessibilityState', + expect.objectContaining({ selected: undefined }) + ); +}); + +it('uses a square resting shape when a round button is selected', async () => { + await render( {}} - style={styles.slightlyRounded} /> ); - expect(toJSON()).toMatchSnapshot(); + expect(screen.getByTestId('icon-button-container')).toHaveStyle({ + borderRadius: theme.shapes.corner.medium, + }); +}); + +it('keeps the inverted resting shape after a tap that selects the button', async () => { + const ToggleIconButton = () => { + const [selected, setSelected] = useState(false); + + return ( + setSelected((value) => !value)} + /> + ); + }; + const user = userEvent.setup(); + + await render(); + await user.press(screen.getByTestId('icon-button')); + + expect(screen.getByTestId('icon-button-container')).toHaveStyle({ + borderRadius: theme.shapes.corner.medium, + }); + + await user.press(screen.getByTestId('icon-button')); + + expect(screen.getByTestId('icon-button-container')).toHaveStyle({ + borderRadius: getDimensions({ theme }).restingRadius, + }); +}); + +it('morphs to the pressed shape with only onLongPress', async () => { + await render( + {}} /> + ); + + await fireEvent(screen.getByTestId('icon-button'), 'pressIn'); + + expect(screen.getByTestId('icon-button-container')).toHaveStyle({ + borderRadius: getDimensions({ theme }).pressedRadius, + }); +}); + +it('is a plain view, not a disabled button, without touch handlers', async () => { + await render(); + + expect(screen.getByTestId('icon-button')).not.toBeDisabled(); + expect(screen.queryByRole('button')).not.toBeOnTheScreen(); }); it('keeps touchable content centered with custom container style', async () => { @@ -140,7 +264,7 @@ describe('getIconButtonColor - icon color', () => { it('should return custom icon color', () => { expect( getIconButtonColor({ - theme: LightTheme, + theme, customIconColor: 'purple', }) ).toMatchObject({ @@ -148,13 +272,11 @@ describe('getIconButtonColor - icon color', () => { }); }); - it('should let the disabled color outrank an explicit icon color', () => { - // a disabled icon reads as disabled whatever color it was given + it('should return disabled icon color', () => { expect( getIconButtonColor({ theme: LightTheme, disabled: true, - customIconColor: 'purple', }) ).toMatchObject({ iconColor: LightTheme.colors.onSurface, @@ -162,11 +284,13 @@ describe('getIconButtonColor - icon color', () => { }); }); - it('should return correct disabled color, for theme version 3', () => { + it('should let the disabled color outrank an explicit icon color', () => { + // a disabled icon reads as disabled whatever color it was given expect( getIconButtonColor({ theme: LightTheme, disabled: true, + customIconColor: 'purple', }) ).toMatchObject({ iconColor: LightTheme.colors.onSurface, @@ -174,93 +298,94 @@ describe('getIconButtonColor - icon color', () => { }); }); - it('should return theme icon color, for theme version 3, mode contained', () => { + it('should return filled default icon color', () => { expect( getIconButtonColor({ - theme: LightTheme, - mode: 'contained', + theme, + mode: 'filled', }) ).toMatchObject({ - iconColor: LightTheme.colors.primary, + iconColor: theme.colors.onPrimary, }); }); - it('should return theme icon color, for theme version 3, mode contained, selected', () => { + it('should return filled selected icon color', () => { expect( getIconButtonColor({ - theme: LightTheme, - mode: 'contained', + theme, + mode: 'filled', selected: true, }) ).toMatchObject({ - iconColor: LightTheme.colors.onPrimary, + iconColor: theme.colors.onPrimary, }); }); - it('should return theme icon color, for theme version 3, mode contained-tonal', () => { + it('should return filled unselected icon color', () => { expect( getIconButtonColor({ - theme: LightTheme, - mode: 'contained-tonal', + theme, + mode: 'filled', + selected: false, }) ).toMatchObject({ - iconColor: LightTheme.colors.onSurfaceVariant, + iconColor: theme.colors.onSurfaceVariant, }); }); - it('should return theme icon color, for theme version 3, mode contained-tonal, selected', () => { + it('should return tonal default icon color', () => { expect( getIconButtonColor({ - theme: LightTheme, - mode: 'contained-tonal', - selected: true, + theme, + mode: 'tonal', }) ).toMatchObject({ - iconColor: LightTheme.colors.onSecondaryContainer, + iconColor: theme.colors.onSecondaryContainer, }); }); - it('should return theme icon color, for theme version 3, mode outlined', () => { + it('should return tonal selected icon color', () => { expect( getIconButtonColor({ - theme: LightTheme, - mode: 'outlined', + theme, + mode: 'tonal', + selected: true, }) ).toMatchObject({ - iconColor: LightTheme.colors.onSurfaceVariant, + iconColor: theme.colors.onSecondary, }); }); - it('should return theme icon color, for theme version 3, mode outlined, selected', () => { + it('should return outlined selected icon color', () => { expect( getIconButtonColor({ - theme: LightTheme, + theme, mode: 'outlined', selected: true, }) ).toMatchObject({ - iconColor: LightTheme.colors.inverseOnSurface, + iconColor: theme.colors.inverseOnSurface, }); }); - it('should return theme icon color, for theme version 3', () => { + it('should return standard selected icon color', () => { expect( getIconButtonColor({ - theme: LightTheme, + theme, + selected: true, }) ).toMatchObject({ - iconColor: LightTheme.colors.onSurfaceVariant, + iconColor: theme.colors.primary, }); }); - it('should return theme icon color, for theme version 3, selected', () => { + it('should return standard default icon color', () => { expect( getIconButtonColor({ - theme: LightTheme, - selected: true, + theme, }) ).toMatchObject({ - iconColor: LightTheme.colors.primary, + iconColor: theme.colors.onSurfaceVariant, }); }); }); @@ -269,7 +394,7 @@ describe('getIconButtonColor - background color', () => { it('should return custom background color', () => { expect( getIconButtonColor({ - theme: LightTheme, + theme, customContainerColor: 'purple', }) ).toMatchObject({ @@ -277,83 +402,84 @@ describe('getIconButtonColor - background color', () => { }); }); - (['contained', 'contained-tonal'] as const).forEach((mode) => - it(`should return correct disabled color, for theme version 3, ${mode} mode`, () => { + (['filled', 'tonal'] as const).forEach((mode) => + it(`should use 0.10 container opacity when disabled in ${mode} mode`, () => { expect( getIconButtonColor({ - theme: LightTheme, + theme, mode, disabled: true, }) ).toMatchObject({ - backgroundColor: LightTheme.colors.onSurface, - backgroundOpacity: stateOpacity.disabled, + backgroundColor: theme.colors.onSurface, + backgroundOpacity: 0.1, }); }) ); - it('should return theme icon color, for theme version 3, mode contained', () => { + it('should return filled default container color', () => { expect( getIconButtonColor({ - theme: LightTheme, - mode: 'contained', + theme, + mode: 'filled', }) ).toMatchObject({ - backgroundColor: LightTheme.colors.surfaceVariant, + backgroundColor: theme.colors.primary, }); }); - it('should return theme icon color, for theme version 3, mode contained, selected', () => { + it('should return filled unselected container color', () => { expect( getIconButtonColor({ - theme: LightTheme, - mode: 'contained', - selected: true, + theme, + mode: 'filled', + selected: false, }) ).toMatchObject({ - backgroundColor: LightTheme.colors.primary, + backgroundColor: theme.colors.surfaceContainer, }); }); - it('should return theme icon color, for theme version 3, mode contained-tonal', () => { + it('should return tonal default container color', () => { expect( getIconButtonColor({ - theme: LightTheme, - mode: 'contained-tonal', + theme, + mode: 'tonal', }) ).toMatchObject({ - backgroundColor: LightTheme.colors.surfaceVariant, + backgroundColor: theme.colors.secondaryContainer, }); }); - it('should return theme icon color, for theme version 3, mode contained-tonal, selected', () => { + it('should return tonal selected container color', () => { expect( getIconButtonColor({ - theme: LightTheme, - mode: 'contained-tonal', + theme, + mode: 'tonal', selected: true, }) ).toMatchObject({ - backgroundColor: LightTheme.colors.secondaryContainer, + backgroundColor: theme.colors.secondary, }); }); - it('should return theme icon color, for theme version 3, mode outlined, selected', () => { + it('should return outlined selected container color', () => { expect( getIconButtonColor({ - theme: LightTheme, + theme, mode: 'outlined', selected: true, }) ).toMatchObject({ - backgroundColor: LightTheme.colors.inverseSurface, + backgroundColor: theme.colors.inverseSurface, + borderWidth: 0, }); }); - it('should return undefined, for theme version 3, if mode not specified', () => { + it('should return undefined container for standard mode', () => { expect( getIconButtonColor({ - theme: LightTheme, + theme, }) ).toMatchObject({ backgroundColor: undefined, @@ -362,24 +488,87 @@ describe('getIconButtonColor - background color', () => { }); describe('getIconButtonColor - border color', () => { - it('should return correct disabled color, for theme version 3', () => { + it('should return outline variant when disabled', () => { expect( getIconButtonColor({ - theme: LightTheme, + theme, disabled: true, }) ).toMatchObject({ - borderColor: LightTheme.colors.outlineVariant, + borderColor: theme.colors.outlineVariant, }); }); - it('should return theme color, for theme version 3', () => { + it('should return outline variant for outlined mode', () => { expect( getIconButtonColor({ - theme: LightTheme, + theme, + mode: 'outlined', + outlineWidth: 1, }) ).toMatchObject({ - borderColor: LightTheme.colors.outlineVariant, + borderColor: theme.colors.outlineVariant, + borderWidth: 1, + }); + }); +}); + +describe('resolveShapeToken', () => { + it('keeps a round button full until it is selected', () => { + expect(resolveShapeToken({ shape: 'round' })).toBe('full'); + expect(resolveShapeToken({ shape: 'round', selected: false })).toBe('full'); + expect(resolveShapeToken({ shape: 'round', selected: true })).toBe( + 'medium' + ); + }); + + it('inverts a square button to full when selected', () => { + expect(resolveShapeToken({ shape: 'square' })).toBe('medium'); + expect(resolveShapeToken({ shape: 'square', selected: true })).toBe('full'); + }); + + it('uses the shared pressed shape for round and square', () => { + expect(resolveShapeToken({ shape: 'round', pressed: true })).toBe('small'); + expect(resolveShapeToken({ shape: 'square', pressed: true })).toBe('small'); + expect( + resolveShapeToken({ shape: 'round', selected: true, pressed: true }) + ).toBe('small'); + }); +}); + +describe('getDimensions', () => { + it('returns small default size', () => { + expect(getDimensions({ theme })).toMatchObject({ + width: 40, + height: 40, + iconSize: 24, + restingRadius: 20, + }); + }); + + it('returns the square resting radius when a round button is selected', () => { + expect(getDimensions({ theme, selected: true })).toMatchObject({ + restingRadius: theme.shapes.corner.medium, + }); + }); + + it('returns extraSmall narrow size', () => { + expect( + getDimensions({ theme, size: 'extraSmall', width: 'narrow' }) + ).toMatchObject({ + width: 28, + height: 32, + iconSize: 20, + }); + }); + + it('returns extraLarge wide size', () => { + expect( + getDimensions({ theme, size: 'extraLarge', width: 'wide' }) + ).toMatchObject({ + width: 184, + height: 136, + iconSize: 40, }); }); }); diff --git a/src/components/__tests__/__snapshots__/DataTable.test.tsx.snap b/src/components/__tests__/__snapshots__/DataTable.test.tsx.snap index 7fe8707383..3368e3e2a1 100644 --- a/src/components/__tests__/__snapshots__/DataTable.test.tsx.snap +++ b/src/components/__tests__/__snapshots__/DataTable.test.tsx.snap @@ -528,120 +528,138 @@ exports[`DataTable.Pagination renders data table pagination 1`] = ` [ { "margin": 6, + "overflow": "visible", }, { - "backgroundColor": undefined, "height": 40, "width": 40, }, + undefined, { - "borderColor": "rgba(202, 196, 208, 1)", "borderRadius": 20, - "borderWidth": 0, }, - undefined, ] } > - + + - chevron-left - + [ + { + "lineHeight": 24, + "transform": [ + { + "scaleX": 1, + }, + ], + }, + { + "backgroundColor": "transparent", + }, + ], + ] + } + > + chevron-left + + @@ -651,120 +669,138 @@ exports[`DataTable.Pagination renders data table pagination 1`] = ` [ { "margin": 6, + "overflow": "visible", }, { - "backgroundColor": undefined, "height": 40, "width": 40, }, + undefined, { - "borderColor": "rgba(202, 196, 208, 1)", "borderRadius": 20, - "borderWidth": 0, }, - undefined, ] } > - + + - chevron-right - + [ + { + "lineHeight": 24, + "transform": [ + { + "scaleX": 1, + }, + ], + }, + { + "backgroundColor": "transparent", + }, + ], + ] + } + > + chevron-right + + @@ -832,120 +868,138 @@ exports[`DataTable.Pagination renders data table pagination with fast-forward bu [ { "margin": 6, + "overflow": "visible", }, { - "backgroundColor": undefined, "height": 40, "width": 40, }, + undefined, { - "borderColor": "rgba(202, 196, 208, 1)", "borderRadius": 20, - "borderWidth": 0, }, - undefined, ] } > - + + - page-first - + [ + { + "lineHeight": 24, + "transform": [ + { + "scaleX": 1, + }, + ], + }, + { + "backgroundColor": "transparent", + }, + ], + ] + } + > + page-first + + @@ -955,120 +1009,138 @@ exports[`DataTable.Pagination renders data table pagination with fast-forward bu [ { "margin": 6, + "overflow": "visible", }, { - "backgroundColor": undefined, "height": 40, "width": 40, }, + undefined, { - "borderColor": "rgba(202, 196, 208, 1)", "borderRadius": 20, - "borderWidth": 0, }, - undefined, ] } > - + + - chevron-left - + [ + { + "lineHeight": 24, + "transform": [ + { + "scaleX": 1, + }, + ], + }, + { + "backgroundColor": "transparent", + }, + ], + ] + } + > + chevron-left + + @@ -1078,120 +1150,138 @@ exports[`DataTable.Pagination renders data table pagination with fast-forward bu [ { "margin": 6, + "overflow": "visible", }, { - "backgroundColor": undefined, "height": 40, "width": 40, }, + undefined, { - "borderColor": "rgba(202, 196, 208, 1)", "borderRadius": 20, - "borderWidth": 0, }, - undefined, ] } > - + + - chevron-right - + [ + { + "lineHeight": 24, + "transform": [ + { + "scaleX": 1, + }, + ], + }, + { + "backgroundColor": "transparent", + }, + ], + ] + } + > + chevron-right + + @@ -1201,120 +1291,138 @@ exports[`DataTable.Pagination renders data table pagination with fast-forward bu [ { "margin": 6, + "overflow": "visible", }, { - "backgroundColor": undefined, "height": 40, "width": 40, }, + undefined, { - "borderColor": "rgba(202, 196, 208, 1)", "borderRadius": 20, - "borderWidth": 0, }, - undefined, ] } > - + + - page-last - + [ + { + "lineHeight": 24, + "transform": [ + { + "scaleX": 1, + }, + ], + }, + { + "backgroundColor": "transparent", + }, + ], + ] + } + > + page-last + + @@ -1382,120 +1490,138 @@ exports[`DataTable.Pagination renders data table pagination with label 1`] = ` [ { "margin": 6, + "overflow": "visible", }, { - "backgroundColor": undefined, "height": 40, "width": 40, }, + undefined, { - "borderColor": "rgba(202, 196, 208, 1)", "borderRadius": 20, - "borderWidth": 0, }, - undefined, ] } > - + + - chevron-left - + [ + { + "lineHeight": 24, + "transform": [ + { + "scaleX": 1, + }, + ], + }, + { + "backgroundColor": "transparent", + }, + ], + ] + } + > + chevron-left + + @@ -1505,120 +1631,138 @@ exports[`DataTable.Pagination renders data table pagination with label 1`] = ` [ { "margin": 6, + "overflow": "visible", }, { - "backgroundColor": undefined, "height": 40, "width": 40, }, + undefined, { - "borderColor": "rgba(202, 196, 208, 1)", "borderRadius": 20, - "borderWidth": 0, }, - undefined, ] } > - + + - chevron-right - + [ + { + "lineHeight": 24, + "transform": [ + { + "scaleX": 1, + }, + ], + }, + { + "backgroundColor": "transparent", + }, + ], + ] + } + > + chevron-right + + @@ -1997,120 +2141,138 @@ exports[`DataTable.Pagination renders data table pagination with options select [ { "margin": 6, + "overflow": "visible", }, { - "backgroundColor": undefined, "height": 40, "width": 40, }, + undefined, { - "borderColor": "rgba(202, 196, 208, 1)", "borderRadius": 20, - "borderWidth": 0, }, - undefined, ] } > - + + - page-first - + [ + { + "lineHeight": 24, + "transform": [ + { + "scaleX": 1, + }, + ], + }, + { + "backgroundColor": "transparent", + }, + ], + ] + } + > + page-first + + @@ -2120,120 +2282,138 @@ exports[`DataTable.Pagination renders data table pagination with options select [ { "margin": 6, + "overflow": "visible", }, { - "backgroundColor": undefined, "height": 40, "width": 40, }, + undefined, { - "borderColor": "rgba(202, 196, 208, 1)", "borderRadius": 20, - "borderWidth": 0, }, - undefined, ] } > - + + - chevron-left - + [ + { + "lineHeight": 24, + "transform": [ + { + "scaleX": 1, + }, + ], + }, + { + "backgroundColor": "transparent", + }, + ], + ] + } + > + chevron-left + + @@ -2243,120 +2423,138 @@ exports[`DataTable.Pagination renders data table pagination with options select [ { "margin": 6, + "overflow": "visible", }, { - "backgroundColor": undefined, "height": 40, "width": 40, }, + undefined, { - "borderColor": "rgba(202, 196, 208, 1)", "borderRadius": 20, - "borderWidth": 0, }, - undefined, ] } > - + + - chevron-right - + [ + { + "lineHeight": 24, + "transform": [ + { + "scaleX": 1, + }, + ], + }, + { + "backgroundColor": "transparent", + }, + ], + ] + } + > + chevron-right + + @@ -2366,120 +2564,138 @@ exports[`DataTable.Pagination renders data table pagination with options select [ { "margin": 6, + "overflow": "visible", }, { - "backgroundColor": undefined, "height": 40, "width": 40, }, + undefined, { - "borderColor": "rgba(202, 196, 208, 1)", "borderRadius": 20, - "borderWidth": 0, }, - undefined, ] } > - + + - page-last - + [ + { + "lineHeight": 24, + "transform": [ + { + "scaleX": 1, + }, + ], + }, + { + "backgroundColor": "transparent", + }, + ], + ] + } + > + page-last + + diff --git a/src/components/__tests__/__snapshots__/IconButton.test.tsx.snap b/src/components/__tests__/__snapshots__/IconButton.test.tsx.snap index c878fd720d..2e4eeef526 100644 --- a/src/components/__tests__/__snapshots__/IconButton.test.tsx.snap +++ b/src/components/__tests__/__snapshots__/IconButton.test.tsx.snap @@ -1,624 +1,835 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`renders disabled icon button 1`] = ` +exports[`applies a static transform from style 1`] = ` - + + - camera - + [ + { + "lineHeight": 24, + "transform": [ + { + "scaleX": 1, + }, + ], + }, + { + "backgroundColor": "transparent", + }, + ], + ] + } + testID="icon-button-icon" + > + camera + + `; -exports[`renders icon button by default 1`] = ` +exports[`renders a square icon button 1`] = ` - + + - camera - + [ + { + "lineHeight": 24, + "transform": [ + { + "scaleX": 1, + }, + ], + }, + { + "backgroundColor": "transparent", + }, + ], + ] + } + testID="icon-button-icon" + > + camera + + `; -exports[`renders icon button with color 1`] = ` +exports[`renders disabled icon button 1`] = ` - + + - camera - + [ + { + "lineHeight": 24, + "transform": [ + { + "scaleX": 1, + }, + ], + }, + { + "backgroundColor": "transparent", + }, + ], + ] + } + > + camera + + `; -exports[`renders icon button with custom border radius 1`] = ` +exports[`renders extraLarge icon button 1`] = ` - + + - camera - + [ + { + "lineHeight": 40, + "transform": [ + { + "scaleX": 1, + }, + ], + }, + { + "backgroundColor": "transparent", + }, + ], + ] + } + > + camera + + `; -exports[`renders icon button with size 1`] = ` +exports[`renders filled icon button 1`] = ` - + + - camera - + [ + { + "lineHeight": 24, + "transform": [ + { + "scaleX": 1, + }, + ], + }, + { + "backgroundColor": "transparent", + }, + ], + ] + } + > + camera + + `; -exports[`renders icon button with small border radius 1`] = ` +exports[`renders icon button by default 1`] = ` + + + + camera + + + + + +`; + +exports[`renders icon button with color 1`] = ` + + - + + - camera - + [ + { + "lineHeight": 24, + "transform": [ + { + "scaleX": 1, + }, + ], + }, + { + "backgroundColor": "transparent", + }, + ], + ] + } + > + camera + + @@ -631,122 +842,360 @@ exports[`renders icon change animated 1`] = ` [ { "margin": 6, + "overflow": "visible", }, { - "backgroundColor": undefined, "height": 40, "width": 40, }, + undefined, { - "borderColor": "rgba(202, 196, 208, 1)", "borderRadius": 20, - "borderWidth": 0, }, - undefined, ] } > - + - - camera - + + camera + + `; + +exports[`renders outlined icon button 1`] = ` + + + + + + camera + + + + + +`; + +exports[`renders tonal icon button 1`] = ` + + + + + + camera + + + + + +`; diff --git a/src/components/__tests__/__snapshots__/Searchbar.test.tsx.snap b/src/components/__tests__/__snapshots__/Searchbar.test.tsx.snap index 5ef225b942..945a3de40b 100644 --- a/src/components/__tests__/__snapshots__/Searchbar.test.tsx.snap +++ b/src/components/__tests__/__snapshots__/Searchbar.test.tsx.snap @@ -96,84 +96,104 @@ exports[`activity indicator snapshot test 1`] = ` [ { "margin": 6, + "overflow": "visible", }, { - "backgroundColor": undefined, "height": 40, "width": 40, }, + undefined, { - "borderColor": "rgba(202, 196, 208, 1)", "borderRadius": 20, - "borderWidth": 0, }, - undefined, ] } > - + + - magnify - + [ + { + "lineHeight": 24, + "transform": [ + { + "scaleX": 1, + }, + ], + }, + { + "backgroundColor": "transparent", + }, + ], + ] + } + > + magnify + + @@ -504,84 +524,104 @@ exports[`renders searchbar in "view" mode 1`] = ` [ { "margin": 6, + "overflow": "visible", }, { - "backgroundColor": undefined, "height": 40, "width": 40, }, + undefined, { - "borderColor": "rgba(202, 196, 208, 1)", "borderRadius": 20, - "borderWidth": 0, }, - undefined, ] } > - + + - magnify - + [ + { + "lineHeight": 24, + "transform": [ + { + "scaleX": 1, + }, + ], + }, + { + "backgroundColor": "transparent", + }, + ], + ] + } + > + magnify + + @@ -639,120 +679,138 @@ exports[`renders searchbar in "view" mode 1`] = ` [ { "margin": 6, + "overflow": "visible", }, { - "backgroundColor": undefined, "height": 40, "width": 40, }, + undefined, { - "borderColor": "rgba(202, 196, 208, 1)", "borderRadius": 20, - "borderWidth": 0, }, - undefined, ] } > - + - close - + + close + + @@ -881,84 +939,104 @@ exports[`renders with placeholder 1`] = ` [ { "margin": 6, + "overflow": "visible", }, { - "backgroundColor": undefined, "height": 40, "width": 40, }, + undefined, { - "borderColor": "rgba(202, 196, 208, 1)", "borderRadius": 20, - "borderWidth": 0, }, - undefined, ] } > - + + - magnify - + [ + { + "lineHeight": 24, + "transform": [ + { + "scaleX": 1, + }, + ], + }, + { + "backgroundColor": "transparent", + }, + ], + ] + } + > + magnify + + @@ -1016,120 +1094,138 @@ exports[`renders with placeholder 1`] = ` [ { "margin": 6, + "overflow": "visible", }, { - "backgroundColor": undefined, "height": 40, "width": 40, }, + undefined, { - "borderColor": "rgba(202, 196, 208, 1)", "borderRadius": 20, - "borderWidth": 0, }, - undefined, ] } > - + + - close - + [ + { + "lineHeight": 24, + "transform": [ + { + "scaleX": 1, + }, + ], + }, + { + "backgroundColor": "transparent", + }, + ], + ] + } + > + close + + @@ -1233,84 +1329,104 @@ exports[`renders with text 1`] = ` [ { "margin": 6, + "overflow": "visible", }, { - "backgroundColor": undefined, "height": 40, "width": 40, }, + undefined, { - "borderColor": "rgba(202, 196, 208, 1)", "borderRadius": 20, - "borderWidth": 0, }, - undefined, ] } > - + + - magnify - + [ + { + "lineHeight": 24, + "transform": [ + { + "scaleX": 1, + }, + ], + }, + { + "backgroundColor": "transparent", + }, + ], + ] + } + > + magnify + + @@ -1364,120 +1480,138 @@ exports[`renders with text 1`] = ` [ { "margin": 6, + "overflow": "visible", }, { - "backgroundColor": undefined, "height": 40, "width": 40, }, + undefined, { - "borderColor": "rgba(202, 196, 208, 1)", "borderRadius": 20, - "borderWidth": 0, }, - undefined, ] } > - + + - close - + [ + { + "lineHeight": 24, + "transform": [ + { + "scaleX": 1, + }, + ], + }, + { + "backgroundColor": "transparent", + }, + ], + ] + } + > + close + + diff --git a/src/components/__tests__/__snapshots__/TextInput.test.tsx.snap b/src/components/__tests__/__snapshots__/TextInput.test.tsx.snap index cdbe2843fe..d30566a6ee 100644 --- a/src/components/__tests__/__snapshots__/TextInput.test.tsx.snap +++ b/src/components/__tests__/__snapshots__/TextInput.test.tsx.snap @@ -175,17 +175,12 @@ exports[`dims a disabled TextInput with TextInput.Icon accessories exactly once [ { "margin": 6, + "overflow": "visible", }, { - "backgroundColor": undefined, "height": 40, "width": 40, }, - { - "borderColor": "rgba(202, 196, 208, 1)", - "borderRadius": 20, - "borderWidth": 0, - }, { "margin": 0, }, @@ -201,72 +196,97 @@ exports[`dims a disabled TextInput with TextInput.Icon accessories exactly once "opacity": 0.38, }, null, + { + "borderRadius": 20, + }, ] } > - + + - magnify - + [ + { + "lineHeight": 24, + "transform": [ + { + "scaleX": 1, + }, + ], + }, + { + "backgroundColor": "transparent", + }, + ], + ] + } + > + magnify + + @@ -342,17 +362,12 @@ exports[`dims a disabled TextInput with TextInput.Icon accessories exactly once [ { "margin": 6, + "overflow": "visible", }, { - "backgroundColor": undefined, "height": 40, "width": 40, }, - { - "borderColor": "rgba(202, 196, 208, 1)", - "borderRadius": 20, - "borderWidth": 0, - }, { "margin": 0, }, @@ -370,100 +385,123 @@ exports[`dims a disabled TextInput with TextInput.Icon accessories exactly once { "opacity": 1, }, + { + "borderRadius": 20, + }, ] } > - + + - close - + [ + { + "lineHeight": 24, + "transform": [ + { + "scaleX": 1, + }, + ], + }, + { + "backgroundColor": "transparent", + }, + ], + ] + } + > + close + + @@ -630,17 +668,12 @@ exports[`renders filled TextInput with TextInput.Icon accessories 1`] = ` [ { "margin": 6, + "overflow": "visible", }, { - "backgroundColor": undefined, "height": 40, "width": 40, }, - { - "borderColor": "rgba(202, 196, 208, 1)", - "borderRadius": 20, - "borderWidth": 0, - }, { "margin": 0, }, @@ -654,72 +687,97 @@ exports[`renders filled TextInput with TextInput.Icon accessories 1`] = ` }, false, null, + { + "borderRadius": 20, + }, ] } > - + + - magnify - + [ + { + "lineHeight": 24, + "transform": [ + { + "scaleX": 1, + }, + ], + }, + { + "backgroundColor": "transparent", + }, + ], + ] + } + > + magnify + + @@ -790,17 +848,12 @@ exports[`renders filled TextInput with TextInput.Icon accessories 1`] = ` [ { "margin": 6, + "overflow": "visible", }, { - "backgroundColor": undefined, "height": 40, "width": 40, }, - { - "borderColor": "rgba(202, 196, 208, 1)", - "borderRadius": 20, - "borderWidth": 0, - }, { "margin": 0, }, @@ -814,72 +867,97 @@ exports[`renders filled TextInput with TextInput.Icon accessories 1`] = ` }, false, null, + { + "borderRadius": 20, + }, ] } > - + + - close - + [ + { + "lineHeight": 24, + "transform": [ + { + "scaleX": 1, + }, + ], + }, + { + "backgroundColor": "transparent", + }, + ], + ] + } + > + close + + @@ -1046,17 +1124,12 @@ exports[`renders filled TextInput with TextInput.Icon accessories when error is [ { "margin": 6, + "overflow": "visible", }, { - "backgroundColor": undefined, "height": 40, "width": 40, }, - { - "borderColor": "rgba(202, 196, 208, 1)", - "borderRadius": 20, - "borderWidth": 0, - }, { "margin": 0, }, @@ -1070,72 +1143,97 @@ exports[`renders filled TextInput with TextInput.Icon accessories when error is }, false, null, + { + "borderRadius": 20, + }, ] } > - + + - magnify - + [ + { + "lineHeight": 24, + "transform": [ + { + "scaleX": 1, + }, + ], + }, + { + "backgroundColor": "transparent", + }, + ], + ] + } + > + magnify + + @@ -1206,17 +1304,12 @@ exports[`renders filled TextInput with TextInput.Icon accessories when error is [ { "margin": 6, + "overflow": "visible", }, { - "backgroundColor": undefined, "height": 40, "width": 40, }, - { - "borderColor": "rgba(202, 196, 208, 1)", - "borderRadius": 20, - "borderWidth": 0, - }, { "margin": 0, }, @@ -1230,108 +1323,131 @@ exports[`renders filled TextInput with TextInput.Icon accessories when error is }, false, null, + { + "borderRadius": 20, + }, ] } > - + + - close - + [ + { + "lineHeight": 24, + "transform": [ + { + "scaleX": 1, + }, + ], + }, + { + "backgroundColor": "transparent", + }, + ], + ] + } + > + close + + @@ -1676,17 +1792,12 @@ exports[`renders outlined TextInput with TextInput.Icon accessories 1`] = ` [ { "margin": 6, + "overflow": "visible", }, { - "backgroundColor": undefined, "height": 40, "width": 40, }, - { - "borderColor": "rgba(202, 196, 208, 1)", - "borderRadius": 20, - "borderWidth": 0, - }, { "margin": 0, }, @@ -1700,72 +1811,97 @@ exports[`renders outlined TextInput with TextInput.Icon accessories 1`] = ` }, false, null, + { + "borderRadius": 20, + }, ] } > - + + - magnify - + [ + { + "lineHeight": 24, + "transform": [ + { + "scaleX": 1, + }, + ], + }, + { + "backgroundColor": "transparent", + }, + ], + ] + } + > + magnify + + @@ -1836,17 +1972,12 @@ exports[`renders outlined TextInput with TextInput.Icon accessories 1`] = ` [ { "margin": 6, + "overflow": "visible", }, { - "backgroundColor": undefined, "height": 40, "width": 40, }, - { - "borderColor": "rgba(202, 196, 208, 1)", - "borderRadius": 20, - "borderWidth": 0, - }, { "margin": 0, }, @@ -1860,72 +1991,97 @@ exports[`renders outlined TextInput with TextInput.Icon accessories 1`] = ` }, false, null, + { + "borderRadius": 20, + }, ] } > - + + - close - + [ + { + "lineHeight": 24, + "transform": [ + { + "scaleX": 1, + }, + ], + }, + { + "backgroundColor": "transparent", + }, + ], + ] + } + > + close + + @@ -2072,17 +2228,12 @@ exports[`renders outlined TextInput with TextInput.Icon accessories when error i [ { "margin": 6, + "overflow": "visible", }, { - "backgroundColor": undefined, "height": 40, "width": 40, }, - { - "borderColor": "rgba(202, 196, 208, 1)", - "borderRadius": 20, - "borderWidth": 0, - }, { "margin": 0, }, @@ -2096,72 +2247,97 @@ exports[`renders outlined TextInput with TextInput.Icon accessories when error i }, false, null, + { + "borderRadius": 20, + }, ] } > - + + - magnify - + [ + { + "lineHeight": 24, + "transform": [ + { + "scaleX": 1, + }, + ], + }, + { + "backgroundColor": "transparent", + }, + ], + ] + } + > + magnify + + @@ -2232,17 +2408,12 @@ exports[`renders outlined TextInput with TextInput.Icon accessories when error i [ { "margin": 6, + "overflow": "visible", }, { - "backgroundColor": undefined, "height": 40, "width": 40, }, - { - "borderColor": "rgba(202, 196, 208, 1)", - "borderRadius": 20, - "borderWidth": 0, - }, { "margin": 0, }, @@ -2256,108 +2427,131 @@ exports[`renders outlined TextInput with TextInput.Icon accessories when error i }, false, null, + { + "borderRadius": 20, + }, ] } > - + + - close - + [ + { + "lineHeight": 24, + "transform": [ + { + "scaleX": 1, + }, + ], + }, + { + "backgroundColor": "transparent", + }, + ], + ] + } + > + close + + diff --git a/src/components/__tests__/focusRingWiring.test.tsx b/src/components/__tests__/focusRingWiring.test.tsx index 80817b0281..61d411aac8 100644 --- a/src/components/__tests__/focusRingWiring.test.tsx +++ b/src/components/__tests__/focusRingWiring.test.tsx @@ -565,6 +565,45 @@ describe('focus ring wiring', () => { }); }); +describe('IconButton focus ring', () => { + it('draws one ring, on the view that carries the outline', async () => { + await render( + {}} + /> + ); + + await focusFirstFocusable(); + + const root = (screen as unknown as { root: Node }).root; + const ringed = walk(root, (n) => style(n).outlineStyle === 'solid'); + expect(ringed).toHaveLength(1); + // The border sits on the same view, so the ring clears it. + expect(style(ringed[0])).toMatchObject({ + outlineOffset: OUTWARD, + borderWidth: 1, + }); + }); + + it('draws no ring while disabled', async () => { + await render( + {}} + disabled + /> + ); + + await focusFirstFocusable(); + + expect(ringOffset()).toBeUndefined(); + }); +}); + // A component's `theme` prop sets its ring color, not only the provider's. describe('focus ring color follows the theme prop', () => { const theme = { colors: { secondary: 'rgb(255, 0, 0)' } }; diff --git a/src/index.tsx b/src/index.tsx index 1807d2f249..be72dc8bff 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -105,6 +105,12 @@ export type { Size as FABSize, } from './components/FAB/tokens'; export type { Props as IconButtonProps } from './components/IconButton/IconButton'; +export type { + Mode as IconButtonMode, + Size as IconButtonSize, + Width as IconButtonWidth, + Shape as IconButtonShape, +} from './components/IconButton/tokens'; export type { Props as ListAccordionProps } from './components/List/ListAccordion'; export type { Props as ListAccordionGroupProps } from './components/List/ListAccordionGroup'; export type { Props as ListIconProps } from './components/List/ListIcon';