rescript-react icon indicating copy to clipboard operation
rescript-react copied to clipboard

Use new WebAPI

Open nojaf opened this issue 1 year ago • 8 comments

This is a sample PR to illustrate the usage of the new WebAPI. Some types in ReactDOM and ReactEvent were changed to match the new bindings. As only the new types are used (which will be quite stable), this might not be as radical a change.

(PS: not saying we should merge this PR anytime soon)

nojaf avatar Dec 06 '24 09:12 nojaf

Hi @nojaf ! I'm really interested to test theses changes on my projects. Is it possible to upgrade rescript and @rescript/webapi please? thanks :)

Freddy03h avatar Dec 03 '25 15:12 Freddy03h

Feel free to push to this branch. Tis probably best to make a new PR for this really.

nojaf avatar Dec 03 '25 16:12 nojaf

Hi, I'm testing the PR.

Is this the way to onChange on input or I'm missing something?

let onChange = (event: ReactEvent.Form.t) => {
  let input: WebAPI.DOMAPI.htmlInputElement =
    event->ReactEvent.Form.target->WebAPI.Prelude.unsafeConversation
  handleChange(input.value)
}

Freddy03h avatar Dec 04 '25 11:12 Freddy03h

Hey Freddy,

Yeah, that is still a bit of a tricky one. The target can be multiple things (for example select) so we can't quite pick a useful type here.

WebAPI.Prelude.unsafeConversation is also really an alias for Obj.magic, which is something we might also want to revisit at some point.

One other thing that comes to mind here is to do an inline %raw instanceof check.

But yeah, no ideal solution.

nojaf avatar Dec 04 '25 13:12 nojaf

The previous {..} wasn’t safe either, but to me it’s better now.

I don’t see a better solution than providing a custom Event for each HTML element, so that currentTarget can be a DOMAPI.htmlInputElement / DOMAPI.htmlSelectElement / etc. instead of a generic DOMAPI.node. This won’t fix target, but bubbling is a less common scenario anyway.

Typing the DOM API is hard. Something like react-strict-dom would probably be easier - but that’s off topic.

I think the current WebAPI approach is fine as it is; it’s permissive. And it’s always possible to build a custom rescript-react library that enforces stricter types (or one that types react-strict-dom).

Freddy03h avatar Dec 04 '25 14:12 Freddy03h

Maybe a helper to get the .value as string (option?) would go a long way. That is what 95% of the users will need.

nojaf avatar Dec 04 '25 15:12 nojaf

For forms & inputs, yes, but that doesn’t solve other types of events. The goal of having detailed records is kind of lost if we need to create a bunch of helpers for common use cases.

On HTML*Element we already have asNode for type converting, so why not also have a fromNode on each element? (because adding all asHTML*Element on the Node module would be huge)

Freddy03h avatar Dec 04 '25 16:12 Freddy03h

Because fromNode would require a runtime check to be safe. Something like instanceof HTMLInputElement, that would no longer make it a zero-runtime binding, which is another of discussion. Maybe we want to go in that direction, maybe not.

nojaf avatar Dec 05 '25 11:12 nojaf