Socket
Book a DemoInstallSign in
Socket

on-el-resize

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

on-el-resize

On HTML element resize event helper

0.0.4
latest
Source
npmnpm
Version published
Weekly downloads
8
100%
Maintainers
1
Weekly downloads
 
Created
Source

on-el-resize - on HTML element resize event helper

NPM

npm version Downloads

on-el-resize is a simple helper which provides an API for attaching resize event firing to HTML elements.

This can simplify creation of some dynamic and responsive components which styles and structure depend on absolute width and height values.

Install

npm install on-el-resize # or yarn add on-el-resize

Usage

TypeScript / JavaScript

import { Resize } from 'on-el-resize';

const resize = new Resize();
const element = document.querySelector('.selector');

const resizeHandler = () => {
  console.log({
    width: element.offsetWidth,
    heigth: element.offsetHeight
  });
};

// Attach event
resize.addResizeListener(element, resizeHandler);

// Detach event
resize.removeResizeListener(element, resizeHandler);

React component sample

import * as React from 'react';
import { Resize } from 'on-el-resize';

export interface IProps {}

export interface IState {
  componentWidth?: number;
  componentHeight?: number;
}

export class Example extends React.Component<IProps, IState> {

  private parentRef: HTMLElement;
  private resize: Resize;

  constructor(props: IProps) {
    super(props);
    this.resize = new Resize();
    this.state = {};
    this.onComponentResize = this.onComponentResize.bind(this);
  }

  public render() {
    return (
      <div ref={el => this.parentRef = el}>
        <div>Width: {this.state.componentWidth}</div>
        <div>Height: {this.state.componentHeight}</div>
      </div>
    );
  }

  public componentDidMount() {
    this.resize.addResizeListener(this.parentRef, this.onComponentResize);
    this.onComponentResize();
  }

  public componentWillUnmount() {
    this.resize.removeResizeListener(this.parentRef, this.onComponentResize);
  }

  private onComponentResize() {
    this.setState({
      componentWidth: this.parentRef.offsetWidth,
      componentHeight: this.parentRef.offsetHeight
    });
  }

}

React declarative approach

Component is in draft status

import * as React from 'react';

import { Resizable } from 'on-el-resize/lib/components';

export default class Example extends React.Component<{}, {}> {

  public render() {
    return (
      <Resizable render={({ width }) => <div>Width: {width}</div>} />
    );
  }

}

Keywords

html

FAQs

Package last updated on 26 Aug 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.