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

Register rendering effects and handle events for shadow root as well #2

Open
jaredcwhite opened this issue Dec 4, 2024 · 1 comment

Comments

@jaredcwhite
Copy link

I appreciate Masto's focus on server-rendered HTML, and now that declarative shadow DOM (DSD) is widely available, it's possible that HTML might include DSD templates as well. It would be cool if Reactive Masto could also set up event listeners / effects rendering for the element's shadowRoot (if present), then the same syntax could be used for both light DOM content and shadow DOM content.

mb21 added a commit that referenced this issue Dec 4, 2024
@mb21
Copy link
Owner

mb21 commented Dec 4, 2024

I think I got something working: 34834ad

With that commit, you can now attach a shadowdom either in your connectedCallback:

connectedCallback() {
  this.attachShadow({ mode: "open" })
  super.connectedCallback()
}

or in your constructor, and in both cases the event listeners and bindings still work. Full example with constructor:

import { html, ReactiveElement, signal } from 'mastro/reactive'

customElements.define('my-counter', class extends ReactiveElement {
  count = signal(0)

  constructor() {
    super()
    this.attachShadow({ mode: "open" })
  }

  initialHtml () {
    return html`
      Counting <slot data-bind="count">0</slot>
      <button data-onclick='inc'>+</button>
    `
  }

  inc () {
    this.count.set(c => c + 1)
  }
})

is this what you had in mind?

mb21 added a commit that referenced this issue Dec 5, 2024
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

2 participants