@gfazioli/mantine-window 0.5.6 → 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.
@@ -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,
@@ -124,317 +134,57 @@ function useMantineWindow(props) {
124
134
  [position, bringToFront]
125
135
  );
126
136
  const resizeDirection = React.useRef("");
127
- const handleMouseDownResizeTopLeft = React.useCallback(
128
- (e) => {
129
- bringToFront();
130
- isResizing.current = true;
131
- resizeDirection.current = "topLeft";
132
- resizeStart.current = {
133
- x: e.clientX,
134
- y: e.clientY,
135
- width: size.width,
136
- height: size.height,
137
- posX: position.x,
138
- posY: position.y
139
- };
140
- document.body.style.cursor = "nwse-resize";
141
- document.body.style.userSelect = "none";
142
- e.preventDefault();
143
- e.stopPropagation();
144
- },
145
- [size, position, bringToFront]
146
- );
147
- const handleTouchStartResizeTopLeft = React.useCallback(
148
- (e) => {
149
- const touch = e.touches[0];
150
- bringToFront();
151
- isResizing.current = true;
152
- resizeDirection.current = "topLeft";
153
- resizeStart.current = {
154
- x: touch.clientX,
155
- y: touch.clientY,
156
- width: size.width,
157
- height: size.height,
158
- posX: position.x,
159
- posY: position.y
160
- };
161
- document.body.style.userSelect = "none";
162
- e.stopPropagation();
163
- },
164
- [size, position, bringToFront]
165
- );
166
- const handleMouseDownResizeTop = React.useCallback(
167
- (e) => {
168
- bringToFront();
169
- isResizing.current = true;
170
- resizeDirection.current = "top";
171
- resizeStart.current = {
172
- x: e.clientX,
173
- y: e.clientY,
174
- width: size.width,
175
- height: size.height,
176
- posX: position.x,
177
- posY: position.y
178
- };
179
- document.body.style.cursor = "ns-resize";
180
- document.body.style.userSelect = "none";
181
- e.preventDefault();
182
- e.stopPropagation();
183
- },
184
- [size, position, bringToFront]
185
- );
186
- const handleTouchStartResizeTop = React.useCallback(
187
- (e) => {
188
- const touch = e.touches[0];
189
- bringToFront();
190
- isResizing.current = true;
191
- resizeDirection.current = "top";
192
- resizeStart.current = {
193
- x: touch.clientX,
194
- y: touch.clientY,
195
- width: size.width,
196
- height: size.height,
197
- posX: position.x,
198
- posY: position.y
199
- };
200
- document.body.style.userSelect = "none";
201
- e.stopPropagation();
202
- },
203
- [size, position, bringToFront]
204
- );
205
- const handleMouseDownResizeTopRight = React.useCallback(
206
- (e) => {
207
- bringToFront();
208
- isResizing.current = true;
209
- resizeDirection.current = "topRight";
210
- resizeStart.current = {
211
- x: e.clientX,
212
- y: e.clientY,
213
- width: size.width,
214
- height: size.height,
215
- posX: position.x,
216
- posY: position.y
217
- };
218
- document.body.style.cursor = "nesw-resize";
219
- document.body.style.userSelect = "none";
220
- e.preventDefault();
221
- e.stopPropagation();
222
- },
223
- [size, position, bringToFront]
224
- );
225
- const handleTouchStartResizeTopRight = React.useCallback(
226
- (e) => {
227
- const touch = e.touches[0];
228
- bringToFront();
229
- isResizing.current = true;
230
- resizeDirection.current = "topRight";
231
- resizeStart.current = {
232
- x: touch.clientX,
233
- y: touch.clientY,
234
- width: size.width,
235
- height: size.height,
236
- posX: position.x,
237
- posY: position.y
238
- };
239
- document.body.style.userSelect = "none";
240
- e.stopPropagation();
241
- },
242
- [size, position, bringToFront]
243
- );
244
- const handleMouseDownResizeRight = React.useCallback(
245
- (e) => {
246
- bringToFront();
247
- isResizing.current = true;
248
- resizeDirection.current = "right";
249
- resizeStart.current = {
250
- x: e.clientX,
251
- y: e.clientY,
252
- width: size.width,
253
- height: size.height,
254
- posX: position.x,
255
- posY: position.y
256
- };
257
- document.body.style.cursor = "ew-resize";
258
- document.body.style.userSelect = "none";
259
- e.preventDefault();
260
- e.stopPropagation();
261
- },
262
- [size, position, bringToFront]
263
- );
264
- const handleTouchStartResizeRight = React.useCallback(
265
- (e) => {
266
- const touch = e.touches[0];
267
- bringToFront();
268
- isResizing.current = true;
269
- resizeDirection.current = "right";
270
- resizeStart.current = {
271
- x: touch.clientX,
272
- y: touch.clientY,
273
- width: size.width,
274
- height: size.height,
275
- posX: position.x,
276
- posY: position.y
277
- };
278
- document.body.style.userSelect = "none";
279
- e.stopPropagation();
280
- },
281
- [size, position, bringToFront]
282
- );
283
- const handleMouseDownResizeBottomRight = React.useCallback(
284
- (e) => {
285
- bringToFront();
286
- isResizing.current = true;
287
- resizeDirection.current = "bottomRight";
288
- resizeStart.current = {
289
- x: e.clientX,
290
- y: e.clientY,
291
- width: size.width,
292
- height: size.height,
293
- posX: position.x,
294
- posY: position.y
295
- };
296
- document.body.style.cursor = "nwse-resize";
297
- document.body.style.userSelect = "none";
298
- e.preventDefault();
299
- e.stopPropagation();
300
- },
301
- [size, position, bringToFront]
302
- );
303
- const handleTouchStartResizeBottomRight = React.useCallback(
304
- (e) => {
305
- const touch = e.touches[0];
306
- bringToFront();
307
- isResizing.current = true;
308
- resizeDirection.current = "bottomRight";
309
- resizeStart.current = {
310
- x: touch.clientX,
311
- y: touch.clientY,
312
- width: size.width,
313
- height: size.height,
314
- posX: position.x,
315
- posY: position.y
316
- };
317
- document.body.style.userSelect = "none";
318
- e.stopPropagation();
319
- },
320
- [size, position, bringToFront]
321
- );
322
- const handleMouseDownResizeBottom = React.useCallback(
323
- (e) => {
324
- bringToFront();
325
- isResizing.current = true;
326
- resizeDirection.current = "bottom";
327
- resizeStart.current = {
328
- x: e.clientX,
329
- y: e.clientY,
330
- width: size.width,
331
- height: size.height,
332
- posX: position.x,
333
- posY: position.y
334
- };
335
- document.body.style.cursor = "ns-resize";
336
- document.body.style.userSelect = "none";
337
- e.preventDefault();
338
- e.stopPropagation();
339
- },
340
- [size, position, bringToFront]
341
- );
342
- const handleTouchStartResizeBottom = React.useCallback(
343
- (e) => {
344
- const touch = e.touches[0];
345
- bringToFront();
346
- isResizing.current = true;
347
- resizeDirection.current = "bottom";
348
- resizeStart.current = {
349
- x: touch.clientX,
350
- y: touch.clientY,
351
- width: size.width,
352
- height: size.height,
353
- posX: position.x,
354
- posY: position.y
355
- };
356
- document.body.style.userSelect = "none";
357
- e.stopPropagation();
358
- },
359
- [size, position, bringToFront]
360
- );
361
- const handleMouseDownResizeBottomLeft = React.useCallback(
362
- (e) => {
363
- bringToFront();
364
- isResizing.current = true;
365
- resizeDirection.current = "bottomLeft";
366
- resizeStart.current = {
367
- x: e.clientX,
368
- y: e.clientY,
369
- width: size.width,
370
- height: size.height,
371
- posX: position.x,
372
- posY: position.y
373
- };
374
- document.body.style.cursor = "nesw-resize";
375
- document.body.style.userSelect = "none";
376
- e.preventDefault();
377
- e.stopPropagation();
378
- },
379
- [size, position, bringToFront]
380
- );
381
- const handleTouchStartResizeBottomLeft = React.useCallback(
382
- (e) => {
383
- const touch = e.touches[0];
384
- bringToFront();
385
- isResizing.current = true;
386
- resizeDirection.current = "bottomLeft";
387
- resizeStart.current = {
388
- x: touch.clientX,
389
- y: touch.clientY,
390
- width: size.width,
391
- height: size.height,
392
- posX: position.x,
393
- 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();
394
155
  };
395
- document.body.style.userSelect = "none";
396
- e.stopPropagation();
397
- },
398
- [size, position, bringToFront]
399
- );
400
- const handleMouseDownResizeLeft = React.useCallback(
401
- (e) => {
402
- bringToFront();
403
- isResizing.current = true;
404
- resizeDirection.current = "left";
405
- resizeStart.current = {
406
- x: e.clientX,
407
- y: e.clientY,
408
- width: size.width,
409
- height: size.height,
410
- posX: position.x,
411
- 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();
412
171
  };
413
- document.body.style.cursor = "ew-resize";
414
- document.body.style.userSelect = "none";
415
- e.preventDefault();
416
- e.stopPropagation();
172
+ return { onMouseDown, onTouchStart };
417
173
  },
418
174
  [size, position, bringToFront]
419
175
  );
