Coder Social home page Coder Social logo

babel-plugin-jsx-adopt's Introduction

babel-plugin-jsx-adopt

This plugin transforms adopt calls to render props. Idea based on this gist. โš ๏ธ Experimental: Code you are likely to write should be transformed just fine, convoluted/edge cases might not be covered yet.

Example

Input

const Example = () => {
  const theme = adopt(<Theme />)
  const counter = adopt(<Counter />)
  const toggle = adopt(<Toggle />)

  return (
    <div style={{ color: theme === 'light' ? '#000' : '#fff' }}>
      <span>{`Count: ${counter}`}</span>
      <button onClick={toggle}>{'Toggle'}</button>
    </div>
  )
}

Output

const Example = () => {
  return (
    <Theme>
      {theme => (
        <Counter>
          {counter => (
            <Toggle>
              {toggle => (
                <div style={{ color: theme === 'light' ? '#000' : '#fff' }}>
                  <span>{`Count: ${counter}`}</span>
                  <button onClick={toggle}>{'Toggle'}</button>
                </div>
              )}
            </Toggle>
          )}
        </Counter>
      )}
    </Theme>
  )
}

Installation

npm install --save-dev babel-plugin-jsx-adopt

If you want to use it with babel@7, you should also install babel-core@^7.0.0-0 (just to prevent peer dep warnings).

Usage

Via .babelrc (Recommended)

.babelrc

{
  "plugins": ["babel-plugin-jsx-adopt"]
}

Via CLI

babel --plugins babel-plugin-jsx-adopt script.js

Via Node API

require('babel-core').transform('code', {
  plugins: ['babel-plugin-jsx-adopt'],
})

babel-plugin-jsx-adopt's People

Contributors

andarist avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

babel-plugin-jsx-adopt's Issues

Export macro?

This is a perfect use case for a babel-plugin-macros macro. Anyone interested in doing that?

Multiple arguments?

Is this possible? I couldn't make it work

<Grid >
    {(item, opened, toggle) => {

a possible solution could be

const [item, opened, toggle] = adopt(<Grid />)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.