diff --git a/docs/6.x/docs/guides/migration.md b/docs/6.x/docs/guides/migration.md index 5fd0c7ad10..91fd29bef3 100644 --- a/docs/6.x/docs/guides/migration.md +++ b/docs/6.x/docs/guides/migration.md @@ -93,6 +93,7 @@ The following component style props no longer support overriding their backgroun - `Card` - `Chip` - `Dialog` +- `IconButton` - `Menu`: `contentStyle` - `Searchbar` - `Snackbar` @@ -506,7 +507,7 @@ A `theme` set on a part is merged over the `theme` of the `Appbar`, so it takes #### Material 3 Expressive - The `mode` prop of `Appbar` and `Appbar.Header` accepts two new values: `medium-flexible` and `large-flexible`. Their height is the minimum height (`112` and `120`), and they grow when the title wraps onto a second line. -- `Appbar.Action` has new `variant` and `width` props. `variant="filled"` and `variant="tonal"` render the action in a container, and `width="wide"` makes the container wider. A filled or tonal action is selected by default, and passing `selected={false}` shows the unselected container. +- `Appbar.Action` has new `variant` and `width` props. `variant="filled"` and `variant="tonal"` render the action in a container, and `width="wide"` makes the container wider. Without `selected` the action shows the primary or the secondary container color. Pass `selected` to make it a toggle, with the selected and unselected colors of the icon button. - `Appbar` and `Appbar.Header` have a new `isScrolled` prop. Set it when the content below has scrolled, to switch the container to the `surfaceContainer` color. Unlike `elevated`, it doesn't add elevation. ```tsx @@ -839,10 +840,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. @@ -1367,6 +1364,141 @@ 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: `'extra-small' | 'small' | 'medium' | 'large' | 'extra-large'`, the same names as `Button`. It defaults to `'small'`, which still uses a 24dp icon. + +To set the glyph size in dp without changing the container scale, use `iconSize`. The container keeps the size of its `size` (a square 40dp for the default), so a glyph larger than it is clipped. Pick a larger `size` instead. + +```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 `extra-small` 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) + + +``` + +#### `containerColor` while disabled + +A disabled `filled` or `tonal` button, or a disabled selected `outlined` one, takes the disabled container color, `onSurface` at 0.10 opacity, and ignores `containerColor`. As in 5.x, a disabled button doesn't keep the color passed for its container, just as `iconColor` doesn't apply either. + +#### `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 90f9f0273c..e18135fc49 100644 --- a/docs/src/data/screenshots.ts +++ b/docs/src/data/screenshots.ts @@ -76,10 +76,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 c283c119e0..e22df5ed4e 100644 --- a/docs/src/data/themeColors.ts +++ b/docs/src/data/themeColors.ts @@ -219,56 +219,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..10e74e9b52 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[] = [ + 'extra-small', + 'small', + 'medium', + 'large', + 'extra-large', +]; +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/ActivityIndicator.tsx b/src/components/ActivityIndicator.tsx index c8a3e295a0..dd794ff2e2 100644 --- a/src/components/ActivityIndicator.tsx +++ b/src/components/ActivityIndicator.tsx @@ -141,11 +141,15 @@ const ActivityIndicator = ({ overflow: 'hidden' as const, }; + // A hidden indicator, such as the one in a loading IconButton, is no element + // of its own. Left accessible, Android would still make it a keyboard stop. + const isHidden = rest['aria-hidden'] === true; + return ( diff --git a/src/components/Appbar/AppbarAction.tsx b/src/components/Appbar/AppbarAction.tsx index 7eb9c00b61..d12645d936 100644 --- a/src/components/Appbar/AppbarAction.tsx +++ b/src/components/Appbar/AppbarAction.tsx @@ -1,5 +1,4 @@ import * as React from 'react'; -import { StyleSheet } from 'react-native'; import type { ColorValue, StyleProp, View, ViewStyle } from 'react-native'; import type { AnimatedStyle } from 'react-native-reanimated'; @@ -20,10 +19,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. */ @@ -52,8 +47,8 @@ export type Props = React.PropsWithoutRef & { * - `tonal` - Icon in a container with the secondary container color. * * The `filled` and `tonal` variants set the `mode` of the icon button, so its `mode` prop is ignored. - * They are selected by default, with the primary or the secondary container color. Pass `selected={false}` to - * show the unselected container, for example to toggle the action. + * Without `selected` they show the primary or the secondary container color. `selected` makes the action a + * toggle, with the selected and unselected colors of the icon button. */ variant?: 'standard' | 'filled' | 'tonal'; /** @@ -95,7 +90,6 @@ export type Props = React.PropsWithoutRef & { * ``` */ const AppbarAction = ({ - size = 24, color: iconColor, icon, disabled, @@ -123,23 +117,22 @@ const AppbarAction = ({ (isLeading ? colors.onSurface : colors.onSurfaceVariant)) : iconColor; - // A filled or tonal action is an icon button in the matching mode, which is - // selected unless `selected` is passed, so `variant` takes over `mode` - const variantProps = isStandard - ? { mode, selected } - : { mode: variantIconButtonModes[variant], selected: selected ?? true }; + // `filled` and `tonal` are also modes of the icon button, so `variant` takes + // over `mode`. `selected` goes through as it is: without it the action is no + // toggle and has the default container color. + const iconButtonMode = isStandard ? mode : variant; 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..82730ced44 100644 --- a/src/components/IconButton/IconButton.tsx +++ b/src/components/IconButton/IconButton.tsx @@ -1,62 +1,98 @@ import * as React from 'react'; import { Platform, StyleSheet, View } from 'react-native'; import type { + BlurEvent, ColorValue, + FocusEvent, GestureResponderEvent, StyleProp, ViewStyle, } from 'react-native'; -import Animated, { type AnimatedStyle } from 'react-native-reanimated'; +import Animated, { + type AnimatedStyle, + linear, + 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 { toSpringCurve } from '../../theme/tokens/sys/motion'; 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 { + getButtonRippleColor, + getButtonShapeCurvePoints, +} from '../Button/utils'; import CrossFadeIcon from '../CrossFadeIcon'; import Icon from '../Icon'; 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'; +const AnimatedTouchableRipple = + Animated.createAnimatedComponent(TouchableRipple); 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. */ iconColor?: ColorValue; /** - * Background color of the icon container. + * Background color of the icon container. A disabled `filled` or `tonal` + * button, or a disabled selected `outlined` one, takes the disabled + * container color instead. */ 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. Screen readers announce the toggle state: `aria-pressed` + * on web, `aria-checked` on native. */ 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. */ - size?: number; + 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. + */ + iconSize?: number; /** * Whether the button is disabled. A disabled button is greyed out and `onPress` is not called on touch. */ @@ -71,13 +107,17 @@ 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; /** * Function to execute on press. */ onPress?: (e: GestureResponderEvent) => void; + /** + * Style of the button container, for positioning and size. The corner comes + * from `shape`, so a `borderRadius` here is ignored. + */ style?: StyleProp>; ref?: React.Ref>; /** @@ -89,7 +129,9 @@ export type Props = Omit< */ theme?: ThemeProp; /** - * Whether to show a loading indicator. + * Whether to show a loading indicator in place of the icon. The button is + * marked busy for assistive technology, but it is not disabled: pass + * `disabled` too if presses should be ignored while it loads. */ loading?: boolean; }; @@ -106,7 +148,7 @@ export type Props = Omit< * console.log('Pressed')} * /> * ); @@ -120,33 +162,54 @@ 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, loading = false, + 'aria-busy': ariaBusy, contentStyle, hitSlop, role = 'button', + focusRing: focusRingPlacement = 'outward', 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 +217,121 @@ const IconButton = ({ mode, customIconColor, customContainerColor, + outlineWidth: dimensions.outlineWidth, }); - const buttonSize = size + 2 * PADDING; - const borderWidth = mode === 'outlined' && !selected ? 1 : 0; + const hasPassedTouchHandler = hasTouchHandler({ + onPress, + onLongPress: rest.onLongPress, + onPressIn: onPressInProp, + onPressOut: onPressOutProp, + }); - const shapeStyle = { borderRadius: buttonSize / 2 }; + const canBePressed = hasPassedTouchHandler && !disabled; - const borderStyles = { - borderWidth, - borderColor, - ...shapeStyle, + // The handlers that reset `pressed` are only attached while the button can be + // pressed, so a stale `pressed` must not outlive that. + const isPressed = pressed && canBePressed; + React.useEffect(() => { + // Clears it too, so the shape doesn't snap back to pressed if the handler + // returns. + if (!canBePressed) { + setPressed(false); + } + }, [canBePressed]); + const borderRadius = isPressed + ? dimensions.pressedRadius + : dimensions.restingRadius; + // The morph follows the MD3 fast spatial spring, as on Button. The curve is + // capped so a bouncy spring can't push the corner below 0 on its way back + // from the largest radius (the round one). + const shapeSpring = React.useMemo( + () => toSpringCurve(theme.motion.spring.fast.spatial), + [theme.motion.spring.fast.spatial] + ); + const largestRadius = Math.max( + dimensions.height / 2, + dimensions.restingRadius, + dimensions.pressedRadius + ); + const shapeTimingFunction = React.useMemo( + () => + linear( + ...getButtonShapeCurvePoints({ + points: shapeSpring.points, + radius: borderRadius, + largestRadius, + }) + ), + [shapeSpring.points, borderRadius, largestRadius] + ); + const radiusStyle: CSSStyle = { + borderRadius, + transitionProperty: ['borderRadius'], + transitionDuration: reduceMotion ? 0 : shapeSpring.duration, + transitionTimingFunction: shapeTimingFunction, }; - const touchableSize = buttonSize - 2 * borderWidth; + // The state layer takes the icon color, as Button's takes its label color. + const rippleColor = getButtonRippleColor({ labelColor: iconColor }); - const defaultHitSlop = disabled - ? undefined - : getMinInteractiveSizeHitSlop({ - width: touchableSize, - height: touchableSize, - }); + const handlePressIn = (e: GestureResponderEvent) => { + setPressed(true); + onPressInProp?.(e); + }; + + const handlePressOut = (e: GestureResponderEvent) => { + setPressed(false); + onPressOutProp?.(e); + }; + + // 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( + !canBePressed, + theme.colors.secondary, + focusRingPlacement, + 'within' + ); + + const handleFocus = (e: FocusEvent) => { + onFocusProp?.(e); + focusTarget.onFocus?.(e); + }; + + const handleBlur = (e: BlurEvent) => { + onBlurProp?.(e); + focusTarget.onBlur?.(); + }; + + // The button is busy while it loads, so the indicator inside it is no + // progress element of its own. Always a boolean: on Android `undefined` + // leaves TalkBack saying "busy" after loading ends. As on React Native, an + // `aria-busy` prop wins over `accessibilityState.busy`. + const busy = loading || (ariaBusy ?? rest.accessibilityState?.busy) === true; + + // Announced like Button: `aria-pressed` on web, `aria-checked` on native. + // `aria-selected` isn't valid on `role="button"`, so it would go unannounced. + // React Native's types have no `aria-pressed`. + const toggleStateProps = + selected === undefined + ? {} + : Platform.OS === 'web' + ? { 'aria-pressed': selected } + : { 'aria-checked': selected }; + + // 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; + + // Disabled and handler-less buttons get no extra target. + const defaultHitSlop = canBePressed + ? getMinInteractiveSizeHitSlop({ + width: touchableWidth, + height: touchableHeight, + }) + : undefined; // 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,13 +342,18 @@ const IconButton = ({ role === 'button' && !disabled && ariaLabel !== undefined && - !hasTouchHandler({ - onPress, - onLongPress: rest.onLongPress, - onPressIn: rest.onPressIn, - onPressOut: rest.onPressOut, - }) && - !carriesToggleState(rest); + !hasPassedTouchHandler && + !carriesToggleState({ ...rest, ...toggleStateProps }); + + // The touchable and the disabled layer sit inside the outline, so their + // corner is the container's less the outline, and they morph with it. The + // touchable clips its own ripple to it, though not on web, where + // `overflow: 'hidden'` would also clip the hit slop target; there the ripple + // container follows the touchable's corner. + const innerRadiusStyle: CSSStyle = { + ...radiusStyle, + borderRadius: Math.max(0, borderRadius - borderWidth), + }; return ( - {backgroundOpacity < 1 && ( - - )} - - - {loading ? ( - - ) : ( - - )} - - + {backgroundOpacity < 1 && ( + + )} + + + {loading ? ( + + ) : ( + + )} + + + ); }; @@ -253,12 +443,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..ce7c081e72 --- /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 = 'extra-small' | 'small' | 'medium' | 'large' | 'extra-large'; + +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 = { + 'extra-small': { + 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 }, + }, + }, + 'extra-large': { + 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..5ba3cd6839 100644 --- a/src/components/IconButton/utils.ts +++ b/src/components/IconButton/utils.ts @@ -1,155 +1,181 @@ 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; - } - - if (typeof customIconColor !== 'undefined') { - return customIconColor; + 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 (isMode('contained')) { - if (selected) { - return theme.colors.onPrimary; - } - return theme.colors.primary; - } + const set = colorSetFor(mode, selected); - if (isMode('contained-tonal')) { - if (selected) { - return theme.colors.onSecondaryContainer; - } - return theme.colors.onSurfaceVariant; - } + return { + iconColor: disabled + ? theme.colors[IconButtonTokens.disabled.icon] + : (customIconColor ?? theme.colors[set.icon]), + iconOpacity, + backgroundColor: + customContainerColor ?? + (set.container ? theme.colors[set.container] : undefined), + 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; + // From the spec icon, not `iconSize`: the container keeps the scale of its + // `size` and a `default` width stays square when the glyph is resized. + const containerWidth = spec.icon + 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 c76650a193..6d6149fc0e 100644 --- a/src/components/TextInput/TextInputIcon.tsx +++ b/src/components/TextInput/TextInputIcon.tsx @@ -26,7 +26,12 @@ export type TextInputAccessoryProps = { }; export type TextInputIconProps = TextInputAccessoryProps & - Omit; + Omit & { + /** + * Size of the icon. `iconSize` isn't accepted: the size comes from here. + */ + size?: number; + }; /** * A component to render a leading / trailing icon in the TextInput @@ -132,7 +137,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/TouchableRipple/TouchableRipple.tsx b/src/components/TouchableRipple/TouchableRipple.tsx index f1bb352835..c488634945 100644 --- a/src/components/TouchableRipple/TouchableRipple.tsx +++ b/src/components/TouchableRipple/TouchableRipple.tsx @@ -185,7 +185,6 @@ const TouchableRipple = ({ const { centered } = rest; const button = e.currentTarget; - const style = window.getComputedStyle(button); const dimensions = button.getBoundingClientRect(); let touchX; @@ -222,10 +221,9 @@ const TouchableRipple = ({ left: '0', right: '0', bottom: '0', - borderTopLeftRadius: style.borderTopLeftRadius, - borderTopRightRadius: style.borderTopRightRadius, - borderBottomRightRadius: style.borderBottomRightRadius, - borderBottomLeftRadius: style.borderBottomLeftRadius, + // Follows the touchable's corner, which can animate while the + // ripple is held, rather than the one it had when pressed. + borderRadius: 'inherit', overflow: 'hidden', }); diff --git a/src/components/__tests__/ActivityIndicator.test.tsx b/src/components/__tests__/ActivityIndicator.test.tsx index c70d6cd3d1..4943523537 100644 --- a/src/components/__tests__/ActivityIndicator.test.tsx +++ b/src/components/__tests__/ActivityIndicator.test.tsx @@ -37,6 +37,23 @@ it('renders colored indicator', async () => { expect(tree).toMatchSnapshot(); }); +it('is a progress bar for assistive technology', async () => { + await render(); + + expect(screen.getByRole('progressbar', { busy: true })).toBeOnTheScreen(); +}); + +it('is no accessible element when hidden, so no keyboard stop either', async () => { + await render(); + + expect( + screen.queryByRole('progressbar', { includeHiddenElements: true }) + ).not.toBeOnTheScreen(); + expect( + screen.getByTestId('indicator', { includeHiddenElements: true }) + ).toHaveProp('accessible', false); +}); + describe('with reduce motion', () => { it.each([ { reduceMotion: 'off', fade: 200 }, diff --git a/src/components/__tests__/Appbar/Appbar.test.tsx b/src/components/__tests__/Appbar/Appbar.test.tsx index f69042930e..28c927d95a 100644 --- a/src/components/__tests__/Appbar/Appbar.test.tsx +++ b/src/components/__tests__/Appbar/Appbar.test.tsx @@ -886,9 +886,9 @@ describe('AppbarAction', () => { ); - // The container of the button is the one with the width + // The container of the button is the one with the width, around the clip expect( - screen.getByRole('button', { name: 'Favorite' }).parent + screen.getByRole('button', { name: 'Favorite' }).parent?.parent ).toHaveStyle({ width: expected }); } ); @@ -896,68 +896,76 @@ describe('AppbarAction', () => { describe.each([ { variant: 'filled', + defaults: { container: 'primary', icon: 'onPrimary' }, selected: { container: 'primary', icon: 'onPrimary' }, - unselected: { container: 'surfaceVariant', icon: 'primary' }, + unselected: { container: 'surfaceContainer', icon: 'onSurfaceVariant' }, }, { variant: 'tonal', - selected: { + defaults: { + container: 'secondaryContainer', + icon: 'onSecondaryContainer', + }, + selected: { container: 'secondary', icon: 'onSecondary' }, + unselected: { container: 'secondaryContainer', icon: 'onSecondaryContainer', }, - unselected: { container: 'surfaceVariant', icon: 'onSurfaceVariant' }, }, - ] as const)('a $variant action', ({ variant, selected, unselected }) => { - const renderAction = async (props: { selected?: boolean }) => { - await render( - - ( - {String(color)} - )} - aria-label="Favorite" - variant={variant} - onPress={() => {}} - {...props} - /> - - ); - }; + ] as const)( + 'a $variant action', + ({ variant, defaults, selected, unselected }) => { + const renderAction = async (props: { selected?: boolean }) => { + await render( + + ( + {String(color)} + )} + aria-label="Favorite" + variant={variant} + onPress={() => {}} + {...props} + /> + + ); + }; - // The container is the view around the button - const container = () => - screen.getByRole('button', { name: 'Favorite' }).parent; + // The clip, the view around the button, has the background + const container = () => + screen.getByRole('button', { name: 'Favorite' }).parent; - it('is selected when selected is not passed', async () => { - await renderAction({}); + it('has the default container color when selected is not passed', async () => { + await renderAction({}); - expect(container()).toHaveStyle({ - backgroundColor: LightTheme.colors[selected.container], + expect(container()).toHaveStyle({ + backgroundColor: LightTheme.colors[defaults.container], + }); + expect(screen.getByTestId('icon-color')).toHaveTextContent( + String(LightTheme.colors[defaults.icon]) + ); }); - expect(screen.getByTestId('icon-color')).toHaveTextContent( - String(LightTheme.colors[selected.icon]) - ); - }); - it('is selected when selected is true', async () => { - await renderAction({ selected: true }); + it('is selected when selected is true', async () => { + await renderAction({ selected: true }); - expect(container()).toHaveStyle({ - backgroundColor: LightTheme.colors[selected.container], + expect(container()).toHaveStyle({ + backgroundColor: LightTheme.colors[selected.container], + }); }); - }); - it('shows the unselected container when selected is false', async () => { - await renderAction({ selected: false }); + it('shows the unselected container when selected is false', async () => { + await renderAction({ selected: false }); - expect(container()).toHaveStyle({ - backgroundColor: LightTheme.colors[unselected.container], + expect(container()).toHaveStyle({ + backgroundColor: LightTheme.colors[unselected.container], + }); + expect(screen.getByTestId('icon-color')).toHaveTextContent( + String(LightTheme.colors[unselected.icon]) + ); }); - expect(screen.getByTestId('icon-color')).toHaveTextContent( - String(LightTheme.colors[unselected.icon]) - ); - }); - }); + } + ); it('does not override the icon color of a filled action by the Appbar', async () => { await render( diff --git a/src/components/__tests__/Appbar/__snapshots__/Appbar.test.tsx.snap b/src/components/__tests__/Appbar/__snapshots__/Appbar.test.tsx.snap index 7a8b69936e..c13c51a6cd 100644 --- a/src/components/__tests__/Appbar/__snapshots__/Appbar.test.tsx.snap +++ b/src/components/__tests__/Appbar/__snapshots__/Appbar.test.tsx.snap @@ -141,84 +141,106 @@ 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 + + @@ -276,120 +298,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 + + @@ -450,163 +490,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, }, - false, - undefined, ] } > - + - + > + + @@ -671,156 +728,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, }, - false, - undefined, ] } > - + - - menu - + + menu + + @@ -881,121 +955,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, }, - false, - undefined, ] } > - + - - menu - + + menu + + @@ -1056,121 +1151,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, }, - false, - undefined, ] } > - + - - menu - + + menu + + @@ -1231,121 +1347,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, }, - false, - undefined, ] } > - + - - menu - + + menu + + @@ -1406,128 +1543,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, }, - false, - undefined, ] } > - + - + > + + diff --git a/src/components/__tests__/IconButton.test.tsx b/src/components/__tests__/IconButton.test.tsx index a1dc4418ff..e05d439558 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 { @@ -8,22 +9,29 @@ import { it, jest, } from '@jest/globals'; +import color from 'color'; -import { render, screen } from '../../test-utils'; +import PaperProvider from '../../core/PaperProvider'; +import { fireEvent, render, screen, userEvent } from '../../test-utils'; import { pink500 } from '../../theme/colors'; import { LightTheme } from '../../theme/schemes'; import { tokens } from '../../theme/tokens'; +import { toSpringCurve } from '../../theme/tokens/sys/motion'; import IconButton from '../IconButton/IconButton'; -import { getIconButtonColor } from '../IconButton/utils'; +import { IconButtonTokens } from '../IconButton/tokens'; +import { + getDimensions, + getIconButtonColor, + resolveShapeToken, +} from '../IconButton/utils'; +import * as rippleUtils from '../TouchableRipple/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 +44,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 tonal icon button', async () => { + const tree = ( + await render() ).toJSON(); expect(tree).toMatchSnapshot(); }); -it('renders icon button with size', async () => { - const tree = (await render()).toJSON(); +it('renders outlined icon button', async () => { + const tree = ( + await render() + ).toJSON(); + + expect(tree).toMatchSnapshot(); +}); + +it('renders extra-large 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 +112,161 @@ 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.each([true, false])( + 'announces the toggle state %s with aria-checked on native', + async (selected) => { + await render( + {}} /> + ); + + expect(screen.getByRole('button')).toHaveProp( + 'accessibilityState', + expect.objectContaining({ checked: selected }) + ); + expect(screen.getByRole('button')).not.toBeSelected(); + } +); + +it('does not claim a toggle state without `selected`', async () => { + await render( {}} />); + + expect(screen.getByRole('button')).not.toHaveProp( + 'accessibilityState', + expect.objectContaining({ checked: expect.any(Boolean) }) + ); +}); + +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')).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')).toHaveStyle({ + borderRadius: theme.shapes.corner.medium, + }); + + await user.press(screen.getByTestId('icon-button')); + + expect(screen.getByTestId('icon-button')).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')).toHaveStyle({ + borderRadius: getDimensions({ theme }).pressedRadius, + }); +}); + +it('drops the pressed shape when the handler is removed mid-press', async () => { + const { rerender } = await render( + {}} /> + ); + + await fireEvent(screen.getByTestId('icon-button'), 'pressIn'); + expect(screen.getByTestId('icon-button')).toHaveStyle({ + borderRadius: getDimensions({ theme }).pressedRadius, + }); + + await rerender(); + expect(screen.getByTestId('icon-button')).toHaveStyle({ + borderRadius: getDimensions({ theme }).restingRadius, + }); + + // Back with a handler, the shape is at rest, not stuck pressed. + await rerender( + {}} /> + ); + expect(screen.getByTestId('icon-button')).toHaveStyle({ + borderRadius: getDimensions({ theme }).restingRadius, + }); +}); + +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 +303,7 @@ describe('getIconButtonColor - icon color', () => { it('should return custom icon color', () => { expect( getIconButtonColor({ - theme: LightTheme, + theme, customIconColor: 'purple', }) ).toMatchObject({ @@ -148,13 +311,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 +323,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 +337,111 @@ describe('getIconButtonColor - icon color', () => { }); }); - it('should return theme icon color, for theme version 3, mode contained', () => { + it.each(['filled', 'tonal'] as const)( + 'should let the disabled container outrank an explicit container color in %s mode', + (mode) => { + expect( + getIconButtonColor({ + theme: LightTheme, + mode, + disabled: true, + customContainerColor: 'purple', + }) + ).toMatchObject({ + backgroundColor: LightTheme.colors.onSurface, + backgroundOpacity: IconButtonTokens.disabled.containerOpacity, + }); + } + ); + + 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 +450,7 @@ describe('getIconButtonColor - background color', () => { it('should return custom background color', () => { expect( getIconButtonColor({ - theme: LightTheme, + theme, customContainerColor: 'purple', }) ).toMatchObject({ @@ -277,83 +458,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,28 +544,124 @@ 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 extra-small narrow size', () => { + expect( + getDimensions({ theme, size: 'extra-small', width: 'narrow' }) + ).toMatchObject({ + width: 28, + height: 32, + iconSize: 20, + }); + }); + + it('keeps a default width square when `iconSize` changes the glyph', () => { + expect(getDimensions({ theme, iconSize: 32 })).toMatchObject({ + width: 40, + height: 40, + iconSize: 32, + }); + }); + + it('returns extra-large wide size', () => { + expect( + getDimensions({ theme, size: 'extra-large', width: 'wide' }) + ).toMatchObject({ + width: 184, + height: 136, + iconSize: 40, + }); + }); +}); + +describe('toggle state on web', () => { + beforeEach(() => { + jest.replaceProperty(Platform, 'OS', 'web'); + }); + + afterEach(() => { + jest.restoreAllMocks(); + }); + + it('is announced with aria-pressed, which fits a button', async () => { + await render( + {}} + /> + ); + + const button = screen.getByRole('button', { name: 'Favorite' }); + expect(button).toHaveProp('aria-pressed', true); + expect(button).not.toHaveProp('aria-selected'); + }); +}); + describe('hitSlop', () => { it('keeps the 48dp default when a forwarded hitSlop is undefined', async () => { await render( @@ -504,3 +782,220 @@ describe('without a touch handler', () => { }); }); }); + +describe('state layer', () => { + afterEach(() => { + jest.restoreAllMocks(); + }); + + it('follows the icon color, not the surface color', async () => { + const spy = jest.spyOn(rippleUtils, 'getTouchableRippleColors'); + + await render( + {}} + /> + ); + + expect(spy).toHaveBeenCalledWith( + expect.objectContaining({ + rippleColor: color(String(theme.colors.onPrimary)) + .alpha(stateOpacity.pressed) + .rgb() + .string(), + }) + ); + }); + + it('follows a custom icon color', async () => { + const spy = jest.spyOn(rippleUtils, 'getTouchableRippleColors'); + + await render( + {}} + /> + ); + + expect(spy).toHaveBeenCalledWith( + expect.objectContaining({ + rippleColor: color('#123456') + .alpha(stateOpacity.pressed) + .rgb() + .string(), + }) + ); + }); +}); + +describe('shape morph', () => { + const springDuration = toSpringCurve( + theme.motion.spring.fast.spatial + ).duration; + + // The touchable sits in the clip, which carries the radius transition. + const transitionDuration = () => { + const clip = screen.getByTestId('icon-button').parent; + + if (!clip) { + throw new Error('Expected the touchable to be wrapped in the clip view.'); + } + + // Reanimated's Jest setup keeps CSS transitions out of `style`. + // eslint-disable-next-line no-restricted-syntax + return StyleSheet.flatten(clip.props.jestInlineStyle).transitionDuration; + }; + + it('runs on the fast spatial spring', async () => { + await render( + {}} /> + ); + + expect(transitionDuration()).toBe(springDuration); + }); + + it('snaps under reduce motion', async () => { + await render( + + {}} /> + + ); + + expect(transitionDuration()).toBe(0); + }); +}); + +describe('inner corner', () => { + it('is the container corner less the outline on the touchable', async () => { + const { restingRadius, outlineWidth } = getDimensions({ + theme, + size: 'extra-large', + }); + + await render( + {}} + /> + ); + + expect(outlineWidth).toBeGreaterThan(0); + expect(screen.getByTestId('icon-button')).toHaveStyle({ + borderRadius: restingRadius - outlineWidth, + }); + }); + + it('is the container corner without an outline', async () => { + const { restingRadius } = getDimensions({ theme, size: 'extra-large' }); + + await render( + {}} + /> + ); + + expect(screen.getByTestId('icon-button')).toHaveStyle({ + borderRadius: restingRadius, + }); + }); +}); + +describe('loading', () => { + it('marks the button busy and hides the indicator', async () => { + await render( + {}} + /> + ); + + expect( + screen.getByRole('button', { name: 'Upload', busy: true }) + ).toBeOnTheScreen(); + // Not even a hidden element: one would still be a keyboard stop on Android. + expect( + screen.queryByRole('progressbar', { includeHiddenElements: true }) + ).not.toBeOnTheScreen(); + }); + + it('does not disable the button', async () => { + const onPress = jest.fn<() => void>(); + + await render( + + ); + await userEvent.press(screen.getByRole('button', { name: 'Upload' })); + + expect(onPress).toHaveBeenCalledTimes(1); + }); + + describe('on Android', () => { + beforeEach(() => { + jest.replaceProperty(Platform, 'OS', 'android'); + }); + + afterEach(() => { + jest.restoreAllMocks(); + }); + + it('passes busy as false, not undefined, once loading ends', async () => { + const { rerender } = await render( + {}} + /> + ); + + expect(screen.getByTestId('icon-button')).toHaveProp( + 'accessibilityState', + expect.objectContaining({ busy: true }) + ); + + await rerender( + {}} + /> + ); + + expect(screen.getByTestId('icon-button')).toHaveProp( + 'accessibilityState', + expect.objectContaining({ busy: false }) + ); + }); + }); + + it('lets a given aria-busy mark a button that is not loading', async () => { + await render( + {}} + /> + ); + + expect( + screen.getByRole('button', { name: 'Upload', busy: true }) + ).toBeOnTheScreen(); + }); +}); diff --git a/src/components/__tests__/TextInput.test.tsx b/src/components/__tests__/TextInput.test.tsx index 61ba6bbe27..8b901f09bd 100644 --- a/src/components/__tests__/TextInput.test.tsx +++ b/src/components/__tests__/TextInput.test.tsx @@ -1821,3 +1821,18 @@ it('keeps the 48dp TextInput.Icon target when a forwarded hitSlop is undefined', right: 12, }); }); + +it('takes the glyph size from `size`, not `iconSize`', async () => { + await render( + ( + // @ts-expect-error `iconSize` is not a TextInput.Icon prop + + )} + /> + ); + + expect(screen.getByLabelText('Search')).toBeOnTheScreen(); +}); diff --git a/src/components/__tests__/TextInputAccessibility.test.tsx b/src/components/__tests__/TextInputAccessibility.test.tsx index 7cbdef5ea1..2fc7ec7f83 100644 --- a/src/components/__tests__/TextInputAccessibility.test.tsx +++ b/src/components/__tests__/TextInputAccessibility.test.tsx @@ -822,10 +822,10 @@ it('preserves decorative loading and content styles without accessible controls' /> ); expect(screen.queryAllByRole('button')).toHaveLength(0); - expect(screen.queryByRole('progressbar')).toBeNull(); + // The indicator is no element at all, not even a hidden one. expect( - screen.getByRole('progressbar', { includeHiddenElements: true }) - ).toBeOnTheScreen(); + screen.queryByRole('progressbar', { includeHiddenElements: true }) + ).toBeNull(); expect( screen.getByTestId('search-decoration', { includeHiddenElements: true }) ).toHaveStyle({ padding: 2 }); diff --git a/src/components/__tests__/__snapshots__/DataTable.test.tsx.snap b/src/components/__tests__/__snapshots__/DataTable.test.tsx.snap index 7fe8707383..adcf8a5921 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..0ff39c064b 100644 --- a/src/components/__tests__/__snapshots__/IconButton.test.tsx.snap +++ b/src/components/__tests__/__snapshots__/IconButton.test.tsx.snap @@ -1,624 +1,841 @@ // 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", + }, + ], + ] + } + > + camera + + `; -exports[`renders icon button by default 1`] = ` +exports[`renders a square icon button 1`] = ` - + + - camera - + [ + { + "lineHeight": 24, + "transform": [ + { + "scaleX": 1, + }, + ], + }, + { + "backgroundColor": "transparent", + }, + ], + ] + } + > + 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 extra-large 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 +848,366 @@ 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..a8d788e309 100644 --- a/src/components/__tests__/__snapshots__/Searchbar.test.tsx.snap +++ b/src/components/__tests__/__snapshots__/Searchbar.test.tsx.snap @@ -96,84 +96,106 @@ 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 +526,106 @@ 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 +683,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 +943,106 @@ 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 +1100,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 +1335,106 @@ 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 +1488,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 01011c6e69..a815b58ef0 100644 --- a/src/components/__tests__/__snapshots__/TextInput.test.tsx.snap +++ b/src/components/__tests__/__snapshots__/TextInput.test.tsx.snap @@ -177,17 +177,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, }, @@ -203,71 +198,98 @@ 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 + + @@ -337,17 +359,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, }, @@ -365,100 +382,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 + + @@ -627,17 +667,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, }, @@ -651,71 +686,98 @@ exports[`renders filled TextInput with TextInput.Icon accessories 1`] = ` }, false, null, + { + "borderRadius": 20, + }, ] } > - + + - magnify - + [ + { + "lineHeight": 24, + "transform": [ + { + "scaleX": 1, + }, + ], + }, + { + "backgroundColor": "transparent", + }, + ], + ] + } + > + magnify + + @@ -781,17 +843,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, }, @@ -805,71 +862,98 @@ exports[`renders filled TextInput with TextInput.Icon accessories 1`] = ` }, false, null, + { + "borderRadius": 20, + }, ] } > - + + - close - + [ + { + "lineHeight": 24, + "transform": [ + { + "scaleX": 1, + }, + ], + }, + { + "backgroundColor": "transparent", + }, + ], + ] + } + > + close + + @@ -1038,17 +1122,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, }, @@ -1062,71 +1141,98 @@ exports[`renders filled TextInput with TextInput.Icon accessories when error is }, false, null, + { + "borderRadius": 20, + }, ] } > - + + - magnify - + [ + { + "lineHeight": 24, + "transform": [ + { + "scaleX": 1, + }, + ], + }, + { + "backgroundColor": "transparent", + }, + ], + ] + } + > + magnify + + @@ -1192,17 +1298,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, }, @@ -1216,108 +1317,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 + + @@ -1657,17 +1781,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, }, @@ -1681,71 +1800,98 @@ exports[`renders outlined TextInput with TextInput.Icon accessories 1`] = ` }, false, null, + { + "borderRadius": 20, + }, ] } > - + + - magnify - + [ + { + "lineHeight": 24, + "transform": [ + { + "scaleX": 1, + }, + ], + }, + { + "backgroundColor": "transparent", + }, + ], + ] + } + > + magnify + + @@ -1811,17 +1957,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, }, @@ -1835,71 +1976,98 @@ exports[`renders outlined TextInput with TextInput.Icon accessories 1`] = ` }, false, null, + { + "borderRadius": 20, + }, ] } > - + + - close - + [ + { + "lineHeight": 24, + "transform": [ + { + "scaleX": 1, + }, + ], + }, + { + "backgroundColor": "transparent", + }, + ], + ] + } + > + close + + @@ -2048,17 +2216,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, }, @@ -2072,71 +2235,98 @@ exports[`renders outlined TextInput with TextInput.Icon accessories when error i }, false, null, + { + "borderRadius": 20, + }, ] } > - + + - magnify - + [ + { + "lineHeight": 24, + "transform": [ + { + "scaleX": 1, + }, + ], + }, + { + "backgroundColor": "transparent", + }, + ], + ] + } + > + magnify + + @@ -2202,17 +2392,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, }, @@ -2226,108 +2411,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..1f9122d5bd 100644 --- a/src/components/__tests__/focusRingWiring.test.tsx +++ b/src/components/__tests__/focusRingWiring.test.tsx @@ -565,6 +565,79 @@ 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('takes its placement from `focusRing`', async () => { + await render( + {}} + focusRing="inward" + /> + ); + + await focusFirstFocusable(); + + const root = (screen as unknown as { root: Node }).root; + const ringed = walk(root, (n) => style(n).outlineStyle === 'solid'); + // One ring only: the caller's value must not also reach the touchable. + expect(ringed).toHaveLength(1); + expect(style(ringed[0]).outlineOffset).toBe(INWARD); + }); + + it('draws no ring with focusRing="none"', async () => { + await render( + {}} + focusRing="none" + /> + ); + + await focusFirstFocusable(); + + expect(ringOffset()).toBeUndefined(); + }); + + 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 6945d219e6..2b0614c8cc 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';