Skip to content
This repository was archived by the owner on May 20, 2023. It is now read-only.

Commit f71f1bf

Browse files
Googlernshahan
authored andcommitted
Add GlobalEscapeDirective to utils
PiperOrigin-RevId: 189299517
1 parent d381ff9 commit f71f1bf

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'dart:async';
6+
import 'dart:html';
7+
8+
import 'package:angular/angular.dart';
9+
10+
/// Directive to listen to the escape key globally.
11+
///
12+
/// Useful for things like having a dialog close when the escape key is pressed
13+
/// even when the dialog is not focused.
14+
///
15+
/// WARNING: If listeners to the onEscape stream are on the page but hidden,
16+
/// they will still be registered to the stream. To avoid that situation, use
17+
/// this directive only within a deferredContent directive.
18+
@Directive(selector: '[globalEscape]')
19+
class GlobalEscapeDirective {
20+
final Window _window;
21+
22+
/// Event triggered when the escape key is pressed.
23+
@Output()
24+
Stream<KeyboardEvent> get globalEscape =>
25+
_window.onKeyUp.where((event) => event.keyCode == KeyCode.ESC);
26+
27+
GlobalEscapeDirective(this._window);
28+
}

0 commit comments

Comments
 (0)