GithubHelp home page GithubHelp logo

itsdouges / react-sticky-header Goto Github PK

View Code? Open in Web Editor NEW
54.0 3.0 3.0 7.38 MB

๐Ÿฏ A sticky header for React.js

License: MIT License

JavaScript 91.55% CSS 8.45%
react sticky header react-sticky reactjs javascript gw2armory

react-sticky-header's Introduction

NPM version NPM downloads Build Status codecov Dependency Status

Lightweight sticky header made for React that works with both colours and images. You can see an example implementation over at Guild Wars 2 Armory.

Installation

npm install react-sticky-header

Usage

// Import the base CSS, if you're using webpack just import them straight.
// Else import them into your base CSS.
import 'react-sticky-header/styles.css';
import StickyHeader from 'react-sticky-header';

const MyHeader = () => (
  <StickyHeader
    // This is the sticky part of the header.
    header={
      <div className="Header_root">
        <h1 className="Header_title">ReactStickyHeader</h1>

        <ul className="Header_links">
          <li className="Header_link">When</li>
          <li className="Header_link">Why</li>
          <li className="Header_link">About</li>
        </ul>
      </div>
    }
  >
    <section>
      <p>
        This section will be what the sticky header scrolls over before entering into
        sticky state. See the gif above or run the test story book to see examples.
      </p>
    </section>
  </StickyHeader>
);
prop type required description
header Children yes A react element that will be the sticky part of the header.
children Children no Elements that you want to appear under the sticky header.
headerOnly boolean no Use this to force the header into "sticky" mode. It will automatically hide the children and calculate the height spacer for header.
onSticky (boolean) => void no Callback fired when the header enters/leaves sticky state. See Sticky State section.
backgroundImage string no Self explanatory.
backgroundColor string no Self explanatory.
className string no Self explanatory.
stickyOffset number no The number to offset the sticky header.

Sticky State

When the component enters sticky state, it will add a class name is-sticky to the root element of the header.

React Story Book

To run the component in various states, run the following command then go to http://localhost:6006/.

npm start

Testing

npm test

react-sticky-header's People

Contributors

hubconnect avatar itsdouges avatar madou avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

react-sticky-header's Issues

Uncaught TypeError: Cannot read property 'offsetHeight' of null at index.js:48

Sticky header works but I'm getting this error on each reload:

Uncaught TypeError: Cannot read property 'offsetHeight' of null
at index.js:48

it's this part of the code:

requestAnimationFrame(function () {
        var stickyHeaderHeight = _this._fixed.offsetHeight;

Implementation of your code:

import 'react-sticky-header/styles.css';
import StickyHeader from 'react-sticky-header';

class Header extends React.Component {
    state = {
        sticked: false,
    };


    changeSticked = isSticky => {
        this.setState({sticked: isSticky});
    }

    render() {
        const logo = require("../../images/logo_v2.svg");
        const logoSmall = require("../../images/logo_v1.svg");
        return (
            <StickyHeader
                // This is the sticky part of the header.
                onSticky={(sticky) => {
                    this.changeSticked(sticky);
                }}
                header={
                    <div className="header-root">
                        <div className="mini-header__logo"><img className="main-logo" src={this.state.sticked ? logoSmall : logo} alt=""/>
                        </div>
                        <div className="mini-header__entry-switch">
                            <ul className="header_menu t-primary-14 t-weight-b">
                                <Link className="header-link" to="/about">About</Link>
                                <Link className="header-link" to="/faq">FAQs</Link>
                                <Link className="header-link" to="/contact">Contact us</Link>
                            </ul>
                            <Link className="mui-button" to="/registration">Sign Up</Link>
                        </div>
                    </div>
                }
            >
                <section>
                    <p style={{height: '200px', backgroundColor: '#ff00ff'}}>
                        This section will be what the sticky header scrolls over before entering into
                        sticky state. See the gif above or run the test story book to see examples.
                    </p>
                </section>
            </StickyHeader>
        )
    }

};

export default Header;

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.