Coder Social home page Coder Social logo

Comments (5)

yyx990803 avatar yyx990803 commented on July 19, 2024 14

Discussion notes from Discord:

Previous decision to use v-slots due to difficulty in determining the case where a dynamic expression is embedded:

const s = {
  foo: () => <div>foo</div>,
  bar: () => <div>bar</div>
}
<Comp>{s}</Comp>

Current output is

createVNode(resolveComponent("Comp"), null, {
  default: () => [s],
  _: 1
})

The solution is to add a runtime check:

const isSlot = s => isFunction(s) || (isObject(s) && !isVNode(s))

createVNode(resolveComponent("Comp"), null, isSlot(s) ? s : {
  default: () => [s],
  _: 1
})

The runtime check should only be needed in the case where:

  • The JSX component has a single children expression
  • The children expression is not a literal value (identifier, function call, other non-literal expressions)

Expected Cases

May not be complete.

  • JSX children: compile to default slot

    <Comp><div>hi</div></Comp>
    
    createVNode(resolveComponent("Comp"), null, {
      default: () => [createVNode("div", null, [createTextVNode("hi")])],
      _: 1
    })
  • Multiple expressions: compile to default slot

    <Comp>{foo} {bar}</Comp>
    
    createVNode(resolveComponent("Comp"), null, {
      default: () => [foo, createTextVNode(" "), bar],
      _: 1
    })
  • Single expression, non-literal value: runtime check

    Note: may need to cache the expression in a variable to avoid multiple evaluations

    <Comp>{foo()}</Comp>
    
    let _slot_1
    createVNode(resolveComponent("Comp"), null, isSlot(_slot_1 = foo()) ? _slot_1 : {
      default: () => [_slot_1],
      _: 1
    })
  • Single expression, object literal: pass as object slots

    <Comp>{{
      default: () => <div>default</div>,
      foo: () => <div>foo</div>
    }}</Comp>
    
    createVNode(resolveComponent("Comp"), null, {
      default: () => createVNode("div", null, [createTextVNode("default")]),
      foo: () => createVNode("div", null, [createTextVNode("foo")]),
      _: 1
    })
  • Single expression, function expression: pass as default slot (no wrapping, no flag)

    <Comp>{() => "foo"}</Comp>
    
    createVNode(resolveComponent("Comp"), null, {
      default: () => "foo"
      // probably should have no flag here
    })
  • Single expression, other literals: pass as default slot

    <Comp>{123}</Comp>
    
    createVNode(resolveComponent("Comp"), null, {
      default: () => [normalizeVNode(123)],
      _: 1
    })

from babel-plugin-jsx.

Amour1688 avatar Amour1688 commented on July 19, 2024 3

Is there any news about this new feature? ๐Ÿ‘€๐Ÿ‘€๐Ÿ‘€๐Ÿ‘€

WIP

from babel-plugin-jsx.

nickmessing avatar nickmessing commented on July 19, 2024 2

Would be nice to add children typing support if this is implemented.

from babel-plugin-jsx.

Zcating avatar Zcating commented on July 19, 2024

Finally, You found it.

The usage of v-slots is quite react-like, may be provide more radical syntax sugar.

<Compo>
  <VSlot name="default">
      <div>default</div>
  </VSlot>
  <VSlot name="foo">
      <div>bar</div>
  </VSlot>
</Compo>

from babel-plugin-jsx.

0x30 avatar 0x30 commented on July 19, 2024

Is there any news about this new feature? ๐Ÿ‘€๐Ÿ‘€๐Ÿ‘€๐Ÿ‘€

from babel-plugin-jsx.

Related Issues (20)

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.