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

babel-plugin-jsx-conditional-component

v0.1.5

Published

Neater control exists component or component type for jsx

Downloads

189

Readme

babel-plugin-jsx-conditional-component

Build Status Coverage Status npm version

The plugin is a fork of babel-plugin-jsx-base-component made by Yurii Khmelvskii. Originally it was forked to add support for Babel 7 but unfortunately my PR wasn't approved.

This is Babel 7 plugin allowing to use <Base /> component in your jsx. It has two properties:

  • exists - specifies whether the component content is shown
  • component - shows which component <Base /> actually is. It is <div /> by default

In addition <Base /> component can take properties, that will be passed to component.

This component can help make your render method more readable and concise.

Syntax and use cases

To show the purpose of <Base /> component let's consider its variants of use and the result of its transformation:

1. List ul content is any.

<Base exists={props.comments.length !== 0} component="ul">
  ...
</Base>

After transformation:

{ props.comments.length !== 0 ? <ul> ... </ul> : null }

2. You can use conditions in component property, and also pass any react-components to it.

<Base component={props.targetBalnk ? 'a' : Link} href="/dashboard">
  Dashboard
</Base>

After transformation:

const Component = props.targetBalnk ? 'a' : Link;
...
return (
  <Component href="/dashboard">
    Dashboard
  </Component>
);

3. Complex Example.

  <Base
    component={props.status === 'important' || props.blockApp ? ImportantBar : AlertBar}
    exists={props.isOpen || props.blockApp}
    className={scss.alertBar} icon="car" text={props.text}
  />

After transformation:

const Component = props.status === 'important' || props.blockApp ? ImportantBar : AlertBar;
...
return (
  {
    props.isOpen || props.blockApp
    ? <Component className={scss.alertBar} icon="car" text={props.text} />
    : null
  }
);

Note: After transformation this plugin does not create any additional spans or other wrapping tags.

Installation

As a prerequisite you need to have Babel installed and configured in your project.

Install via npm:

  npm install babel-plugin-jsx-conditional-component --save-dev

Then you only need to specify jsx-base-component as Babel plugin, which you would typically do in your .babelrc:

"plugins": [
  ...
  "jsx-base-component"
]

Linting

Since <Base /> component is transformed using Babel, you don't need to import (import) or plug it in (require). But in its turn it will result in ESLint warning that Base variable is undefined. To fix this, just add Base as global variable in your .eslintrc

"globals": {
  ...
  "Base": true
}