| 24 | * (on a `tab` key `keydown` event). |
| 25 | */ |
| 26 | export 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 | this.container.removeEventListener("keydown", this.handleKeyDown); |
| 51 | this.container.removeEventListener("mousedown", this.handleMouseDown); |
| 52 | } |
| 53 | |
| 54 | private handleKeyDown = (e: KeyboardEvent) => { |
| 55 | // HACKHACK: https://github.com/palantir/blueprint/issues/4165 |
| 56 | // eslint-disable-next-line deprecation/deprecation |
| 57 | if (e.which === TAB_KEY_CODE) { |
| 58 | this.reset(); |
| 59 | this.container.addEventListener("mousedown", this.handleMouseDown); |
| 60 | } |
| 61 | }; |
| 62 | |
| 63 | private handleMouseDown = () => { |
| 64 | this.reset(); |
| 65 | this.container.classList.add(this.className); |
| 66 | this.container.addEventListener("keydown", this.handleKeyDown); |
| 67 | }; |
| 68 | } |