forked from revery-ui/revery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HoverExample.re
161 lines (152 loc) · 4.44 KB
/
HoverExample.re
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
open Revery;
open Revery.UI;
type state = {
parentBackground: Color.t,
childOneBackground: Color.t,
childTwoBackground: Color.t,
childThreeBackground: Color.t,
childFourBackground: Color.t,
};
type action =
| SetParentBackground(Color.t)
| SetChildOneBackground(Color.t)
| SetChildTwoBackground(Color.t)
| SetChildThreeBackground(Color.t)
| SetChildFourBackground(Color.t);
let reducer = (action: action, state) => {
switch (action) {
| SetParentBackground(c) => {...state, parentBackground: c}
| SetChildOneBackground(c) => {...state, childOneBackground: c}
| SetChildTwoBackground(c) => {...state, childTwoBackground: c}
| SetChildThreeBackground(c) => {...state, childThreeBackground: c}
| SetChildFourBackground(c) => {...state, childFourBackground: c}
};
};
module HoverExample = {
let component = React.component("HoverExample");
let initialState = {
parentBackground: Colors.darkGray,
childOneBackground: Colors.blanchedAlmond,
childTwoBackground: Colors.blueViolet,
childThreeBackground: Colors.burlyWood,
childFourBackground: Colors.cornflowerBlue,
};
let createElement = (~children as _, ()) =>
component(hooks => {
let (state, dispatch, hooks) =
Hooks.reducer(~initialState, reducer, hooks);
let hooks =
Hooks.effect(
OnMount,
() => {
let dispose =
Mouse.registerListeners(
~onMouseEnterWindow=_ => print_endline("enter window"),
~onMouseLeaveWindow=_ => print_endline("leave window"),
(),
);
Some(dispose);
},
hooks,
);
(
hooks,
<View
style=Style.[
flexDirection(`Row),
justifyContent(`Center),
alignItems(`Center),
width(500),
height(500),
backgroundColor(state.parentBackground),
]
onMouseOver={_ => dispatch(SetParentBackground(Colors.lightGray))}
onMouseOut={_ =>
dispatch(SetParentBackground(initialState.parentBackground))
}>
<View
style=Style.[
flexDirection(`Row),
flexWrap(`Wrap),
justifyContent(`SpaceAround),
alignItems(`Center),
]>
<View
style=Style.[
width(175),
height(175),
backgroundColor(state.childOneBackground),
]
onMouseEnter={_ =>
dispatch(SetChildOneBackground(Colors.aquamarine))
}
onMouseLeave={_ =>
dispatch(
SetChildOneBackground(initialState.childOneBackground),
)
}
/>
<View
style=Style.[
width(175),
height(175),
backgroundColor(state.childTwoBackground),
]
onMouseOver={_ =>
dispatch(SetChildTwoBackground(Colors.forestGreen))
}
onMouseOut={_ =>
dispatch(
SetChildTwoBackground(initialState.childTwoBackground),
)
}
/>
<View
style=Style.[
width(175),
height(175),
backgroundColor(state.childThreeBackground),
]
onMouseOver={_ =>
dispatch(SetChildThreeBackground(Colors.darkSalmon))
}
onMouseOut={_ =>
dispatch(
SetChildThreeBackground(initialState.childThreeBackground),
)
}
/>
<View
style=Style.[
width(175),
height(175),
backgroundColor(state.childFourBackground),
]
onMouseEnter={_ =>
dispatch(SetChildFourBackground(Colors.tomato))
}
onMouseLeave={_ =>
dispatch(
SetChildFourBackground(initialState.childFourBackground),
)
}
/>
</View>
</View>,
);
});
};
let render = () => {
<View
style=Style.[
position(`Absolute),
justifyContent(`Center),
alignItems(`Center),
bottom(0),
top(0),
left(0),
right(0),
]>
<HoverExample />
</View>;
};