Add useViewportMatch react hook alternative to withViewportMatch#18816
Add useViewportMatch react hook alternative to withViewportMatch#18816youknowriad merged 6 commits intomasterfrom
Conversation
3f67029 to
3c85f9e
Compare
|
|
||
| // If the block is selected and we're typing the block should not appear. | ||
| // Empty paragraph blocks should always show up as unselected. | ||
| const isFocusModeActive = isFocusMode && isLargeViewport; |
packages/edit-post/src/components/header/post-publish-button-or-toggle.js
Outdated
Show resolved
Hide resolved
3c85f9e to
dea8960
Compare
@Tug , can you have a quick look and follow up here? We at least need to make sure we're not going down a path that'll be incompat with RN and, whether we can afford merging the web side while we will work on the native side in a separate PR.Thanks! |
|
Concerning incompatibilities I think we're fine, if we look at the impacted components:
Afaik we only use |
|
@Tug do you think it's possible to build a mobile version for the hook (not necessarily now) but eventually, the viewport package could be deprecated. |
aduth
left a comment
There was a problem hiding this comment.
I left some ideas for you to consider, but this seems like a very sensible improvement over the viewport package/store/HoC.
On the point of performance: I think this will probably be fine. Not sure if there's really much overhead with MediaQueryList#addListener.
|
An interesting advantage that higher-order components have over hooks is relevant for this, in that if a composition of higher-order components includes one which conditionally renders, it can prevent the logic of the next from being unnecessarily evaluated. This is in contrast to hooks, where all hooks must be called before any conditions are made about whether the component renders. It's especially notable for something like That being said, I'm more in favor of being consistent in how we approach this, and I think hooks are probably the way we want to go for most everything. I expect it's probably worth the trade-off. |
181f77a to
fecbfaa
Compare
Was this the reason for not replacing the use of |
|
Yeah, that's the main reason (I didn't measure though), there's another reason which BC of the filter, that said, I believe this is a prop that is not so important and that we can remove with a dev note. |
I wonder about this actually. Depending on whether the browser internally normalizes multiple It reminds me of And this doesn't even account for any benefits we might see simply by migrating from a higher-order component to a hook (tree flattening). |
In an effort to flatten the React Tree, I've added a useViewportMatch to the compose package. It relies on the existing useMediaQuery similar to useReducedMotion.
Pros
One nice side-effect is that this removes the dependency to
@wordpress/viewportwhich itself depends on@wordpress/dataand we were always reluctant to use it the@wordpress/componentspackage because of this. Right now, the dependency is not removed from the existing packages because we still have class components where we can't use hooks yet.Cons
The potential downside is that the viewport package alwas trigger a fixed number of listerners, I think it's 10 or 12 listeners, while the number here depends on how much the hook is used. That said, adding a cache for useMediaQuery React hook is possible and it will be useful for all media query hooks, not just the viewport one.
Performance
In terms of performance, I did a comparison with master and I didn't notice any impact so far, I think once we remove the "viewport" package dependency entirely, we might notice a gain in performance.
Mobile
I'm not familiar with react-native enough to build the alternative mobile version as well, but if possible, it would be good to provide one there as well. cc @hypest