diff --git a/src/components/FAB/Extended.tsx b/src/components/FAB/Extended.tsx index 5a35159f4f..3cc17107ba 100644 --- a/src/components/FAB/Extended.tsx +++ b/src/components/FAB/Extended.tsx @@ -68,6 +68,18 @@ export type Props = { * Function to execute on press. */ onPress?: (e: GestureResponderEvent) => void; + /** + * Function to execute on long press. + */ + onLongPress?: (e: GestureResponderEvent) => void; + /** + * Function to execute as soon as the touch is released. + */ + onPressOut?: (e: GestureResponderEvent) => void; + /** + * The number of milliseconds a user must touch the element before executing `onLongPress`. + */ + delayLongPress?: number; /** * Accessibility label. Falls back to `label` if unset. */ @@ -161,6 +173,9 @@ const Extended = ({ expanded, visible = true, onPress, + onLongPress, + onPressOut, + delayLongPress, 'aria-label': ariaLabel = label, 'aria-checked': ariaChecked, 'aria-selected': ariaSelected, @@ -266,6 +281,9 @@ const Extended = ({ size={size} visible={visible} onPress={onPress} + onLongPress={onLongPress} + onPressOut={onPressOut} + delayLongPress={delayLongPress} aria-label={ariaLabel} aria-checked={ariaChecked} aria-selected={ariaSelected} diff --git a/src/components/FAB/FAB.tsx b/src/components/FAB/FAB.tsx index b6e862aa08..5b92548c47 100644 --- a/src/components/FAB/FAB.tsx +++ b/src/components/FAB/FAB.tsx @@ -45,6 +45,18 @@ export type Props = { * Function to execute on press. */ onPress?: (e: GestureResponderEvent) => void; + /** + * Function to execute on long press. + */ + onLongPress?: (e: GestureResponderEvent) => void; + /** + * Function to execute as soon as the touch is released. + */ + onPressOut?: (e: GestureResponderEvent) => void; + /** + * The number of milliseconds a user must touch the element before executing `onLongPress`. + */ + delayLongPress?: number; /** * Accessibility label. Falls back to nothing if unset. */ @@ -122,6 +134,9 @@ const FAB = ({ size = 'default', visible = true, onPress, + onLongPress, + onPressOut, + delayLongPress, containerColor, contentColor, 'aria-label': ariaLabel, @@ -142,6 +157,9 @@ const FAB = ({ size={size} visible={visible} onPress={onPress} + onLongPress={onLongPress} + onPressOut={onPressOut} + delayLongPress={delayLongPress} containerColor={containerColor} contentColor={contentColor} aria-label={ariaLabel} diff --git a/src/components/FAB/Shell.tsx b/src/components/FAB/Shell.tsx index e7c45cd50c..0b709be805 100644 --- a/src/components/FAB/Shell.tsx +++ b/src/components/FAB/Shell.tsx @@ -25,6 +25,7 @@ import { useReduceMotion } from '../../theme/accessibility/ReduceMotionContext'; import { toRawSpring } from '../../theme/tokens/sys/motion'; import type { Elevation, ThemeProp } from '../../theme/types'; import type { ShapeToken } from '../../theme/utils/shape'; +import hasTouchHandler from '../../utils/hasTouchHandler'; import { useFocusRing } from '../../utils/useFocusRing'; import type { IconSource } from '../Icon'; import Surface from '../Surface'; @@ -86,6 +87,18 @@ export type ShellProps = { * Function to execute on press. */ onPress?: (e: GestureResponderEvent) => void; + /** + * Function to execute on long press. + */ + onLongPress?: (e: GestureResponderEvent) => void; + /** + * Function to execute as soon as the touch is released. + */ + onPressOut?: (e: GestureResponderEvent) => void; + /** + * The number of milliseconds a user must touch the element before executing `onLongPress`. + */ + delayLongPress?: number; /** * Whether the shell can't be pressed. Turns a control off without removing * `onPress`, which would render another element and remount the content. @@ -197,6 +210,9 @@ const Shell = ({ elevation = Tokens.stateElevation.enabled, visible = true, onPress, + onLongPress, + onPressOut, + delayLongPress, disabled, 'aria-label': ariaLabel = label, 'aria-checked': ariaChecked, @@ -303,12 +319,14 @@ const Shell = ({ // stylesheet hides the browser's own outline there, so only the clip view's // ring shows, or only the outline in a browser without `:has()`. // - // Off when `disabled` or without `onPress`: the inner TouchableRipple is - // then a plain View, and a focused one unmounts without a blur reaching the - // hook. The public FAB has no `disabled`, so dropping `onPress` is how an app - // turns it off. + const hasHandler = hasTouchHandler({ onPress, onLongPress, onPressOut }); + + // Off when `disabled` or without a touch handler: the inner TouchableRipple + // is then a plain View, and a focused one unmounts without a blur reaching + // the hook. The public FAB has no `disabled`, so dropping the handlers is how + // an app turns it off. const { target: focusTarget, ring: focusRing } = useFocusRing( - disabled || !onPress, + disabled || !hasHandler, theme.colors.secondary, 'outward', 'within' @@ -319,7 +337,7 @@ const Shell = ({ // icon-only FAB that does nothing is an image there, named by its label. const isWebImage = Platform.OS === 'web' && - !onPress && + !hasHandler && !disabled && label === undefined && children === undefined && @@ -350,6 +368,9 @@ const Shell = ({ background={background} focusRing="none" onPress={onPress} + onLongPress={onLongPress} + onPressOut={onPressOut} + delayLongPress={delayLongPress} disabled={disabled} onFocus={focusTarget.onFocus} onBlur={focusTarget.onBlur} diff --git a/src/components/__tests__/FAB.test.tsx b/src/components/__tests__/FAB.test.tsx index 1c577f0b1a..395ff9aa79 100644 --- a/src/components/__tests__/FAB.test.tsx +++ b/src/components/__tests__/FAB.test.tsx @@ -10,8 +10,10 @@ import { } from '@jest/globals'; import { fireEvent, userEvent } from '@testing-library/react-native'; +import PaperProvider from '../../core/PaperProvider'; import { render, screen } from '../../test-utils'; import FAB from '../FAB'; +import Tooltip from '../Tooltip/Tooltip'; it('renders FAB with default props', async () => { const tree = (await render()).toJSON(); @@ -111,6 +113,39 @@ it('forwards event object to onPress', async () => { expect(onPress).toHaveBeenCalledWith({ key: 'value' }); }); +it('calls onLongPress and onPressOut when FAB is long pressed', async () => { + const user = userEvent.setup(); + const onLongPress = jest.fn(); + const onPressOut = jest.fn(); + await render( + + ); + await user.longPress(screen.getByRole('button', { name: 'Add item' }), { + duration: 400, + }); + expect(onLongPress).toHaveBeenCalledTimes(1); + expect(onPressOut).toHaveBeenCalledTimes(1); +}); + +it('shows Tooltip when FAB is long pressed', async () => { + const user = userEvent.setup(); + await render( + + + {}} /> + + + ); + await user.longPress(screen.getByRole('button', { name: 'Add item' })); + expect(screen.getByText('Press Me')).toBeOnTheScreen(); +}); + describe('without a touch handler', () => { describe.each(['ios', 'android'] as const)('on %s', (os) => { beforeEach(() => { @@ -154,18 +189,12 @@ describe('without a touch handler', () => { ); }); - it('stays a button with a touch handler', async () => { - await render( - {}} /> - ); - - expect( - screen.getByRole('button', { name: 'Favorite' }) - ).toBeOnTheScreen(); - }); - - it('stays a button while it reports a toggle state', async () => { - await render(); + it.each([ + ['with only onPress', { onPress: () => {} }], + ['with only onLongPress', { onLongPress: () => {} }], + ['while it reports a toggle state', { 'aria-checked': true }], + ])('stays a button %s', async (_, props) => { + await render(); expect( screen.getByRole('button', { name: 'Favorite' })