Skip to content

Commit 06fd98d

Browse files
committed
chore: esc
1 parent 8eb6531 commit 06fd98d

File tree

6 files changed

+50
-129
lines changed

6 files changed

+50
-129
lines changed

docs/demo/simple.md

Lines changed: 0 additions & 3 deletions
This file was deleted.

docs/examples/assets/motion.less

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@duration: 3s;
1+
@duration: 0.3s;
22

33
.mask-motion {
44
&-enter,

docs/examples/forceRender.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ export default () => {
2424
forceRender
2525
{...motionProps}
2626
>
27-
Hello World!
27+
<input />
28+
<input />
29+
<input />
2830
</Drawer>
2931
<Drawer
3032
width="20vw"

docs/examples/simple.tsx

Lines changed: 0 additions & 104 deletions
This file was deleted.

src/DrawerPanel.tsx

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export interface DrawerPanelProps {
1414
width?: number | string;
1515
placement: Placement;
1616
children?: React.ReactNode;
17+
onClose?: React.KeyboardEventHandler<HTMLElement>;
1718
}
1819

1920
const sentinelStyle: React.CSSProperties = {
@@ -26,7 +27,8 @@ const sentinelStyle: React.CSSProperties = {
2627

2728
const DrawerPanel = React.forwardRef<DrawerPanelRef, DrawerPanelProps>(
2829
(props, ref) => {
29-
const { prefixCls, className, style, placement, width, children } = props;
30+
const { prefixCls, className, style, placement, width, children, onClose } =
31+
props;
3032

3133
// ================================ Refs ================================
3234
const panelRef = React.useRef<HTMLDivElement>();
@@ -41,20 +43,34 @@ const DrawerPanel = React.forwardRef<DrawerPanelRef, DrawerPanelProps>(
4143
},
4244
}));
4345

44-
const onPanelKeyDown: React.KeyboardEventHandler<HTMLDivElement> = ({
45-
keyCode,
46-
shiftKey,
47-
}) => {
46+
const onPanelKeyDown: React.KeyboardEventHandler<
47+
HTMLDivElement
48+
> = event => {
49+
const { keyCode, shiftKey } = event;
50+
4851
switch (keyCode) {
52+
// Tab active
4953
case KeyCode.TAB: {
50-
if (!shiftKey && document.activeElement === sentinelEndRef.current) {
51-
sentinelStartRef.current?.focus({ preventScroll: true });
52-
} else if (
53-
shiftKey &&
54-
document.activeElement === sentinelStartRef.current
55-
) {
56-
sentinelEndRef.current?.focus({ preventScroll: true });
54+
if (keyCode === KeyCode.TAB) {
55+
if (
56+
!shiftKey &&
57+
document.activeElement === sentinelEndRef.current
58+
) {
59+
sentinelStartRef.current?.focus({ preventScroll: true });
60+
} else if (
61+
shiftKey &&
62+
document.activeElement === sentinelStartRef.current
63+
) {
64+
sentinelEndRef.current?.focus({ preventScroll: true });
65+
}
5766
}
67+
break;
68+
}
69+
70+
// Close
71+
case KeyCode.ESC: {
72+
onClose(event);
73+
break;
5874
}
5975
}
6076
};

src/DrawerPopup.tsx

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ export interface DrawerPopupProps {
4444
maskMotion?: CSSMotionProps;
4545

4646
// Events
47-
onClose?: React.MouseEventHandler<HTMLElement>;
47+
onClose?: (
48+
event: React.MouseEvent<HTMLElement> | React.KeyboardEvent<HTMLElement>,
49+
) => void;
4850
}
4951

5052
export default function DrawerPopup(props: DrawerPopupProps) {
@@ -115,13 +117,6 @@ export default function DrawerPopup(props: DrawerPopupProps) {
115117
}
116118
}, [open]);
117119

118-
// Auto Focus
119-
React.useEffect(() => {
120-
if (open && autoFocus) {
121-
panelRef.current?.focus();
122-
}
123-
}, [open, autoFocus]);
124-
125120
React.useEffect(
126121
() => () => {
127122
scrollLocker?.unLock();
@@ -130,6 +125,20 @@ export default function DrawerPopup(props: DrawerPopupProps) {
130125
[],
131126
);
132127

128+
// ========================== Control ===========================
129+
// Auto Focus
130+
React.useEffect(() => {
131+
if (open && autoFocus) {
132+
panelRef.current?.focus();
133+
}
134+
}, [open, autoFocus]);
135+
136+
const onPanelClose: React.KeyboardEventHandler<HTMLDivElement> = event => {
137+
if (onClose) {
138+
onClose(event);
139+
}
140+
};
141+
133142
// ============================ Mask ============================
134143
const maskNode: React.ReactNode = mask && (
135144
<CSSMotion key="mask" {...maskMotion} visible={open}>
@@ -188,6 +197,7 @@ export default function DrawerPopup(props: DrawerPopupProps) {
188197
}}
189198
width={width}
190199
placement={placement}
200+
onClose={onPanelClose}
191201
>
192202
{children}
193203
</DrawerPanel>

0 commit comments

Comments
 (0)