Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sweet-scroll as react component #49

Open
Futos opened this issue Aug 1, 2018 · 2 comments
Open

sweet-scroll as react component #49

Futos opened this issue Aug 1, 2018 · 2 comments

Comments

@Futos
Copy link

Futos commented Aug 1, 2018

Hello @tsuyoshiwada,

thanks for your awesome project. It makes a lot of my work much easier. 💪

I just wonder me if there is a react component or wrapper for sweet-scroll. Because I would like to use it in an react project. would be aweome if there is a way to get it running ;)

Cheers!
Tobias

@bubblybunii
Copy link

I m using react & where I use document.addEventListener, it says document is not defined, anyone knows how to define document in typescript?

 document.addEventListener('DOMContentLoaded', () => {
      const sweetScroll = new SweetScroll({ /* some options */ });
    }, false);

@wadackel
Copy link
Owner

wadackel commented Mar 7, 2019

Hi @Futos, Thank you for issues. And I'm sorry I got very late... 😢

When I use it in React's world, I do not publish a rapper because I do not feel the necessity.
In many cases, I think that we can respond by responding in the following case 🐶

Target the entire page

document.addEventListener('DOMContentLoaded', () => {
  SweetScroll.create();
}, false);

This has nothing to do with React's world 😃

Target the container element

When creating a scrollable area elements, it can be realized with the following code.

import * as React from 'react';
import SweetScroll from 'sweet-scroll';

// Minimum example
class ScrollableContainer extends React.Component {
  container = React.createRef();
  scroller = null;

  componentDidMount() {
    this.scroller = SweetScroll.create(
      {
        trigger: "a[href^='#']"
      },
      this.container.current
    );
  }

  componentWillUnmount() {
    this.scroller.destroy();
  }

  render() {
    const { children } = this.props;

    return (
      <div
        ref={this.container}
        style={{
          overflowX: "auto",
          overflowY: "auto",
          width: 300,
          height: 300
        }}
      >
        {children}
      </div>
    );
  }
}

// Usecase
class App extends React.Component {
  render() {
    return (
      <div>
        <ScrollableContainer>
          <a href="#section">Go to Section</a>
          {/* Long content ... */}
          <div id="section">...</div>
        </ScrollableContainer>
      </div>
    );
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants