Skip to content

Commit ed712d3

Browse files
committed
Refine navigation, factor hooks in separate hooks file
1 parent bdc9374 commit ed712d3

File tree

5 files changed

+422
-415
lines changed

5 files changed

+422
-415
lines changed

public/static/[email protected]

-241 Bytes
Loading

src/common/Hooks.js

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

src/common/Hooks.res

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/* Contains some generic hooks */
2+
3+
let useOutsideClick: (ReactDOM.Ref.t, unit => unit) => unit = %raw(j`(outerRef, trigger) => {
4+
const React = require('react')
5+
function handleClickOutside(event) {
6+
if (outerRef.current && !outerRef.current.contains(event.target)) {
7+
trigger();
8+
}
9+
}
10+
11+
React.useEffect(() => {
12+
document.addEventListener("mousedown", handleClickOutside);
13+
return () => {
14+
document.removeEventListener("mousedown", handleClickOutside);
15+
};
16+
});
17+
}`)
18+
19+
let useWindowWidth: unit => option<int> = %raw(j` () => {
20+
const React = require('react')
21+
const isClient = typeof window === 'object';
22+
23+
function getSize() {
24+
return {
25+
width: isClient ? window.innerWidth : undefined,
26+
height: isClient ? window.innerHeight : undefined
27+
};
28+
}
29+
30+
const [windowSize, setWindowSize] = React.useState(getSize);
31+
32+
React.useEffect(() => {
33+
if (!isClient) {
34+
return false;
35+
}
36+
37+
function handleResize() {
38+
setWindowSize(getSize());
39+
}
40+
41+
window.addEventListener('resize', handleResize);
42+
return () => window.removeEventListener('resize', handleResize);
43+
}, []); // Empty array ensures that effect is only run on mount and unmount
44+
45+
if(windowSize) {
46+
return windowSize.width;
47+
}
48+
return null;
49+
}`)

0 commit comments

Comments
 (0)