-
Notifications
You must be signed in to change notification settings - Fork 359
Expand file tree
/
Copy pathDialogContent.tsx
More file actions
202 lines (194 loc) · 6.25 KB
/
DialogContent.tsx
File metadata and controls
202 lines (194 loc) · 6.25 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import React, { cloneElement, CSSProperties, forwardRef, ReactElement, useCallback, useEffect, useRef } from "react";
import cx from "classnames";
import { camelCase } from "lodash-es";
import { CSSTransition } from "react-transition-group";
import { CSSTransitionProps } from "react-transition-group/CSSTransition";
import useClickOutside from "../../../hooks/useClickOutside";
import { chainFunctions, NOOP } from "../../../utils/function-utils";
import useKeyEvent from "../../../hooks/useKeyEvent";
import { VibeComponentProps } from "../../../types";
import { keyCodes } from "../../../constants";
import { DialogAnimationType, DialogTriggerEvent } from "../Dialog.types";
import * as PopperJS from "@popperjs/core";
import { getStyle } from "../../../helpers/typesciptCssModulesHelper";
import styles from "./DialogContent.module.scss";
import useDisableScroll from "../../../hooks/useDisableScroll";
const EMPTY_OBJECT = {};
const ESCAPE_KEYS = [keyCodes.ESCAPE];
export interface DialogContentProps extends VibeComponentProps {
/**
* The content inside the dialog.
*/
children?: ReactElement | ReactElement[];
/**
* The placement of the dialog relative to the reference element.
*/
position?: PopperJS.Placement;
/**
* Class name applied to the dialog wrapper.
*/
wrapperClassName?: string;
/**
* If true, the dialog is open.
*/
isOpen?: boolean;
// TODO: [breaking] use type
/**
* The starting edge of the dialog.
*/
startingEdge?: any;
/**
* The animation type used for showing/hiding the dialog.
*/
animationType?: DialogAnimationType;
/**
* Callback fired when the Escape key is pressed.
*/
onEsc?: (event: React.KeyboardEvent) => void;
/**
* Callback fired when the mouse enters the dialog.
*/
onMouseEnter?: (event: React.MouseEvent) => void;
/**
* Callback fired when the mouse leaves the dialog.
*/
onMouseLeave?: (event: React.MouseEvent) => void;
/**
* Callback fired when clicking outside the dialog.
*/
onClickOutside?: (event: React.MouseEvent, hideShowEvent: DialogTriggerEvent) => void;
/**
* Callback fired when clicking inside the dialog.
*/
onClick?: (event: React.MouseEvent) => void;
/**
* Delay before showing the dialog.
*/
showDelay?: number;
/**
* Inline styles applied to the dialog.
*/
styleObject?: CSSProperties;
/**
* If true, hides the reference element when the dialog is shown.
*/
isReferenceHidden?: boolean;
/**
* If true, applies tooltip arrow to the dialog.
*/
hasTooltip?: boolean;
/**
* The CSS selector of the container where the dialog should be rendered.
*/
containerSelector?: string;
/**
* If true, disables scrolling in the container when the dialog is open.
*/
disableContainerScroll?: boolean | string;
/**
* Callback fired when the context menu (right-click) event occurs outside the dialog.
*/
onContextMenu?: (e: React.MouseEvent) => void;
}
const DialogContent = forwardRef(
(
{
onEsc = NOOP,
children,
position,
wrapperClassName,
isOpen = false,
startingEdge,
animationType = "expand",
onMouseEnter = NOOP,
onMouseLeave = NOOP,
onClickOutside = NOOP,
onClick = NOOP,
onContextMenu = NOOP,
showDelay,
styleObject = EMPTY_OBJECT,
isReferenceHidden,
hasTooltip = false,
containerSelector,
disableContainerScroll = false,
"data-testid": dataTestId
}: DialogContentProps,
forwardRef: React.ForwardedRef<HTMLElement>
) => {
const clickOutsideRef = useRef(null);
const onOutSideClick = useCallback(
(event: React.MouseEvent) => {
if (isOpen) {
return onClickOutside(event, "clickoutside");
}
},
[isOpen, onClickOutside]
);
const overrideOnContextMenu = useCallback(
(event: React.MouseEvent) => {
if (isOpen) {
onContextMenu(event);
}
},
[isOpen, onContextMenu]
);
useKeyEvent({ keys: ESCAPE_KEYS, callback: onEsc });
useClickOutside({ callback: onOutSideClick, ref: clickOutsideRef });
useClickOutside({ eventName: "contextmenu", callback: overrideOnContextMenu, ref: clickOutsideRef });
const selectorToDisable = typeof disableContainerScroll === "string" ? disableContainerScroll : containerSelector;
const { disableScroll, enableScroll } = useDisableScroll(selectorToDisable);
useEffect(() => {
if (disableContainerScroll) {
if (isOpen) {
disableScroll();
} else {
enableScroll();
}
}
}, [disableContainerScroll, disableScroll, enableScroll, isOpen]);
const transitionOptions: Partial<CSSTransitionProps> = { classNames: undefined };
switch (animationType) {
case "expand":
transitionOptions.classNames = {
appear: styles.expandAppear,
appearActive: styles.expandAppearActive,
exit: styles.expandExit
};
break;
case "opacity-and-slide":
transitionOptions.classNames = {
appear: styles.opacitySlideAppear,
appearActive: styles.opacitySlideAppearActive
};
break;
}
return (
<span
className={cx("monday-style-dialog-content-wrapper", styles.contentWrapper, wrapperClassName)}
ref={forwardRef}
data-testid={dataTestId}
style={styleObject}
onClickCapture={onClick}
data-popper-reference-hidden={isReferenceHidden}
>
<CSSTransition {...transitionOptions} in={isOpen} appear={!!animationType} timeout={showDelay}>
<div
className={cx(styles.contentComponent, getStyle(styles, camelCase(position)), {
[getStyle(styles, camelCase("edge-" + startingEdge))]: startingEdge,
[styles.hasTooltip]: hasTooltip
})}
ref={clickOutsideRef}
>
{React.Children.toArray(children).map((child: ReactElement) => {
return cloneElement(child, {
onMouseEnter: chainFunctions([child.props.onMouseEnter, onMouseEnter]),
onMouseLeave: chainFunctions([child.props.onMouseLeave, onMouseLeave])
});
})}
</div>
</CSSTransition>
</span>
);
}
);
export default DialogContent;