• Stars
    star
    490
  • Rank 89,811 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 8 years ago
  • Updated about 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

β‡²πŸ‘ A simple React Hook which allows to listen the resize event of any target element when it changes sizes

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, ~600 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

yarn add react-resize-aware

or with npm:

npm install --save react-resize-aware

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>
  );
}

More Repositories

1

snackbarjs

Create Material Design snackbars and toasts with ease.
JavaScript
641
star
2

dropdown.js

A dropdown plugin that does what you expect. (NEEDS MAINTAINER)
JavaScript
189
star
3

react-element-queries

🐣🐷 Conditionally render pieces of UI based on element queries.
JavaScript
72
star
4

skrollr-colors

skrollr-colors is a plugin for skrollr which allow you to use mixed colors formats in skrollr's keyframes.
JavaScript
52
star
5

node-red-matter

Create Virtual Devices with Ease for the Matter Protocol!
TypeScript
43
star
6

rpi4-docker-compose-home-assistant

Dockerfile
29
star
7

bootstrap-material-design

Redirect repository
HTML
23
star
8

gist-manager

UNMAINTAINED - View and create gists within Brackets editor
JavaScript
18
star
9

spectacle-image-slide

Show a slide with a big image and a title on top
JavaScript
16
star
10

ama

Ask Me Anything
14
star
11

netflix

Not associated with Netflix
JavaScript
14
star
12

brackets-upper-titlebar

Moves the titlebar of Brackets for linux on top of the window.
CSS
7
star
13

material-design-iconfont

Material Design iconic font
CSS
7
star
14

google-assistant-mac

🚨WORK IN PROGRESS 🚨 - Bring Google Assistant on your Mac, with a Siri-like interface.
JavaScript
6
star
15

bootstrap-material-datepicker

Datepicker for bootstrap-material
HTML
5
star
16

parallax-slideshow

Simple slideshow with Parallax effect
JavaScript
4
star
17

tado-clone

Tado app clone built with React Native and Expo - works with Home Assistant
JavaScript
4
star
18

ghat

A chat with GitHub integration.
CSS
3
star
19

digital-wall

React app to show informations on TV screen
JavaScript
3
star
20

raycast-toolkit

Utilities and Components to extend the Raycast Extensions API functionalities
TypeScript
3
star
21

FezVrasta.github.io

My portfolio
HTML
2
star
22

brackets-darkbar

Stupid extension to uniform the color of the main toolbar with the rest of Brackets.
JavaScript
2
star
23

react-axe-es

react-axe, with improved support for ES modules imports
JavaScript
2
star
24

nodebb-widget-steamserverstatus

NodeBB widget to provide informations about a server which supports Steam API.
JavaScript
2
star
25

nuget-publish

Publish npm packages to NuGet using package.json
JavaScript
1
star
26

webpack-context-relative-resolver

JavaScript
1
star
27

which-pm-runs-cli

Detects what package manager executes the process from CLI
JavaScript
1
star
28

eslint-plugin-no-iife

ESLint plugin to disallow IIFE
JavaScript
1
star
29

graphql-relay-cross-tab-sync

TypeScript
1
star