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-transform-jsx-abem

v1.2.0

Published

JSX with ABEM format via Babel

Downloads

8

Readme

babel-plugin-transform-jsx-abem

Build Status Coverage Status Standard - JavaScript Style Guide npm downloads

Babel plugin for ABEM class names generation in JSX.

Install

Require it as any other babel plugin and add the abem package to your dependencies (so Webpack/Rollbar/etc. can use it in the frontend).

$ npm install babel-plugin-transform-jsx-abem --save-dev
$ npm install abem --save
{
  plugins: ['transform-jsx-abem']
}

Usage

Add ABEM properties in your tags and the plugin will generate the className for you.

The plugin will try to resolve the className during the compilation (className="block") and fallback to runtime if not possible (className={_abem("block")} - helper will be included automatically)

If the tag already have a className, then it'll be skipped.

Properties

| name | type | | :---- | :--------------------------------------- | | block | string | | elem | string | | mods | string | array | object | expression* | | mix | string | array | object | expression* |

* must return a string, an array or an object

Scopes

The block property creates a scope and should only be used at the top-level of the JSX tag. It will be automatically generated if it's inside a class or a function (if the class/function name is prefixed by a A, O or M, then it'll add the prefix 'a-', 'o-', or 'm-')

Examples

Input

<div block="m-main" mix="panel" mods={{ warning: true }}>
  <div elem="header" mods="header">Title</div>
  <div elem="body">Text</div>
</div>

Output

<div className="m-main -warning panel">
  <div className="m-main__header -header">Title</div>
  <div className="m-main__body">Text</div>
</div>

Input

const Message = ({ title, text }) => {
  return <div>
    <div elem="header">{title }</div>
    <div elem="body">{text}</div>
  </div>
}

Output

const Message = ({ title, text }) => {
  return <div className="message">
    <div className="message__header">{title}</div>
    <div className="message__body">{text}</div>
  </div>;
};

Input

class OMessage extends Component {
  ...
  render() {
    ...
    return <div mods={this.getMods()}>
      <div elem="header">{title}</div>
      <div elem="body">{text}</div>
    </div>
  }
}

Output

class OMessage extends Component {
  ...
  render() {
    ...
    return <div className={_abem("o-message", null, this.getMods())}>
      <div className="o-message__header">{title}</div>
      <div className="o-message__body">{text}</div>
    </div>;
  }
}

Input

function OEmailMessage ({ title, html }) {
  return <Wrapper>
    <div mods={{ disabled: true }}>
      <div elem="header">{title}</div>
      <div elem="bodyHtml">{html}</div>
    </div>
  </Wrapper>
}

Output

function OEmailMessage ({ title, html }) {
  return <Wrapper>
    <div className="o-emailMessage -disabled">
      <div className="o-emailMessage__header">{title}</div>
      <div className="o-emailMessage__bodyHtml">{html}</div>
    </div>
  </Wrapper>
}

Options

properties

Set custom naming properties. Default values:

properties: {
  block: 'block',
  element: 'elem',
  modifiers: 'mods',
  mixin: 'mix'
}

separators

Set custom abem separators. Default values:

separators: {
  element: '__',
  modifier: '-',
}

Limitations

Passing options with spread operator won't work as expected. In this example, html modifier won't be added.

const OMessage = ({ title, text, ...props }) => {
  return <div {...props}>
    <div elem="header">{title}</div>
    <div elem="body">{text}</div>
  </div>
}

[...]

const html = true
<OMessage title="Hello" text="World" mods={{ html }} />

ABEM propetrties shouldn't be passed this way anyway. Instead, you should do:

const OMessage = ({ title, text, html }) => {
  return <div mods={{ html }}>
    <div elem="header">{title}</div>
    <div elem="body">{text}</div>
  </div>
}

[...]

const html = true
<OMessage title="Hello" text="World" html={html} />

Send some love

You like this package?

Buy me a coffee