Skip to content

Commit

Permalink
[core] feat(Drawer): return focus to active element after closing (#4422
Browse files Browse the repository at this point in the history
)

Fixes #3592
  • Loading branch information
adidahiya authored Nov 17, 2020
1 parent 28d5bca commit 241b20d
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion packages/core/src/components/drawer/drawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ export class Drawer extends AbstractPureComponent2<IDrawerProps> {
public static readonly SIZE_STANDARD = "50%";
public static readonly SIZE_LARGE = "90%";

private lastActiveElementBeforeOpened: Element | null | undefined;

public render() {
// eslint-disable-next-line deprecation/deprecation
const { size, style, position, vertical } = this.props;
Expand All @@ -130,7 +132,12 @@ export class Drawer extends AbstractPureComponent2<IDrawerProps> {
[(realPosition ? isPositionHorizontal(realPosition) : vertical) ? "height" : "width"]: size,
};
return (
<Overlay {...this.props} className={Classes.OVERLAY_CONTAINER}>
<Overlay
{...this.props}
className={Classes.OVERLAY_CONTAINER}
onOpening={this.handleOpening}
onClosed={this.handleClosed}
>
<div className={classes} style={styleProp}>
{this.maybeRenderHeader()}
{this.props.children}
Expand Down Expand Up @@ -190,4 +197,16 @@ export class Drawer extends AbstractPureComponent2<IDrawerProps> {
</div>
);
}

private handleOpening = (node: HTMLElement) => {
this.lastActiveElementBeforeOpened = document.activeElement;
this.props.onOpening?.(node);
};

private handleClosed = (node: HTMLElement) => {
if (this.lastActiveElementBeforeOpened instanceof HTMLElement) {
this.lastActiveElementBeforeOpened.focus();
}
this.props.onClosed?.(node);
};
}

1 comment on commit 241b20d

@blueprint-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[core] feat(Drawer): return focus to active element after closing (#4422)

Previews: documentation | landing | table

Please sign in to comment.