-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmojiActionButtion.tsx
More file actions
170 lines (162 loc) · 5.01 KB
/
Copy pathEmojiActionButtion.tsx
File metadata and controls
170 lines (162 loc) · 5.01 KB
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
import {
ActionIcon,
Center,
Title,
rem,
Modal as MantineModal,
Stack,
Indicator,
Loader,
} from "@mantine/core";
import { Theme } from "emoji-picker-react";
import dynamic from "next/dynamic";
import { useDisclosure, useMediaQuery } from "@mantine/hooks";
import { IconChevronLeft, IconPencil, IconRefresh } from "@tabler/icons-react";
import { UseFormReturnType } from "@mantine/form";
import { useEffect, useRef, useState } from "react";
import { useEmojiSuggestions } from "@/hooks/useEmojiSuggestions";
const Picker = dynamic(
() => {
return import("emoji-picker-react");
},
{ ssr: false }
);
type EmojiActionButtionProps = {
form: UseFormReturnType<any>;
suggestionQuery?: string;
onRandomize?: () => void;
};
const EmojiActionButtion = ({
form,
suggestionQuery,
onRandomize,
}: EmojiActionButtionProps) => {
const [opened, { open, close }] = useDisclosure(false);
const isMobile = useMediaQuery("(max-width: 50em)") || false;
const wasManuallySet = useRef(false);
const [suggestionIndex, setSuggestionIndex] = useState(0);
const { suggestions, loading } = useEmojiSuggestions(suggestionQuery ?? "");
useEffect(() => {
if (!wasManuallySet.current && suggestions.length > 0) {
form.setFieldValue("avatar", { emoji: suggestions[0], unified: "" });
setSuggestionIndex(0);
}
}, [suggestions]);
const handleCycle = () => {
if (suggestions.length < 2) return;
const next = (suggestionIndex + 1) % suggestions.length;
setSuggestionIndex(next);
form.setFieldValue("avatar", { emoji: suggestions[next], unified: "" });
};
const showCycleButton =
suggestions.length > 1 && !wasManuallySet.current && !opened;
return (
<>
<MantineModal.Root
opened={opened}
onClose={close}
fullScreen={isMobile}
transitionProps={{ transition: "fade", duration: 200 }}
centered
zIndex={201}
>
<MantineModal.Overlay />
<MantineModal.Content radius={isMobile ? 0 : "lg"}>
<MantineModal.Header>
<ActionIcon
variant="transparent"
color="gray"
aria-label="Settings"
onClick={close}
>
<IconChevronLeft
style={{ width: "70%", height: "70%" }}
stroke={1.5}
/>
</ActionIcon>
</MantineModal.Header>
<MantineModal.Body>
<Stack mih={rem(550)} justify="space-between">
<Center>
<ActionIcon variant="default" size={rem(100)} radius={rem(100)}>
<Title order={1} style={{ fontSize: rem(60) }}>
{form.values.avatar.emoji}
</Title>
</ActionIcon>
</Center>
<Center>
<Picker
height={390}
theme={Theme.AUTO}
onEmojiClick={(res) => {
wasManuallySet.current = true;
form.setFieldValue("avatar", {
emoji: res.emoji,
unified: res.unified,
});
close();
}}
previewConfig={{ showPreview: false }}
lazyLoadEmojis
/>
</Center>
</Stack>
</MantineModal.Body>
</MantineModal.Content>
</MantineModal.Root>
{!opened && (
<Indicator
color="gray"
offset={13}
position="bottom-end"
size={35}
withBorder
label={<IconPencil size={20} width={20} height={20} stroke={1.5} />}
onClick={open}
>
<div style={{ position: "relative" }}>
<ActionIcon
variant="default"
size={rem(100)}
radius={rem(100)}
onClick={open}
style={{
opacity: loading && !wasManuallySet.current ? 0.5 : 1,
transition: "opacity 0.2s",
}}
>
{loading && !wasManuallySet.current ? (
<Loader size="sm" />
) : (
<Title order={1} style={{ fontSize: rem(60) }}>
{form.values.avatar.emoji}
</Title>
)}
</ActionIcon>
{(showCycleButton || onRandomize) && (
<ActionIcon
variant="default"
size={rem(28)}
radius={rem(28)}
style={{
position: "absolute",
bottom: 0,
left: 0,
zIndex: 1,
}}
onClick={(e) => {
e.stopPropagation();
if (onRandomize) onRandomize();
else handleCycle();
}}
>
<IconRefresh size={14} stroke={1.5} />
</ActionIcon>
)}
</div>
</Indicator>
)}
</>
);
};
export default EmojiActionButtion;