WebReflection / uce-template

A Vue 3 inspired Custom Elements toolless alternative.
https://medium.com/@WebReflection/a-new-web-components-wonder-f9e042785a91
ISC License
108 stars 7 forks source link

<script setup> by default? #1

Closed dy closed 3 years ago

dy commented 3 years ago

What was the reason for not making script a direct setup, as in the proposal? If that could also enable scoped-like context for the script, uce-template would be invaluable, because refs could be solved with export live bindings:

<template is="uce-template">
  <x-clock>{{ time }}</x-clock>
  <script type="module" scoped>
    export let time = new Date;
    let id
    this.onconnected = e => setInterval(() => time = new Date);
    this.ondisconnected = e => clearInterval(id)
  </script>
</template>
WebReflection commented 3 years ago

export live bindings

nope, this ain't gonna work because the module system is CommonJS, not ESM, however, when setup is encountered the module might as well be the setup itself so it's a good hint, but the example would be:

  <x-clock></x-clock>
  <template is="uce-template">
    <x-clock>{{time}}</x-clock>
    <script type="module" setup>
      let id = 0;
      export default {
        get time() {
          return (new Date).toISOString();
        }
      };
      this.connected = e => id = setInterval(this.render, 1000 / 30);
      this.disconnected = e => clearInterval(id);
    </script>
  </template>
WebReflection commented 3 years ago

It's in, v0.2 has this behavior already 👋

WebReflection commented 3 years ago

Btw ... that proposal is something I haven't read at all ... like ... I've read nothing about Vue and I've never used it, so if there are other obvious things I'm missing in here, and these won't bloat the library, I'd be happy to consider other features, thanks!

WebReflection commented 3 years ago

P.S. <script setup="props"> also works now, so that exporting props = {a: 1, b: 2} would make these reactive through the element

dy commented 3 years ago

Looks awesome, lightning fast response. The context is a great DX I believe. The concern I have though is missing vue-like reactivity - to update clock this.render is called (30 times more often than needed), instead of just setting this.time = new Date. Initially I imagined the scheme this.props.time - inspired by your el.props idea (https://ghub.io/element-props) some months ago - seems intuitive and also react-like, wdyt?

On Thu, Oct 1, 2020 at 5:36 AM Andrea Giammarchi notifications@github.com wrote:

P.S. Githubissues.

  • Githubissues is a development platform for aggregating issues.