-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.res
186 lines (162 loc) · 5.27 KB
/
index.res
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
module ClickOutside = {
@react.component
let make = () => {
let dataListRef = React.useRef(Js.Nullable.null)
let (dropdownIsOpen, setDropdownIsOpen) = React.useState(() => false)
let (text, setText) = React.useState(_ => "")
ClickOutsideHook.use0(~refs=[dataListRef], () => setDropdownIsOpen(_ => false))
<div style={ReactDOMStyle.make(~width="200px", ())} ref={ReactDOM.Ref.domRef(dataListRef)}>
{"Click in the input and outside"->React.string}
<input readOnly=true onFocus={_ => setDropdownIsOpen(_ => true)} value=text />
{!dropdownIsOpen
? React.null
: <div
style={ReactDOM.Style.make(
~border="1px solid black",
~width="200px",
~position="absolute",
~backgroundColor="white",
(),
)}>
{["apple", "banana", "orange"]
->Js.Array2.map(fruit =>
<div key={fruit} onClick={_ => setText(_ => fruit)}> {fruit->React.string} </div>
)
->React.array}
</div>}
</div>
}
}
module WindowListeners = {
@react.component
let make = () => {
let (windowClicks, setWindowClicks) = React.useState(() => 0)
let ((mousePositionX, mousePositionY), setMousePosition) = React.useState(() => (0, 0))
WindowListenersHook.Click.use0(_event => {
setWindowClicks(clicks => clicks + 1)
})
WindowListenersHook.MouseMove.use0(event => {
setMousePosition(_ => (event->Webapi.Dom.MouseEvent.x, event->Webapi.Dom.MouseEvent.y))
})
<div>
<div> {"Window events"->React.string} </div>
<div> {`Window clicks ${windowClicks->Belt.Int.toString}`->React.string} </div>
<div>
{`Mouse position x:${mousePositionX->Belt.Int.toString}, y:${mousePositionY->Belt.Int.toString}`->React.string}
</div>
</div>
}
}
module OpenFolder = {
@react.component
let make = () => {
let (disabled, setDisabled) = React.useState(() => false)
let {files, openFolder, openFolderProps} = OpenFolderHook.use()
React.useEffect1(() => {
if files->Belt.Option.isSome {
Js.log("Files changed")
}
None
}, [files])
<div>
<OpenFolderHook.Input openFolderProps accept="image/jpeg" disabled multiple=true />
<div> {"Open Folder Hook"->React.string} </div>
<div>
<button type_="checkbox" onClick={_ => setDisabled(not)}>
{`${disabled ? "Enable" : "Disable"} input`->React.string}
</button>
</div>
{switch files {
| None | Some([]) => <div> {"No file open"->React.string} </div>
| Some(files) =>
<div>
<div> {"All opened files:"->React.string} </div>
{files
->Js.Array2.map(file =>
<div key={file->Webapi.File.name}> {file->Webapi.File.name->React.string} </div>
)
->React.array}
</div>
}}
<div />
<button disabled onClick={_ => openFolder()}>
{"Click to open the folder"->React.string}
</button>
</div>
}
}
module Device = {
module DeviceHookResolver = {
type t = [#unsupported | #mobile | #tablet | #desktop]
let resolve = ({WindowHook.innerWidth: innerWidth}): t =>
switch () {
| _ if innerWidth < 375 => #unsupported
| _ if innerWidth < 768 => #mobile
| _ if innerWidth < 1024 => #tablet
| _ => #desktop
}
}
module DeviceHook = DeviceHook.Make(DeviceHookResolver)
// Or, using the `make` function:
// module DeviceHook = unpack(DeviceHook.make(~resolve=DeviceHookResolver.resolve))
@react.component
let make = () => {
let device = DeviceHook.use()
<div>
<div>
{`Current device (based on screen size): ${device->Belt.Option.mapWithDefault(
"Unknown",
device => (device :> string),
)}`->React.string}
</div>
</div>
}
}
module Window = {
@react.component
let make = () => {
let window = WindowHook.use()
<div>
<div> {"Window data"->React.string} </div>
{switch window {
| None => React.null
| Some({innerWidth, innerHeight, outerWidth, outerHeight, scrollX, scrollY}) => <>
<div>
{`Window inner size width:${innerWidth->Belt.Int.toString}, height:${innerHeight->Belt.Int.toString}`->React.string}
</div>
<div>
{`Window outer size width:${outerWidth->Belt.Int.toString}, height:${outerHeight->Belt.Int.toString}`->React.string}
</div>
<div
style={ReactDOM.Style.make(
~height="1000px",
~width="2000px",
~display="flex",
~alignItems="center",
~justifyContent="center",
(),
)}>
{`Window scroll x:${scrollX->Belt.Float.toString}, y:${scrollY->Belt.Float.toString}`->React.string}
</div>
</>
}}
</div>
}
}
module App = {
@react.component
let make = () => {
<div>
<div> {"ReHooks"->React.string} </div>
<div> <ClickOutside /> </div>
<div> <WindowListeners /> </div>
<div> <OpenFolder /> </div>
<div> <Device /> </div>
<div> <Window /> </div>
</div>
}
}
ReactDOM.render(
<App />,
Webapi.Dom.document->Webapi.Dom.Document.getElementById("root", _)->Belt.Option.getExn,
)