Repository navigation
fix(react-toast): keep the Toaster inside narrow viewports - #36962
Jake Salvesen (jakesalvesen) wants to merge 8 commits into
Conversation
915dc8d to
3e6400e
Compare
251faa4 to
5cc9103
Compare
|
Test evidence from a clean Linux VM (Debian 13, Node 22.23.5), running the committed test file through Jest and ts-jest: Separately, the formula was checked against a layout model of the offset arithmetic across 5 viewport widths, 6 positions and 8 non-negative offsets (240 combinations). No case overflows on either edge. Negative offsets are excluded: they position the element outside the viewport by design, and a width cap cannot correct that. The Cypress cases were run as well, and they are a clean red-green. Full output is in a later comment on this PR: 3 passing with the change, and 2 failing against this PR's original implementation, with |
|
Cypress component tests now run on the VM, so the earlier "not run locally" caveat is closed. The result is a clean red-green. With the change applied: Against this PR's original implementation ( Both failures land on the numbers in the table above: Environment: Debian 13, Node 22.23.5, Cypress 14.4.1 (the version this repo pins), headless Electron via Separately, the CSS was checked in Chromium against a layout model of 32 combinations (4 viewport widths x 8 position/offset cases): no overflow with the change, 16 overflows without it. That check used a hand-copied transcription of |
d563a46 to
b815c70
Compare
|
Paul Mardling (@PaulGMardling), I pushed an update and rewrote the description to match. I also owe you a correction on the test point from your review. Correction. In Cypress component tests the spec runs in the same frame as the mounted component. I checked it in this repo: after What changed since your review.
The description has the measurements, a before and after screenshot, and the three behavior changes worth knowing about. |
b815c70 to
1d2c8ea
Compare
|
Paul Mardling (@PaulGMardling), thanks for approving the workflows. Neither failure comes from this change, so I rebased onto current
The code is the same as on the head you approved. The workflows on the new head need approval again. Sorry for the extra step. |
📊 Bundle size reportUnchanged fixtures
|
Paul Mardling (PaulGMardling)
left a comment
There was a problem hiding this comment.
One issue to look at in getPositionStyles.ts
| Object.assign(positionStyles, { | ||
| bottom: vertical, | ||
| [end]: horizontal, | ||
| maxWidth, |
There was a problem hiding this comment.
Could we preserve stricter caller-provided width limits when applying this cap? For example, <Toaster style={{ maxWidth: 200 }} /> previously stayed 200px wide, but the existing Object.assign now replaces that limit, expanding it to 292px on desktop. Could we combine the limits and add a regression test so viewport protection doesn’t widen existing customized toasters?
There was a problem hiding this comment.
Good catch, thank you. Fixed in cde8856, with a different approach, so that the caller's limit is not touched at all.
- The cap is no longer an inline
max-width.getPositionStylesreturns it as a custom property, and the width of the Toaster ismin(292px, var(--fui-Toaster--max-width, 100%)). max-widthbelongs to the caller again.<Toaster style={{ maxWidth: 200 }} />stays 200px wide on desktop. So does amax-widthfromclassName, which the inline cap also overrode.- In a narrow viewport the stricter of the two limits applies. At 180px that Toaster is 140px wide, with the 20px gutter on both sides.
- A Toaster whose
widthis overridden keeps that width, as onmaster.
Toaster.cy.tsx has four new cases for this. Three of them fail on the previous head. The description has the measurements and the browser support note for min().
Edited on November 23, 2026: updated the commit id after the branch was rebased onto master.
1d2c8ea to
e8a7fa8
Compare
There was a problem hiding this comment.
🟡 Changes recommended
Position slots share the caller’s style object, causing width caps to overwrite one another and potentially allowing toasts to overflow when multiple positions are used.
1 open issue
What changed in this PR
This PR addresses #37124 by making Fluent UI’s Toaster shrink in narrow viewports, preserving page gutters and preventing toasts from capturing pointer events there.
Changes:
- Derives position-specific width caps from horizontal offsets and applies them through a CSS custom property.
- Preserves caller-provided
max-widthlimits while leaving explicit width overrides unchanged. - Adds unit and Cypress coverage for offsets, narrow viewports, gutter hit testing, and caller limits.
| File | Description |
|---|---|
packages/react-components/react-toast/library/src/state/vanilla/index.ts |
Re-exports the width-cap custom property name. |
packages/react-components/react-toast/library/src/state/vanilla/getPositionStyles.ts |
Calculates width caps for each position and offset. |
packages/react-components/react-toast/library/src/state/vanilla/getPositionStyles.test.ts |
Tests width caps and per-position offsets. |
packages/react-components/react-toast/library/src/state/index.ts |
Exposes the custom property to component styles. |
packages/react-components/react-toast/library/src/components/Toaster/useToasterStyles.styles.ts |
Applies the cap to the Toaster’s width. |
packages/react-components/react-toast/library/src/components/Toaster/Toaster.cy.tsx |
Adds browser coverage for sizing and gutter behavior. |
change/@fluentui-react-toast-0e392a43-5fbd-4cec-8da6-5a1af31ffeab.json |
Records the patch release change. |
Review effort: Balanced
… toast The Toaster root is position fixed with width 292px and no viewport clamp, so on a viewport narrower than the toast plus its 20px edge offset the root starts at a negative left offset. The toast then covers the left edge of the screen and takes pointer events over the content beneath it. max-width: calc(100vw - 40px) keeps the default offset on both sides at any width.
…ange Follow the repository's contributing documentation: generate the change file with `yarn beachball change` and the `fix(react-toast): ...` message format instead of a hand-written file. No code change.
…fset The cap was a hardcoded calc(100vw - 40px), which assumed the default 20px offset and ignored Toaster.offset. It also used 100vw, which includes the scrollbar, so it could still overflow by the scrollbar width. Move the cap into getPositionStyles, where the offset is already resolved, and compute it as calc(100% - max(2 * abs(horizontal), 40)px). The floor keeps the default 20px gutter, so the default offset produces the same 40px as before. A centered toast is shifted by the offset and keeps its own width, so it needs the same 2x term on one side. 100% replaces 100vw: on a fixed element it resolves against the viewport minus the scrollbar, and in inline mode against the positioned ancestor, which is the intended bound in both cases. The Cypress assertion compared against window.innerWidth, which in a spec is the runner window rather than the application viewport that cy.viewport resizes, so it could pass while the toast was outside the application viewport. It now reads ownerDocument.defaultView. Two cases are added alongside the existing one, chosen so the old hardcoded cap fails: a bottom-end toast with a 60px offset, which put the left edge 20px outside the viewport, and a centered toast with a 32px offset, which put the right edge 12px outside.
These files were written on Windows and committed with CRLF, which the repository's Prettier configuration rejects (endOfLine defaults to lf). No content changes.
No behaviour change. The repository's Prettier configuration (printWidth 120) wants one assertion collapsed to a single line and two table rows expanded.
…Toaster is capped A centered Toaster with a horizontal offset of 20px or more ended flush with the edge it moved toward, and a start or end Toaster with a small offset was narrower than it had to be. The cap now keeps the configured offset at the near edge and the default gutter at the far edge, and a centered Toaster keeps the gutter at both edges. The Cypress spec measures the toast as well as the Toaster, covers every kind of position, and checks that the gutter stays free for pointer events. An unrelated import change in useToasterStyles.styles.ts is reverted.
…rom the caller still applies The width cap was an inline max-width. It replaced a maxWidth from the style prop and won over a max-width from className, so a Toaster that was limited to 200px grew back to 292px. The position styles now set the cap as a custom property, and the width of the Toaster is min(292px, cap). A max-width from the caller is a separate declaration again, so the stricter of the two limits applies.
The position slots are created from the same root props, so they share the style object of the caller, and the position styles were written into that object. With a style prop the width cap and the position styles of one position ended up on the others. React also freezes a style object in development once it has rendered it, so the next render of the Toaster threw. Each position slot now gets a new object: the style of the caller with its own position styles on top. Fixes microsoft#37209
ccffeee to
8d2bacd
Compare



Previous Behavior
The Toaster is
position: fixedwith a fixedwidth: 292pxand no maximum width. With the default 20px horizontal offset it needs 312px, so in a narrower viewport it runs past the edge. At 280px abottom-endtoast starts 32px outside the left edge. The part that is still on screen also sits on top of the page gutter and takes the pointer events there. A customoffset.horizontalchanges the width at which this starts.New Behavior
getPositionStylesnow returns a width cap with the position styles, as the custom property--fui-Toaster--max-width. The width of the Toaster ismin(292px, var(--fui-Toaster--max-width, 100%)).calc(100% - (offset + 20px)).calc(100% - 2 * (|offset| + 20px)).100%resolves against the same containing block as the offsets. That is the viewport without the scrollbar for the default Toaster, and the positioned ancestor for aninlineToaster.The cap is on
widthand not onmax-width. Amax-widththat the caller sets throughstyleorclassNamestays a declaration of its own, and the stricter of the two limits applies.Each position of the Toaster also gets its own style object. The position slots are created from the same root props, so on
masterthey share the object that is passed asstyle, and the position styles are written into that object. With astyleprop the styles of one position, and now its cap, would end up on the others. In a development build the Toaster also throws on its next render, because React freezes a style object once it has rendered it. That is #37209. Each slot now gets a new object: the caller's styles with the styles of its own position on top.A
bottom-endtoast with the default offset in a 280px wide viewport:masterMeasured in Electron 130 at 280px wide. Each cell is the left gutter, the right gutter and the width of the Toaster in px. A negative gutter is an overflow.
masterbottom-endbottom-endbottom-startbottom-starttop-endtop-startbottombottomtopA
bottom-endToaster with its own limit, measured the same way. Each cell is the width in px.mastermaxWidth: 200instyle, ormax-width: 200pxthroughclassNamewidth: 400pxinstyleor throughclassNameBehavior changes to be aware of
mastershows a 292px toast with 8px and 20px.inlineToaster in a container narrower than 332px narrows in the same way.max-widthfrom the caller keeps it, and the cap applies only where it is stricter. A Toaster whosewidthis overridden keeps that width and is not capped, as onmaster.style.min()with a custom property, as the thumb position ofSliderusesclamp(). Firefox 69 to 74 and Opera 64 and 65, which are only in the partial support matrix, do not supportmin(). By the CSS rules for custom properties thewidthdeclaration is then invalid at computed-value time, so the Toaster takes its width from its content. I did not test those browsers.Tests
Toaster.cy.tsxis new. In a 280px viewport it mounts a toast for the nine combinations in the first table and checks, for both the Toaster and the toast inside it, that the near edge keeps the configured offset and the far edge keeps the 20px gutter. A tenth case useselementFromPointto check that the toast no longer covers the page gutter. These ten fail onmaster.maxWidthof 200px fromstyleand fromclassNamestays 200px in a 600px viewport, the cap applies when it is stricter than amaxWidthfromstyle, and the 200px fromclassNameapplies in the 280px viewport.bottom-startandbottom-endwith different offsets and astyleprop, and checks that each keeps its own position and its own cap.Toaster.test.tsxgains a case that shows two toasts in a Toaster with astyleprop and checks that the object of the caller is not changed. It throws onmaster.getPositionStyles.test.tscovers the expected cap for start, end and centered positions, negative offsets, and per-position offsets.Run on this branch:
react-toastunit suite: 224 tests pass, also under React 17 and React 18. Lint, type-check andformat:checkpass.Toaster.cy.tsx: 15 of 15 pass, and 20 runs in a row passed without a retry. Thereact-toastCypress suite also passes under React 17 and React 18.How to test
Open a Toast story, for example Offset, narrow the browser to 280px and dispatch a toast. Then try 320px, a horizontal offset of 0, 32 and 60, and the
bottomposition. Addstyle={{ maxWidth: 200 }}to the Toaster to see a caller's limit kept.Related Issue(s)