Skip to content

Conversation

@tbranyen
Copy link
Owner

@tbranyen tbranyen commented Sep 21, 2016

This is much more usable as a wrapped React component, consumption looks like:

import React, { Component, PropTypes } from 'react';
import HyperList from 'react-hyperlist';

export default class MyComponent extends Component {
  render() {
    return (
      <HyperList
        itemHeight={120}
        total={10000}
        generate={this.generate}
      />
    );
  }

  generate() {
    return (
      <div>Testing this out!</div>
    );
  }
}

With the experimental implementation being:

import React, { Component, PropTypes } from 'react';
import HyperListCore from 'hyperlist';

export default class HyperList extends Component {
  render() {
    const { element, scroller, fragment } = this.state;

    // Ensure the scroller is relative in our app.
    scroller.style = Object.assign({}, scroller.style, {
      position: 'relative'
    });

    return (
      <div {...element}>
        <div {...scroller} />
        {fragment}
      </div>
    );
  }

  constructor(props) {
    super(props);

    this.state = { element: {}, scroller: {}, fragment: [], };
  }

  componentDidMount() {
    const {
      height = window.innerHeight,
      itemHeight = 50,
      total = 0,
      reverse = false,
      generate = () => {},
    } = this.props;

    const config = {
      height,
      itemHeight,
      total,
      generate,

      isReact: true,
      useFragment: false,
      overrideScrollPosition: () => document.body.scrollTop,
      cloneElement: React.cloneElement,

      applyPatch: (element, fragment, scroller) => {
        this.setState({ element, fragment, scroller });
      },
    };

    this.list = HyperListCore.create(this, config);

    // Bind to the resize event, and since you should only ever have one
    // handler bound to this, we pave over whatever you had set before.
    window.onresize = e => {
      config.height = window.innerHeight;
      this.list.refresh(this, config);
    };
  }

  componentWillUnmount() {
    window.onresize = null;
    this.list.destroy();
  }
}

@tbranyen tbranyen force-pushed the make-react-first-or-second-class branch from f087f4e to af00c3c Compare June 28, 2017 17:47
@tbranyen
Copy link
Owner Author

Some thoughts on this PR:

  • I don't really like all the isReact-specific conditionals necessary.
  • Probably shouldn't bundle the wrapper w/ this lib
  • Is there a way to make this tool more generic (for say Node) using POJOs instead of DOM Elements?

@tbranyen tbranyen force-pushed the make-react-first-or-second-class branch from 6680c39 to 12e9421 Compare June 28, 2017 17:53

// Decorate the container element with styles that will match
// If using React, the element object turns into a props object.
if (config.isReact) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I'm not fond of those conditions. I'm sure that there is a way to manage vanillajs and react with the same code no?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, I agree. For now this allows it to work so I can move forward, but I don't think we should merge this as-is.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll give this a shot this weekend and let you know my findings.

I don't really like all the isReact-specific conditionals necessary.

I didn't read this, but yeah adding framework-specific code isn't that clean :p

return config.applyPatch(element, fragment, scroller)
}

element.innerHTML = ''
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand this correctly, React is not DOM-friendly? I mean that for react you need some kind of virtual dom based on objects? Isn't there a way to transform real DOM elements to react-like dom outside this script?

Copy link
Owner Author

@tbranyen tbranyen Jun 29, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are, like React Faux DOM, but this performs significantly better and allows for a cleaner wrapper to built in React. We might want to approach this as just being VDOM-friendly and not focusing on React-specifically. Like diffHTML could benefit from these changes as well, but allow its VTree representation of a DOM Node to pass through HyperList and get the right positional values associated with it.

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

Successfully merging this pull request may close these issues.

2 participants