-
Notifications
You must be signed in to change notification settings - Fork 349
Add data-* attrs support
#901
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
base: main
Are you sure you want to change the base?
Changes from 3 commits
8066745
b3f1f52
740158f
916cd1f
bf982de
db9ead6
a2a9f48
a467adf
d42a9a5
0470056
3bbc1a6
b89c750
f8e2288
4ca0aa3
2a41068
b37645d
cfd5e5f
1cf9af5
4edc014
5fd0014
ede06ea
7f7ab19
6a6f72e
7942e3e
de819ed
9d8d6d8
e61aa5a
3c7d5dc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| version = 0.26.0 | ||
| version = 0.27.0 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -484,30 +484,35 @@ module Experimental = { | |
| external preloadOptions: | ||
| ( | ||
| ~_as: [ | ||
| | `audio | ||
| | `document | ||
| | `embed | ||
| | `fetch | ||
| | `font | ||
| | `image | ||
| | [@mel.as "object"] `object_ | ||
| | `script | ||
| | `style | ||
| | `track | ||
| | `video | ||
| | `worker | ||
| ], | ||
| ~fetchPriority: [ | `auto | `high | `low]=?, | ||
| ~referrerPolicy: [ | ||
| | [@mel.as "no-referrer"] `noReferrer | ||
| | [@mel.as "no-referrer-when-downgrade"] | ||
| `noReferrerWhenDowngrade | ||
| | [@mel.as "origin"] `origin | ||
| | [@mel.as "origin-when-cross-origin"] | ||
| `originWhenCrossOrigin | ||
| | [@mel.as "unsafe-url"] `unsafeUrl | ||
| ] | ||
| =?, | ||
| | `audio | ||
| | `document | ||
| | `embed | ||
| | `fetch | ||
| | `font | ||
| | `image | ||
| | [@mel.as "object"] `object_ | ||
| | `script | ||
| | `style | ||
| | `track | ||
| | `video | ||
| | `worker | ||
| ], | ||
| ~fetchPriority: | ||
| [ | ||
| | `auto | ||
| | `high | ||
| | `low | ||
| ] | ||
| =?, | ||
| ~referrerPolicy: | ||
| [ | ||
| | [@mel.as "no-referrer"] `noReferrer | ||
| | [@mel.as "no-referrer-when-downgrade"] `noReferrerWhenDowngrade | ||
| | [@mel.as "origin"] `origin | ||
| | [@mel.as "origin-when-cross-origin"] `originWhenCrossOrigin | ||
| | [@mel.as "unsafe-url"] `unsafeUrl | ||
| ] | ||
| =?, | ||
| ~imageSrcSet: string=?, | ||
| ~imageSizes: string=?, | ||
| ~crossOrigin: string=?, | ||
|
|
@@ -520,11 +525,29 @@ module Experimental = { | |
| [@deriving jsProperties] | ||
| type preinitOptions = { | ||
| [@mel.as "as"] | ||
| _as: [ | `script | `style], | ||
| _as: [ | ||
| | `script | ||
| | `style | ||
| ], | ||
| [@mel.optional] | ||
| fetchPriority: option([ | `auto | `high | `low]), | ||
| fetchPriority: | ||
| option( | ||
| [ | ||
| | `auto | ||
| | `high | ||
| | `low | ||
| ], | ||
| ), | ||
| [@mel.optional] | ||
| precedence: option([ | `reset | `low | `medium | `high]), | ||
| precedence: | ||
| option( | ||
| [ | ||
| | `reset | ||
| | `low | ||
| | `medium | ||
| | `high | ||
| ], | ||
| ), | ||
| [@mel.optional] | ||
| crossOrigin: option(string), | ||
| [@mel.optional] | ||
|
|
@@ -1609,6 +1632,9 @@ type domProps = { | |
| suppressContentEditableWarning: option(bool), | ||
| [@mel.optional] | ||
| suppressHydrationWarning: option(bool), | ||
| /* data attributes */ | ||
| [@mel.optional] | ||
| dataAttrs: option(Js.Dict.t(string)), | ||
|
||
| }; | ||
|
|
||
| // As we've removed `ReactDOMRe.createElement`, this enables patterns like | ||
|
|
@@ -1625,16 +1651,58 @@ external createDOMElementVariadic: | |
| (string, ~props: domProps=?, array(React.element)) => React.element = | ||
| "createElement"; | ||
|
|
||
| // Helper function to process dataAttrs | ||
| let processDataAttrs = (props: domProps): domProps => { | ||
|
||
| switch (props.dataAttrs) { | ||
| | None => props // Short circuit: if no data attributes provided, return props unchanged for better performance | ||
| | Some(_) => | ||
| props | ||
| |> Obj.magic | ||
| |> Js.Dict.entries | ||
| |> Js.Array.reduce( | ||
| ~f= | ||
| acc => | ||
| fun | ||
| | ("dataAttrs", dataAttrsDict) => | ||
| dataAttrsDict | ||
| |> Obj.magic | ||
| |> Js.Dict.entries | ||
| |> Js.Array.reduce( | ||
| ~f= | ||
| (acc, (dataKey, dataValue)) => | ||
| [ | ||
| ("data-" ++ dataKey, dataValue |> Obj.magic: string), | ||
| ...acc, | ||
| ], | ||
| ~init=acc, | ||
| ) | ||
| | (key, value) => [(key, value), ...acc], | ||
| ~init=[], | ||
| ) | ||
| |> Js.Dict.fromList | ||
| |> Obj.magic | ||
| }; | ||
| }; | ||
|
|
||
| // JSX functions with dataAttrs processing | ||
| [@mel.module "react/jsx-runtime"] | ||
| external jsxKeyed: (string, domProps, ~key: string=?, unit) => React.element = | ||
| "jsx"; | ||
| let jsxKeyed = (component: string, props: domProps, ~key=?, ()) => | ||
| jsxKeyed(component, processDataAttrs(props), ~key?, ()); | ||
|
||
|
|
||
| [@mel.module "react/jsx-runtime"] | ||
| external jsx: (string, domProps) => React.element = "jsx"; | ||
| let jsx = (component: string, props: domProps) => | ||
| jsx(component, processDataAttrs(props)); | ||
|
|
||
| [@mel.module "react/jsx-runtime"] | ||
| external jsxs: (string, domProps) => React.element = "jsxs"; | ||
| let jsxs = (component: string, props: domProps) => | ||
| jsxs(component, processDataAttrs(props)); | ||
|
|
||
| [@mel.module "react/jsx-runtime"] | ||
| external jsxsKeyed: (string, domProps, ~key: string=?, unit) => React.element = | ||
| "jsxs"; | ||
| let jsxsKeyed = (component: string, props: domProps, ~key=?, ()) => | ||
| jsxsKeyed(component, processDataAttrs(props), ~key?, ()); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wouldn't change the ocamlformat version in a PR that adds a feature
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had that split out to #900, if that PR is good then this change will be removed from this PR when that other is added. I don't know how to do a merge chain from multiple PRs from a fork :(