Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
4e7abfc
fix(fab): align MD3 variants and interaction states
likevy Sep 7, 2026
7ed7db3
fix(fab): address review feedback
likevy Sep 11, 2026
873f654
fix(fab): skip hidden focus restoration targets
likevy Sep 23, 2026
afdb35f
fix(fab): remove the branded variant
likevy Oct 30, 2026
707adba
refactor(fab): drop the redundant document check
likevy Oct 30, 2026
a09b6fb
fix(fab): name the menu trigger in the example
likevy Oct 30, 2026
cbcf406
fix(fab): restore focus only after keyboard focus
likevy Oct 30, 2026
f4c1fe6
test(fab): name the disabled FAB test after its behavior
likevy Oct 30, 2026
eb1fca0
test(fab): cover the press guard on a hidden FAB
likevy Oct 30, 2026
d553d57
test(fab): cover focus restoration when a FAB hides on web
likevy Oct 30, 2026
a80ae79
test(fab): compare elevation between interaction states
likevy Oct 30, 2026
967933a
test(fab): check that focus alone keeps the resting elevation
likevy Oct 30, 2026
d79ff09
test(fab): pin the direction of the hover elevation
likevy Oct 30, 2026
aa6137c
test(fab): cover the blur fallback when focus can't move back
likevy Oct 30, 2026
b226ec6
fix(fab): type the touchable ref for React Native 0.88
likevy Nov 6, 2026
0018e80
test(fab): type the FAB test refs for React Native 0.88
likevy Nov 6, 2026
60713d7
fix(fab): keep a FAB without onPress a plain view
likevy Nov 17, 2026
e1e57ef
test(fab): update the FAB snapshots
likevy Nov 17, 2026
a15a1d8
docs(fab): use the container variant names in the migration guide
likevy Nov 17, 2026
1a92e1f
test(fab): name React Native 0.88 in the native hover comment
likevy Nov 17, 2026
0174492
perf(fab): track the press only while the FAB is hovered
likevy Nov 18, 2026
cd5c7d1
test(fab): check that onPressOut fires alongside the press state
likevy Nov 18, 2026
85f54bb
test(fab): cover a FAB that hides mid-press
likevy Nov 18, 2026
9770bb1
test(fab): cover a press cut short by hiding or losing the action
likevy Nov 18, 2026
4568f7a
test(fab): name the handler-less FAB test after any touch handler
likevy Nov 18, 2026
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
10 changes: 5 additions & 5 deletions docs/6.x/docs/guides/migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -1266,18 +1266,18 @@ mutually exclusive options, use `SegmentedButtons`.

#### `variant`

The variants changed meaning. In 5.x, `primary`, `secondary` and `tertiary` painted the container role, e.g. `primaryContainer`. In 6.x they paint the role itself, e.g. `primary`, and the new tonal variants paint the container role. So a FAB with an explicit `variant` looks different. To keep the 5.x colors:
The variants changed meaning. In 5.x, `primary`, `secondary` and `tertiary` painted the container role, e.g. `primaryContainer`. In 6.x they paint the role itself, e.g. `primary`, and the new `primaryContainer`, `secondaryContainer` and `tertiaryContainer` variants paint the container role. So a FAB with an explicit `variant` looks different. To keep the 5.x colors:

- **`primary`** (the 5.x default) → **`tonalPrimary`** (the 6.x default)
- **`secondary`** → **`tonalSecondary`**
- **`tertiary`** → **`tonalTertiary`**
- **`primary`** (the 5.x default) → **`primaryContainer`** (the 6.x default)
- **`secondary`** → **`secondaryContainer`**
- **`tertiary`** → **`tertiaryContainer`**
- **`surface`** has no variant. Set `containerColor` and `contentColor` instead, e.g. to `theme.colors.surfaceContainerHigh` and `theme.colors.primary`.

A FAB without a `variant` keeps its 5.x colors.

