npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

list-node-hooks

v0.1.0

Published

A React list component that allows you to manipulate dom-nodes on transitions

Downloads

2

Readme

list-node-hooks

npm install list-node-hooks --save

A React list component that allows you to manipulate dom-nodes on transitions

Overview

When will this be usefull? Well, for animation purposes for instance. It might not be a good idea to manipulate dom-nodes outside of React (anti-pattern), but sometimes you have choose simplicity over correctness and be pragmatic.

This higher order component figures out how it's children change over time, and passes the corresponding dom-nodes to the 'hooks' you've provided.

Basic usage

import listNodeHooks from 'list-node-hooks';
import React, { Component } from 'react';

const hooks = {
    listMount(nodes) {
        // do something with these nodes...
    }
};

// decorate a class
@listNodeHooks(hooks)
class List extends Component {
    render() {
        return (
            <div>
                {this.props.children}
            </div>
        );
    }
}

// or shortcut
const List = listNodeHooks(hooks)('div');

class App extends Component {

    state = {
        items: [
            { id: 1, text: 'item1' },
            { id: 2, text: 'item2' },
            { id: 3, text: 'item3' },
            { id: 4, text: 'item4' }
        ]
    };

    render() {
        return (
            <List>
                { this.state.items.map(({ id, text }) => <div key={id}>{text}</div>) }
            </List>
        );
    }
}

API

listNodeHooks(hooks: Object): (Component | Element | string): Component

Typical decorator pattern.

'hooks' is an object with certain functions that will be called by the decorated component. (See 'Hooks' for the specs). It will return a function that wraps a Component, Element or string.

Usage:

// es6
const Enhanced = listNodeHooks({})(Component);

// es7
@listNodeHooks({})

Hooks

listMount(nodes: Node[])

Will be called after the List has mounted (componentDidMount) or when it receives new children after the instance method 'flush' has been called.

listOrder({ node: Node, x: Int, y: Int, w: Int, h: Int}[])

Will be called after the order of the children has changed. 'x', 'y', 'w', 'h' represent the amount of pixels left, top, width and height has changed since the node's last position. Cool for animation!

itemsAdd(nodes: Node[])

Will be called when chilren are added to the list.

itemsDelete(nodes: Node[], cb: Function)

Will be called when chilren are deleted. The callback must be called after DOM manipulation in order for the list component to know that the elements are ready to be unmounted.

itemsChange(nodes: Node[])

Will be called when props of children changed.

Instance.flush(cb: Function, ?hook: string = 'itemsDelete')

Enables you to delete all children in the list without unmouting. By default the hook 'itemsDelete' will be called, unless you provide a custom hook for it.

Example:

import listNodeHooks from 'list-node-hooks';
import React, { Component } from 'react';

const hooks = {
    itemsFlush(nodes, cb) {
        // do something with these nodes...

        // invoke callback to continue unmouting
        cb();
    }
};

const List = listNodeHooks(hooks)('div');

class App extends Component {

    state = {
        items: [
            { id: 1, text: 'item1' },
            { id: 2, text: 'item2' },
            { id: 3, text: 'item3' },
            { id: 4, text: 'item4' }
        ]
    };

    _flush = () => this.list.flush(() => {
        // do something here, like fetching new data
    }, 'itemsFlush')

    render() {
        return (
            <div>
                <button onClick={this._flush}>Flush</button>
                <List ref={c => this.list = c}>
                    { this.state.items.map(({ id, text }) => <div key={id}>{text}</div>) }
                </List>
            </div>
        );
    }
}

Example

clone this repo, and:

npm install && npm run example