Skip to content

Commit 316780e

Browse files
committed
feat: add keyboard navigation controller
1 parent d2c4016 commit 316780e

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

core/blockly.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,10 @@ import {IVariableModel, IVariableState} from './interfaces/i_variable_model.js';
173173
import * as internalConstants from './internal_constants.js';
174174
import {LineCursor} from './keyboard_nav/line_cursor.js';
175175
import {Marker} from './keyboard_nav/marker.js';
176+
import {
177+
KeyboardNavigationController,
178+
keyboardNavigationController,
179+
} from './keyboard_navigation_controller.js';
176180
import type {LayerManager} from './layer_manager.js';
177181
import * as layers from './layers.js';
178182
import {MarkerManager} from './marker_manager.js';
@@ -580,6 +584,7 @@ export {
580584
ImageProperties,
581585
Input,
582586
InsertionMarkerPreviewer,
587+
KeyboardNavigationController,
583588
LabelFlyoutInflater,
584589
LayerManager,
585590
Marker,
@@ -631,6 +636,7 @@ export {
631636
isSelectable,
632637
isSerializable,
633638
isVariableBackedParameterModel,
639+
keyboardNavigationController,
634640
layers,
635641
renderManagement,
636642
serialization,

core/gesture.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import {IDraggable, isDraggable} from './interfaces/i_draggable.js';
3131
import {IDragger} from './interfaces/i_dragger.js';
3232
import type {IFlyout} from './interfaces/i_flyout.js';
3333
import type {IIcon} from './interfaces/i_icon.js';
34+
import {keyboardNavigationController} from './keyboard_navigation_controller.js';
3435
import * as registry from './registry.js';
3536
import * as Tooltip from './tooltip.js';
3637
import * as Touch from './touch.js';
@@ -541,8 +542,10 @@ export class Gesture {
541542
// have higher priority than workspaces. The ordering within drags does
542543
// not matter, because the three types of dragging are exclusive.
543544
if (this.dragger) {
545+
keyboardNavigationController.setIsActive(false);
544546
this.dragger.onDragEnd(e, this.currentDragDeltaXY);
545547
} else if (this.workspaceDragger) {
548+
keyboardNavigationController.setIsActive(false);
546549
this.workspaceDragger.endDrag(this.currentDragDeltaXY);
547550
} else if (this.isBubbleClick()) {
548551
// Do nothing, bubbles don't currently respond to clicks.
@@ -743,6 +746,8 @@ export class Gesture {
743746
e.preventDefault();
744747
e.stopPropagation();
745748

749+
keyboardNavigationController.setIsActive(false);
750+
746751
this.dispose();
747752
}
748753

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 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

Comments
 (0)