All Projects → peakchen90 → babel-plugin-react-directives

peakchen90 / babel-plugin-react-directives

Licence: MIT license
A babel plugin that provides some directives for react(JSX), similar to directives of vue.

Programming Languages

javascript
184084 projects - #8 most used programming language
CSS
56736 projects
HTML
75241 projects

Projects that are alternatives of or similar to babel-plugin-react-directives

vue-visible
v-visible directive for VueJS
Stars: ✭ 31 (-61.25%)
Mutual labels:  directive, v-show
angular-downloader
Angular Downloader is an angularjs directive that enables you to manage browser download - https://720kb.github.io/angular-downloader
Stars: ✭ 16 (-80%)
Mutual labels:  directive
angular-ellipsis
A simple lightweight library for Angular which removes excess text and add ellipsis symbol to end of text before text overflows container
Stars: ✭ 16 (-80%)
Mutual labels:  directive
penv.macro
A macro used with babel-plugin-macros to write configurations for multiple environments, and remove configurations are irrelevant with the specified environment from your codes finally.
Stars: ✭ 73 (-8.75%)
Mutual labels:  babel-plugin
babel-plugin-hyperscript-to-jsx
This plugin transforms react-hyperscript into JSX. Intended to be used as codemod.
Stars: ✭ 20 (-75%)
Mutual labels:  babel-plugin
babel-plugin-transform-for-of-as-array
Transform all for-of loops into the equivalent array for loop
Stars: ✭ 14 (-82.5%)
Mutual labels:  babel-plugin
graphql-directive-rest
GraphQL directive for easy integration with REST API
Stars: ✭ 39 (-51.25%)
Mutual labels:  directive
babel-plugin-storybook-csf-title
A Babel plugin to generate titles for Storybook CSF stories at compile time, typically based on the story file's file name.
Stars: ✭ 17 (-78.75%)
Mutual labels:  babel-plugin
angular-paypal-checkout
Angular directive for running PayPal's in-context checkout flow
Stars: ✭ 14 (-82.5%)
Mutual labels:  directive
angular-simple-slider
An AngularJS directive providing a simple slider functionality
Stars: ✭ 15 (-81.25%)
Mutual labels:  directive
ngx-localstorage
An Angular wrapper for localstorage/sessionstorage access.
Stars: ✭ 27 (-66.25%)
Mutual labels:  directive
nornj
More exciting JS/JSX based on Template Engine, support control flow tags, custom directives, two-way binding, filters and custom operators.
Stars: ✭ 97 (+21.25%)
Mutual labels:  babel-plugin
babel-plugin-feature-flags
A babel transform for managing feature flags
Stars: ✭ 57 (-28.75%)
Mutual labels:  babel-plugin
babel-plugin-rewire-exports
Babel plugin for stubbing [ES6, ES2015] module exports
Stars: ✭ 62 (-22.5%)
Mutual labels:  babel-plugin
babel-plugin-solid-undestructure
A Babel plugin for SolidJS that allows you to destructure component props without losing reactivity.
Stars: ✭ 45 (-43.75%)
Mutual labels:  babel-plugin
angular-cron-gen
A basic way to for users to graphically build a cron expression using Angular.
Stars: ✭ 36 (-55%)
Mutual labels:  directive
ngx-malihu-scrollbar
Angular 2+ scrollbar customization using Malihu jQuery Custom Scrollbar plugin
Stars: ✭ 59 (-26.25%)
Mutual labels:  directive
idomizer
An HTML template parser compiling an incremental-dom render factory.
Stars: ✭ 15 (-81.25%)
Mutual labels:  babel-plugin
babel-plugin-console-source
Add the file name and line numbers to all console logs.
Stars: ✭ 38 (-52.5%)
Mutual labels:  babel-plugin
babel-plugin-transform-rename-properties
A Babel plugin for renaming JavaScript properties
Stars: ✭ 19 (-76.25%)
Mutual labels:  babel-plugin

babel-plugin-react-directives

A babel plugin that provides some directives for react(any JSX), similar to directives of vue. And you can now try it online at playground.

Travis (.org) branch Codecov node npm GitHub

中文文档

Table of Contents

Usage

Requires node v10.0.0 or higher, babel v7.0.0 or higher.

Installation

use npm:

npm install --save-dev babel-plugin-react-directives

