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

directive-x

v1.0.0

Published

Directives for JSX

Downloads

1

Readme

Directive-X is a JSX extension that simplifies coding in JSX. It uses Babel to transpile its 'xProps' (that manipulate JSX element structures).

Installation

Install package via npm:

npm install directive-x --save-dev

Since Directive-X is a Babel plugin, you'll need to include it in your .babelrc file:

{
  "plugins": ["es2015", "react-preset", "directive-x"]
}

You may also include it in your package.json file if you prefer that way.

Directive-X allows you to write better JSX by allowing you to avoid common curfews that beginners find to be annoying about JSX. Check this:

xRepeat

The xRepeat directive/prop allows you to iterate arrays the same way ng-repeat does. However, after it is transpiled, it achieves this through a declarative method by making use of Array.map() function. Check the following example:

const ContactList = user => { 
  return (
    <View xRepeat={contact in user.contacts}>
      <Image source={contact.avatarThumbSrc} style={styles.avatar} />
      <Link to={`/chat/${contact.id}`}>{contact.username}</Link>
    </View>
  )
}

The above code get's transpiled to:

const ContactList = user => {
  return user.contacts.map((contact, i) => {
    return <View style={styles.contact}>
      <Avatar source={contact.avatarThumbSrc} style={styles.avatar} />
      <Link to={`/chat/${contact.id}`}>{contact.username}</Link>
    </View>;
  });
};

xIf

The xIf directive displays the element it belongs to if the given condition is true. This allows for quick control logic. Eg:

<Text xIf={loggedIn}>{username}</Text>

The above code would be transpiled to:

loggedIn ? <Text>{username}</Text> : null;

xBind

The xBind directive handles the binding of this to all events on the element that it is given. eg:

class Button extends Component {
  handleClick(e) {/*...*/}

  handleHover(e) {/*...*/}
  
  render() {
    return(
      <View xBind onClick={(e) => this.handleClick(e)}>
        <Text xBind={SomeComp} onHover={this.handleHover} children="Click me" />
      </View>
    )
  }
}

The above script will be transpiled to the following:

class Button extends Component {
  xBindEvenHandlers() {
    this.handleClick = this.handleClick.bind(this);
    this.handleHover = this.handleHover.bind(SomeComp);
  }

  constructor() {
    this.xBindEvenHandlers();
  }

  handleClick(e) {/*...*/}

  handleHover(e) {/*...*/}

  render() {
    return <View xBind onClick={e => this.handleClick(e)}>
        <Text xBind={SomeComp} onHover={this.handleHover} children="Click me" />
      </View>;
  }
}

Notice that the xBind directive accepts one arg that defaults to this, it then binds that arg to all the events of the element it belongs to. If no constructor method exists in the class, the xBind directive creates one. Without Directive-X's xBind prop, the above code would've seem more tedious.

xGet

The purpose of the xGet directive is to require data/objects from external files and import them in an encapsulated scope to be used by the component it belongs to. Eg:

<Text
  xGet={ {red, dark, yellow} in './theme/colors' }
  style={{
    backgroundColor: red,
    color: dark,
    boxShadow: `1px 2px 2px ${yellow}`,
    padding: '7px 12px'
  }}
  children="Hello, World"
/>

Directive-X is an open source library, and I'd appreciate any help you're willing to give - be it fixing bugs, improving documentation, or suggesting new features or enhancements.

Directive-X is licensed under the MIT License which makes it great for both personal and commercial use.