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..77c74769ff 100644 --- a/docs/component-docs.config.ts +++ b/docs/component-docs.config.ts @@ -120,9 +120,7 @@ const pages = { ProgressBar: 'ProgressBar', RadioButton: { RadioButton: 'RadioButton/RadioButton', - RadioButtonAndroid: 'RadioButton/RadioButtonAndroid', RadioButtonGroup: 'RadioButton/RadioButtonGroup', - RadioButtonIOS: 'RadioButton/RadioButtonIOS', RadioButtonItem: 'RadioButton/RadioButtonItem', }, Searchbar: 'Searchbar', diff --git a/docs/src/data/screenshots.ts b/docs/src/data/screenshots.ts index e18135fc49..2014d9ae2a 100644 --- a/docs/src/data/screenshots.ts +++ b/docs/src/data/screenshots.ts @@ -114,24 +114,11 @@ 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', }, - 'RadioButton.Group': { - 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', + // The iOS radio assets show a checkmark that 6.x no longer draws. + 'RadioButton.Group': 'screenshots/radio-button-group-android.gif', Searchbar: { bar: 'screenshots/searchbar.png', view: 'screenshots/searchbar-view.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..c89962f2ec 100644 --- a/src/components/RadioButton/RadioButton.tsx +++ b/src/components/RadioButton/RadioButton.tsx @@ -1,12 +1,39 @@ -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 { useSpaceKeyPress } from '../../utils/useSpaceKeyPress'; +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 +60,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 +76,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 +114,304 @@ 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, + color, + uncheckedColor, + 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: color, + customUncheckedColor: 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', + }; + + // 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 Button = Platform.select({ - default: RadioButtonAndroid, - ios: RadioButtonIOS, + const stateLayerProps = { + theme, + checked, + disabled, + error, + customColor: color, + customUncheckedColor: uncheckedColor, + }; + + const stateLayer = getSelectionStateLayer({ + ...stateLayerProps, + interaction, }); - return