crutchcorn / the-fun-framework

🛝 An experimental homegrown JS framework.
https://crutchcorn.github.io/the-fun-framework/
MIT License
23 stars 2 forks source link

[QUESTION] Reusable Components #1

Open go4cas opened 1 year ago

go4cas commented 1 year ago

Very interesting concept! How would one create reusable named components?

crutchcorn commented 1 year ago

Nothing yet! I have some ideas on this, but it requires a bit of fiddling, truth be told.

I'm leaning towards Web Components + HTML string templates being the way to go.

Using Vite, it might look something like:

<!-- counter.component.html -->
<button data-on-click="updateCount()">Add one to {{count.value}}</button>
/* counter.component.ts */
import template from "./counter.component.html?raw";
function Counter() {
  let count = createState(
    0
  );

  function updateCount() {
    count.value++;
  }

  return {
    count,
    updateCount
  }
}

Counter.selector = "app-counter";
Counter.template = template;
registerComponent(Counter);
/* app.component.ts */
function App() {
}

// Register with the same name as `data-island-comp`
App.selector = "app-app";
App.template = `<app-counter></app-counter>`;
registerComponent(App);
render()