Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[utils] Add useResizeObserver hook #27736

Closed
wants to merge 7 commits into from

Conversation

hbjORbj
Copy link
Member

@hbjORbj hbjORbj commented Aug 13, 2021

This pull request is a continuation of this issue: #22519

@hbjORbj hbjORbj mentioned this pull request Aug 13, 2021
1 task
@mui-pr-bot
Copy link

mui-pr-bot commented Aug 13, 2021

Details of bundle changes (experimental)

@material-ui/core: parsed: +0.05% , gzip: +0.09%
@material-ui/lab: parsed: +0.08% , gzip: +0.12%
@material-ui/unstyled: parsed: +1.14% , gzip: +1.01%

Generated by 🚫 dangerJS against 0ddba62

@hbjORbj hbjORbj added the new feature New feature or request label Aug 13, 2021
Copy link
Member

@oliviertassinari oliviertassinari left a comment

Choose a reason for hiding this comment

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

It could be interesting to write the hook in TypeScript directly, e.g. https://github.com/react-restart/hooks/blob/master/src/useResizeObserver.ts

package.json Outdated Show resolved Hide resolved
packages/material-ui-utils/src/useResizeObserver.js Outdated Show resolved Hide resolved
packages/material-ui-utils/src/index.ts Outdated Show resolved Hide resolved
packages/material-ui-utils/src/useResizeObserver.js Outdated Show resolved Hide resolved
@hbjORbj
Copy link
Member Author

hbjORbj commented Aug 14, 2021

Thank you for your feedback. I will solidify it further as I look more closely into the components that can benefit from this hook; e.g., the five components linked in #22519.

In the meantime, an example of how this hook will be used for a component (Tabs in this instance) can be found here: 3f49153

Copy link
Member

@eps1lon eps1lon left a comment

Choose a reason for hiding this comment

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

I don't see why this should be a generic hook. Especially since this is synchronous works that re-renders whenever the rect changes even though it's probably only worth it to re-render on either width or height.

Let's not build something nobody has asked for.


export default function useResizeObserver(): [Rect | null, (elem: Element) => () => void] {
const [rect, setRect] = React.useState<Rect | null>(null);
const ref = React.useCallback((elem: Element) => {
Copy link
Member

Choose a reason for hiding this comment

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

How would this be used? We can't just pass it to a ref prop since a ref prop does not support cleanup.

Copy link
Member Author

@hbjORbj hbjORbj Aug 14, 2021

Choose a reason for hiding this comment

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

I removed useCallback and tried this way: 3f49153

Let me know!

@oliviertassinari
Copy link
Member

oliviertassinari commented Aug 14, 2021

Let's not build something nobody has asked for.

@eps1lon It does feel wrong to add it without solving a problem end-to-end. In #22519, we link 4 unrelated issues where we can directly use this hook. Maybe we could pick (solve) any one of them to prove the module works (is the best solution), keep the module unstable/undocumented until we cover them all.

@hbjORbj
Copy link
Member Author

hbjORbj commented Aug 14, 2021

Maybe we could pick (solve) any one of them to prove the module works (is the best solution), keep the module unstable/undocumented until we cover them all.

@oliviertassinari I agree. I made commits
(1) to use this resize observer hook in Tabs component (3f49153) in order to resolve the issue mentioned here: #9337, and
(2) to use this resize observer hook in TextareaAutosize component (ad591d5) in order to resolve the issue mentioned here: #23641

@hbjORbj hbjORbj force-pushed the resize-observer-hook branch from a3260a7 to 0ddba62 Compare August 16, 2021 10:28
@eps1lon
Copy link
Member

eps1lon commented Aug 16, 2021

let's fix the individual issues first and then think about the abstraction. Not the other way around. I guarantee you the current abstraction is incorrect.

@eps1lon
Copy link
Member

eps1lon commented Aug 16, 2021

And I really mean each individual issue. This is not nearly as straight forward as you think it is. We need to slow down in areas where we lack the expertise. Not just push everything through and assume we know better.

@eps1lon eps1lon closed this Aug 16, 2021
@eps1lon eps1lon reopened this Aug 16, 2021
@mnajdova
Copy link
Member

@hbjORbj should we close this one? Looks like we have dedicated PRs for Tabs and TextareaAutosize

@hbjORbj
Copy link
Member Author

hbjORbj commented Aug 25, 2021

@hbjORbj should we close this one? Looks like we have dedicated PRs for Tabs and TextareaAutosize

Yes, I will close this. @oliviertassinari also mentioned here that this hook may not be needed.

@hbjORbj hbjORbj closed this Aug 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new feature New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants