Coder Social home page Coder Social logo

react-window-mixins's Introduction

react-window-mixins

React mixins for working with browser window events.

Both mixins are throttled not to hang your browser.

Install

npm install react-window-mixins

OnResize

Useful for calculating sizes for more complicated layouts, handling changes for for responsive layouts (sadly not everything is possible via CSS), etc...

var OnResize = require("react-window-mixins").OnResize;

React.createClass({
  mixins: [ OnResize ],

  render: function() {
    return React.DOM.div(
      null,
      "current window size: " + this.state.window.width + ", " + this.state.window.height,
      "current document size: " + this.state.document.width + ", " + this.state.document.height
    );
  }
});

You can also write your own onResize handler:

var OnResize = require("react-window-mixins").OnResize;

React.createClass({
  mixins: [ OnResize ],

  onResize: function() {
    this.setState({
      componentWidth: this.getDOMNode().clientWidth
    });
  },

  render: function() {
    return React.DOM.div(
      null,
      "current component width: " + this.state.componentWidth
    );
  }
});

OnScroll

Useful for elements interacting with scroll position, and even writing parallax websites with React.

var OnScroll = require("react-window-mixins").OnScroll;

React.createClass({
  mixins: [ OnScroll ],

  render: function() {
    return React.DOM.div(
      null,
      "current scroll offset: " + this.state.scroll.x + ", " + this.state.scroll.y
    );
  }
});

You can also write your own onScroll handler:

var OnScroll = require("react-window-mixins").OnScroll;

React.createClass({
  mixins: [ OnScroll ],

  onScroll: function() {
    this.setState({ 
      scrollPosition: window.pageYOffset - this.getDOMNode().offsetTop 
    });
  },

  render: function() {
    return React.DOM.div(
      null,
      "current component scroll offset: " + this.state.scrollPosition
    );
  }
});

OnUnload

Useful for defining handlers for the unload and beforeunload events. Event handlers are only added to the window if they are defined. No default handlers are added automatically.

var OnUnload = require("react-window-mixins").OnUnload;

React.createClass({
  mixins: [ OnUnload ],

  onUnload: function() {
    // Clean up any resources
  },

  onBeforeUnload: function() {
    return 'Are you sure you want to leave the page?';
  }
});

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.