Skip to content

Commit b1435ab

Browse files
committed
chore: refactor pos
1 parent 1e91a7e commit b1435ab

File tree

4 files changed

+12
-26
lines changed

4 files changed

+12
-26
lines changed

docs/examples/motion.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export const maskMotion: DrawerProps['maskMotion'] = {
1010
export const motion: DrawerProps['motion'] = placement => ({
1111
motionAppear: true,
1212
motionName: `panel-motion-${placement}`,
13+
motionDeadline: 1000,
1314
});
1415

1516
const motionProps: Partial<DrawerProps> = {

src/Drawer.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export interface DrawerProps
4949
onResizeStart?: () => void;
5050
onResizeEnd?: () => void;
5151
};
52+
focusTriggerAfterClose?: boolean;
5253
}
5354

5455
const Drawer: React.FC<DrawerProps> = props => {
@@ -77,6 +78,7 @@ const Drawer: React.FC<DrawerProps> = props => {
7778
onClose,
7879
resizable,
7980
defaultSize,
81+
focusTriggerAfterClose,
8082

8183
// Refs
8284
panelRef,
@@ -116,6 +118,7 @@ const Drawer: React.FC<DrawerProps> = props => {
116118

117119
if (
118120
!nextVisible &&
121+
focusTriggerAfterClose !== false &&
119122
lastActiveRef.current &&
120123
!popupRef.current?.contains(lastActiveRef.current)
121124
) {

src/DrawerPopup.tsx

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ export interface PushConfig {
2424

2525
export interface FocusableConfig {
2626
autoFocus?: boolean;
27-
focusTriggerAfterClose?: boolean;
28-
trap?: boolean;
2927
}
3028

3129
export interface DrawerPopupProps
@@ -39,7 +37,7 @@ export interface DrawerPopupProps
3937

4038
// Focus
4139
autoFocus?: boolean;
42-
focusable?: FocusableConfig;
40+
focusTrap?: boolean;
4341

4442
// Root
4543
rootClassName?: string;
@@ -108,7 +106,7 @@ const DrawerPopup: React.ForwardRefRenderFunction<
108106

109107
// Focus
110108
autoFocus,
111-
focusable,
109+
focusTrap,
112110

113111
// classNames
114112
classNames: drawerClassNames,
@@ -157,7 +155,7 @@ const DrawerPopup: React.ForwardRefRenderFunction<
157155
React.useImperativeHandle(ref, () => panelRef.current);
158156

159157
// ========================= Focusable ==========================
160-
useFocusable(() => panelRef.current, open, focusable, autoFocus, mask);
158+
useFocusable(() => panelRef.current, open, autoFocus, focusTrap, mask);
161159

162160
// ============================ Push ============================
163161
const [pushed, setPushed] = React.useState(false);
@@ -321,9 +319,7 @@ const DrawerPopup: React.ForwardRefRenderFunction<
321319
{...motionProps}
322320
visible={open}
323321
forceRender={forceRender}
324-
onVisibleChanged={nextVisible => {
325-
afterOpenChange?.(nextVisible);
326-
}}
322+
onVisibleChanged={afterOpenChange}
327323
removeOnLeave={false}
328324
leavedClassName={`${prefixCls}-content-wrapper-hidden`}
329325
>

src/hooks/useFocusable.ts

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,21 @@
11
import React from 'react';
2-
import type { FocusableConfig } from '../DrawerPopup';
32
import { useLockFocus } from '@rc-component/util/lib/Dom/focus';
43

54
export default function useFocusable(
65
getContainer: () => HTMLElement,
76
open: boolean,
8-
focusable?: FocusableConfig,
97
autoFocus?: boolean,
8+
focusTrap?: boolean,
109
mask?: boolean,
1110
) {
12-
// Parse config
13-
const config = React.useMemo<FocusableConfig>(() => {
14-
const merged: FocusableConfig = {
15-
trap: mask !== false,
16-
focusTriggerAfterClose: true,
17-
...focusable,
18-
};
19-
20-
if (autoFocus !== undefined) {
21-
merged.autoFocus = autoFocus;
22-
}
23-
24-
return merged;
25-
}, [focusable, autoFocus, mask]);
11+
const mergedFocusTrap = focusTrap ?? mask !== false;
2612

2713
// Focus lock
28-
useLockFocus(open && config.trap, getContainer);
14+
useLockFocus(open && mergedFocusTrap, getContainer);
2915

3016
// Auto Focus
3117
React.useEffect(() => {
32-
if (open && config.autoFocus === true) {
18+
if (open && autoFocus === true) {
3319
getContainer()?.focus({ preventScroll: true });
3420
}
3521
}, [open]);

0 commit comments

Comments
 (0)