Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom Elements #484

Open
bennypowers opened this issue Sep 6, 2023 · 0 comments
Open

Custom Elements #484

bennypowers opened this issue Sep 6, 2023 · 0 comments

Comments

@bennypowers
Copy link
Member

This looks like a perfect example of a web components use case. Instead of creating a Game class and having it "mount" on some div in the DOM, what about having something like this:

class Game extends HTMLElement {
  static template = document.createElement('template');
  static {
    this.template.innerHTML = `
      <p>game DOM here</p>
      <canvas></canvas>
    `;
  }

  constructor() {
    super();
    // create private Shadow DOM for the game
    this.attachShadow({ mode: 'open' })
      // efficiently clone game DOM
      .append(Game.template.content.cloneNode(true));
  }
  
  connectedCallback() {
    // initialize game...
  }
}

customElements.define('rose-game', Game);

Then in your page HTML

<rose-game></rose-game>

etc etc

You could also use a web components framework (I recommend Lit) to help with dynamic updates. You don't need a build step to do this, you can load Lit as es modules from a CDN.

I'm available to pair on this if you're interested in learning more

Originally posted by @bennypowers in #475 (review)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant