Giter VIP home page Giter VIP logo

react-sticky's Introduction

react-sticky Build Status

Make your React components sticky!

Perfect for headers, menus, or any other element where you want to maximize usability as your users scroll. Tons of options to make this component fit your project's needs, or simply rely on the ready-to-go defaults.

Highlights:
  • Not bound to any version of React so it will scale along with the community! Note: this changed with version 3.0.0
  • Only 12K minified, with 0 external dependencies (besides React, of course)!
  • Allows multiple Sticky elements on the page at once!

Installation

npm install react-sticky

Tip: run npm install to build the compressed UMD version suitable for inclusion via CommonJS, AMD, and even good old fashioned <script> tags.

Code Example

First, you will want to wrap the element you want to be sticky with tags. When the element is scrolled past the point where it would start to move off screen, the stickiness is activated.

app.jsx

var React = require('react'),
  Sticky = require('react-sticky');

var Header = React.createClass({
  render: function() {
    return (
      <Sticky>
        <header>
          <nav />
        </header>
      </Sticky>
    );
  },
});

When the "stickiness" becomes activated, the following css style rules are applied to the Sticky element:

  position: fixed;
  top: 0;
  left: 0;
  right: 0;

Props

stickyStyle

In the event that you wish to override the style rules applied, simply pass in the style object as a prop:

app.jsx

<Sticky stickyStyle={customStyleObject}>
  <header />
</Sticky>

Note: For more information on the style object see http://facebook.github.io/react/tips/inline-styles.html

stickyClass

You can also specify a class name ('sticky' by default) to be applied when the element becomes sticky:

app.jsx

<Sticky stickyClass={customClassName}>
  <header />
</Sticky>

If you prefer to use external CSS rules instead of inline styles, you will need to pass an empty object to the stickyStyle property. Doing so will prevent the default inline styles from taking precedence over your own CSS rules. An example of how to do this is found below:

app.jsx

<Sticky stickyClass="supersticky" stickyStyle={{}}>
  <header />
</Sticky>

topOffset

Sticky state will be triggered when the top of the element is topOffset pixels from the top of the window (0 by default). Positive numbers give the impression of a lazy sticky state, whereas negative numbers are more eager in their attachment.

app.jsx

<Sticky topOffset={80}>
  <SomeChild />
</Sticky>

The above would result in an element that becomes sticky once its top is greater than or equal to 80px away from the top of the screen.

className

You can specify a class name that would be applied to the resulting element:

app.jsx

<Sticky className={className}>
  <header />
</Sticky>

style

You can specify a style object that would be applied to the resulting element:

app.jsx

<Sticky style={{background: 'red'}}>
  <header />
</Sticky>

Note: In the event that stickyStyle rules conflict with style rules, stickyStyle rules take precedence ONLY while sticky state is active.

type

You can specify the type of element (React.DOM.div by default) that will be rendered:

app.jsx

<Sticky type={React.DOM.header}>
  <SomeChild />
</Sticky>

The above would result in all sticky state attributes (class and style) being applied directly to the element you specify, rather than being wrapped in a React.DOM.div.

onStickyStateChange

Use the onStickyStateChange prop to fire a callback function when the sticky state changes:

app.jsx

<Sticky onStickyStateChange={this.handleStickyStateChange}>
  <header />
</Sticky

Contributors

Aaron Goin

Alejandro Tardin

Ankit Sardesai (@amsardesai)

Josh Carr (@joshcarr)

By all means, if you see room for improvement, let us know!

License

MIT License

react-sticky's People

Contributors

4lejandrito avatar aarongoin avatar amsardesai avatar daviferreira avatar dbarbalato avatar glifchits avatar joshcarr avatar rikukissa avatar

Watchers

 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.