Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JSX support #3

Closed
ClassicOldSong opened this issue May 4, 2017 · 1 comment
Closed

Add JSX support #3

ClassicOldSong opened this issue May 4, 2017 · 1 comment

Comments

@ClassicOldSong
Copy link
Member

ClassicOldSong commented May 4, 2017

Jsx is now widely used in multiple frontend frameworks like React and Vue.js. After a while of thinking I found that having jsx in ef.js projects is pretty possible. Here is an example:

const {t} = ef

const MyDiv = t`
>div.{{className}}
  #my-component
  #style = {{style}}
  +children
`

const Hello = t`
>h1.{{className}}
  #my-component
  #style = {{style}}
  .Hello, {{name}}!
`

const MyTextInput = t`
>input.{{className}}
  #my-component
  #type = text
  #style = {{style}}
  @input = input:{{value}}
  %value = {{value}}
`

const myHelloInstance = new Hello()

const myComponent = <MyDiv
  $data={{
    className: 'some class names',
    style: 'background-color: #CCC;'
  }}
>
  {myHelloInstance},
  <MyTextInput
    $methods={{
      input({value}) {
        myHelloInstance.$data.name = value
      }
    }}
  />
</MyDiv>

myComponent.$mount({target: document.body})

the jsx part will be transpiled into:

const myComponent = new MyDiv({
  $data: {
    className: 'some class names',
    style: 'background-color: #CCC;'
  },
  children: [
    myHelloInstance,
    new MyTextInput({
      $methods: {
        input({value}) {
          myHelloInstance.$data.name = value
        }
    })
  ]
)

or with ef.createElement:

const myComponent = ef.createElement(MyDiv, {...propsForMyDiv}, myHelloInstance, ef.createElement(MyTextInput, {...propsForMyTextInput}))

If we map needed props to the component class it self, things could be more easy like:

const myComponent = <MyDiv className='some class names' style='background-color: #CCC;'>
  {myHelloInstance}
  <MyInput input={({value}) => {
    myHelloInstance.name = value
  }/>
</MyDiv>

Using the first implementation we should write our own transpilers, while with the second implementation, we could just use the existing jsx transpilers to have the transpiling done like babel and buble. Using the second approach could be more convenient when project bootstraps.

This was referenced Jun 11, 2019
ClassicOldSong added a commit that referenced this issue Jun 12, 2019
1. Added fragment support #12, 2. Added text fragment support #13, 3. Added jsx support #3
ClassicOldSong added a commit that referenced this issue Jun 13, 2019
Resolve #3, #12, #13
Drop support for under IE11(needs WeapMap polyfill)
@ClassicOldSong
Copy link
Member Author

Fully implemented in v0.9.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant