Skip to content

Commit 5f5b585

Browse files
committed
Proof of concept querySelector helpers
1 parent a0d5168 commit 5f5b585

File tree

11 files changed

+132
-59
lines changed

11 files changed

+132
-59
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Experimental successor to [rescript-webapi](https://github.com/TheSpyder/rescrip
99
Install the package using your favorite package manager:
1010

1111
```shell
12-
npm i -D @rescript/webapi@experimental
12+
npm i @rescript/webapi@experimental
1313
```
1414

1515
and add `@rescript/webapi` to your `rescript.json`:

docs/content/docs/index.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ Install the package using your favorite package manager:
3030
<Code
3131
lang="shell"
3232
frame="none"
33-
code="npm i -D @rescript/webapi@experimental"
33+
code="npm i @rescript/webapi@experimental"
3434
/>
3535
</TabItem>
3636
<TabItem label="bun">
3737
<Code
3838
lang="shell"
3939
frame="none"
40-
code="bun i -D @rescript/webapi@experimental"
40+
code="bun i @rescript/webapi@experimental"
4141
/>
4242
</TabItem>
4343
</Tabs>

src/DOMAPI/Document.js

Lines changed: 11 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/DOMAPI/Document.res

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,9 @@ Throws a "HierarchyRequestError" DOMException if the constraints of the node tre
7777
@send
7878
external replaceChildren2: (document, string) => unit = "replaceChildren"
7979

80-
/**
81-
Returns the first element that is a descendant of node that matches selectors.
82-
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/Document/querySelector)
83-
*/
84-
@send
85-
external querySelector: (document, string) => element = "querySelector"
80+
include QuerySelector.Impl({
81+
type t = document
82+
})
8683

8784
/**
8885
Returns all element descendants of node that match selectors.

src/DOMAPI/FillStyle.js

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/DOMAPI/HTMLCanvasElement.js

Lines changed: 10 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/DOMAPI/HTMLCanvasElement.res

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ external asHTMLElement: htmlCanvasElement => htmlElement = "%identity"
88
external asElement: htmlCanvasElement => element = "%identity"
99
external asNode: htmlCanvasElement => node = "%identity"
1010
external asEventTarget: htmlCanvasElement => eventTarget = "%identity"
11+
let isInstanceOf = (_: 't): bool => %raw(`param instanceof HTMLCanvasElement`)
1112
/**
1213
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/HTMLElement/focus)
1314
*/

src/DOMAPI/QuerySelector.js

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/DOMAPI/QuerySelector.res

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
open Prelude
2+
open DOMAPI
3+
4+
module Impl = (
5+
T: {
6+
type t
7+
},
8+
) => {
9+
/**
10+
`querySelector(T.t, string)`
11+
12+
Returns the first element that is a descendant of node that matches selectors.
13+
If nothing matches, the result is `null`. Use `querySelector_` helpers to get a typed result.
14+
15+
```res
16+
t->querySelector("#myCanvas")
17+
```
18+
19+
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/Document/querySelector)
20+
*/
21+
@send
22+
external querySelector: (T.t, string) => null<element> = "querySelector"
23+
24+
let querySelector_htmlCanvasElement = (t: T.t, selector: string): option<htmlCanvasElement> => {
25+
let e = querySelector(t, selector)
26+
switch e {
27+
| Null.Null => None
28+
| Null.Value(e) =>
29+
if HTMLCanvasElement.isInstanceOf(e) {
30+
Some(unsafeConversation(e))
31+
} else {
32+
None
33+
}
34+
}
35+
}
36+
}

tests/DOMAPI/HTMLCanvasElement__tes.js

Lines changed: 25 additions & 30 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
open WebAPI.Global
22

3-
let myCanvas: DOMAPI.htmlCanvasElement =
4-
document->Document.getElementById("myCanvas")->Prelude.unsafeConversation
5-
let ctx = myCanvas->HTMLCanvasElement.getContext_2D
3+
let myCanvas: option<DOMAPI.htmlCanvasElement> =
4+
document->Document.querySelector_htmlCanvasElement("#myCanvas")
65

7-
ctx.fillStyle = FillStyle.fromString("red")
8-
ctx->CanvasRenderingContext2D.fillRect(~x=50., ~y=50., ~w=200., ~h=200.)
6+
myCanvas->Option.forEach(myCanvas => {
7+
let ctx = myCanvas->HTMLCanvasElement.getContext_2D
98

10-
ctx.fillStyle = FillStyle.fromString("black")
11-
ctx.font = "2px Tahoma"
12-
ctx.textBaseline = CanvasAPI.Top
13-
ctx->CanvasRenderingContext2D.fillText(~text="MY TEXT", ~x=60., ~y=60.)
9+
ctx.fillStyle = FillStyle.fromString("red")
10+
ctx->CanvasRenderingContext2D.fillRect(~x=50., ~y=50., ~w=200., ~h=200.)
1411

15-
switch ctx.fillStyle->FillStyle.decode {
16-
| FillStyle.String(color) => Console.log(`Color: ${color}`)
17-
| FillStyle.CanvasGradient(_) => Console.log("CanvasGradient")
18-
| FillStyle.CanvasPattern(_) => Console.log("CanvasPattern")
19-
}
12+
ctx.fillStyle = FillStyle.fromString("black")
13+
ctx.font = "2px Tahoma"
14+
ctx.textBaseline = CanvasAPI.Top
15+
ctx->CanvasRenderingContext2D.fillText(~text="MY TEXT", ~x=60., ~y=60.)
16+
17+
switch ctx.fillStyle->FillStyle.decode {
18+
| FillStyle.String(color) => Console.log(`Color: ${color}`)
19+
| FillStyle.CanvasGradient(_) => Console.log("CanvasGradient")
20+
| FillStyle.CanvasPattern(_) => Console.log("CanvasPattern")
21+
}
22+
})

0 commit comments

Comments
 (0)