```diff
-<FAB icon="plus" variant="secondary" onPress={onPress} />
+<FAB icon="plus" variant="tonalSecondary" onPress={onPress} />
+<FAB icon="plus" variant="secondaryContainer" onPress={onPress} />
```

#### `size`
Expand Down
10 changes: 6 additions & 4 deletions example/src/Examples/FABExample.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ const variants: FabColor[] = [
'primary',
'secondary',
'tertiary',
'tonalPrimary',
'tonalSecondary',
'tonalTertiary',
'primaryContainer',
'secondaryContainer',
'tertiaryContainer',
'custom',
];

Expand Down Expand Up @@ -91,7 +91,7 @@ const FABExample = () => {
const { colors } = useTheme();
const insets = useSafeAreaInsets();

const [variant, setVariant] = React.useState<FabColor>('tonalPrimary');
const [variant, setVariant] = React.useState<FabColor>('primaryContainer');
const activeVariant = variant === 'custom' ? undefined : variant;
const activeContainerColor =
variant === 'custom' ? CUSTOM_CONTAINER_COLOR : undefined;
Expand Down Expand Up @@ -187,6 +187,7 @@ const FABExample = () => {
{type === 'icon' && (
<FAB
icon="pencil"
aria-label="Compose"
variant={activeVariant}
containerColor={activeContainerColor}
size={size}
Expand Down Expand Up @@ -214,6 +215,7 @@ const FABExample = () => {
alignment={position}
trigger={{
icon: 'pencil',
Comment thread
likevy marked this conversation as resolved.
Resolved
'aria-label': 'Compose',
variant: activeVariant,
containerColor: activeContainerColor,
size,
Expand Down
4 changes: 2 additions & 2 deletions src/components/FAB/Extended.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export type Props = {
*/
label: string;
/**
* Role-color preset. Defaults to `tonalPrimary`.
* Role-color preset. Defaults to `primaryContainer`.
*/
variant?: Variant;
/**
Expand Down Expand Up @@ -167,7 +167,7 @@ export type Props = {
const Extended = ({
icon,
label,
variant = 'tonalPrimary',
variant = 'primaryContainer',
containerColor,
contentColor,
size = 'default',
Expand Down
4 changes: 2 additions & 2 deletions src/components/FAB/FAB.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export type Props = {
*/
icon: IconSource;
/**
* Role-color preset. Defaults to `tonalPrimary`.
* Role-color preset. Defaults to `primaryContainer`.
*/
variant?: Variant;
/**
Expand Down Expand Up @@ -130,7 +130,7 @@ export type Props = {
*/
const FAB = ({
icon,
variant = 'tonalPrimary',
variant = 'primaryContainer',
size = 'default',
visible = true,
onPress,
Expand Down
22 changes: 14 additions & 8 deletions src/components/FAB/Menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,23 +117,29 @@ export type MenuProps = {
* tonal (container) role color.
*/
const getCloseVariant = (triggerVariant: Variant): Variant => {
if (triggerVariant === 'primary' || triggerVariant === 'tonalPrimary') {
if (triggerVariant === 'primary' || triggerVariant === 'primaryContainer') {
return 'primary';
}
if (triggerVariant === 'secondary' || triggerVariant === 'tonalSecondary') {
if (
triggerVariant === 'secondary' ||
triggerVariant === 'secondaryContainer'
) {
return 'secondary';
}
return 'tertiary';
};

const getItemsVariant = (triggerVariant: Variant): Variant => {
if (triggerVariant === 'primary' || triggerVariant === 'tonalPrimary') {
return 'tonalPrimary';
if (triggerVariant === 'primary' || triggerVariant === 'primaryContainer') {
return 'primaryContainer';
}
if (triggerVariant === 'secondary' || triggerVariant === 'tonalSecondary') {
return 'tonalSecondary';
if (
triggerVariant === 'secondary' ||
triggerVariant === 'secondaryContainer'
) {
return 'secondaryContainer';
}
return 'tonalTertiary';
return 'tertiaryContainer';
};

// Per-item delay used by the stagger. Compose uses a single SlowEffects-driven
Expand Down Expand Up @@ -551,7 +557,7 @@ const Menu = ({
const isRTL = direction === 'rtl';
const insets = useSafeAreaInsets();

const triggerVariant: Variant = trigger.variant ?? 'tonalPrimary';
const triggerVariant: Variant = trigger.variant ?? 'primaryContainer';
const size: Size = trigger.size ?? 'default';
const openIcon: IconSource = trigger.icon;
const openOnPress = trigger.onPress;
Expand Down
140 changes: 122 additions & 18 deletions src/components/FAB/Shell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { getDimensions, resolveColors } from './utils';
import { useInternalTheme } from '../../core/theming';
import { useReduceMotion } from '../../theme/accessibility/ReduceMotionContext';
import { toRawSpring } from '../../theme/tokens/sys/motion';
import type { Elevation, ThemeProp } from '../../theme/types';
import type { ThemeProp } from '../../theme/types';
import type { ShapeToken } from '../../theme/utils/shape';
import hasTouchHandler from '../../utils/hasTouchHandler';
import { useFocusRing } from '../../utils/useFocusRing';
Expand All @@ -43,7 +43,7 @@ export type ShellProps = {
*/
label?: string;
/**
* Role-color preset. Defaults to `tonalPrimary`.
* Role-color preset. Defaults to `primaryContainer`.
*/
variant?: Variant;
/**
Expand Down Expand Up @@ -74,11 +74,6 @@ export type ShellProps = {
* Trailing-padding override.
*/
trailing?: number;
/**
* Resting elevation level. Defaults to the FAB's enabled-state elevation.
* Pass `0` to disable the shadow entirely.
*/
elevation?: Elevation;
/**
* When `false`, the shell animates out (scale + alpha) and stops accepting
* touches.
Expand Down Expand Up @@ -200,15 +195,14 @@ export type ShellProps = {
const Shell = ({
icon,
label,
variant = 'tonalPrimary',
variant = 'primaryContainer',
size = 'default',
containerColor,
contentColor,
shape,
iconSize,
leading,
trailing,
elevation = Tokens.stateElevation.enabled,
visible = true,
onPress,
onLongPress,
Expand All @@ -235,6 +229,23 @@ const Shell = ({
ref,
}: ShellProps) => {
const theme = useInternalTheme(themeOverrides);
const [hovered, setHovered] = React.useState(false);
const [pressed, setPressed] = React.useState(false);
Comment thread
likevy marked this conversation as resolved.
Resolved
const touchableRef = React.useRef<React.ComponentRef<typeof View>>(null);
const previousFocusedElement = React.useRef<HTMLElement | null>(null);
const hoveredRef = React.useRef(false);
const pressingRef = React.useRef(false);

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

const resolvedElevation =
visible && hasHandler
? pressed
? Tokens.stateElevation.pressed
: hovered
? Tokens.stateElevation.hover
: Tokens.stateElevation.enabled
: Tokens.stateElevation.enabled;

const dimensions = React.useMemo(
() => getDimensions({ theme, size, shape, iconSize, leading, trailing }),
Expand Down Expand Up @@ -315,8 +326,6 @@ 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
Expand All @@ -325,14 +334,82 @@ const Shell = ({
// 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.
// an app turns it off. Also off while hidden, which clears the focus state on
// native when the FAB hides while focused.
const { target: focusTarget, ring: focusRing } = useFocusRing(
disabled || !hasHandler,
disabled || !hasHandler || !visible,
theme.colors.secondary,
'outward',
'within'
);

React.useEffect(() => {
if (!visible) {
if (Platform.OS === 'web') {
const target: unknown = touchableRef.current;
if (
target instanceof HTMLElement &&
target === document.activeElement
) {
const previous = previousFocusedElement.current;
// Only restore keyboard focus. A mouse click or tap also focuses the
// FAB, and moving focus back could jump into an earlier text field.
if (
target.matches(':focus-visible') &&
previous?.isConnected &&
!previous.closest('[aria-hidden="true"], [inert]')
) {
previous.focus({ preventScroll: true });
}
// Blur when focus didn't move back, e.g. after a mouse click or when
// the previous element is gone or can't take focus.
if (target === document.activeElement) target.blur();
}
}
hoveredRef.current = false;
pressingRef.current = false;
setHovered(false);
setPressed(false);
}
}, [visible]);

React.useEffect(() => {
if (!hasHandler) {
hoveredRef.current = false;
pressingRef.current = false;
setHovered(false);
setPressed(false);
}
}, [hasHandler]);

// The pressed elevation matches the resting one, so a press only shows on a
// hovered FAB, and `pressed` is set only then. iOS and Android send no hover
// events by default, so a tap there doesn't re-render the FAB itself. The
// refs hold the pointer state between events: a quick click on web runs both
// press handlers before a render, and a press can start before the hover.
const handleHoverIn = () => {
hoveredRef.current = true;
setHovered(true);
if (pressingRef.current) setPressed(true);
};

const handleHoverOut = () => {
hoveredRef.current = false;
setHovered(false);
setPressed(false);
};

const handlePressIn = () => {
pressingRef.current = true;
if (hoveredRef.current) setPressed(true);
};

const handlePressOut = (event: GestureResponderEvent) => {
pressingRef.current = false;
if (hoveredRef.current) setPressed(false);
onPressOut?.(event);
};

// 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-only FAB that does nothing is an image there, named by its label.
Expand All @@ -348,6 +425,7 @@ const Shell = ({
return (
<Surface
ref={ref}
aria-hidden={visible ? undefined : true}
backgroundColor={containerBg}
borderRadius={borderRadius}
style={[
Expand All @@ -356,7 +434,7 @@ const Shell = ({
outerStyle,
visible ? styles.pointerEventsAuto : styles.pointerEventsNone,
]}
elevation={elevation}
elevation={resolvedElevation}
theme={theme}
>
<Animated.View
Expand All @@ -365,15 +443,41 @@ const Shell = ({
>
{overlay}
<TouchableRipple
ref={Platform.OS === 'web' ? touchableRef : undefined}
borderless
background={background}
focusRing="none"
onPress={onPress}
onLongPress={onLongPress}
onPressOut={onPressOut}
onPress={visible ? onPress : undefined}
Comment thread
likevy marked this conversation as resolved.
Resolved
onLongPress={visible ? onLongPress : undefined}
delayLongPress={delayLongPress}
disabled={disabled}
onFocus={focusTarget.onFocus}
accessible={visible ? undefined : false}
focusable={visible ? undefined : false}
tabIndex={visible ? undefined : -1}
// Only with a touch handler: a FAB without one is a plain View that
// takes no focus, and the press handlers alone would make it a
// control. They also keep a hidden FAB's touchable mounted without
// `onPress` or `onLongPress`, so the effect above still finds the
// focused element.
onHoverIn={hasHandler ? handleHoverIn : undefined}
onHoverOut={hasHandler ? handleHoverOut : undefined}
onPressIn={hasHandler ? handlePressIn : undefined}
onPressOut={hasHandler ? handlePressOut : undefined}
onFocus={
hasHandler
? (event) => {
if (Platform.OS === 'web') {
const previous =
'relatedTarget' in event.nativeEvent
? event.nativeEvent.relatedTarget
: null;
previousFocusedElement.current =
previous instanceof HTMLElement ? previous : null;
}
focusTarget.onFocus?.(event);
}
: undefined
}
onBlur={focusTarget.onBlur}
aria-label={ariaLabel}
role={isWebImage ? 'img' : 'button'}
Expand Down
Loading
Loading