Skip to content

refactor(snackbar)!: modernize layout and action API - #5135

Open
grzegorzkruk wants to merge 3 commits into
callstack:mainfrom
grzegorzkruk:refactor/snackbar-md3
Open

grzegorzkruk wants to merge 3 commits into
callstack:mainfrom
grzegorzkruk:refactor/snackbar-md3

Conversation

@grzegorzkruk

@grzegorzkruk grzegorzkruk commented Oct 1, 2026 •

Copy link
Copy Markdown

Move the Snackbar to its own directory and align it with the MD3 spec.

Refs #4932

Motivation

The Snackbar API is built around arbitrary children and a full Button props object, so the layout cannot follow the Material Design 3 message-and-action model, and consumers have to wrap the component in a Portal themselves to keep it above other content.

Related issue

Fixes #4932

Screenshots / Videos

Checked on the Android example, Snackbar screen:

  • Default: a single-line inverse surface with "Single-line snackbar" and a trailing "Action".
image
  • Longer message, longer action, and close icon: the message wraps to two lines, and "Toggle theme" plus the close icon sit on a trailing line below it.
image
  • With "Render in portal" off, the same bar still pins to the bottom of the screen.
image

Test plan

  • Open the example app and go to Snackbar.
  • Press Show. Confirm a single-line snackbar with the message and an Action button.
  • Press Action. Confirm the snackbar dismisses without calling onDismiss from the action handler.
  • Turn on Longer message and Show again. Confirm the message wraps to a second line.
  • Turn on Longer action. Confirm the action moves onto its own trailing line.
  • Turn on Close icon button and press the icon. Confirm the snackbar dismisses.
  • Turn off Render in portal and Show again. Confirm the snackbar still appears at the bottom of the screen.
  • Leave it visible. Confirm it dismisses on its own after the medium duration, and that it stays up when duration is Infinity.

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>
@github-actions

github-actions Bot commented Oct 1, 2026 •

Copy link
Copy Markdown

Found potential problems with the pull request:

  • The description is too long. Please keep it under 1000 characters.

@satya164

satya164 commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Review by Claude

iOS (iPhone 17 simulator)

  1. The Snackbar has no side margin on a phone.
    Steps: open Snackbar, press Show.
    What happens: the bar touches both screen edges. Only the bottom margin is there.
    What should happen: an 8dp gap on the left and right, as on main.
    iOS: Snackbar touches both screen edges
    (via Claude, verify if it makes sense)

  2. A short message with a longer action wraps the action onto a second line, though both fit on one line.
    Steps: turn on Close icon button and Longer action, keep Longer message off, press Show.
    What happens: "Single-line snackbar" sits alone on the first line, and "Toggle theme" plus the close icon move to a second line.
    What should happen: one line. The message and the actions need about 340pt of the 402pt screen.
    iOS: short message with the action wrapped onto a second line
    (via Claude, verify if it makes sense)

Android (Pixel emulator)

  1. Same missing side margin as on iOS.
    Steps: open Snackbar, press Show.
    Android: Snackbar touches both screen edges
    (via Claude, verify if it makes sense)

Web (375px wide viewport)

  1. Same missing side margin. getBoundingClientRect() on the Surface gives left: 0, right: 375 while its computed margin is 8px.
    Web, 375px: Snackbar touches both edges
    (via Claude, verify if it makes sense)

  2. Double-clicking Action keeps the Snackbar on screen and runs action.onPress twice.
    Steps: Show, then double-click Action.
    What happens: the theme toggles twice and the Snackbar stays visible. The second click lands during the exit animation, calls onPress again, and the example's onDismiss toggles showSnackbar back to true.
    What should happen: one onPress call, and the Snackbar hides.
    (via Claude, verify if it makes sense)

@satya164 satya164 left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Comment thread src/components/Snackbar/Snackbar.tsx Resolved
Comment thread src/components/Snackbar/tokens.ts Resolved
Comment thread src/components/Snackbar/Snackbar.tsx Resolved
Comment thread src/components/Snackbar/Snackbar.tsx Resolved
Comment thread src/components/Snackbar/Snackbar.tsx Resolved
Comment thread src/components/Snackbar/Snackbar.tsx Resolved
Comment thread docs/6.x/docs/guides/migration.md Resolved
Comment thread src/components/Snackbar/tokens.ts Resolved

@satya164 satya164 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review by Claude

See the inline comment. (via Claude, verify if it makes sense)

Comment thread src/components/Snackbar/Snackbar.tsx Resolved
- 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
Comment on lines +374 to +375
const stacked =
Boolean(action) && isMultiline && actionWidth > maxInlineActionWidth;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

${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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 elevation changed from 2 to 3.

@lukewalczak

Copy link
Copy Markdown
Member

/review

@apex-sentinel-staging

Copy link
Copy Markdown

Apex Sentinel · ✅ Approve · review of ebee158

4 finding(s): 4 minor.

🟡 Minor (4)

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 out
  • src/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.

@apex-sentinel-staging apex-sentinel-staging Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apex Sentinel: 4 new findings on ebee158. The verdict and the full list are in the summary comment.

Comment on lines +359 to +375
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;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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.

Comment on lines +265 to +271
const handleShown = useLatestCallback(() => {
clearHideTimeout();

if (duration !== Number.POSITIVE_INFINITY) {
hideTimeout.current = setTimeout(onDismiss, duration);
}
});

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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.

⚠️ Check this patch before you commit it: the review doubted it would be right as written.

Suggested change
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.

Comment on lines +434 to +444
// 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}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants