@colisweb/rescript-toolkit 2.50.3 → 2.51.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": "2.50.3",
3
+ "version": "2.51.1",
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,104 @@
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
+ ~onButtonClick=?,
17
+ ~buttonColor: Toolkit__Ui_Button.color=#white,
18
+ ~buttonSize: Toolkit__Ui_Button.size=#md,
19
+ ~buttonVariant: Toolkit__Ui_Button.variant=#default,
20
+ ) => {
21
+ let dropDownRef = React.useRef(Js.Nullable.null)
22
+ let buttonRef = React.useRef(Js.Nullable.null)
23
+ let {isOpen, hide, toggle} = Toolkit__Hooks.useDisclosure(~defaultIsOpen, ())
24
+
25
+ Toolkit__Hooks.useOnClickOutside(dropDownRef, _ => {
26
+ hide()
27
+ })
28
+
29
+ let (adjustmentStyle, setAdjustmentStyle) = React.useState(() => None)
30
+
31
+ React.useEffect1(() => {
32
+ if isOpen {
33
+ ()
34
+ } else {
35
+ setAdjustmentStyle(_ => None)
36
+ }
37
+ None
38
+ }, [isOpen])
39
+
40
+ <div className={cx(["relative", containerClassName])}>
41
+ <Toolkit__Ui_Button
42
+ variant=buttonVariant
43
+ size=buttonSize
44
+ type_="button"
45
+ color=buttonColor
46
+ buttonRef={ReactDOM.Ref.domRef(buttonRef)}
47
+ onClick={event => {
48
+ onButtonClick->Option.forEach(fn => fn())
49
+ isOpen ? hide() : toggle()
50
+ }}
51
+ className={cx([buttonClassName, "dropdown-button"])}>
52
+ label
53
+ </Toolkit__Ui_Button>
54
+ {isOpen
55
+ ? <ReachUi.Portal>
56
+ <div
57
+ ref={ReactDOM.Ref.callbackDomRef(ref => {
58
+ dropDownRef.current = ref
59
+
60
+ if adjustmentStyle->Option.isNone {
61
+ let ref = ref->Js.Nullable.toOption
62
+ let buttonRef = buttonRef.current->Js.Nullable.toOption
63
+
64
+ switch (ref, buttonRef) {
65
+ | (Some(ref), Some(buttonRef)) => {
66
+ let {left, top, width, height} =
67
+ buttonRef->Browser.DomElement.getBoundingClientRect
68
+ let dropdown = ref->Browser.DomElement.getBoundingClientRect
69
+
70
+ let left = left +. width /. 2. -. dropdown.width /. 2.
71
+
72
+ let left = switch left {
73
+ | left if left < 0. => "0px"
74
+ | left if left +. dropdown.width > Browser.innerWidth->Int.toFloat =>
75
+ `${(Browser.innerWidth->Int.toFloat -. dropdown.width)->Js.Float.toString}px`
76
+ | left => `${left->Js.Float.toString}px`
77
+ }
78
+
79
+ setAdjustmentStyle(_ => Some(
80
+ ReactDOM.Style.make(
81
+ ~left,
82
+ ~top=`${(top +. height)->Js.Float.toString}px`,
83
+ ~opacity="1",
84
+ (),
85
+ ),
86
+ ))
87
+ }
88
+
89
+ | _ => ()
90
+ }
91
+ }
92
+ })}
93
+ className={cx([
94
+ "dropdown",
95
+ "absolute z-20 bg-white p-2 transform shadow rounded text-base font-normal text-neutral-700 opacity-0",
96
+ dropdownClassName,
97
+ ])}
98
+ style={adjustmentStyle->Option.getWithDefault(ReactDOM.Style.make())}>
99
+ children
100
+ </div>
101
+ </ReachUi.Portal>
102
+ : React.null}
103
+ </div>
104
+ }