-
Notifications
You must be signed in to change notification settings - Fork 183
/
Panel.tsx
163 lines (144 loc) · 4.53 KB
/
Panel.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import classNames from 'classnames';
import { useComposeRef } from 'rc-util/lib/ref';
import React, { useMemo, useRef } from 'react';
import { RefContext } from '../../context';
import type { IDialogPropTypes } from '../../IDialogPropTypes';
import MemoChildren from './MemoChildren';
import pickAttrs from 'rc-util/lib/pickAttrs';
const sentinelStyle = { width: 0, height: 0, overflow: 'hidden', outline: 'none' };
const entityStyle = { outline: 'none' };
export interface PanelProps extends Omit<IDialogPropTypes, 'getOpenCount'> {
prefixCls: string;
ariaId?: string;
onMouseDown?: React.MouseEventHandler;
onMouseUp?: React.MouseEventHandler;
holderRef?: React.Ref<HTMLDivElement>;
}
export type ContentRef = {
focus: () => void;
changeActive: (next: boolean) => void;
};
const Panel = React.forwardRef<ContentRef, 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,
} = props;
// ================================= Refs =================================
const { panel: panelRef } = React.useContext(RefContext);
const mergedRef = useComposeRef(holderRef, panelRef);
const sentinelStartRef = useRef<HTMLDivElement>();
const sentinelEndRef = useRef<HTMLDivElement>();
const entityRef = useRef<HTMLDivElement>();
React.useImperativeHandle(ref, () => ({
focus: () => {
entityRef.current?.focus();
},
changeActive: (next) => {
const { activeElement } = document;
if (next && activeElement === sentinelEndRef.current) {
sentinelStartRef.current.focus();
} else if (!next && activeElement === sentinelStartRef.current) {
sentinelEndRef.current.focus();
}
},
}));
// ================================ Style =================================
const contentStyle: React.CSSProperties = {};
if (width !== undefined) {
contentStyle.width = width;
}
if (height !== undefined) {
contentStyle.height = height;
}
// ================================ Render ================================
let footerNode: React.ReactNode;
if (footer) {
footerNode = <div className={classNames(`${prefixCls}-footer`, modalClassNames?.footer)} style={{...modalStyles?.footer}}>{footer}</div>;
}
let headerNode: React.ReactNode;
if (title) {
headerNode = (
<div className={classNames(`${prefixCls}-header`, modalClassNames?.header)} style={{...modalStyles?.header}}>
<div className={`${prefixCls}-title`} id={ariaId}>
{title}
</div>
</div>
);
}
const closableObj = useMemo(() => {
if (typeof closable === 'object' && closable !== null) {
return closable;
}
if (closable) {
return { closeIcon: closeIcon ?? <span className={`${prefixCls}-close-x`} /> };
}
return {};
}, [closable, closeIcon]);
const ariaProps = pickAttrs(closableObj, true);
let closer: React.ReactNode;
if (closable) {
closer = (
<button type="button" onClick={onClose} aria-label="Close" {...ariaProps} className={`${prefixCls}-close`}>
{closableObj.closeIcon}
</button>
);
}
const content = (
<div className={classNames(`${prefixCls}-content`, modalClassNames?.content)} style={modalStyles?.content}>
{closer}
{headerNode}
<div className={classNames(`${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={classNames(prefixCls, className)}
onMouseDown={onMouseDown}
onMouseUp={onMouseUp}
>
<div tabIndex={0} ref={sentinelStartRef} style={sentinelStyle} aria-hidden="true" />
<div ref={entityRef} tabIndex={-1} style={entityStyle}>
<MemoChildren shouldUpdate={visible || forceRender}>
{modalRender ? modalRender(content) : content}
</MemoChildren>
</div>
<div tabIndex={0} ref={sentinelEndRef} style={sentinelStyle} aria-hidden="true" />
</div>
);
});
if (process.env.NODE_ENV !== 'production') {
Panel.displayName = 'Panel';
}
export default Panel;