Skip to content

Commit

Permalink
fix: drawer order esc (#396)
Browse files Browse the repository at this point in the history
  • Loading branch information
zombieJ authored Feb 16, 2023
1 parent f356b79 commit 52084c2
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
7 changes: 5 additions & 2 deletions docs/examples/multiple.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ class Demo extends React.Component {
openChild: !this.state.openChild,
});
};
public onChildrenClick = () => {
public onChildrenClick = e => {
this.setState({
openChildren: !this.state.openChildren,
openChildren: e.currentTarget instanceof HTMLButtonElement,
});
};

Expand All @@ -55,6 +55,7 @@ class Demo extends React.Component {
className="drawer1"
placement="right"
push={{ distance: 64 }}
rootClassName="level-0"
// zIndex={99999}
{...motionProps}
>
Expand All @@ -66,6 +67,7 @@ class Demo extends React.Component {
className="drawer2"
placement="right"
// zIndex={88888}
rootClassName="level-1"
{...motionProps}
>
<div style={{ width: 200 }}>
Expand All @@ -75,6 +77,7 @@ class Demo extends React.Component {
open={this.state.openChildren}
onClose={this.onChildrenClick}
placement="right"
rootClassName="level-2"
{...motionProps}
>
<div style={{ width: 200 }}>三级抽屉</div>
Expand Down
20 changes: 20 additions & 0 deletions src/Drawer.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as React from 'react';
import Portal from '@rc-component/portal';
import type { PortalProps } from '@rc-component/portal';
import useLayoutEffect from 'rc-util/lib/hooks/useLayoutEffect';
import DrawerPopup from './DrawerPopup';
import type { DrawerPopupProps } from './DrawerPopup';
import { warnCheck } from './util';
Expand Down Expand Up @@ -39,11 +40,29 @@ const Drawer: React.FC<DrawerProps> = props => {
warnCheck(props);
}

// ============================ Focus =============================
const panelRef = React.useRef<HTMLDivElement>();

const lastActiveRef = React.useRef<HTMLElement>();
useLayoutEffect(() => {
if (open) {
lastActiveRef.current = document.activeElement as HTMLElement;
}
}, [open]);

// ============================= Open =============================
const internalAfterOpenChange: DrawerProps['afterOpenChange'] =
nextVisible => {
setAnimatedVisible(nextVisible);
afterOpenChange?.(nextVisible);

if (
!nextVisible &&
lastActiveRef.current &&
!panelRef.current?.contains(lastActiveRef.current)
) {
lastActiveRef.current?.focus();
}
};

// ============================ Render ============================
Expand All @@ -63,6 +82,7 @@ const Drawer: React.FC<DrawerProps> = props => {
maskClosable,
inline: getContainer === false,
afterOpenChange: internalAfterOpenChange,
ref: panelRef,
};

return (
Expand Down
16 changes: 13 additions & 3 deletions src/DrawerPopup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import classNames from 'classnames';
import CSSMotion from 'rc-motion';
import type { CSSMotionProps } from 'rc-motion';
import DrawerPanel from './DrawerPanel';
// import type ScrollLocker from 'rc-util/lib/Dom/scrollLocker';
import DrawerContext from './context';
import type { DrawerContextProps } from './context';
import KeyCode from 'rc-util/lib/KeyCode';
Expand Down Expand Up @@ -63,7 +62,7 @@ export interface DrawerPopupProps {
) => void;
}

export default function DrawerPopup(props: DrawerPopupProps) {
function DrawerPopup(props: DrawerPopupProps, ref: React.Ref<HTMLDivElement>) {
const {
prefixCls,
open,
Expand Down Expand Up @@ -105,6 +104,8 @@ export default function DrawerPopup(props: DrawerPopupProps) {
const sentinelStartRef = React.useRef<HTMLDivElement>();
const sentinelEndRef = React.useRef<HTMLDivElement>();

React.useImperativeHandle(ref, () => panelRef.current);

const onPanelKeyDown: React.KeyboardEventHandler<HTMLDivElement> = event => {
const { keyCode, shiftKey } = event;

Expand All @@ -127,6 +128,7 @@ export default function DrawerPopup(props: DrawerPopupProps) {
// Close
case KeyCode.ESC: {
if (onClose && keyboard) {
event.stopPropagation();
onClose(event);
}
break;
Expand All @@ -140,7 +142,7 @@ export default function DrawerPopup(props: DrawerPopupProps) {
if (open && autoFocus) {
panelRef.current?.focus({ preventScroll: true });
}
}, [open, autoFocus]);
}, [open]);

// ============================ Push ============================
const [pushed, setPushed] = React.useState(false);
Expand Down Expand Up @@ -332,3 +334,11 @@ export default function DrawerPopup(props: DrawerPopupProps) {
</DrawerContext.Provider>
);
}

const RefDrawerPopup = React.forwardRef(DrawerPopup);

if (process.env.NODE_ENV !== 'production') {
RefDrawerPopup.displayName = 'DrawerPopup';
}

export default RefDrawerPopup;

0 comments on commit 52084c2

Please sign in to comment.