Giter VIP home page Giter VIP logo

react-resize-aware's Introduction

react-resize-aware

It does one thing, it does it well: listens to resize events on any HTML element.

react-resize-aware is a zero dependency, ~400 bytes React Hook you can use to detect resize events without relying on intervals, loops, DOM manipulation detection or CSS redraws.

It takes advantage of the resize event on the HTMLObjectElement, works on any browser I know of, and it's super lightweight.

In addition, it doesn't directly alters the DOM, everything is handled by React.

Looking for the 2.0 docs? Click here

Installation

or with npm:

npm install --save [email protected]

Usage

The API is simple yet powerful, the useResizeAware Hook returns a React node you will place inside the measured element, and an object containing its sizes:

import React from 'react';
import useResizeAware from 'react-resize-aware';

const App = () => {
  const [resizeListener, sizes] = useResizeAware();

  return (
    <div style={{ position: 'relative' }}>
      {resizeListener}
      Your content here. (div sizes are {sizes.width} x {sizes.height})
    </div>
  );
};

Heads up!: Make sure to assign a position != initial to the HTMLElement you want to target (relative, absolute, or fixed will work).

API

The Hook returns an array with two elements inside:

[resizeListener, ...] (first element)

This is an invisible React node that must be placed as direct-child of the HTMLElement you want to listen the resize events of.

The node is not going to interfer with your layouts, I promise.

[..., sizes] (second element)

This object contains the width and height properties, these properties are going to be null before the component rendered, and will return a number after the component rendered.

Custom reporter

You can customize the properties of the sizes object by passing a custom reporter function as first argument of useResizeAware.

const customReporter = target => ({
  clientWidth: target != null ? target.clientWidth : null,
});

const [resizeListener, sizes] = useResizeAware(customReporter);

return (
  <div style={{ position: 'relative' }}>
    {resizeListener}
    Your content here. (div clientWidth is {sizes.clientWidth})
  </div>
);

The above example will report the clientWidth rather than the default offsetWidth and offsetHeight.

React to size variations

For completeness, below you can find an example to show how to make your code react to size variations using React Hooks:

const App = () => {
  const [resizeListener, sizes] = useResizeAware();
  
  React.useEffect(() => {
    console.log('Do something with the new size values');
  }, [sizes.width, sizes.height]);

  return (
    <div style={{ position: 'relative' }}>
      {resizeListener}
      Your content here.
    </div>
  );
}

react-resize-aware's People

Contributors

fezvrasta avatar spadarshut avatar bengummer avatar cullophid avatar aurerua avatar brianium avatar simianhacker avatar joefatora avatar bebraw avatar third774 avatar andarist avatar zhangyijiang avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.