@colisweb/rescript-toolkit 2.50.3 → 2.51.0

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": "2.50.3",
3
+ "version": "2.51.0",
4
4
  "scripts": {
5
5
  "clean": "rescript clean",
6
6
  "build": "rescript build -with-deps",
@@ -25,6 +25,7 @@ module Reference = Toolkit__Ui_Reference
25
25
  module RichText = Toolkit__Ui_RichText
26
26
  module ProgressBar = Toolkit__Ui_ProgressBar
27
27
  module Dropdown = Toolkit__Ui_Dropdown
28
+ module PortalDropdown = Toolkit__Ui_PortalDropdown
28
29
  module DropdownList = Toolkit__Ui_DropdownList
29
30
  module Layout = Toolkit__Ui_Layout
30
31
  module SpinnerFullScreen = Toolkit__Ui_SpinnerFullScreen
@@ -0,0 +1,102 @@
1
+ type position = [
2
+ | #bottom
3
+ | #right
4
+ | #top
5
+ | #left
6
+ ]
7
+
8
+ @react.component
9
+ let make = (
10
+ ~label: React.element,
11
+ ~children,
12
+ ~dropdownClassName="",
13
+ ~buttonClassName="",
14
+ ~containerClassName="",
15
+ ~defaultIsOpen=false,
16
+ ~buttonColor: Toolkit__Ui_Button.color=#white,
17
+ ~buttonSize: Toolkit__Ui_Button.size=#md,
18
+ ~buttonVariant: Toolkit__Ui_Button.variant=#default,
19
+ ) => {
20
+ let dropDownRef = React.useRef(Js.Nullable.null)
21
+ let buttonRef = React.useRef(Js.Nullable.null)
22
+ let {isOpen, hide, toggle} = Toolkit__Hooks.useDisclosure(~defaultIsOpen, ())
23
+
24
+ Toolkit__Hooks.useOnClickOutside(dropDownRef, _ => {
25
+ hide()
26
+ })
27
+
28
+ let (adjustmentStyle, setAdjustmentStyle) = React.useState(() => None)
29
+
30
+ React.useEffect1(() => {
31
+ if isOpen {
32
+ ()
33
+ } else {
34
+ setAdjustmentStyle(_ => None)
35
+ }
36
+ None
37
+ }, [isOpen])
38
+
39
+ <div className={cx(["relative", containerClassName])}>
40
+ <Toolkit__Ui_Button
41
+ variant=buttonVariant
42
+ size=buttonSize
43
+ type_="button"
44
+ color=buttonColor
45
+ buttonRef={ReactDOM.Ref.domRef(buttonRef)}
46
+ onClick={event => {
47
+ isOpen ? hide() : toggle()
48
+ }}
49
+ className={cx([buttonClassName, "dropdown-button"])}>
50
+ label
51
+ </Toolkit__Ui_Button>
52
+ {isOpen
53
+ ? <ReachUi.Portal>
54
+ <div
55
+ ref={ReactDOM.Ref.callbackDomRef(ref => {
56
+ dropDownRef.current = ref
57
+
58
+ if adjustmentStyle->Option.isNone {
59
+ let ref = ref->Js.Nullable.toOption
60
+ let buttonRef = buttonRef.current->Js.Nullable.toOption
61
+
62
+ switch (ref, buttonRef) {
63
+ | (Some(ref), Some(buttonRef)) => {
64
+ let {left, top, width, height} =
65
+ buttonRef->Browser.DomElement.getBoundingClientRect
66
+ let dropdown = ref->Browser.DomElement.getBoundingClientRect
67
+
68
+ let left = left +. width /. 2. -. dropdown.width /. 2.
69
+
70
+ let left = switch left {
71
+ | left if left < 0. => "0px"
72
+ | left if left +. dropdown.width > Browser.innerWidth->Int.toFloat =>
73
+ `${(Browser.innerWidth->Int.toFloat -. dropdown.width)->Js.Float.toString}px`
74
+ | left => `${left->Js.Float.toString}px`
75
+ }
76
+
77
+ setAdjustmentStyle(_ => Some(
78
+ ReactDOM.Style.make(
79
+ ~left,
80
+ ~top=`${(top +. height)->Js.Float.toString}px`,
81
+ ~opacity="1",
82
+ (),
83
+ ),
84
+ ))
85
+ }
86
+
87
+ | _ => ()
88
+ }
89
+ }
90
+ })}
91
+ className={cx([
92
+ "dropdown",
93
+ "absolute z-20 bg-white p-2 transform shadow rounded text-base font-normal text-neutral-700 opacity-0",
94
+ dropdownClassName,
95
+ ])}
96
+ style={adjustmentStyle->Option.getWithDefault(ReactDOM.Style.make())}>
97
+ children
98
+ </div>
99
+ </ReachUi.Portal>
100
+ : React.null}
101
+ </div>
102
+ }