Skip to content

Commit be6ad3e

Browse files
committed
feat: add keyboard navigation controller
1 parent c6e58c4 commit be6ad3e

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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 registering shortcut handlers
10+
* or enabling/disabling full cursor visualization.
11+
*/
12+
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 = 'blocklyActiveKeyboardNav';
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

Comments
 (0)