←back to thread

367 points lemonberry | 1 comments | | HN request time: 0s | source
Show context
c-smile ◴[] No.24642754[source]
> Can we fix this?

Sure, by returning to basics.

Let's assume that Web Component is a DOM element with a code associated with it + lifecycle events.

Here is how Components are done in Sciter ( https://sciter.com )

1. CSS has got `prototype` property:

   div.my-component { prototype: MyComponent url(script/components.js); } 
where MyComponent is the name of class in JS, url (optional) is an URL where this MyComponent can be found.

2. In script that custom component is usually represented by ES6 class (+ decorators)

    class MyComponent extends Element {
       // lifecycle event/method
       componentDidMount() {  }
       // lifecycle event/method 
       componentWillUnmount() {  }

       // component specific methods
       foo() { }
       bar() { }

       // event handlers - decorated functions
       @event("mousedown")
         onMouseDown(evt) { }

       @event("click", "button#submit")
         onSubmit(evt, button) { }
      
       @event("change", "input.name")
         onNameChange(evt, input) { }
    }
and that's it. Sciter lives with this schema more than 10 years - proven to be very convenient and flexible.

Yet simple: one property `prototype` + simple wiring of lifecycle events.

replies(2): >>24643504 #>>24645818 #
agumonkey ◴[] No.24643504[source]
after doing a bit of vue I wanted this :)
replies(1): >>24644243 #
1. c-smile ◴[] No.24644243[source]
The good thing about this mechanism is that you just need CSS in order to declare your components.

The script will run only if you have elements with matching selectors. So you may have component libraries. Browser will instantiate only used components.