forked from mantinedev/mantine
/
DropzoneFullScreen.tsx
136 lines (121 loc) · 3.58 KB
/
DropzoneFullScreen.tsx
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
import React, { forwardRef, useEffect } from 'react';
import {
Box,
useComponentDefaultProps,
OptionalPortal,
DefaultProps,
Selectors,
getDefaultZIndex,
} from '@mantine/core';
import { useDisclosure } from '@mantine/hooks';
import { DropzoneStylesNames, DropzoneProps, _Dropzone } from './Dropzone';
import useFullScreenStyles from './DropzoneFullScreen.styles';
export type DropzoneFullScreenStylesName =
| DropzoneStylesNames
| Exclude<Selectors<typeof useFullScreenStyles>, 'dropzone'>;
export interface DropzoneFullScreenProps
extends Omit<DropzoneProps, 'styles' | 'classNames'>,
DefaultProps<DropzoneFullScreenStylesName> {
/** Determines whether user can drop files to browser window, true by default */
active?: boolean;
/** z-index value, 9999 by default */
zIndex?: React.CSSProperties['zIndex'];
/** Determines whether component should be rendered within Portal, true by default */
withinPortal?: boolean;
}
const fullScreenDefaultProps: Partial<DropzoneFullScreenProps> = {
padding: 'md',
loading: false,
multiple: true,
maxSize: Infinity,
autoFocus: false,
activateOnClick: true,
activateOnDrag: true,
dragEventsBubbling: true,
activateOnKeyboard: true,
active: true,
zIndex: getDefaultZIndex('max'),
withinPortal: true,
};
export const DropzoneFullScreen = forwardRef<HTMLDivElement, DropzoneFullScreenProps>(
(props, ref) => {
const {
classNames,
styles,
sx,
className,
style,
unstyled,
active,
onDrop,
onReject,
onDragLeave,
zIndex,
withinPortal,
...others
} = useComponentDefaultProps('DropzoneFullScreen', fullScreenDefaultProps, props);
const [counter, setCounter] = React.useState(0);
const [visible, { open, close }] = useDisclosure(false);
const { classes, cx } = useFullScreenStyles(null, {
name: 'DropzoneFullScreen',
classNames,
styles,
unstyled,
});
const handleDragEnter = () => {
setCounter((prev) => prev + 1);
open();
};
const handleDragLeave = () => {
setCounter((prev) => prev - 1);
};
useEffect(() => {
counter === 0 && close();
}, [counter]);
useEffect(() => {
if (!active) return undefined;
document.addEventListener('dragenter', handleDragEnter, false);
document.addEventListener('dragleave', handleDragLeave, false);
return () => {
document.removeEventListener('dragover', handleDragEnter, false);
document.removeEventListener('dragleave', handleDragLeave, false);
};
}, [active]);
return (
<OptionalPortal withinPortal={withinPortal}>
<Box
className={cx(classes.wrapper, className)}
sx={sx}
style={{
...style,
opacity: visible ? 1 : 0,
pointerEvents: visible ? 'all' : 'none',
zIndex,
}}
>
<_Dropzone
{...others}
classNames={classNames}
styles={styles}
unstyled={unstyled}
ref={ref}
className={classes.dropzone}
onDrop={(files: any) => {
onDrop?.(files);
close();
}}
onReject={(files: any) => {
onReject?.(files);
close();
}}
onDragLeave={(event: any) => {
onDragLeave?.(event);
}}
/>
</Box>
</OptionalPortal>
);
}
);
DropzoneFullScreen.displayName = '@mantine/dropzone/DropzoneFullScreen';
export type DropzoneFullScreenType = typeof DropzoneFullScreen;