UNPKG

2.54 kBPlain TextView Raw
1/*
2 * Copyright 2016 Palantir Technologies, Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17const TAB_KEY_CODE = 9;
18
19/* istanbul ignore next */
20
21/**
22 * A nifty little class that maintains event handlers to add a class to the container element
23 * when entering "mouse mode" (on a `mousedown` event) and remove it when entering "keyboard mode"
24 * (on a `tab` key `keydown` event).
25 */
26export class InteractionModeEngine {
27 private isRunning = false;
28
29 constructor(private container: Element, private className: string) {}
30
31 /** Returns whether the engine is currently running. */
32 public isActive() {
33 return this.isRunning;
34 }
35
36 /** Enable behavior which applies the given className when in mouse mode. */
37 public start() {
38 this.container.addEventListener("mousedown", this.handleMouseDown);
39 this.isRunning = true;
40 }
41
42 /** Disable interaction mode behavior and remove className from container. */
43 public stop() {
44 this.reset();
45 this.isRunning = false;
46 }
47
48 private reset() {
49 this.container.classList.remove(this.className);
50 // HACKHACK: see https://github.com/palantir/blueprint/issues/4342
51 this.container.removeEventListener("keydown", this.handleKeyDown as EventListener);
52 this.container.removeEventListener("mousedown", this.handleMouseDown);
53 }
54
55 private handleKeyDown = (e: KeyboardEvent) => {
56 // HACKHACK: https://github.com/palantir/blueprint/issues/4165
57 // eslint-disable-next-line deprecation/deprecation
58 if (e.which === TAB_KEY_CODE) {
59 this.reset();
60 this.container.addEventListener("mousedown", this.handleMouseDown);
61 }
62 };
63
64 private handleMouseDown = () => {
65 this.reset();
66 this.container.classList.add(this.className);
67 // HACKHACK: see https://github.com/palantir/blueprint/issues/4342
68 this.container.addEventListener("keydown", this.handleKeyDown as EventListener);
69 };
70}