captivationsoftware / react-sticky

<Sticky /> component for awesome React apps
MIT License
2.64k stars 385 forks source link

add a className #212

Closed isaacalves closed 6 years ago

isaacalves commented 7 years ago

is it possible to add a class to the Sticky element when the element's stickiness is activated?

martinbroos commented 7 years ago

you can do this by checking the isSticky variable. The way i'm doing this is with https://github.com/JedWatson/classnames

<Sticky {...stickyProps}>
  {({ style, isSticky }) => {
    var className = classNameFactory("article-header", {
      "article-header--sticky": isSticky
    });

    return (
      <div>
        <section className={className} style={style}>
          {this.props.children}
        </section>
      </div>
    );
  }}
</Sticky>;
alxvallejo commented 6 years ago

What is { ...stickyProps } ? I don't see that in the documentation...

vcarl commented 6 years ago

That appears to be code from @martinbroos' code, not the library. It's not related to how to apply a className to an element when it's sticky.

vcarl commented 6 years ago

I formatted his snippet with Prettier, here it is without some of the intermediate values and unnecessary props.

<Sticky>
  {({ style, isSticky }) => (
    <div>
      <section
        className={classNames("article-header", {
          "article-header--sticky": isSticky
        })}
        style={style}
      >
        {this.props.children}
      </section>
    </div>
  )}
</Sticky>;