@rpg-engine/long-bow 0.1.78 → 0.1.794
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +20 -20
- package/README.md +181 -181
- package/dist/components/store/UI.store.d.ts +4 -0
- package/dist/long-bow.cjs.development.js +48 -6
- package/dist/long-bow.cjs.development.js.map +1 -1
- package/dist/long-bow.cjs.production.min.js +1 -1
- package/dist/long-bow.cjs.production.min.js.map +1 -1
- package/dist/long-bow.esm.js +48 -6
- package/dist/long-bow.esm.js.map +1 -1
- package/package.json +96 -96
- package/src/components/Abstractions/SlotsContainer.tsx +42 -42
- package/src/components/Button.tsx +29 -29
- package/src/components/Chat/Chat.tsx +193 -193
- package/src/components/CheckButton.tsx +65 -65
- package/src/components/DraggableContainer.tsx +150 -150
- package/src/components/Dropdown.tsx +57 -57
- package/src/components/Equipment/EquipmentSet.tsx +180 -179
- package/src/components/Input.tsx +11 -11
- package/src/components/Item/Cards/ItemCard.tsx +36 -36
- package/src/components/Item/Inventory/ItemContainer.tsx +113 -113
- package/src/components/Item/Inventory/ItemSlot.tsx +158 -158
- package/src/components/Item/Inventory/itemContainerHelper.ts +81 -81
- package/src/components/ListMenu.tsx +65 -65
- package/src/components/Multitab/Tab.tsx +57 -57
- package/src/components/Multitab/TabBody.tsx +13 -13
- package/src/components/Multitab/TabsContainer.tsx +97 -97
- package/src/components/NPCDialog/NPCDialog.tsx +145 -145
- package/src/components/NPCDialog/NPCDialogText.tsx +53 -53
- package/src/components/NPCDialog/QuestionDialog/QuestionDialog.tsx +242 -242
- package/src/components/ProgressBar.tsx +91 -91
- package/src/components/RPGUIContainer.tsx +47 -47
- package/src/components/RPGUIRoot.tsx +14 -14
- package/src/components/RadioButton.tsx +53 -53
- package/src/components/RangeSlider.tsx +68 -68
- package/src/components/ScrollList.tsx +77 -77
- package/src/components/SimpleProgressBar.tsx +62 -62
- package/src/components/SkillProgressBar.tsx +124 -124
- package/src/components/SkillsContainer.tsx +235 -235
- package/src/components/TextArea.tsx +11 -11
- package/src/components/Truncate.tsx +25 -25
- package/src/components/shared/Column.tsx +16 -16
- package/src/components/shared/SpriteFromAtlas.tsx +99 -99
- package/src/components/shared/SpriteIcon.tsx +67 -67
- package/src/components/store/UI.store.ts +232 -192
- package/src/components/typography/DynamicText.tsx +49 -49
- package/src/constants/uiColors.ts +10 -10
- package/src/hooks/useEventListener.ts +21 -21
- package/src/hooks/useOutsideAlerter.ts +25 -25
- package/src/index.tsx +25 -25
- package/src/libs/StringHelpers.ts +3 -3
- package/src/mocks/atlas/icons/icons.json +303 -303
- package/src/mocks/atlas/items/items.json +5195 -5195
- package/src/mocks/equipmentSet.mocks.ts +347 -347
- package/src/mocks/itemContainer.mocks.ts +249 -249
- package/src/mocks/skills.mocks.ts +122 -122
- package/src/types/eventTypes.ts +4 -4
- package/src/types/index.d.ts +2 -2
|
@@ -1,192 +1,232 @@
|
|
|
1
|
-
import {
|
|
2
|
-
IItem,
|
|
3
|
-
IPayloadProps,
|
|
4
|
-
ItemSocketEvents,
|
|
5
|
-
ItemSocketEventsDisplayLabels,
|
|
6
|
-
ItemType,
|
|
7
|
-
} from '@rpg-engine/shared';
|
|
8
|
-
import { makeAutoObservable, toJS } from 'mobx';
|
|
9
|
-
import {
|
|
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
|
-
public
|
|
100
|
-
this.
|
|
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
|
-
item.
|
|
139
|
-
);
|
|
140
|
-
break;
|
|
141
|
-
|
|
142
|
-
this.setContextMenu(
|
|
143
|
-
{
|
|
144
|
-
visible: true,
|
|
145
|
-
posX,
|
|
146
|
-
posY,
|
|
147
|
-
slotItem: item,
|
|
148
|
-
},
|
|
149
|
-
item.type
|
|
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
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
1
|
+
import {
|
|
2
|
+
IItem,
|
|
3
|
+
IPayloadProps,
|
|
4
|
+
ItemSocketEvents,
|
|
5
|
+
ItemSocketEventsDisplayLabels,
|
|
6
|
+
ItemType,
|
|
7
|
+
} from '@rpg-engine/shared';
|
|
8
|
+
import { makeAutoObservable, toJS } from 'mobx';
|
|
9
|
+
import { IPosition } from '../../types/eventTypes';
|
|
10
|
+
import {
|
|
11
|
+
handleContextMenuList,
|
|
12
|
+
handleEquipmentContextMenuList,
|
|
13
|
+
IContextMenuItem,
|
|
14
|
+
} from '../Item/Inventory/itemContainerHelper';
|
|
15
|
+
import { SlotContainerType } from '../Item/Inventory/ItemSlot';
|
|
16
|
+
|
|
17
|
+
interface IContextMenu {
|
|
18
|
+
visible: boolean;
|
|
19
|
+
posX: number;
|
|
20
|
+
posY: number;
|
|
21
|
+
slotItem: IItem | null;
|
|
22
|
+
slotIndex?: number | null;
|
|
23
|
+
contextActions: IContextMenuItem[];
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
interface IHoverDetail {
|
|
27
|
+
visible: boolean;
|
|
28
|
+
posX: number;
|
|
29
|
+
posY: number;
|
|
30
|
+
item: IItem | null;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
interface ISetContextMenu extends Omit<IContextMenu, 'contextActions'> {}
|
|
34
|
+
|
|
35
|
+
const initialState = {
|
|
36
|
+
visible: false,
|
|
37
|
+
posX: 0,
|
|
38
|
+
posY: 0,
|
|
39
|
+
contextActions: [],
|
|
40
|
+
slotItem: null,
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
const initialHoverState = {
|
|
44
|
+
visible: false,
|
|
45
|
+
posX: 0,
|
|
46
|
+
posY: 0,
|
|
47
|
+
item: null,
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
const initialPosition = {
|
|
51
|
+
x: 0,
|
|
52
|
+
y: 0
|
|
53
|
+
}
|
|
54
|
+
class UIStore {
|
|
55
|
+
public contextMenu: IContextMenu | null = initialState;
|
|
56
|
+
public onHoverDetail: IHoverDetail | null = initialHoverState;
|
|
57
|
+
|
|
58
|
+
public draggablePosition: IPosition = initialPosition;
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
constructor() {
|
|
62
|
+
makeAutoObservable(this);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
public setContextMenu(contextMenu: ISetContextMenu, itemType: ItemType) {
|
|
66
|
+
const contextActions = handleContextMenuList(itemType);
|
|
67
|
+
|
|
68
|
+
this.contextMenu = {
|
|
69
|
+
...contextMenu,
|
|
70
|
+
contextActions,
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
console.log(toJS(this.contextMenu));
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
public setEquipContextMenu(
|
|
77
|
+
contextMenu: ISetContextMenu,
|
|
78
|
+
itemType: ItemType,
|
|
79
|
+
isItemContainer: boolean | undefined
|
|
80
|
+
) {
|
|
81
|
+
const contextActions = handleEquipmentContextMenuList(itemType);
|
|
82
|
+
if (isItemContainer)
|
|
83
|
+
contextActions.push({
|
|
84
|
+
id: ItemSocketEvents.ContainerOpen,
|
|
85
|
+
text: ItemSocketEventsDisplayLabels[ItemSocketEvents.ContainerOpen],
|
|
86
|
+
});
|
|
87
|
+
this.contextMenu = {
|
|
88
|
+
...contextMenu,
|
|
89
|
+
contextActions,
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
console.log(toJS(this.contextMenu));
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
public clearContextMenu() {
|
|
96
|
+
this.contextMenu = initialState;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
public setItemHoverDetail(hoverDetail: IHoverDetail) {
|
|
100
|
+
if (hoverDetail?.item === null) this.clearItemHoverDetail();
|
|
101
|
+
|
|
102
|
+
this.onHoverDetail = {
|
|
103
|
+
...hoverDetail,
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
public clearItemHoverDetail() {
|
|
108
|
+
this.onHoverDetail = initialHoverState;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
public handleOnItemClick = (
|
|
112
|
+
item: IItem,
|
|
113
|
+
posX: number,
|
|
114
|
+
posY: number,
|
|
115
|
+
slotContainerType: SlotContainerType | null
|
|
116
|
+
): void => {
|
|
117
|
+
if (
|
|
118
|
+
!item ||
|
|
119
|
+
JSON.stringify(this.contextMenu?.slotItem) === JSON.stringify(item)
|
|
120
|
+
) {
|
|
121
|
+
this.clearContextMenu();
|
|
122
|
+
this.clearItemHoverDetail();
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
this.setDraggablePosition(this.getOffsets())
|
|
127
|
+
|
|
128
|
+
switch (slotContainerType) {
|
|
129
|
+
case SlotContainerType.EQUIPMENT_SET:
|
|
130
|
+
this.setEquipContextMenu(
|
|
131
|
+
{
|
|
132
|
+
visible: true,
|
|
133
|
+
posX,
|
|
134
|
+
posY,
|
|
135
|
+
slotItem: item,
|
|
136
|
+
},
|
|
137
|
+
item.type,
|
|
138
|
+
item.isItemContainer
|
|
139
|
+
);
|
|
140
|
+
break;
|
|
141
|
+
case SlotContainerType.INVENTORY:
|
|
142
|
+
this.setContextMenu(
|
|
143
|
+
{
|
|
144
|
+
visible: true,
|
|
145
|
+
posX,
|
|
146
|
+
posY,
|
|
147
|
+
slotItem: item,
|
|
148
|
+
},
|
|
149
|
+
item.type
|
|
150
|
+
);
|
|
151
|
+
break;
|
|
152
|
+
default:
|
|
153
|
+
this.setContextMenu(
|
|
154
|
+
{
|
|
155
|
+
visible: true,
|
|
156
|
+
posX,
|
|
157
|
+
posY,
|
|
158
|
+
slotItem: item,
|
|
159
|
+
},
|
|
160
|
+
item.type
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
this.clearItemHoverDetail();
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
public handleOnMouseHover = (
|
|
168
|
+
event: any,
|
|
169
|
+
slotIndex: number,
|
|
170
|
+
item: IItem | null,
|
|
171
|
+
posX: number,
|
|
172
|
+
posY: number,
|
|
173
|
+
onMouseOver: any
|
|
174
|
+
): void => {
|
|
175
|
+
if (item) {
|
|
176
|
+
this.setDraggablePosition(this.getOffsets())
|
|
177
|
+
this.setItemHoverDetail({
|
|
178
|
+
visible: true,
|
|
179
|
+
posX: posX,
|
|
180
|
+
posY: posY,
|
|
181
|
+
item: item,
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
if (onMouseOver) {
|
|
185
|
+
onMouseOver(event, slotIndex, item);
|
|
186
|
+
}
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
public onSelected(
|
|
190
|
+
selectedActionId: ItemSocketEvents | string,
|
|
191
|
+
onActionSelected: any
|
|
192
|
+
) {
|
|
193
|
+
let payloadData: IPayloadProps = {
|
|
194
|
+
actionType: selectedActionId,
|
|
195
|
+
item: this.contextMenu?.slotItem!,
|
|
196
|
+
};
|
|
197
|
+
if (onActionSelected) {
|
|
198
|
+
onActionSelected(payloadData);
|
|
199
|
+
}
|
|
200
|
+
this.clearContextMenu!();
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
public setDraggablePosition(currentPosition: IPosition){
|
|
204
|
+
this.draggablePosition = { ...currentPosition }
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
private getOffsets = () => {
|
|
208
|
+
const p = { x: 0, y: 0};
|
|
209
|
+
// const body = document.getElementsByTagName('body')
|
|
210
|
+
let body = document.querySelector('body') as any | null;
|
|
211
|
+
|
|
212
|
+
if( body != null){
|
|
213
|
+
p.x = body.offsetLeft;
|
|
214
|
+
p.y = body.offsetTop;
|
|
215
|
+
while(body!.offsetParent){
|
|
216
|
+
console.log(`offsetParent: ${body!.offsetParent}`)
|
|
217
|
+
p.x = p.x + body!.offsetLeft
|
|
218
|
+
p.y = p.y + body!.offsetTop;
|
|
219
|
+
if (body == document.getElementsByTagName("body")[0]) {
|
|
220
|
+
break;
|
|
221
|
+
}
|
|
222
|
+
else {
|
|
223
|
+
body = body!.offsetParent;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
return p;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
export const uiStore = new UIStore();
|
|
@@ -1,49 +1,49 @@
|
|
|
1
|
-
import React, { useEffect, useState } from 'react';
|
|
2
|
-
import styled from 'styled-components';
|
|
3
|
-
|
|
4
|
-
interface IProps {
|
|
5
|
-
text: string;
|
|
6
|
-
onFinish?: () => void;
|
|
7
|
-
onStart?: () => void;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export const DynamicText: React.FC<IProps> = ({ text, onFinish, onStart }) => {
|
|
11
|
-
const [textState, setTextState] = useState<string>('');
|
|
12
|
-
|
|
13
|
-
useEffect(() => {
|
|
14
|
-
let i = 0;
|
|
15
|
-
const interval = setInterval(() => {
|
|
16
|
-
// on every interval, show one more character
|
|
17
|
-
|
|
18
|
-
if (i === 0) {
|
|
19
|
-
if (onStart) {
|
|
20
|
-
onStart();
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
if (i < text.length) {
|
|
25
|
-
setTextState(text.substring(0, i + 1));
|
|
26
|
-
i++;
|
|
27
|
-
} else {
|
|
28
|
-
clearInterval(interval);
|
|
29
|
-
if (onFinish) {
|
|
30
|
-
onFinish();
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
}, 50);
|
|
34
|
-
|
|
35
|
-
return () => {
|
|
36
|
-
clearInterval(interval);
|
|
37
|
-
};
|
|
38
|
-
}, [text]);
|
|
39
|
-
|
|
40
|
-
return <TextContainer>{textState}</TextContainer>;
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
const TextContainer = styled.p`
|
|
44
|
-
font-size: 0.7rem !important;
|
|
45
|
-
color: white;
|
|
46
|
-
text-shadow: 1px 1px 0px #000000;
|
|
47
|
-
letter-spacing: 1.2px;
|
|
48
|
-
word-break: normal;
|
|
49
|
-
`;
|
|
1
|
+
import React, { useEffect, useState } from 'react';
|
|
2
|
+
import styled from 'styled-components';
|
|
3
|
+
|
|
4
|
+
interface IProps {
|
|
5
|
+
text: string;
|
|
6
|
+
onFinish?: () => void;
|
|
7
|
+
onStart?: () => void;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export const DynamicText: React.FC<IProps> = ({ text, onFinish, onStart }) => {
|
|
11
|
+
const [textState, setTextState] = useState<string>('');
|
|
12
|
+
|
|
13
|
+
useEffect(() => {
|
|
14
|
+
let i = 0;
|
|
15
|
+
const interval = setInterval(() => {
|
|
16
|
+
// on every interval, show one more character
|
|
17
|
+
|
|
18
|
+
if (i === 0) {
|
|
19
|
+
if (onStart) {
|
|
20
|
+
onStart();
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (i < text.length) {
|
|
25
|
+
setTextState(text.substring(0, i + 1));
|
|
26
|
+
i++;
|
|
27
|
+
} else {
|
|
28
|
+
clearInterval(interval);
|
|
29
|
+
if (onFinish) {
|
|
30
|
+
onFinish();
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}, 50);
|
|
34
|
+
|
|
35
|
+
return () => {
|
|
36
|
+
clearInterval(interval);
|
|
37
|
+
};
|
|
38
|
+
}, [text]);
|
|
39
|
+
|
|
40
|
+
return <TextContainer>{textState}</TextContainer>;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
const TextContainer = styled.p`
|
|
44
|
+
font-size: 0.7rem !important;
|
|
45
|
+
color: white;
|
|
46
|
+
text-shadow: 1px 1px 0px #000000;
|
|
47
|
+
letter-spacing: 1.2px;
|
|
48
|
+
word-break: normal;
|
|
49
|
+
`;
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
export const colors = {
|
|
2
|
-
darkGrey: '#3e3e3e',
|
|
3
|
-
darkYellow: '#FFC857',
|
|
4
|
-
orange: '#E9724C',
|
|
5
|
-
cardinal: '#C5283D',
|
|
6
|
-
raisinBlack: '#191923',
|
|
7
|
-
navyBlue: '#0E79B2',
|
|
8
|
-
purple: '#8C3D8C',
|
|
9
|
-
blue: '#3772FF',
|
|
10
|
-
};
|
|
1
|
+
export const colors = {
|
|
2
|
+
darkGrey: '#3e3e3e',
|
|
3
|
+
darkYellow: '#FFC857',
|
|
4
|
+
orange: '#E9724C',
|
|
5
|
+
cardinal: '#C5283D',
|
|
6
|
+
raisinBlack: '#191923',
|
|
7
|
+
navyBlue: '#0E79B2',
|
|
8
|
+
purple: '#8C3D8C',
|
|
9
|
+
blue: '#3772FF',
|
|
10
|
+
};
|
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
|
-
|
|
3
|
-
//@ts-ignore
|
|
4
|
-
export const useEventListener = (type, handler, el = window) => {
|
|
5
|
-
const savedHandler = React.useRef();
|
|
6
|
-
|
|
7
|
-
React.useEffect(() => {
|
|
8
|
-
savedHandler.current = handler;
|
|
9
|
-
}, [handler]);
|
|
10
|
-
|
|
11
|
-
React.useEffect(() => {
|
|
12
|
-
//@ts-ignore
|
|
13
|
-
const listener = e => savedHandler.current(e);
|
|
14
|
-
|
|
15
|
-
el.addEventListener(type, listener);
|
|
16
|
-
|
|
17
|
-
return () => {
|
|
18
|
-
el.removeEventListener(type, listener);
|
|
19
|
-
};
|
|
20
|
-
}, [type, el]);
|
|
21
|
-
};
|
|
1
|
+
import React from 'react';
|
|
2
|
+
|
|
3
|
+
//@ts-ignore
|
|
4
|
+
export const useEventListener = (type, handler, el = window) => {
|
|
5
|
+
const savedHandler = React.useRef();
|
|
6
|
+
|
|
7
|
+
React.useEffect(() => {
|
|
8
|
+
savedHandler.current = handler;
|
|
9
|
+
}, [handler]);
|
|
10
|
+
|
|
11
|
+
React.useEffect(() => {
|
|
12
|
+
//@ts-ignore
|
|
13
|
+
const listener = e => savedHandler.current(e);
|
|
14
|
+
|
|
15
|
+
el.addEventListener(type, listener);
|
|
16
|
+
|
|
17
|
+
return () => {
|
|
18
|
+
el.removeEventListener(type, listener);
|
|
19
|
+
};
|
|
20
|
+
}, [type, el]);
|
|
21
|
+
};
|
|
@@ -1,25 +1,25 @@
|
|
|
1
|
-
import { useEffect } from 'react';
|
|
2
|
-
|
|
3
|
-
export function useOutsideClick(ref: any, id: string) {
|
|
4
|
-
useEffect(() => {
|
|
5
|
-
/**
|
|
6
|
-
* Alert if clicked on outside of element
|
|
7
|
-
*/
|
|
8
|
-
function handleClickOutside(event: any) {
|
|
9
|
-
if (ref.current && !ref.current.contains(event.target)) {
|
|
10
|
-
const event = new CustomEvent('clickOutside', {
|
|
11
|
-
detail: {
|
|
12
|
-
id,
|
|
13
|
-
},
|
|
14
|
-
});
|
|
15
|
-
document.dispatchEvent(event);
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
// Bind the event listener
|
|
19
|
-
document.addEventListener('mousedown', handleClickOutside);
|
|
20
|
-
return () => {
|
|
21
|
-
// Unbind the event listener on clean up
|
|
22
|
-
document.removeEventListener('mousedown', handleClickOutside);
|
|
23
|
-
};
|
|
24
|
-
}, [ref]);
|
|
25
|
-
}
|
|
1
|
+
import { useEffect } from 'react';
|
|
2
|
+
|
|
3
|
+
export function useOutsideClick(ref: any, id: string) {
|
|
4
|
+
useEffect(() => {
|
|
5
|
+
/**
|
|
6
|
+
* Alert if clicked on outside of element
|
|
7
|
+
*/
|
|
8
|
+
function handleClickOutside(event: any) {
|
|
9
|
+
if (ref.current && !ref.current.contains(event.target)) {
|
|
10
|
+
const event = new CustomEvent('clickOutside', {
|
|
11
|
+
detail: {
|
|
12
|
+
id,
|
|
13
|
+
},
|
|
14
|
+
});
|
|
15
|
+
document.dispatchEvent(event);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
// Bind the event listener
|
|
19
|
+
document.addEventListener('mousedown', handleClickOutside);
|
|
20
|
+
return () => {
|
|
21
|
+
// Unbind the event listener on clean up
|
|
22
|
+
document.removeEventListener('mousedown', handleClickOutside);
|
|
23
|
+
};
|
|
24
|
+
}, [ref]);
|
|
25
|
+
}
|