Close lightbox on escape key press
This commit is contained in:
parent
86da80fd23
commit
d797ede791
1 changed files with 18 additions and 0 deletions
|
@ -55,6 +55,16 @@ const IconButton = ({ onClick, type }: IconButtonProps) => (
|
|||
);
|
||||
|
||||
export class Lightbox extends React.Component<Props, {}> {
|
||||
public componentDidMount() {
|
||||
const useCapture = true;
|
||||
document.addEventListener('keyup', this.onKeyUp, useCapture);
|
||||
}
|
||||
|
||||
public componentWillMount() {
|
||||
const useCapture = true;
|
||||
document.removeEventListener('keyup', this.onKeyUp, useCapture);
|
||||
}
|
||||
|
||||
public render() {
|
||||
const { imageURL } = this.props;
|
||||
return (
|
||||
|
@ -69,4 +79,12 @@ export class Lightbox extends React.Component<Props, {}> {
|
|||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
private onKeyUp = (event: KeyboardEvent) => {
|
||||
if (event.key !== 'Escape') {
|
||||
return;
|
||||
}
|
||||
|
||||
this.props.close();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue