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

Event handlers in custom tags #205

Open
propi opened this issue Aug 24, 2019 · 1 comment
Open

Event handlers in custom tags #205

propi opened this issue Aug 24, 2019 · 1 comment

Comments

@propi
Copy link

propi commented Aug 24, 2019

Hi,

in my web page, I use HTML custom tags and shadow DOM. I would like to use Binding.scala to render contents with bindings, but I have problem to add event handlers into custom tags.

In native JavaScript I have defined custom tag <top-menu-bar>. In Scala.js I have written this code:

@dom
def view: Binding[Div] = {
<div>
    <data:top-menu-bar data:onclick={_: Event => some action here}></data:top-menu-bar>
</div>
}

Unfortunately, the compiler returns type mismatch

type mismatch;
[error]  found   : org.scalajs.dom.Event => Boolean
[error]     (which expands to)  org.scalajs.dom.raw.Event => Boolean
[error]  required: String

If the attribute is without the data: prefix, it also does not work because value onclick is not a member of org.scalajs.dom.raw.Element

Is it possible to add an event handler to a custom tag?

@Atry
Copy link
Collaborator

Atry commented Feb 1, 2023

I recently announced LatestEvent.scala, which can be used for handling events.

Something like this would work

def view: Binding[Div] = {
  val topMenuBar = html"<top-menu-bar></top-menu-bar>"
  Binding {
    LatestEvent.click(topMenuBar.bind).bind
    // some action here
  }.bind
  html"<div>$topMenuBar</div>"
}

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