-
-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathDrawer.tsx
More file actions
148 lines (128 loc) · 3.81 KB
/
Drawer.tsx
File metadata and controls
148 lines (128 loc) · 3.81 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
import type { PortalProps } from '@rc-component/portal';
import Portal from '@rc-component/portal';
import useLayoutEffect from '@rc-component/util/lib/hooks/useLayoutEffect';
import * as React from 'react';
import { RefContext } from './context';
import type {
DrawerPanelAccessibility,
DrawerPanelEvents,
} from './DrawerPanel';
import type { DrawerPopupProps } from './DrawerPopup';
import DrawerPopup from './DrawerPopup';
import { warnCheck } from './util';
import type { DrawerClassNames, DrawerStyles } from './inter';
export type Placement = 'left' | 'top' | 'right' | 'bottom';
export interface DrawerProps
extends Omit<DrawerPopupProps, 'prefixCls' | 'inline' | 'scrollLocker'>,
DrawerPanelEvents,
DrawerPanelAccessibility {
prefixCls?: string;
open?: boolean;
onClose?: (e: React.MouseEvent | React.KeyboardEvent) => void;
destroyOnHidden?: boolean;
getContainer?: PortalProps['getContainer'];
panelRef?: React.Ref<HTMLDivElement>;
classNames?: DrawerClassNames;
styles?: DrawerStyles;
}
const Drawer: React.FC<DrawerProps> = props => {
const {
open = false,
prefixCls = 'rc-drawer',
placement = 'right' as Placement,
autoFocus = true,
keyboard = true,
width = 378,
mask = true,
maskClosable = true,
getContainer,
forceRender,
afterOpenChange,
destroyOnHidden,
onMouseEnter,
onMouseOver,
onMouseLeave,
onClick,
onKeyDown,
onKeyUp,
// Refs
panelRef,
} = props;
const [animatedVisible, setAnimatedVisible] = React.useState(false);
// ============================= Warn =============================
if (process.env.NODE_ENV !== 'production') {
warnCheck(props);
}
// ============================= Open =============================
const [mounted, setMounted] = React.useState(false);
useLayoutEffect(() => {
setMounted(true);
}, []);
const mergedOpen = mounted ? open : false;
// ============================ Focus =============================
const popupRef = React.useRef<HTMLDivElement>(null);
const lastActiveRef = React.useRef<HTMLElement>(null);
useLayoutEffect(() => {
if (mergedOpen) {
lastActiveRef.current = document.activeElement as HTMLElement;
}
}, [mergedOpen]);
// ============================= Open =============================
const internalAfterOpenChange: DrawerProps['afterOpenChange'] =
nextVisible => {
setAnimatedVisible(nextVisible);
afterOpenChange?.(nextVisible);
if (
!nextVisible &&
lastActiveRef.current &&
!popupRef.current?.contains(lastActiveRef.current)
) {
lastActiveRef.current?.focus({ preventScroll: true });
}
};
// =========================== Context ============================
const refContext = React.useMemo(() => ({ panel: panelRef }), [panelRef]);
// ============================ Render ============================
if (!forceRender && !animatedVisible && !mergedOpen && destroyOnHidden) {
return null;
}
const eventHandlers = {
onMouseEnter,
onMouseOver,
onMouseLeave,
onClick,
onKeyDown,
onKeyUp,
};
const drawerPopupProps = {
...props,
open: mergedOpen,
prefixCls,
placement,
autoFocus,
keyboard,
width,
mask,
maskClosable,
inline: getContainer === false,
afterOpenChange: internalAfterOpenChange,
ref: popupRef,
...eventHandlers,
};
return (
<RefContext.Provider value={refContext}>
<Portal
open={mergedOpen || forceRender || animatedVisible}
autoDestroy={false}
getContainer={getContainer}
autoLock={mergedOpen || animatedVisible}
>
<DrawerPopup {...drawerPopupProps} />
</Portal>
</RefContext.Provider>
);
};
if (process.env.NODE_ENV !== 'production') {
Drawer.displayName = 'Drawer';
}
export default Drawer;