uosis / laminar-web-components

Web Component definitions for Laminar
MIT License
36 stars 8 forks source link

Fix Element.apply return type #8

Closed mathieuleclaire closed 3 years ago

mathieuleclaire commented 3 years ago

Hi, I can't find how trigger the show() function of a SnackBar component or a Dialog.

Thanks !

uosis commented 3 years ago

Here is an example of how to call a function on web component.

mathieuleclaire commented 3 years ago

Well, It is not what I want to do. I would like to trigger a SnackBar element from an action triggered any time (like a button click) and not at the mount time. I don't know how to call the relevant methods from an already built snack bar element. Note that it could be the same with a dialog we want to trigger from a button click.

raquo commented 3 years ago

@mathieuleclaire ctx.thisNode is the Laminar element, i.e. the same instance as the result of Slider() in the linked example (see Slider.scala). However, Slider.apply return type is, incorrectly, a generic HtmlElement, whereas it should actually be the more specific El.

Same for the SnackBar component, its apply method return type should be El. Then you'll be able to say something like:

val snackbar = Snackbar(...)

div(
  "Hello",
  snackbar,  // add a snackbar in the DOM
  button(
    "Show",
    onClick --> (_ => snackbar.ref.show()) // call this whenever
  )
)
mathieuleclaire commented 3 years ago

Well, when this ref call is done outside a onMountCallback, it returns a wrong type, so that the show() method does not exist. Here is the error I get:

value show is not a member of org.scalajs.dom.html.Element
[error]     snackbar.ref.show()
raquo commented 3 years ago

Yes, that's exactly what I'm saying.

Snackbar.apply return type needs to be changed from HtmlElement to El. Just the type signature, the content of the method is ok. Then it will work.

uosis commented 3 years ago

Looks like a bug in the generator - I will change this issue to that. Easy fix here.

uosis commented 3 years ago

Fixed by 62c203bff63570d0c516efc95dce3e9727e013ec

mathieuleclaire commented 3 years ago

Excellent. Sorry, I was focusing on Laminar, to learn it before fixing issues in this project.