File size: 1,189 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# Close on Escape

Allows for an ordered stack of components that carry out an action when the <kbd>esc</kbd> key is pressed. Each instance will carry out their `onEscape` action before being removed from the stack on a first-in-last-out basis.

This is useful for components such as `<Dialog />` and `<Popover />`, particularly in flows that see multiple `Dialog`s that 'stack'.

## Usage

To use this component, either nest it inside of an existing component or as a sibling, passing it a function as `onEscape` to be called when <kbd>esc</kbd> is pressed.

```jsx
import CloseOnEscape from 'calypso/components/close-on-escape';

function closeDialog() {
	// Take care of closing this component
}

function render() {
	// Nested:

	return (
		<Dialog>
			<CloseOnEscape onEscape={ this.closeDialog } />
		</Dialog>
	);

	// Or as a sibling:
	/*
	return (
		<div>
			<CloseOnEscape onEscape={ this.closeDialog } />
			<Dialog />
		</div>
	);
	*/
}
```

## Props

### `onEscape`

<table>
	<tr><td>Type</td><td>function</td></tr>
	<tr><td>Required</td><td>No</td></tr>
	<tr><td>Default</td><td><code>noop</code></td></tr>
</table>

The function to be called when <kbd>esc</kbd> is pressed.