@colisweb/rescript-toolkit 1.29.0 → 1.29.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@colisweb/rescript-toolkit",
3
- "version": "1.29.0",
3
+ "version": "1.29.1",
4
4
  "scripts": {
5
5
  "clean": "rescript clean",
6
6
  "build": "rescript build",
@@ -23,21 +23,37 @@ let make = (
23
23
  ref.current
24
24
  ->Js.Nullable.toOption
25
25
  ->Option.forEach(dom => {
26
- let {left, right} = dom->Browser.DomElement.getBoundingClientRect
26
+ let {left, width, right, top} = dom->Browser.DomElement.getBoundingClientRect
27
+
28
+ let overflowFromRight = left +. width > Browser.innerWidth->Float.fromInt
29
+ let overflowFromLeft = left <= 0.
30
+
31
+ let overflowCorrection = {
32
+ if overflowFromRight {
33
+ Some(-.(width -. (Browser.innerWidth->Int.toFloat -. left)))
34
+ } else if overflowFromLeft {
35
+ Some(-.left)
36
+ } else {
37
+ None
38
+ }
39
+ }
27
40
 
28
41
  switch position {
29
42
  | #left if left < 0. => setPosition(_ => #right)
30
43
  | #right if right < 0. => setPosition(_ => #left)
31
- | _ =>
32
- setAdjustmentStyle(_ => Some(
33
- ReactDOM.Style.make(
34
- ~marginLeft=left < 0. ? `${-.left->Js.Float.toString}px` : "",
35
- ~marginRight=right < 0. ? `${-.right->Js.Float.toString}px` : "",
36
- ~opacity="1",
37
- (),
38
- ),
39
- ))
44
+ | #top if top < 0. => setPosition(_ => #bottom)
45
+ | _ => ()
40
46
  }
47
+
48
+ setAdjustmentStyle(_ => Some(
49
+ ReactDOM.Style.make(
50
+ ~marginLeft=overflowCorrection->Option.mapWithDefault("", correction =>
51
+ `${correction->Js.Float.toString}px`
52
+ ),
53
+ ~opacity="1",
54
+ (),
55
+ ),
56
+ ))
41
57
  })
42
58
  } else {
43
59
  setAdjustmentStyle(_ => None)
@@ -45,3 +45,6 @@ module DomElement = {
45
45
  }
46
46
  @send external getBoundingClientRect: Dom.element => rect = "getBoundingClientRect"
47
47
  }
48
+
49
+ @val
50
+ external innerWidth: int = "window.innerWidth"
@@ -33,3 +33,5 @@ module DomElement: {
33
33
  }
34
34
  let getBoundingClientRect: Dom.element => rect
35
35
  }
36
+
37
+ let innerWidth: int