adobe / react-spectrum

A collection of libraries and tools that help you build adaptive, accessible, and robust user experiences.
https://react-spectrum.adobe.com
Apache License 2.0
12.64k stars 1.09k forks source link

BreakpointProvider matched breakpoints do not update when Provider breakpoints change #2912

Open zbrownell opened 2 years ago

zbrownell commented 2 years ago

πŸ› Bug Report

When a Provider is added to the DOM, any value passed in to its breakpoints prop will be evaluated at the current settings (browser/window size) and the result (matched breakpoints in the breakpoints provider) will be cached. This appears to be "as designed" under the assumption that breakpoints won't change (see: https://github.com/adobe/react-spectrum/pull/2412#discussion_r720705953), but if they do change, then the stale breakpoints are always returned, since the resize method caches the "current" breakpoints handler method.

πŸ€” Expected Behavior

If the breakpoints prop changes in a Provider, the breakpoints handler method should be recomputed so that the actual active breakpoints (matched breakpoints) are returned to components beneath this Provider (in the DOM)

😯 Current Behavior

Any updates to the breakpoints will reflect that the Provider code has the new breakpoints, but the actual matched breakpoints will be stale (never update) - so, any components beneath this Provider will still be operating on the old set of matches

πŸ’ Possible Solution

Make the breakpointHandler in BreakpointProvider a callback method (useCallback) that is recomputed if the breakpoints value is changed.

Update the dependency list for the resize effect to update the resize method when the breakpointHandler changes.

πŸ”¦ Context

I need to create a Provider that is able to dynamically adjust its own breakpoints, given the actual amount of horizontal space it has available (at the time - so, this can grow/shrink at runtime).

This "responsive" Provider is necessary to support a higher-level AngularJS component (sidebar/help drawer) that "pushes" in from the side, decreasing the amount of horizontal space actually afforded to the Provider (i.e. - window size minus help drawer size). Ideally, if the horizontal space shrinks, it would adjust breakpoints and reflow content given the actual amount of space available.

We are transitioning our website away from AngularJS and into React. So, we really need this piece because we can't easily go from one UI framework to another and back again. So, we have to build in React from the bottom -> up in the DOM (only ever going from an AngularJS context -> React). I mention this because we are unable to place a React component "above" this one (at this point) to manage that sidebar interaction and the overall layout (ideal solution) because there are too many other components that sit underneath it that have not been ported (yet)... and, we can't easily go AngularJS -> React -> AngularJS (again).

πŸ’» Code Sample

Here is a code sandbox illustrating a very basic approach to implementing this type of Provider

🌍 Your Environment

Software Version(s)
react-spectrum latest
Browser all
Operating System all

🧒 Your Company/Team

Adobe | BPS | Admin Console

πŸ•· Tracking Issue (optional)

Not publicly available (sorry)

tgelu commented 1 year ago

Hi. Experiencing the same issue. The PR seems to be abandoned. Any plans to give this any attention sometime?