Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/components/FAB/Extended.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,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.
*/
Expand Down Expand Up @@ -162,6 +174,9 @@ const Extended = ({
expanded,
visible = true,
onPress,
onLongPress,
onPressOut,
delayLongPress,
'aria-label': ariaLabel = label,
'aria-checked': ariaChecked,
'aria-selected': ariaSelected,
Expand Down Expand Up @@ -273,6 +288,9 @@ const Extended = ({
size={size}
visible={visible}
onPress={onPress}
onLongPress={onLongPress}
onPressOut={onPressOut}
delayLongPress={delayLongPress}
aria-label={ariaLabel}
aria-checked={ariaChecked}
aria-selected={ariaSelected}
Expand Down
18 changes: 18 additions & 0 deletions src/components/FAB/FAB.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -122,6 +134,9 @@ const FAB = ({
size = 'default',
visible = true,
onPress,
onLongPress,
onPressOut,
delayLongPress,
containerColor,
contentColor,
'aria-label': ariaLabel,
Expand All @@ -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}
Expand Down
33 changes: 27 additions & 6 deletions src/components/FAB/Shell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,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';
Expand Down Expand Up @@ -87,6 +88,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.
Expand Down Expand Up @@ -198,6 +211,9 @@ const Shell = ({
elevation = Tokens.stateElevation.enabled,
visible = true,
onPress,
onLongPress,
onPressOut,
delayLongPress,
disabled,
'aria-label': ariaLabel = label,
'aria-checked': ariaChecked,
Expand Down Expand Up @@ -299,17 +315,19 @@ const Shell = ({
[borderRadius, containerBg]
);

const hasHandler = hasTouchHandler({ onPress, onLongPress, onPressOut });

// `scope: 'within'`: the ring is drawn on the clip view below, not the
// inner `TouchableRipple` that actually receives focus. On web the shared
// 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.
// 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'
Expand All @@ -320,7 +338,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 &&
Expand Down Expand Up @@ -351,6 +369,9 @@ const Shell = ({
background={background}
focusRing="none"
onPress={onPress}
onLongPress={onLongPress}
onPressOut={onPressOut}
delayLongPress={delayLongPress}
disabled={disabled}
onFocus={focusTarget.onFocus}
onBlur={focusTarget.onBlur}
Expand Down
52 changes: 40 additions & 12 deletions src/components/__tests__/FAB.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import * as Reanimated from 'react-native-reanimated';
import PaperProvider from '../../core/PaperProvider';
import { render, screen } from '../../test-utils';
import FAB from '../FAB';
import Tooltip from '../Tooltip/Tooltip';

jest.mock('react-native-reanimated', () => {
const ReanimatedModule = jest.requireActual<
Expand Down Expand Up @@ -157,6 +158,39 @@ it('forwards event object to onPress', async () => {
expect(onPress).toHaveBeenCalledWith({ key: 'value' });
});

it('calls onLongPress and onPressOut when FAB is long pressed', async () => {
Comment thread
lukewalczak marked this conversation as resolved.
Resolved
const user = userEvent.setup();
const onLongPress = jest.fn();
const onPressOut = jest.fn();
await render(
<FAB
icon="plus"
aria-label="Add item"
onLongPress={onLongPress}
onPressOut={onPressOut}
delayLongPress={300}
/>
);
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(
<PaperProvider>
<Tooltip title="Press Me">
<FAB icon="plus" aria-label="Add item" onPress={() => {}} />
</Tooltip>
</PaperProvider>
);
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(() => {
Expand Down Expand Up @@ -200,18 +234,12 @@ describe('without a touch handler', () => {
);
});

it('stays a button with a touch handler', async () => {
await render(
<FAB icon="star" aria-label="Favorite" onPress={() => {}} />
);

expect(
screen.getByRole('button', { name: 'Favorite' })
).toBeOnTheScreen();
});

it('stays a button while it reports a toggle state', async () => {
await render(<FAB icon="star" aria-label="Favorite" aria-checked />);
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(<FAB icon="star" aria-label="Favorite" {...props} />);

expect(
screen.getByRole('button', { name: 'Favorite' })
Expand Down
21 changes: 21 additions & 0 deletions src/components/__tests__/FABExtended.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,24 @@ it('forwards event object to onPress', async () => {
);
expect(onPress).toHaveBeenCalledWith({ key: 'value' });
});

it('calls onLongPress and onPressOut when long pressed', async () => {
const user = userEvent.setup();
const onLongPress = jest.fn();
const onPressOut = jest.fn();
await render(
<FAB.Extended
icon="plus"
label="New message"
expanded
onLongPress={onLongPress}
onPressOut={onPressOut}
delayLongPress={300}
/>
);
await user.longPress(screen.getByRole('button', { name: 'New message' }), {
duration: 400,
});
expect(onLongPress).toHaveBeenCalledTimes(1);
expect(onPressOut).toHaveBeenCalledTimes(1);
});
Loading