From 6e9d1605dc02f313ebee4f3c680f464b6dc69a1d Mon Sep 17 00:00:00 2001 From: Bartek Dybowski Date: Mon, 23 Nov 2026 01:57:20 +0100 Subject: [PATCH 1/3] feat(radio-button)!: modernize RadioButton for MD3 --- docs/6.x/docs/guides/migration.md | 24 +- docs/component-docs.config.ts | 12 +- docs/src/data/screenshots.ts | 10 - example/src/Examples/RadioButtonExample.tsx | 46 +- .../src/Examples/RadioButtonGroupExample.tsx | 4 +- .../src/Examples/RadioButtonItemExample.tsx | 38 +- src/components/RadioButton/RadioButton.tsx | 332 +++++++++++++- .../RadioButton/RadioButtonAndroid.tsx | 215 --------- .../RadioButton/RadioButtonGroup.tsx | 22 +- src/components/RadioButton/RadioButtonIOS.tsx | 144 ------ .../RadioButton/RadioButtonItem.tsx | 167 ++++--- src/components/RadioButton/index.ts | 6 - src/components/RadioButton/tokens.ts | 30 ++ src/components/RadioButton/utils.ts | 146 +++--- .../RadioButton/RadioButton.test.tsx | 231 +++++++--- .../RadioButton/RadioButtonAndroid.test.tsx | 70 --- .../RadioButton/RadioButtonGroup.test.tsx | 27 +- .../RadioButton/RadioButtonItem.test.tsx | 44 +- .../RadioButton/RadioButtonReadOnly.test.tsx | 16 +- .../__snapshots__/RadioButton.test.tsx.snap | 316 +++++++------ .../RadioButtonGroup.test.tsx.snap | 85 +++- .../RadioButtonItem.test.tsx.snap | 430 ++++++------------ .../__tests__/RadioButton/utils.test.tsx | 72 ++- src/index.tsx | 2 - 24 files changed, 1262 insertions(+), 1227 deletions(-) delete mode 100644 src/components/RadioButton/RadioButtonAndroid.tsx delete mode 100644 src/components/RadioButton/RadioButtonIOS.tsx create mode 100644 src/components/RadioButton/tokens.ts delete mode 100644 src/components/__tests__/RadioButton/RadioButtonAndroid.test.tsx diff --git a/docs/6.x/docs/guides/migration.md b/docs/6.x/docs/guides/migration.md index e8e5ec373a..121105af66 100644 --- a/docs/6.x/docs/guides/migration.md +++ b/docs/6.x/docs/guides/migration.md @@ -394,7 +394,7 @@ On web, an `IconButton` or icon-only `FAB` labelled with `aria-label`, with no h `hitSlop` applies only to a touchable that can be pressed. Without a handler, or with `disabled`, a touchable takes no touches outside its bounds, so it doesn't take them from its neighbors. -`RadioButton`, `RadioButton.Android`, `RadioButton.IOS` and `RadioButton.Item` with no press handler and no `RadioButton.Group` no longer take the press. They stay read-only radios, a row around them gets the press, and on web they're no longer a tab stop. +`RadioButton` and `RadioButton.Item` with no press handler and no `RadioButton.Group` no longer take the press. They stay read-only radios, a row around them gets the press, and on web they're no longer a tab stop. To have something look and announce disabled, pass `disabled`, which keeps the control and its role. Pass it to switch interactivity off at runtime too, instead of removing `onPress`. A touchable renders another element whenever it goes between having a handler or `disabled` and having neither, so its children remount and lose their state and focus. That includes toggling `disabled` on a touchable with no handler. `FAB.Menu` does this for its trigger: a trigger without `onPress` is a disabled button while the menu is closed, so it keeps its element when the menu opens. @@ -905,6 +905,28 @@ On web, a menu now closes on the Escape key when it is pressed down, instead of On Android, an open menu now also keeps the back press: it calls `onDismiss`, and the screen behind no longer goes back. Previously the press reached that screen as well. Without an `onDismiss`, the menu leaves the press to whatever is underneath, so pass one to close the menu on back. +### RadioButton + +`RadioButton` renders one Material 3 circular radio control on every platform. The platform-specific components and their prop types are gone. On iOS checked controls no longer render a checkmark; they use the same ring-and-dot indicator as other platforms. `RadioButton` now also forwards `TouchableRipple` props (`accessible`, `onFocus`, `onBlur`, `borderless`, `rippleColor`, ...). + +The control now occupies a 48×48dp layout box instead of 36×36dp, so rows containing it may become 12dp taller. The 20dp radio indicator is unchanged. `RadioButton.Item` rows grow from 52dp to 64dp. + +#### Interaction state colors + +Interaction states are now painted as a Material Design 3 state layer on every platform, so the tint follows the spec: hover & focus fill a flat 40dp layer behind the indicator with `primary` when checked and `onSurface` when not, and press inverts the two. An `error` radio stays on `error` throughout and `color`/`uncheckedColor` replace the role they already override on the ring, so custom radio no longer picks up `primary` halo. The platform ripple is turned off to make room for it; passing `rippleColor` on any platform, `background` on Android or `underlayColor` on iOS turns it back on and disables the built-in press. Setting `rippleEffectEnabled: false` on the `settings` prop of `PaperProvider` still suppresses both. + +| v5 | v6 | +| ------------------------------------------------ | ------------------------------------------------------------------------ | +| `RadioButton.Android`, `RadioButton.IOS` | `RadioButton` | +| `RadioButtonAndroidProps`, `RadioButtonIOSProps` | `RadioButtonProps` | +| `RadioButton.Item` `mode="android" \| "ios"` | `mode` removed; `RadioButton.Item` always renders `RadioButton` | +| n/a | new `error` (uses `theme.colors.error`) and `style` (touch-target style) | + +```diff +- ++ +``` + ### Searchbar The misspelled `traileringIcon` props have been renamed: diff --git a/docs/component-docs.config.ts b/docs/component-docs.config.ts index 73ebb3b3b2..3eec56daf6 100644 --- a/docs/component-docs.config.ts +++ b/docs/component-docs.config.ts @@ -5,6 +5,14 @@ import { extendedExamples } from './src/data/extendedExamples.ts'; import { screenshots } from './src/data/screenshots.ts'; import { themeColors } from './src/data/themeColors.ts'; +// 6.x-only overrides: the v5 iOS radio assets show a control this release +// removes, but `generate-5x-docs.ts` still renders them from the shared map. +const screenshotsV6: Record = { + ...screenshots, + 'RadioButton.Group': 'screenshots/radio-button-group-android.gif', +}; +delete screenshotsV6['RadioButton.Item']; + const __dirname = path.dirname(fileURLToPath(import.meta.url)); export type Page = @@ -120,9 +128,7 @@ const pages = { ProgressBar: 'ProgressBar', RadioButton: { RadioButton: 'RadioButton/RadioButton', - RadioButtonAndroid: 'RadioButton/RadioButtonAndroid', RadioButtonGroup: 'RadioButton/RadioButtonGroup', - RadioButtonIOS: 'RadioButton/RadioButtonIOS', RadioButtonItem: 'RadioButton/RadioButtonItem', }, Searchbar: 'Searchbar', @@ -174,7 +180,7 @@ const componentDocsConfig: ComponentDocsConfig = { }, knownIssues: {}, themeColors, - screenshots, + screenshots: screenshotsV6, extendedExamples, }, }; diff --git a/docs/src/data/screenshots.ts b/docs/src/data/screenshots.ts index e18135fc49..01dd7ad315 100644 --- a/docs/src/data/screenshots.ts +++ b/docs/src/data/screenshots.ts @@ -114,12 +114,6 @@ export const screenshots = { Modal: 'screenshots/modal.gif', ProgressBar: 'screenshots/progress-bar.png', RadioButton: { - 'Android (enabled)': 'screenshots/radio-enabled.android.png', - 'Android (disabled)': 'screenshots/radio-disabled.android.png', - 'iOS (enabled)': 'screenshots/radio-enabled.ios.png', - 'iOS (disabled)': 'screenshots/radio-disabled.ios.png', - }, - 'RadioButton.Android': { enabled: 'screenshots/radio-enabled.android.png', disabled: 'screenshots/radio-disabled.android.png', }, @@ -127,10 +121,6 @@ export const screenshots = { Android: 'screenshots/radio-button-group-android.gif', iOS: 'screenshots/radio-button-group-ios.gif', }, - 'RadioButton.IOS': { - enabled: 'screenshots/radio-enabled.ios.png', - disabled: 'screenshots/radio-disabled.ios.png', - }, 'RadioButton.Item': 'screenshots/radio-item.ios.png', Searchbar: { bar: 'screenshots/searchbar.png', diff --git a/example/src/Examples/RadioButtonExample.tsx b/example/src/Examples/RadioButtonExample.tsx index 6ab1869dd9..c30e49e00f 100644 --- a/example/src/Examples/RadioButtonExample.tsx +++ b/example/src/Examples/RadioButtonExample.tsx @@ -10,7 +10,7 @@ import { import ScreenWrapper from '../ScreenWrapper'; -type State = 'normal' | 'normal-ios' | 'normal-item' | 'custom'; +type State = 'normal' | 'normal-item' | 'custom'; const RadioButtonExample = () => { const [checked, setChecked] = React.useState('normal'); @@ -19,26 +19,15 @@ const RadioButtonExample = () => { setChecked('normal')}> - Normal - Material Design + Normal - - setChecked('normal-ios')}> - - Normal 2 - IOS - - - - - setChecked('custom')}> Custom @@ -58,11 +47,31 @@ const RadioButtonExample = () => { onPress={() => setChecked('normal-item')} /> - Checked (Disabled) + Error (Checked) + + + + Error (Unchecked) + + + + + + Checked (Disabled) - Unchecked (Disabled) + Unchecked (Disabled) { Second - + Third - + diff --git a/example/src/Examples/RadioButtonItemExample.tsx b/example/src/Examples/RadioButtonItemExample.tsx index e640a1dd0a..efd4eee32a 100644 --- a/example/src/Examples/RadioButtonItemExample.tsx +++ b/example/src/Examples/RadioButtonItemExample.tsx @@ -7,55 +7,47 @@ import ScreenWrapper from '../ScreenWrapper'; const RadioButtonItemExample = () => { const [checkedDefault, setCheckedDefault] = React.useState(true); - const [checkedAndroid, setCheckedAndroid] = React.useState(true); - const [checkedIOS, setCheckedIOS] = React.useState(true); const [checkedLeadingControl, setCheckedLeadingControl] = React.useState(true); const [checkedDisabled, setCheckedDisabled] = React.useState(true); + const [checkedError, setCheckedError] = React.useState(true); const [checkedLabelVariant, setCheckedLabelVariant] = React.useState(true); return ( setCheckedDefault(!checkedDefault)} value="default" /> setCheckedAndroid(!checkedAndroid)} - value="android" - /> - setCheckedIOS(!checkedIOS)} - value="iOS" - /> - setCheckedLeadingControl(!checkedLeadingControl)} - value="iOS" + value="leading" position="leading" /> setCheckedError(!checkedError)} + value="error" + error + /> + setCheckedDisabled(!checkedDisabled)} - value="iOS" + value="disabled" disabled /> setCheckedLabelVariant(!checkedLabelVariant)} - value="default" + value="variant" /> ); diff --git a/src/components/RadioButton/RadioButton.tsx b/src/components/RadioButton/RadioButton.tsx index 4f114b1bca..ec427318f2 100644 --- a/src/components/RadioButton/RadioButton.tsx +++ b/src/components/RadioButton/RadioButton.tsx @@ -1,12 +1,38 @@ -import { Platform } from 'react-native'; -import type { GestureResponderEvent } from 'react-native'; +import * as React from 'react'; +import { Platform, StyleSheet, View } from 'react-native'; +import type { + GestureResponderEvent, + MouseEvent, + NativeSyntheticEvent, + TargetedEvent, + ViewStyle, +} from 'react-native'; -import RadioButtonAndroid from './RadioButtonAndroid'; -import RadioButtonIOS from './RadioButtonIOS'; +import Animated, { cubicBezier, type CSSStyle } from 'react-native-reanimated'; + +import { RadioButtonContext } from './RadioButtonGroup'; +import { RadioButtonTokens } from './tokens'; +import { + getSelectionControlColor, + getSelectionStateLayer, + handlePress, + isChecked, + getInteraction, +} from './utils'; +import { SettingsContext } from '../../core/settings'; import { useInternalTheme } from '../../core/theming'; +import { useReduceMotion } from '../../theme/accessibility/ReduceMotionContext'; import type { ThemeProp } from '../../theme/types'; +import hasTouchHandler from '../../utils/hasTouchHandler'; +import { isKeyboardFocusEvent } from '../../utils/isKeyboardFocusEvent'; +import type { PressableStateCallbackType } from '../TouchableRipple/Pressable'; +import TouchableRipple from '../TouchableRipple/TouchableRipple'; +import type { Props as TouchableRippleProps } from '../TouchableRipple/TouchableRipple'; -export type Props = { +export type Props = Omit< + React.PropsWithoutRef, + 'children' +> & { /** * Value of the radio button */ @@ -33,6 +59,12 @@ export type Props = { * Custom color for radio. */ color?: string; + /** + * Whether the radio button is in an error state. When true, the ring + * (unchecked) and dot (selected) use `theme.colors.error`. `disabled` + * and explicit `color`/`uncheckedColor` overrides take precedence. + */ + error?: boolean; /** * @optional */ @@ -43,6 +75,14 @@ export type Props = { testID?: string; }; +const { + ringSize, + dotSize, + outlineWidth: OUTLINE_WIDTH, + stateLayerSize, + touchTargetSize, +} = RadioButtonTokens; + /** * Radio buttons allow the selection a single option from a set. * @@ -73,16 +113,288 @@ export type Props = { * * export default MyComponent; * ``` + * + * @extends TouchableRipple props https://callstack.github.io/react-native-paper/docs/components/TouchableRipple */ -const RadioButton = ({ theme: themeOverrides, ...props }: Props) => { +const RadioButton = ({ + disabled, + onPress, + theme: themeOverrides, + value, + status, + testID, + error, + onFocus, + onBlur, + onHoverIn, + onHoverOut, + onPressIn, + onPressOut, + rippleColor, + background, + underlayColor, + ...rest +}: Props) => { const theme = useInternalTheme(themeOverrides); + const context = React.useContext(RadioButtonContext); + const { rippleEffectEnabled } = React.useContext(SettingsContext); + + const checked = + isChecked({ + contextValue: context?.value, + status, + value, + }) === 'checked'; + + const reduceMotion = useReduceMotion(); + + const [focused, setFocused] = React.useState(false); + const [hovered, setHovered] = React.useState(false); + const [pressed, setPressed] = React.useState(false); + + const { selectionControlColor, selectionControlOpacity } = + getSelectionControlColor({ + theme, + disabled, + checked, + error, + customColor: rest.color, + customUncheckedColor: rest.uncheckedColor, + }); + + const dotStyle: CSSStyle = { + backgroundColor: selectionControlColor, + transform: [{ scale: checked ? 1 : 0 }], + transitionProperty: 'transform', + transitionDuration: reduceMotion ? 0 : theme.motion.duration.short3, + transitionTimingFunction: cubicBezier(...theme.motion.easing.standard), + }; + + const platformOwnsPress = + rippleColor != null || + (Platform.OS === 'android' && background != null) || + (Platform.OS === 'ios' && underlayColor != null); + + // Without `onPress` and a `RadioButton.Group` there is nothing to select. + const isPressable = Boolean(onPress || context?.onValueChange); + // A handler-less radio would claim the touch and do nothing with it, so the + // press handlers stay off unless there is something to call. + const hasHandler = + isPressable || + hasTouchHandler({ onLongPress: rest.onLongPress, onPressIn, onPressOut }); + const isInteractive = !disabled && hasHandler; + + const ownsPress = + isInteractive && !platformOwnsPress && Boolean(rippleEffectEnabled); + + const platformPressProps = { + background, + underlayColor, + rippleColor: platformOwnsPress ? rippleColor : 'transparent', + }; - const Button = Platform.select({ - default: RadioButtonAndroid, - ios: RadioButtonIOS, + // A read-only or disabled radio can't be pressed, so it shows no state. + const interaction = isInteractive + ? getInteraction({ + pressed: pressed && ownsPress, + focused, + hovered, + }) + : null; + + const stateLayerProps = { + theme, + checked, + disabled, + error, + customColor: rest.color, + customUncheckedColor: rest.uncheckedColor, + }; + + const stateLayer = getSelectionStateLayer({ + ...stateLayerProps, + interaction, }); - return