/
Dialog.tsx
139 lines (124 loc) · 3.16 KB
/
Dialog.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
137
138
139
import {
BoxProps,
createVarsResolver,
ElementProps,
factory,
Factory,
getSize,
MantineSize,
StylesApiProps,
useProps,
useStyles,
} from '../../core';
import { Affix, AffixBaseProps } from '../Affix';
import { CloseButton } from '../CloseButton';
import { Paper, PaperBaseProps } from '../Paper';
import { Transition, TransitionOverride } from '../Transition';
import classes from './Dialog.module.css';
export type DialogStylesNames = 'root' | 'closeButton';
export type DialogCssVariables = {
root: '--dialog-size';
};
export interface DialogProps
extends BoxProps,
AffixBaseProps,
PaperBaseProps,
StylesApiProps<DialogFactory>,
ElementProps<'div'> {
/** If set dialog will not be unmounted from the DOM when it is hidden, display: none styles will be added instead */
keepMounted?: boolean;
/** Determines whether the close button should be displayed, `true` by default */
withCloseButton?: boolean;
/** Called when the close button is clicked */
onClose?: () => void;
/** Dialog content */
children?: React.ReactNode;
/** Opened state */
opened: boolean;
/** Overrides default transition, `{ transition: 'pop-top-right', duration: 200 }` by default */
transitionProps?: TransitionOverride;
/** Controls `width` of the dialog, `'md'` by default */
size?: MantineSize | (string & {}) | number;
}
export type DialogFactory = Factory<{
props: DialogProps;
ref: HTMLDivElement;
stylesNames: DialogStylesNames;
vars: DialogCssVariables;
}>;
const defaultProps: Partial<DialogProps> = {
shadow: 'md',
p: 'md',
withBorder: false,
transitionProps: { transition: 'pop-top-right', duration: 200 },
position: {
bottom: 30,
right: 30,
},
};
const varsResolver = createVarsResolver<DialogFactory>((_, { size }) => ({
root: {
'--dialog-size': getSize(size, 'dialog-size'),
},
}));
export const Dialog = factory<DialogFactory>((_props, ref) => {
const props = useProps('Dialog', defaultProps, _props);
const {
classNames,
className,
style,
styles,
unstyled,
vars,
zIndex,
position,
keepMounted,
opened,
transitionProps,
withCloseButton,
withinPortal,
children,
onClose,
portalProps,
...others
} = props;
const getStyles = useStyles<DialogFactory>({
name: 'Dialog',
classes,
props,
className,
style,
classNames,
styles,
unstyled,
vars,
varsResolver,
});
return (
<Affix
zIndex={zIndex}
position={position}
ref={ref}
withinPortal={withinPortal}
portalProps={portalProps}
unstyled={unstyled}
>
<Transition keepMounted={keepMounted} mounted={opened} {...transitionProps}>
{(transitionStyles) => (
<Paper
unstyled={unstyled}
{...getStyles('root', { style: transitionStyles })}
{...others}
>
{withCloseButton && (
<CloseButton onClick={onClose} unstyled={unstyled} {...getStyles('closeButton')} />
)}
{children}
</Paper>
)}
</Transition>
</Affix>
);
});
Dialog.classes = classes;
Dialog.displayName = '@mantine/core/Dialog';