@gfazioli/mantine-window 0.5.7 → 0.6.0

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