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

resize hook #49

Open
Janpot opened this issue Nov 1, 2018 · 2 comments
Open

resize hook #49

Janpot opened this issue Nov 1, 2018 · 2 comments

Comments

@Janpot
Copy link

Janpot commented Nov 1, 2018

I've been using this hook

 function useContentRect (ref) {
  const [contentRect, setContentRect] = useState(ref.current ? ref.current.getBoundingClientRect() : {});
  useEffect(() => {
    if (!ref.current) return;
    const observer = new window.ResizeObserver(entries => setContentRect(entries[0].contentRect));
    observer.observe(ref.current);
    return () => observer.disconnect(ref.current);
  }, [ref]);
  return contentRect;
}

To measure elements and react on size changes. Can be used as

function Component () {
  const ref = useRef(null);
  const { width, height } = useContentRect(ref);
  return <div ref={ref}>{width} {height}</div>;
}

Looks like something like this could be a nice addition to this library. If not, feel free to just close and ignore. 🙂

@sagar-gavhane
Copy link

sagar-gavhane commented Nov 7, 2018

@Janpot, I just playing 🤾‍♂️ around with useContentRect(ref). It's fantastic 👍 and first I'm a little bit confusing 😕 from its name. I think you should rename it something else so it can easily understand the meaning behind this logic. We have put a step ahead to include this hook inside the platform library.

I have created gist of this hook for later use. --> https://gist.github.com/sagar-gavhane/6b8b52978206242b4b7b7e16aeee4a8d

What's your opinion?

@Janpot
Copy link
Author

Janpot commented Nov 7, 2018

I picked the name because it was consistent with the naming of properties in the ResizeObserver. I prefer to use the terms of the underlying platform if they make sense. Other than that, I don't really care how it's named.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants