Coder Social home page Coder Social logo

Component composition? about tonic HOT 3 CLOSED

socketsupply avatar socketsupply commented on May 13, 2024
Component composition?

from tonic.

Comments (3)

heapwolf avatar heapwolf commented on May 13, 2024 1

Thanks for opening this issue, I think we could use a better composition example in the docs! The idea is, you take the innerHTML of the element (before the component is first rendered) and save it on a property. We could create this property automatically for the user, that might be nice, I'm using this pattern quite a bit.

class Parent extends Tonic {
  render () {
    this.children = this.children || this.root.innerHTML

    return `
      <div class="parent">${this.children}</div>
    `
  }
}

Tonic.add(Parent)

class Child extends Tonic {
  render () {
    return `
      <div class="child">
        ${this.props.value}
      </div>
    `
  }
}

Tonic.add(Child)

INPUT HTML

<parent>
  <child value="hello world"></child>
</parent>

OUTPUT HTML

(Like web-components, we want to preserve the host elements)

<parent>
  <div class="parent">
    <child>
      <div class="child">hello world</div>
    </child>
  </div>
</parent>

from tonic.

heapwolf avatar heapwolf commented on May 13, 2024 1

I thought about this a bit and I decided it's totally reasonable to add some sugar for this (especially since I use the pattern a lot when creating components). I added a section to the docs (https://hxoht.github.io/components/#custom). Let me know what you think, if you like it we can close this out. :)

from tonic.

benwiley4000 avatar benwiley4000 commented on May 13, 2024 1

Looks great! Thanks. :)

from tonic.

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.