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

xeact

v0.0.21

Published

[![Build Status](https://travis-ci.org/pengzhanlee/xeact.svg?branch=master)](https://travis-ci.org/pengzhanlee/xeact) [![Coverage Status](https://coveralls.io/repos/github/pengzhanlee/xeact/badge.svg?branch=master&service=github)](https://coveralls.io/git

Downloads

7

Readme

xeact

Build Status Coverage Status

xeact is a JavaScript library for connecting React Components and Web Components - Custom Elements

README 中文

Installation

npm install xeact --save

Quick Start

  1. Register a component

    import xeact, {observed, exposed, dispatchEvent, Component} from "xeact";
    
    // register a Component with tag name 'box'
    @xeact('box')
    
    export default class Box extends Component {
    
        static propTypes = {
    
            // observe 'header' attribute change from dom
            @observed
            header: PropTypes.string,
        };
    
        @exposed
        method() {
            // this method can be called from dom api
        }
    
        headerClick() {
            dispatchEvent(this, 'headerClick' , {
                header: this.props.header
            });
        }
    
        render() {
            let {header} = this.props;
    
            return <div className="box">
                {header &&
                <div className="box-header" onClick={()=> {this.headerClick()}}>{header}</div>
                }
    
                {/* childNodes of the <x-box> will be append to element which has a `body` x-ref attribute. */}
                <div className="box-body" x-ref="body" />
            </div>
        }
    
    }
  2. ~~Import Custom Elements v1 polyfills~~

    <script src="/xeact/dist/env.min.js"></script>
  3. Build your components.

    rollup -c
  4. Use the registered component as a Custom Element in HTML

    <script src="path/to/your/components.build.js" />
    
    <x-box header="Hello">
        <p>World</p>
    </x-box>
  5. Done

API

xeact(name, options)

Define a custom element and connect it to React component.

  • tagName string

    Define tag name of an element.

    There are a few notices on this argument:

    • It can only contain lowercase letter a-z and -.

    • A x- prefix will be added to tag name. For example 'box' means 'x-box' will be defined as a custom element name.

  • options.isContainer boolean

    The custom element can own childNodes or not.

    An example of non-container component :

    import xeact from 'xeact';
    
    @xeact('button')
    export default class Button extends Component {
    
        ...
    
        render() {
            return <span>Button</span>
        }
    }

    An example container component:

    import xeact from 'xeact';
    
    @xeact('box')
    export default class Box extends Component {
    
        ...
    
        render() {
            return <div>
                <div className="box-header">Header</header>
                <div x-ref="body"></body>
            </div>
        }
    }

observed

Observe an dom attribute change.

import {observed} from 'xeact';

static
propTypes = {
  @observed
  header: PropTypes.string,
};

<x-box>...</x-box>
<script>
document.querySelector('x-box').setAttribute('header', 'new header');
</script>

The box component header prop will receive new value : 'new header'.

exposed

Expose react method to dom api.

import {exposed} from 'xeact';

@exposed
method(...args)
{
...
}

<x-box>...</x-box>
<script>
document.querySelector('x-box').method(arg);
</script>

dispatchEvent(context, name, eventData)

Dispatch an event from react component.

  • context object

    Always point to the component instance.

  • name string

    Event name.

  • eventData object

    An object containing data that will be passed to the event handler.

    import {dispatchEvent} from 'xeact';
    
    method(...args) {
        dispatch(this, 'catch', {
            data: 'data'
        });
    }
    <x-box>...</x-box>
    <script>
        document.querySelector('x-box').addEventListener('catch', function(e){
            e.detail.data === 'data';   // true
        });
    </script>

Component && PureComponent