use yarn:

yarn add --dev babel-plugin-react-directives

Configuring via .babelrc

{
  "plugins": [
    "react-directives"
  ]
}

Plugin options

{
  "plugins": [
    [
      "react-directives",
      {
        "prefix": "x"
      }
    ]
  ]
}
  • prefix: JSX props prefix for directives. Default: "x", example usage: x-if

Directives

x-if

If the x-if value is truthy, this element will be rendered, otherwise do not.

Example:

const foo = <div x-if={true}>text</div>

Convert to:

const foo = true ? <div>text</div> : null

x-else-if and x-else

The x-else-if must have a corresponding x-if. if x-if value is falsy, and x-else-if value is truthy, it will be rendered.

The x-else must have the corresponding x-if or x-if-else. When all corresponding x-if or x-else-if value are falsy, it will be rendered.

Example:

const foo = (
  <div>
    <p x-if={data === 'a'}>A</p>
    <p x-else-if={data === 'b'}>B</p>
    <p x-else-if={data === 'c'}>C</p>
    <p x-else>D</p>
  </div>
)

Convert to:

const foo = (
  <div>
    {data === 'a'
      ? <p>A</p>
      : data === 'b'
        ? <p>B</p>
        : data === 'c'
          ? <p>C</p>
          : <p>D</p>
    }
  </div>
)

x-show

The x-show controls the display or hiding of elements through the display of the style prop. If the x-show value is falsy, will set style.display = "none", otherwise do nothing.

Example:

const foo = <div x-show={true}>text</div>

Convert to:

const foo = (
  <div style={{
    display: true ? undefined : "none"
  }}>text
  </div>
)

Of course, it will also merge other style props by calling the mergeProps method, for example:

const foo = (
  <div
    style={{ color: 'red' }}
    x-show={true}
    {...extraProps}>
    text
  </div>
)

will be converted to:

const foo = (
  <div
    {...extraProps}
    style={{
      ...mergeProps.call(this, "style", [
        { style: { color: 'red' } },
        extraProps
      ]),
      display: true ? undefined : "none"
    }}>text
  </div>
)

x-for

The x-for is used to traverse arrays to generate elements.

The value should like: (item, index) in list

  • list: array for traversal
  • item: current value
  • index: current index (optional)

Note: If you use ESLint, you may receive an error that item and index are undeclared variables. Please install eslint-plugin-react-directives plugin to solve it.

Example:

const foo = (
  <ul>
    <li
      x-for={item in list}
      key={item.id}>{item.name}
    </li>
  </ul>
)

Convert to:

const foo = (
  <ul>
    {list.map(item => (
      <li key={item.id}>{item.name}</li>
    ))}
  </ul>
)

Also note that if used with x-if, the x-for has a higher priority, for example:

const foo = (
  <ul>
    <li
      x-for={item in list}
      x-if={item.name === 'alice'}
      key={item.id}>{item.name}
    </li>
  </ul>
)

will be converted to:

const foo = (
  <ul>
    {list.map(item => (
      item.name === 'alice'
        ? <li key={item.id}>{item.name}</li>
        : null
    ))}
  </ul>
)

x-class

The x-class for conditionally joining classNames together by classnames, and it is useful for dynamically generating className. Usage is the same as classnames, the binding value will be passed as a parameter to the classNames method.

Example:

const foo = <div x-class={{ abc: true, def: false }}>

Convert to:

const foo = <div className={classNames({ abc: true, def: false })}>
// className="abc"

Note: classNames method references runtime/classnames.js.

Of course, it will also merge other className props, for example:

const foo = <div x-class={{ abc: true, def: false }} className="xyz">

will be converted to:

const foo = <div className={classNames(["xyz", { abc: true, def: false }])}>
// className="xyz abc"

The x-class can also be used with css-modules, the usage is as follows:

import styles from './style.css';

const foo = (
  <div
    className={styles.foo}
    x-class={{
      [styles.bar]: true,
      [styles.qux]: false
    }}
  />
)

Related Packages

Known Issues

  • When using x-for in Typescript, the binding value item will report an error. The temporary solution is to declare the item variable before use. Such as declare let item: any. And it is not recommended to use x-for in Typescript.

CHANGELOG

See more information at: CHANGELOG

LICENSE

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].