Repository navigation
refactor(snackbar)!: modernize layout and action API - #5135
grzegorzkruk wants to merge 3 commits into
Conversation
Move the Snackbar to its own directory and align it with the MD3 spec.
- The message is passed via the `message` prop instead of `children`, which
removes the arbitrary child composition the layout had to account for.
- `action` takes a typed `SnackbarAction` instead of every `Button` prop, and
dismisses the Snackbar on its own.
- `contentStyle` becomes `messageStyle`, applied to the message text.
- The Snackbar renders in a `Portal` by default, so it overlays all other
content without the consumer wrapping it. Set `portal={false}` to opt out.
- Layout, color roles and motion come from `tokens.ts` and the theme: 48dp
minimum height, 600dp max width, 16/14dp message padding, `extraSmall`
shape via `resolveCornerRadius`, and `inverseSurface` / `inverseOnSurface`
/ `inversePrimary`.
- An action which doesn't fit next to the message wraps onto its own trailing
line, as the spec's longer-action configuration asks for.
- The enter/exit animations use the theme motion tokens, and the auto-dismiss
timer starts once the enter animation has settled.
Refs callstack#4932
Co-authored-by: Cursor <cursoragent@cursor.com>
|
Found potential problems with the pull request:
|
There was a problem hiding this comment.
Review by Claude
A Snackbar rendered inside a React Native Modal now mounts in the root Portal host, which sits behind the native modal. With the default portal, the Snackbar does not show while the modal is open on iOS, Android and web. With portal={false} it shows inside the modal. The migration guide should mention portal={false} for this case. (via Claude, verify if it makes sense)
satya164
left a comment
There was a problem hiding this comment.
Review by Claude
See the inline comment. (via Claude, verify if it makes sense)
- Updated Snackbar to no longer require wrapping in a Portal, allowing it to render alongside sibling components when `portal={false}`.
- Enhanced accessibility by announcing Snackbar messages on iOS when visible.
- Adjusted action handling to prevent multiple presses on the same action.
- Improved layout logic to stack actions under multiline messages when necessary.
- Updated tests to cover new behavior and ensure proper rendering in various scenarios.
Refs callstack#4932
Co-authored-by: Cursor <cursoragent@cursor.com> # Conflicts: # docs/6.x/docs/guides/migration.md # src/components/Snackbar.tsx # src/components/__tests__/Snackbar.test.tsx # src/components/__tests__/__snapshots__/Snackbar.test.tsx.snap
| const stacked = | ||
| Boolean(action) && isMultiline && actionWidth > maxInlineActionWidth; |
There was a problem hiding this comment.
I think stacked can loop. isMultiline is measured in the layout that stacked picks. If a message wraps next to the action but fits on one line at full width, stacking makes it single-line, which unstacks it, which makes it wrap again.
To reproduce: in the example, turn on the close icon and the longer action, then try a message of about 30–60 characters, for example "Couldn't send message. Check connection." On iOS and Android the layout seemed to flip every frame while the Snackbar was visible. Could you check it?
| ]} | ||
| maxFontSizeMultiplier={maxFontSizeMultiplier} | ||
| numberOfLines={messageMaxLines} | ||
| onTextLayout={handleMessageTextLayout} |
There was a problem hiding this comment.
onTextLayout isn't supported by Text in react-native-web (exports/Text only wires onLayout), so isMultiline never turns true there and the longer-action layout never shows on web.
| const handleShown = useLatestCallback(() => { | ||
| clearHideTimeout(); | ||
|
|
||
| if (duration !== Number.POSITIVE_INFINITY) { |
There was a problem hiding this comment.
The old handleShowAnimationFinished bailed out on !visible || !isMounted.current. handleShown dropped that guard, but it still runs asynchronously via scheduleOnRN after the enter animation finishes on the UI thread. If visible turns false or the Snackbar unmounts in that window, the effect / unmount cleanup clears the timer first, then handleShown arms a new one, and onDismiss fires a second time or after unmount.
const handleShown = useLatestCallback(() => {
clearHideTimeout();
- if (duration !== Number.POSITIVE_INFINITY) {
+ if (!visible) {
+ return;
+ }
+
+ if (Number.isFinite(duration)) {
hideTimeout.current = setTimeout(onDismiss, duration);
}
});(Number.isFinite also restores the old -Infinity handling, which now dismisses immediately.) The unmount case still needs a mounted ref.
| : styles.actionsTrailing, | ||
| ]} | ||
| onLayout={handleActionsLayout} | ||
| testID={testID ? `${testID}-actions` : undefined} |
There was a problem hiding this comment.
${testID}-actions sits on an internal wrapper and is used only by the stacking test (Snackbar.test.tsx:323). Per code.instructions.md: "Avoid IDs on internal wrappers solely for tests". That test also asserts toHaveStyle({ flexDirection: 'column' }) (Snackbar.test.tsx:328), an exact internal style value, which tests.instructions.md flags. Assert observable behavior instead, e.g. that the action renders below the message, and drop the wrapper ID.
| /> | ||
| ``` | ||
|
|
||
| ### Snackbar |
There was a problem hiding this comment.
Two behavior changes aren't in the guide:
- The message is now capped at 2 lines (
messageMaxLines). Longer text that used to wrap is silently truncated. - Default
elevationchanged from2to3.
|
/review |
Apex Sentinel · ✅ Approve · review of
|
| Where | Finding |
|---|---|
src/components/Snackbar/Snackbar.tsx:359 |
stacked and isMultiline feed each other, so a wrapping message can oscillate layouts |
src/components/Snackbar/Snackbar.tsx:265 |
Dismiss timer can be armed after the Snackbar is already dismissed |
src/components/Snackbar/Snackbar.tsx:434 |
Migration entry does not say which former action Button props are now ignored |
src/components/Snackbar/Snackbar.tsx:441 |
Long messages are now silently truncated to two lines, undocumented |
Not in the reviewers' prompt (2 files)
src/components/__tests__/Snackbar/__snapshots__/Snackbar.test.tsx.snap: generated or a lockfile, so its lines were left outsrc/components/__tests__/__snapshots__/Snackbar.test.tsx.snap: generated or a lockfile, so its lines were left out
2 findings below minor hidden (user_settings.min_severity).
Reviewed with this repository's own Sentinel configuration (e4256fd06b0f).
💬 Talking to Sentinel
In a finding's thread, reply with:
| Command | What it does |
|---|---|
@apex-sentinel-staging note <context> |
Adds context; Sentinel re-checks the cited code |
@apex-sentinel-staging pivot <why> |
Says the finding is wrong; Sentinel re-checks the cited code |
@apex-sentinel-staging like |
Marks the finding as useful |
@apex-sentinel-staging dislike <why> |
Marks it as not useful, so it is not raised again |
On this conversation, comment @apex-sentinel-staging review to run a full review once again.
There was a problem hiding this comment.
Apex Sentinel: 4 new findings on ebee158. The verdict and the full list are in the summary comment.
| const handleMessageTextLayout = React.useCallback( | ||
| (event: TextLayoutEvent) => { | ||
| setIsMultiline(event.nativeEvent.lines.length > 1); | ||
| }, | ||
| [] | ||
| ); | ||
|
|
||
| const handleActionsLayout = React.useCallback((event: LayoutChangeEvent) => { | ||
| setActionWidth(event.nativeEvent.layout.width); | ||
| }, []); | ||
|
|
||
| const isIconButton = Boolean(onIconPress); | ||
| const hasActions = Boolean(action) || isIconButton; | ||
| // Match Material Components Android: stack the action only when the message | ||
| // already wraps and the action is wider than the inline limit. | ||
| const stacked = | ||
| Boolean(action) && isMultiline && actionWidth > maxInlineActionWidth; |
There was a problem hiding this comment.
🟡 Minor · stacked and isMultiline feed each other, so a wrapping message can oscillate layouts
A message that wraps to two lines only when the action sits inline, with an action wider than maxInlineActionWidth, causes stacked to toggle between true and false indefinitely (render loop or bar flicker). isMultiline is measured from the message Text in whichever layout stacked selects, so flipping stacked changes the measurement that produced it. No hysteresis prevents the cycle, and actionWidth is measured on the actions container that also changes size when stacking.
Suggested change
Measure the message's natural (unconstrained) line count once, or latch isMultiline to the inline layout only, so that flipping stacked cannot change the measurement that produced it.
Details
persona: react-native-paper
Useful? Reply @apex-sentinel-staging like, or @apex-sentinel-staging dislike <why> if it is not.
Reply @apex-sentinel-staging note ... to add context, or @apex-sentinel-staging pivot ... if it is wrong — I will re-check the cited code.
| const handleShown = useLatestCallback(() => { | ||
| clearHideTimeout(); | ||
|
|
||
| if (duration !== Number.POSITIVE_INFINITY) { | ||
| hideTimeout.current = setTimeout(onDismiss, duration); | ||
| } | ||
| }); |
There was a problem hiding this comment.
🟡 Minor · Dismiss timer can be armed after the Snackbar is already dismissed
If the parent sets visible={false} in the same tick the enter animation finishes, handleShown runs after the visible={false} effect has already called clearHideTimeout(), arming setTimeout(onDismiss, duration) that fires ~duration ms after the snackbar is gone. handleShown checks neither visible nor mounted state, unlike its predecessor handleShowAnimationFinished.
| const handleShown = useLatestCallback(() => { | |
| clearHideTimeout(); | |
| if (duration !== Number.POSITIVE_INFINITY) { | |
| hideTimeout.current = setTimeout(onDismiss, duration); | |
| } | |
| }); | |
| const handleShown = useLatestCallback(() => { | |
| clearHideTimeout(); | |
| if (!visibleRef.current || duration === Number.POSITIVE_INFINITY) { | |
| return; | |
| } | |
| hideTimeout.current = setTimeout(onDismiss, duration); | |
| }); |
Details
persona: react-native-paper, senior-developer, javascript
Useful? Reply @apex-sentinel-staging like, or @apex-sentinel-staging dislike <why> if it is not.
Reply @apex-sentinel-staging note ... to add context, or @apex-sentinel-staging pivot ... if it is wrong — I will re-check the cited code.
| // Figma puts a 4dp gap only between the message and an action; a | ||
| // close icon alone sits flush against the trailing edge. | ||
| !stacked && action ? styles.messageWithAction : null, | ||
| { color: colors[messageColor] }, | ||
| messageStyle, | ||
| ]} | ||
| maxFontSizeMultiplier={maxFontSizeMultiplier} | ||
| numberOfLines={messageMaxLines} | ||
| onTextLayout={handleMessageTextLayout} | ||
| > | ||
| {message} |
There was a problem hiding this comment.
🟡 Minor · Migration entry does not say which former action Button props are now ignored
The Snackbar migration section says the action takes "a narrow set of options" but never names which keys survive, so an upgrading app that passed numberOfLines, labelStyle, accessibilityRole or hitSlop has nothing to check against when those are silently dropped with no type or runtime error.
Suggested change
In the Snackbar migration section, state that only label, onPress, accessibilityLabel, testID and style are honoured on action (all other Button props are now ignored) and that the action button's default horizontal margins were replaced by the MD3 layout.
Details
persona: senior-developer, maintainability
Useful? Reply @apex-sentinel-staging like, or @apex-sentinel-staging dislike <why> if it is not.
Reply @apex-sentinel-staging note ... to add context, or @apex-sentinel-staging pivot ... if it is wrong — I will re-check the cited code.
| messageStyle, | ||
| ]} | ||
| maxFontSizeMultiplier={maxFontSizeMultiplier} | ||
| numberOfLines={messageMaxLines} |
There was a problem hiding this comment.
🟡 Minor · Long messages are now silently truncated to two lines, undocumented
numberOfLines={messageMaxLines} caps the message Text at two lines, and the container's minHeight assumes a two-line block. The default elevation change from 2 to 3 is also undocumented in docs/6.x/docs/guides/migration.md.
Suggested change
Add two lines to the Snackbar section of docs/6.x/docs/guides/migration.md documenting that the message is capped at two lines (numberOfLines: 2) and that the default elevation changed from 2 to 3.
Details
persona: javascript, maintainability
Useful? Reply @apex-sentinel-staging like, or @apex-sentinel-staging dislike <why> if it is not.
Reply @apex-sentinel-staging note ... to add context, or @apex-sentinel-staging pivot ... if it is wrong — I will re-check the cited code.




Move the Snackbar to its own directory and align it with the MD3 spec.
Refs #4932
Motivation
The Snackbar API is built around arbitrary
childrenand a fullButtonprops object, so the layout cannot follow the Material Design 3 message-and-action model, and consumers have to wrap the component in aPortalthemselves to keep it above other content.Related issue
Fixes #4932
Screenshots / Videos
Checked on the Android example, Snackbar screen:
Test plan
onDismissfrom the action handler.durationisInfinity.