diff --git a/docs/6.x/docs/guides/migration.md b/docs/6.x/docs/guides/migration.md index bb5821f1bb..6d0b583eb3 100644 --- a/docs/6.x/docs/guides/migration.md +++ b/docs/6.x/docs/guides/migration.md @@ -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 - -+ ++ ``` #### `size` diff --git a/example/src/Examples/FABExample.tsx b/example/src/Examples/FABExample.tsx index 09ce39aa14..e6470cc091 100644 --- a/example/src/Examples/FABExample.tsx +++ b/example/src/Examples/FABExample.tsx @@ -34,9 +34,9 @@ const variants: FabColor[] = [ 'primary', 'secondary', 'tertiary', - 'tonalPrimary', - 'tonalSecondary', - 'tonalTertiary', + 'primaryContainer', + 'secondaryContainer', + 'tertiaryContainer', 'custom', ]; @@ -91,7 +91,7 @@ const FABExample = () => { const { colors } = useTheme(); const insets = useSafeAreaInsets(); - const [variant, setVariant] = React.useState('tonalPrimary'); + const [variant, setVariant] = React.useState('primaryContainer'); const activeVariant = variant === 'custom' ? undefined : variant; const activeContainerColor = variant === 'custom' ? CUSTOM_CONTAINER_COLOR : undefined; @@ -187,6 +187,7 @@ const FABExample = () => { {type === 'icon' && ( { alignment={position} trigger={{ icon: 'pencil', + 'aria-label': 'Compose', variant: activeVariant, containerColor: activeContainerColor, size, diff --git a/src/components/FAB/Extended.tsx b/src/components/FAB/Extended.tsx index 15a971f457..15c9a0ad1e 100644 --- a/src/components/FAB/Extended.tsx +++ b/src/components/FAB/Extended.tsx @@ -39,7 +39,7 @@ export type Props = { */ label: string; /** - * Role-color preset. Defaults to `tonalPrimary`. + * Role-color preset. Defaults to `primaryContainer`. */ variant?: Variant; /** @@ -167,7 +167,7 @@ export type Props = { const Extended = ({ icon, label, - variant = 'tonalPrimary', + variant = 'primaryContainer', containerColor, contentColor, size = 'default', diff --git a/src/components/FAB/FAB.tsx b/src/components/FAB/FAB.tsx index 5b92548c47..961dfdd16b 100644 --- a/src/components/FAB/FAB.tsx +++ b/src/components/FAB/FAB.tsx @@ -21,7 +21,7 @@ export type Props = { */ icon: IconSource; /** - * Role-color preset. Defaults to `tonalPrimary`. + * Role-color preset. Defaults to `primaryContainer`. */ variant?: Variant; /** @@ -130,7 +130,7 @@ export type Props = { */ const FAB = ({ icon, - variant = 'tonalPrimary', + variant = 'primaryContainer', size = 'default', visible = true, onPress, diff --git a/src/components/FAB/Menu.tsx b/src/components/FAB/Menu.tsx index 77f6e0b0bc..2aa808bb5e 100644 --- a/src/components/FAB/Menu.tsx +++ b/src/components/FAB/Menu.tsx @@ -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 @@ -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; diff --git a/src/components/FAB/Shell.tsx b/src/components/FAB/Shell.tsx index f8a94b4beb..01fe53496e 100644 --- a/src/components/FAB/Shell.tsx +++ b/src/components/FAB/Shell.tsx @@ -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'; @@ -43,7 +43,7 @@ export type ShellProps = { */ label?: string; /** - * Role-color preset. Defaults to `tonalPrimary`. + * Role-color preset. Defaults to `primaryContainer`. */ variant?: Variant; /** @@ -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. @@ -200,7 +195,7 @@ export type ShellProps = { const Shell = ({ icon, label, - variant = 'tonalPrimary', + variant = 'primaryContainer', size = 'default', containerColor, contentColor, @@ -208,7 +203,6 @@ const Shell = ({ iconSize, leading, trailing, - elevation = Tokens.stateElevation.enabled, visible = true, onPress, onLongPress, @@ -235,6 +229,23 @@ const Shell = ({ ref, }: ShellProps) => { const theme = useInternalTheme(themeOverrides); + const [hovered, setHovered] = React.useState(false); + const [pressed, setPressed] = React.useState(false); + const touchableRef = React.useRef>(null); + const previousFocusedElement = React.useRef(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 }), @@ -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 @@ -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. @@ -348,6 +425,7 @@ const Shell = ({ return ( {overlay}