420
- const handleTouchStartResizeLeft = React.useCallback(
421
- (e) => {
422
- const touch = e.touches[0];
423
- bringToFront();
424
- isResizing.current = true;
425
- resizeDirection.current = "left";
426
- resizeStart.current = {
427
- x: touch.clientX,
428
- y: touch.clientY,
429
- width: size.width,
430
- height: size.height,
431
- posX: position.x,
432
- posY: position.y
433
- };
434
- document.body.style.userSelect = "none";
435
- e.stopPropagation();
436
- },
437
- [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]
438
188
  );
439
189
  const handleClose = React.useCallback(() => {
440
190
  if (onClose) {
@@ -617,22 +367,7 @@ function useMantineWindow(props) {
617
367
  windowRef,
618
368
  handleMouseDownDrag,
619
369
  handleTouchStartDrag,
620
- handleMouseDownResizeTopLeft,
621
- handleTouchStartResizeTopLeft,
622
- handleMouseDownResizeTop,
623
- handleTouchStartResizeTop,
624
- handleMouseDownResizeTopRight,
625
- handleTouchStartResizeTopRight,
626
- handleMouseDownResizeRight,
627
- handleTouchStartResizeRight,
628
- handleMouseDownResizeBottomRight,
629
- handleTouchStartResizeBottomRight,
630
- handleMouseDownResizeBottom,
631
- handleTouchStartResizeBottom,
632
- handleMouseDownResizeBottomLeft,
633
- handleTouchStartResizeBottomLeft,
634
- handleMouseDownResizeLeft,
635
- handleTouchStartResizeLeft,
370
+ resizeHandlers,
636
371
  handleClose,
637
372
  bringToFront
638
373
  };