@gfazioli/mantine-window 0.5.7 → 0.6.1

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.
@@ -4,6 +4,16 @@
4
4
  var React = require('react');
5
5
  var hooks = require('@mantine/hooks');
6
6
 
7
+ const CURSOR_MAP = {
8
+ topLeft: "nwse-resize",
9
+ top: "ns-resize",
10
+ topRight: "nesw-resize",
11
+ right: "ew-resize",
12
+ bottomRight: "nwse-resize",
13
+ bottom: "ns-resize",
14
+ bottomLeft: "nesw-resize",
15
+ left: "ew-resize"
16
+ };
7
17
  function useMantineWindow(props) {
8
18
  const {
9
19
  title,
@@ -111,9 +121,6 @@ function useMantineWindow(props) {
111
121
  if (e.target.closest("[data-resize-handle]")) {
112
122
  return;
113
123
  }
114
- if (!e.touches[0]) {
115
- return;
116
- }
117
124
  const touch = e.touches[0];
118
125
  bringToFront();
119
126
  isDragging.current = true;
@@ -127,341 +134,57 @@ function useMantineWindow(props) {
127
134
  [position, bringToFront]
128
135
  );
129
136
  const resizeDirection = React.useRef("");
130
- const handleMouseDownResizeTopLeft = React.useCallback(
131
- (e) => {
132
- bringToFront();
133
- isResizing.current = true;
134
- resizeDirection.current = "topLeft";
135
- resizeStart.current = {
136
- x: e.clientX,
137
- y: e.clientY,
138
- width: size.width,
139
- height: size.height,
140
- posX: position.x,
141
- posY: position.y
142
- };
143
- document.body.style.cursor = "nwse-resize";
144
- document.body.style.userSelect = "none";
145
- e.preventDefault();
146
- e.stopPropagation();
147
- },
148
- [size, position, bringToFront]
149
- );
150
- const handleTouchStartResizeTopLeft = React.useCallback(
151
- (e) => {
152
- if (!e.touches[0]) {
153
- return;
154
- }
155
- const touch = e.touches[0];
156
- bringToFront();
157
- isResizing.current = true;
158
- resizeDirection.current = "topLeft";
159
- resizeStart.current = {
160
- x: touch.clientX,
161
- y: touch.clientY,
162
- width: size.width,
163
- height: size.height,
164
- posX: position.x,
165
- posY: position.y
166
- };
167
- document.body.style.userSelect = "none";
168
- e.stopPropagation();
169
- },
170
- [size, position, bringToFront]
171
- );
172
- const handleMouseDownResizeTop = React.useCallback(
173
- (e) => {
174
- bringToFront();
175
- isResizing.current = true;
176
- resizeDirection.current = "top";
177
- resizeStart.current = {
178
- x: e.clientX,
179
- y: e.clientY,
180
- width: size.width,
181
- height: size.height,
182
- posX: position.x,
183
- posY: position.y
184
- };
185
- document.body.style.cursor = "ns-resize";
186
- document.body.style.userSelect = "none";
187
- e.preventDefault();
188
- e.stopPropagation();
189
- },
190
- [size, position, bringToFront]
191
- );
192
- const handleTouchStartResizeTop = React.useCallback(
193
- (e) => {
194
- if (!e.touches[0]) {
195
- return;
196
- }
197
- const touch = e.touches[0];
198
- bringToFront();
199
- isResizing.current = true;
200
- resizeDirection.current = "top";
201
- resizeStart.current = {
202
- x: touch.clientX,
203
- y: touch.clientY,
204
- width: size.width,
205
- height: size.height,
206
- posX: position.x,
207
- posY: position.y
208
- };
209
- document.body.style.userSelect = "none";
210
- e.stopPropagation();
211
- },
212
- [size, position, bringToFront]
213
- );
214
- const handleMouseDownResizeTopRight = React.useCallback(
215
- (e) => {
216
- bringToFront();
217
- isResizing.current = true;
218
- resizeDirection.current = "topRight";
219
- resizeStart.current = {
220
- x: e.clientX,
221
- y: e.clientY,
222
- width: size.width,
223
- height: size.height,
224
- posX: position.x,
225
- posY: position.y
226
- };
227
- document.body.style.cursor = "nesw-resize";
228
- document.body.style.userSelect = "none";
229
- e.preventDefault();
230
- e.stopPropagation();
231
- },
232
- [size, position, bringToFront]
233
- );
234
- const handleTouchStartResizeTopRight = React.useCallback(
235
- (e) => {
236
- if (!e.touches[0]) {
237
- return;
238
- }
239
- const touch = e.touches[0];
240
- bringToFront();
241
- isResizing.current = true;
242
- resizeDirection.current = "topRight";
243
- resizeStart.current = {
244
- x: touch.clientX,
245
- y: touch.clientY,
246
- width: size.width,
247
- height: size.height,
248
- posX: position.x,
249
- posY: position.y
250
- };
251
- document.body.style.userSelect = "none";
252
- e.stopPropagation();
253
- },
254
- [size, position, bringToFront]
255
- );
256
- const handleMouseDownResizeRight = React.useCallback(
257
- (e) => {
258
- bringToFront();
259
- isResizing.current = true;
260
- resizeDirection.current = "right";
261
- resizeStart.current = {
262
- x: e.clientX,
263
- y: e.clientY,
264
- width: size.width,
265
- height: size.height,
266
- posX: position.x,
267
- posY: position.y
268
- };
269
- document.body.style.cursor = "ew-resize";
270
- document.body.style.userSelect = "none";
271
- e.preventDefault();
272
- e.stopPropagation();
273
- },
274
- [size, position, bringToFront]
275
- );
276
- const handleTouchStartResizeRight = React.useCallback(
277
- (e) => {
278
- if (!e.touches[0]) {
279
- return;
280
- }
281
- const touch = e.touches[0];
282
- bringToFront();
283
- isResizing.current = true;
284
- resizeDirection.current = "right";
285
- resizeStart.current = {
286
- x: touch.clientX,
287
- y: touch.clientY,
288
- width: size.width,
289
- height: size.height,
290
- posX: position.x,
291
- posY: position.y
292
- };
293
- document.body.style.userSelect = "none";
294
- e.stopPropagation();
295
- },
296
- [size, position, bringToFront]
297
- );
298
- const handleMouseDownResizeBottomRight = React.useCallback(
299
- (e) => {
300
- bringToFront();
301
- isResizing.current = true;
302
- resizeDirection.current = "bottomRight";
303
- resizeStart.current = {
304
- x: e.clientX,
305
- y: e.clientY,
306
- width: size.width,
307
- height: size.height,
308
- posX: position.x,
309
- posY: position.y
310
- };
311
- document.body.style.cursor = "nwse-resize";
312
- document.body.style.userSelect = "none";
313
- e.preventDefault();
314
- e.stopPropagation();
315
- },
316
- [size, position, bringToFront]
317
- );
318
- const handleTouchStartResizeBottomRight = React.useCallback(
319
- (e) => {
320
- if (!e.touches[0]) {
321
- return;
322
- }
323
- const touch = e.touches[0];
324
- bringToFront();
325
- isResizing.current = true;
326
- resizeDirection.current = "bottomRight";
327
- resizeStart.current = {
328
- x: touch.clientX,
329
- y: touch.clientY,
330
- width: size.width,
331
- height: size.height,
332
- posX: position.x,
333
- posY: position.y
334
- };
335
- document.body.style.userSelect = "none";
336
- e.stopPropagation();
337
- },
338
- [size, position, bringToFront]
339
- );
340
- const handleMouseDownResizeBottom = React.useCallback(
341
- (e) => {
342
- bringToFront();
343
- isResizing.current = true;
344
- resizeDirection.current = "bottom";
345
- resizeStart.current = {
346
- x: e.clientX,
347
- y: e.clientY,
348
- width: size.width,
349
- height: size.height,
350
- posX: position.x,
351
- posY: position.y
352
- };
353
- document.body.style.cursor = "ns-resize";
354
- document.body.style.userSelect = "none";
355
- e.preventDefault();
356
- e.stopPropagation();
357
- },
358
- [size, position, bringToFront]
359
- );
360
- const handleTouchStartResizeBottom = React.useCallback(
361
- (e) => {
362
- if (!e.touches[0]) {
363
- return;
364
- }
365
- const touch = e.touches[0];
366
- bringToFront();
367
- isResizing.current = true;
368
- resizeDirection.current = "bottom";
369
- resizeStart.current = {
370
- x: touch.clientX,
371
- y: touch.clientY,
372
- width: size.width,
373
- height: size.height,
374
- posX: position.x,
375
- posY: position.y
376
- };
377
- document.body.style.userSelect = "none";
378
- e.stopPropagation();
379
- },
380
- [size, position, bringToFront]
381
- );
382
- const handleMouseDownResizeBottomLeft = React.useCallback(
383
- (e) => {
384
- bringToFront();
385
- isResizing.current = true;
386
- resizeDirection.current = "bottomLeft";
387
- resizeStart.current = {
388
- x: e.clientX,
389
- y: e.clientY,
390
- width: size.width,
391
- height: size.height,
392
- posX: position.x,
393
- posY: position.y
394
- };
395
- document.body.style.cursor = "nesw-resize";
396
- document.body.style.userSelect = "none";
397
- e.preventDefault();
398
- e.stopPropagation();
399
- },
400
- [size, position, bringToFront]
401
- );
402
- const handleTouchStartResizeBottomLeft = React.useCallback(
403
- (e) => {
404
- if (!e.touches[0]) {
405
- return;
406
- }
407
- const touch = e.touches[0];
408
- bringToFront();
409
- isResizing.current = true;
410
- resizeDirection.current = "bottomLeft";
411
- resizeStart.current = {
412
- x: touch.clientX,
413
- y: touch.clientY,
414
- width: size.width,
415
- height: size.height,
416
- posX: position.x,
417
- posY: position.y
137
+ const createResizeHandlers = React.useCallback(
138
+ (direction) => {
139
+ const onMouseDown = (e) => {
140
+ bringToFront();
141
+ isResizing.current = true;
142
+ resizeDirection.current = direction;
143
+ resizeStart.current = {
144
+ x: e.clientX,
145
+ y: e.clientY,
146
+ width: size.width,
147
+ height: size.height,
148
+ posX: position.x,
149
+ posY: position.y
150
+ };
151
+ document.body.style.cursor = CURSOR_MAP[direction];
152
+ document.body.style.userSelect = "none";
153
+ e.preventDefault();
154
+ e.stopPropagation();
418
155
  };
419
- document.body.style.userSelect = "none";
420
- e.stopPropagation();
421
- },
422
- [size, position, bringToFront]
423
- );
424
- const handleMouseDownResizeLeft = React.useCallback(
425
- (e) => {
426
- bringToFront();
427
- isResizing.current = true;
428
- resizeDirection.current = "left";
429
- resizeStart.current = {
430
- x: e.clientX,
431
- y: e.clientY,
432
- width: size.width,
433
- height: size.height,
434
- posX: position.x,
435
- posY: position.y
156
+ const onTouchStart = (e) => {
157
+ const touch = e.touches[0];
158
+ bringToFront();
159
+ isResizing.current = true;
160
+ resizeDirection.current = direction;
161
+ resizeStart.current = {
162
+ x: touch.clientX,
163
+ y: touch.clientY,
164
+ width: size.width,
165
+ height: size.height,
166
+ posX: position.x,
167
+ posY: position.y
168
+ };
169
+ document.body.style.userSelect = "none";
170
+ e.stopPropagation();
436
171
  };
437
- document.body.style.cursor = "ew-resize";
438
- document.body.style.userSelect = "none";
439
- e.preventDefault();
440
- e.stopPropagation();
172
+ return { onMouseDown, onTouchStart };
441
173
  },
442
174
  [size, position, bringToFront]
443
175
  );
444
- const handleTouchStartResizeLeft = React.useCallback(
445
- (e) => {
446
- if (!e.touches[0]) {
447
- return;
448
- }
449
- const touch = e.touches[0];
450
- bringToFront();
451
- isResizing.current = true;
452
- resizeDirection.current = "left";
453
- resizeStart.current = {
454
- x: touch.clientX,
455
- y: touch.clientY,
456
- width: size.width,
457
- height: size.height,
458
- posX: position.x,
459
- posY: position.y
460
- };
461
- document.body.style.userSelect = "none";
462
- e.stopPropagation();
463
- },
464
- [size, position, bringToFront]
176
+ const resizeHandlers = React.useMemo(
177
+ () => ({
178
+ topLeft: createResizeHandlers("topLeft"),
179
+ top: createResizeHandlers("top"),
180
+ topRight: createResizeHandlers("topRight"),
181
+ right: createResizeHandlers("right"),
182
+ bottomRight: createResizeHandlers("bottomRight"),
183
+ bottom: createResizeHandlers("bottom"),
184
+ bottomLeft: createResizeHandlers("bottomLeft"),
185
+ left: createResizeHandlers("left")
186
+ }),
187
+ [createResizeHandlers]
465
188
  );
466
189
  const handleClose = React.useCallback(() => {
467
190
  if (onClose) {
@@ -587,9 +310,6 @@ function useMantineWindow(props) {
587
310
  }
588
311
  };
589
312
  const handleTouchMove = (e) => {
590
- if (!e.touches[0]) {
591
- return;
592
- }
593
313
  if (isDragging.current || isResizing.current) {
594
314
  const touch = e.touches[0];
595
315
  if (isDragging.current) {
@@ -647,22 +367,7 @@ function useMantineWindow(props) {
647
367
  windowRef,
648
368
  handleMouseDownDrag,
649
369
  handleTouchStartDrag,
650
- handleMouseDownResizeTopLeft,
651
- handleTouchStartResizeTopLeft,
652
- handleMouseDownResizeTop,
653
- handleTouchStartResizeTop,
654
- handleMouseDownResizeTopRight,
655
- handleTouchStartResizeTopRight,
656
- handleMouseDownResizeRight,
657
- handleTouchStartResizeRight,
658
- handleMouseDownResizeBottomRight,
659
- handleTouchStartResizeBottomRight,
660
- handleMouseDownResizeBottom,
661
- handleTouchStartResizeBottom,
662
- handleMouseDownResizeBottomLeft,
663
- handleTouchStartResizeBottomLeft,
664
- handleMouseDownResizeLeft,
665
- handleTouchStartResizeLeft,
370
+ resizeHandlers,
666
371
  handleClose,
667
372
  bringToFront
668
373
  };