-
-
Notifications
You must be signed in to change notification settings - Fork 208
Expand file tree
/
Copy pathPanel.tsx
More file actions
167 lines (151 loc) · 4.27 KB
/
Copy pathPanel.tsx
File metadata and controls
167 lines (151 loc) · 4.27 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
import { clsx } from 'clsx';
import { useComposeRef } from '@rc-component/util/lib/ref';
import { useLockFocus } from '@rc-component/util/lib/Dom/focus';
import React, { useMemo, useRef } from 'react';
import { RefContext } from '../../context';
import type { IDialogPropTypes } from '../../IDialogPropTypes';
import MemoChildren from './MemoChildren';
import pickAttrs from '@rc-component/util/lib/pickAttrs';
export interface PanelProps extends Omit<IDialogPropTypes, 'getOpenCount'> {
prefixCls: string;
ariaId?: string;
onMouseDown?: React.MouseEventHandler;
onMouseUp?: React.MouseEventHandler;
holderRef?: React.Ref<HTMLDivElement>;
/** Used for focus lock. When true and open, focus will lock into the panel */
isFixedPos?: boolean;
}
export type PanelRef = {
focus: () => void;
};
const Panel = React.forwardRef<PanelRef, PanelProps>((props, ref) => {
const {
prefixCls,
className,
style,
title,
ariaId,
footer,
closable,
closeIcon,
onClose,
children,
bodyStyle,
bodyProps,
modalRender,
onMouseDown,
onMouseUp,
holderRef,
visible,
forceRender,
width,
height,
classNames: modalClassNames,
styles: modalStyles,
isFixedPos,
focusTrap,
} = props;
// ================================= Refs =================================
const { panel: panelRef } = React.useContext(RefContext);
const internalRef = useRef<HTMLDivElement>(null);
const mergedRef = useComposeRef(holderRef, panelRef, internalRef);
useLockFocus(visible && isFixedPos && focusTrap !== false, () => internalRef.current);
React.useImperativeHandle(ref, () => ({
focus: () => {
internalRef.current?.focus({ preventScroll: true });
},
}));
// ================================ Style =================================
const contentStyle: React.CSSProperties = {};
if (width !== undefined) {
contentStyle.width = width;
}
if (height !== undefined) {
contentStyle.height = height;
}
// ================================ Render ================================
const footerNode = footer ? (
<div
className={clsx(`${prefixCls}-footer`, modalClassNames?.footer)}
style={{ ...modalStyles?.footer }}
>
{footer}
</div>
) : null;
const headerNode = title ? (
<div
className={clsx(`${prefixCls}-header`, modalClassNames?.header)}
style={{ ...modalStyles?.header }}
>
<div
className={clsx(`${prefixCls}-title`, modalClassNames?.title)}
id={ariaId}
style={{ ...modalStyles?.title }}
>
{title}
</div>
</div>
) : null;
const closableObj = useMemo(() => {
if (typeof closable === 'object' && closable !== null) {
return closable;
}
if (closable) {
return { closeIcon: closeIcon ?? <span className={`${prefixCls}-close-x`} /> };
}
return {};
}, [closable, closeIcon, prefixCls]);
const ariaProps = pickAttrs(closableObj, true);
const closeBtnIsDisabled = typeof closable === 'object' && closable.disabled;
const closerNode = closable ? (
<button
type="button"
onClick={onClose}
aria-label="Close"
{...ariaProps}
className={`${prefixCls}-close`}
disabled={closeBtnIsDisabled}
>
{closableObj.closeIcon}
</button>
) : null;
const content = (
<div
className={clsx(`${prefixCls}-container`, modalClassNames?.container)}
style={modalStyles?.container}
>
{closerNode}
{headerNode}
<div
className={clsx(`${prefixCls}-body`, modalClassNames?.body)}
style={{ ...bodyStyle, ...modalStyles?.body }}
{...bodyProps}
>
{children}
</div>
{footerNode}
</div>
);
return (
<div
key="dialog-element"
role="dialog"
aria-labelledby={title ? ariaId : null}
aria-modal="true"
ref={mergedRef}
style={{ ...style, ...contentStyle }}
className={clsx(prefixCls, className)}
onMouseDown={onMouseDown}
onMouseUp={onMouseUp}
tabIndex={-1}
>
<MemoChildren shouldUpdate={visible || forceRender}>
{modalRender ? modalRender(content) : content}
</MemoChildren>
</div>
);
});
if (process.env.NODE_ENV !== 'production') {
Panel.displayName = 'Panel';
}
export default Panel;