This repository has been archived by the owner on Apr 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
112 lines (92 loc) · 2.69 KB
/
main.js
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
import { createEvent, combine, sample, restore, createEffect } from "effector";
import { spec, list, h, remap, using, node, handler } from "forest";
import {
$messages,
$currentMessage,
currentMessageChanged,
messageSent,
} from "../../models/simple-chat";
import "./index.css";
function App() {
const colorlessModeChanged = createEvent();
const $colorlessMode = restore(colorlessModeChanged, false);
h("main", () => {
spec({ classList: ["container", "mx-auto"] });
h("aside", () => {
spec({
classList: ["sticky", "inset-0", "bg-white", "py-1", "flex gap-8"],
});
h("form", () => {
spec({
classList: ["bg-slate-300", "rounded-xl", "p-4", "flex gap-4"],
});
handler({ prevent: true }, { submit: messageSent });
h("label", () => {
spec({ classList: ["flex", "gap-4"] });
spec({ text: "Message " });
h("input", {
attr: { value: $currentMessage },
handler: {
input: currentMessageChanged.prepend((e) => e.target.value),
},
});
h("button", { text: "Send" });
});
});
h("label", () => {
spec({
classList: [
"bg-slate-300",
"rounded-xl",
"p-4",
"flex",
"gap-4",
"items-center",
],
text: "No colors",
});
h("input", {
attr: { type: "checkbox", checked: $colorlessMode },
handler: {
change: colorlessModeChanged.prepend((e) => e.target.checked),
},
});
});
});
h("section", () => {
list($messages, ({ store: $message }) => {
const $user = remap($message, "user");
const $hightlight = combine(
[$colorlessMode, $user],
([noColors, user]) => (noColors ? "black" : user.hightlight)
);
h("div", () => {
h("b", {
classList: ["pr-2"],
style: {
color: $hightlight,
},
text: remap($user, "name"),
});
h("span", { text: remap($message, "body") });
});
});
const newMessagesRef = createEvent();
const $messagesRef = restore(newMessagesRef, null);
const scrollIntoViewFx = createEffect((messagesRef) => {
messagesRef?.lastElementChild?.scrollIntoView({
behavior: "smooth",
block: "end",
inline: "nearest",
});
});
sample({
clock: $messages,
source: $messagesRef,
target: scrollIntoViewFx,
});
node(newMessagesRef);
});
});
}
using(document.getElementById("root"), App);