You can also replace the open JS object type definition ({..}) with a more concrete definition as well to restrict to certain set of attributes…
Currently not aware of any copy-pastable module that covers the whole CSS spec, except the one implemented in rescript-react here… you might just copy paste that and adapt it to your needs and call it Style.res or something.
If you want to write single attributes, that’s going to be a little different. Something like this:
Can I set arbitrary individual properties? I was hoping to support this sort of api: let setStyle = (element: Webapi.Dom.Element.t, property: string, value: string): unit => ...
type element
@get external style: (element) => {..} = "style"
@val external someElement: element = "someElement"
(someElement->style)["background-color"] = "blue"
Unfortunately, our precedence for -> and [] is pretty messed up, that’s why you need to wrap the pipe call in parenthesis. You can actually also just put style in a binding and manipulate that separately… same results
I think @nickretallack is trying to set a style using a dynamic key, something like setStyle(element, "color", "red") right?
If yes, you can do this using Js.Dict.t (but I don’t recommend it):
module Dom = {
type element
@get external style: element => Js.Dict.t<string> = "style"
}
let setStyle = (element, key, value) => Js.Dict.set(Dom.style(element), key, value)