npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

use-motion-resize-observer

v3.0.1

Published

A React hook that allows you to use a ResizeObserver to measure an element's size (using Motion Values).

Downloads

13

Readme

use-motion-resize-observer

A fork of use-resize-observer that uses Motion Values from Framer Motion.

What's different?

Unlike useMotionResizeObserver, the hook will not trigger a re-render on all changes to the target element's width and / or height. Instead, it will update the height and width motion values. You can use these values to drive other changes to motion components.

In Action

CodeSandbox Demo

Install

yarn add use-motion-resize-observer --dev
# or
npm install use-motion-resize-observer --save-dev

Basic Usage

Note that the default builds are not polyfilled! For instructions and alternatives, see the Transpilation / Polyfilling section.

import React from "react";
import useMotionResizeObserver from "use-motion-resize-observer";
import { motion, useTransform } from "framer-motion";

const App = () => {
  const { ref, width, height } = useMotionResizeObserver({
    initial: { width: 100, height: 100 },
  });

  const background = useTransform(width, [100, 300], ["#52cb9a", "#2d8a9a"]);

  return <motion.div ref={ref} style={{ background }} />;
};

Passing in Your Own ref

You can pass in your own ref instead of using the one provided. This can be useful if you already have a ref you want to measure.

const ref = useRef(null);
const { width, height } = useMotionResizeObserver({ ref });

You can even reuse the same hook instance to measure different elements:

CodeSandbox Demo

The "onResize" callback

You can provide an onResize callback function, which will receive the width and height of the element as numbers (not motion values) when it changes, so that you can decide what to do with it:

import React from "react";
import useMotionResizeObserver from "use-motion-resize-observer";

const App = () => {
  const { ref } = useMotionResizeObserver({
    onResize: ({ width, height }) => {
      // do something here.
    },
  });

  return <div ref={ref} />;
};

Defaults (SSR)

On initial mount the ResizeObserver will take a little time to report on the actual size.

Until the hook receives the first measurement, it returns 0 for width and height by default.

You can override this behaviour, which could be useful for SSR as well.

const { ref, width, height } = useMotionResizeObserver({
  initial: { width: 100, height: 50 },
});

Here "width" and "height" motion values will be 100 and 50 respectively, until the ResizeObserver kicks in and reports the actual size.

Without Defaults

If you only want real measurements (only values from the ResizeObserver without any default values), then you can just leave defaults off:

const { ref, width, height } = useMotionResizeObserver();

Here the "width" and "height" motion values will have 0 values until the ResizeObserver takes its first measurement.

const { ref, width, height } = useMotionResizeObserver();

Container/Element Query with CSS-in-JS

It's possible to apply styles conditionally based on the width / height of an element using a CSS-in-JS solution, which is the basic idea behind container/element queries:

CodeSandbox Demo

...but this is much easier with motion elements from Framer Motion.

const { ref, width, height } = useMotionResizeObserver();
const background = useTransform(width, [100, 300], ["#52cb9a", "#2d8a9a"]);

return <motion.div ref={ref} style={{ background }} />;

Changing State

The goal of Framer Motion is to allow for visual data to flow without triggering component re-renders by avoiding React's state/props. That said, if you do want to change state, you can use the onResize callback.

const { ref, width, height } = useMotionResizeObserver({
  onResize: (size) => setSize(size),
});

CodeSandbox Demo

Transpilation / Polyfilling

By default the library provides transpiled ES5 modules in CJS / ESM module formats.

Polyfilling is recommended to be done in the host app, and not within imported libraries, as that way consumers have control over the exact polyfills being used.

That said, there's a polyfilled CJS module that can be used for convenience (Not affecting globals):

import useMotionResizeObserver from "use-motion-resize-observer/polyfilled";

Related

License

MIT