|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2025 Google LLC |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +/** |
| 8 | + * The KeyboardNavigationController handles coordinating Blockly-wide |
| 9 | + * keyboard navigation behavior, such as enabling/disabling full |
| 10 | + * cursor visualization. |
| 11 | + */ |
| 12 | +export class KeyboardNavigationController { |
| 13 | + /** Whether the user is actively using keyboard navigation. */ |
| 14 | + private isActive = false; |
| 15 | + /** Css class name added to body if keyboard nav is active. */ |
| 16 | + private activeClassName = 'blocklyKeyboardNavigation'; |
| 17 | + |
| 18 | + /** |
| 19 | + * Sets whether we think the user is actively using keyboard navigation. |
| 20 | + * |
| 21 | + * If they are, we'll apply a css class to the entire page so that |
| 22 | + * focused items can apply additional styling for keyboard users. |
| 23 | + * |
| 24 | + * @param isUsing |
| 25 | + */ |
| 26 | + setIsActive(isUsing: boolean = true) { |
| 27 | + this.isActive = isUsing; |
| 28 | + this.updateActiveVisualization(); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * @returns true if we think the user is actively using keyboard navigation |
| 33 | + * (e.g., has recently taken some action that is only relevant to keyboard users) |
| 34 | + */ |
| 35 | + getIsActive(): boolean { |
| 36 | + return this.isActive; |
| 37 | + } |
| 38 | + |
| 39 | + /** Adds or removes the css class that indicates keyboard navigation is active. */ |
| 40 | + private updateActiveVisualization() { |
| 41 | + if (this.isActive) { |
| 42 | + document.body.classList.add(this.activeClassName); |
| 43 | + } else { |
| 44 | + document.body.classList.remove(this.activeClassName); |
| 45 | + } |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +/** Singleton instance of the keyboard navigation controller. */ |
| 50 | +export const keyboardNavigationController = new KeyboardNavigationController(); |
0 commit comments