Files
blockly/core/toolbox/toolbox_item.ts
Ben Henning 5bc83808bf feat: Make toolbox and flyout focusable (#8920)
## The basics

- [x] I [validated my changes](https://developers.google.com/blockly/guides/contribute/core#making_and_verifying_a_change)

## The details
### Resolves

Fixes #8918
Fixes #8919
Fixes part of #8771

### Proposed Changes

This updates several classes in order to make toolboxes and flyouts focusable:
- `IFlyout` is now an `IFocusableTree` with corresponding implementations in `FlyoutBase`.
- `IToolbox` is now an `IFocusableTree` with corresponding implementations in `Toolbox`.
- `IToolboxItem` is now an `IFocusableNode` with corresponding implementations in `ToolboxItem`.
- As the primary toolbox items, `ToolboxCategory` and `ToolboxSeparator` were updated to have -1 tab indexes and defined IDs to help `ToolboxItem` fulfill its contracted for `IFocusableNode.getFocusableElement`.

Each of these two new focusable trees have specific noteworthy behaviors behaviors:
- `Toolbox` will automatically indicate that its first item should be focused (if one is present), even overriding the ability to focus the toolbox's root (however there are some cases where that can still happen).
- `Toolbox` will automatically synchronize its selection state with its item nodes being focused.
- `FlyoutBase`, now being a focusable tree, has had a tab index of 0 added. Normally a tab index of -1 is all that's needed, but the keyboard navigation plugin specifically uses 0 for flyout so that the flyout is tabbable. This is a **new** tab stop being introduced.
- `FlyoutBase` holds a workspace (for rendering blocks) and, since `WorkspaceSvg` is already set up to be a focusable tree, it's represented as a subtree to `FlyoutBase`. This does introduce some wonky behaviors: the flyout's root will have passive focus while its contents have active focus. This could be manually disabled with some CSS if it ends up being a confusing user experience.
- Both `FlyoutBase` and `WorkspaceSvg` have built-in behaviors for detecting when a user tries navigating away from an open flyout to ensure that the flyout is closed when it's supposed to be. That is, the flyout is auto-hideable and a non-flyout, non-toolbox node has then been focused. This matches parity with the `T`/`Esc` flows supported in the keyboard navigation plugin playground.

One other thing to note: `Toolbox` had a few tests to update that were trying to reinit a toolbox without first disposing of it (which was caught by one of `FocusManager`'s state guardrails).

### Reason for Changes

This is part of an ongoing effort to ensure key components of Blockly are focusable so that they can be keyboard-navigable (with other needed changes yet both in Core Blockly and the keyboard navigation plugin).

### Test Coverage

No new tests have been added. It's certainly possible to add unit tests for the focusable configurations being introduced in this PR, but it may not be highly beneficial. It's largely assumed that the individual implementations should work due to a highly tested FocusManager, and it may be the case that the interactions of the components working together is far more important to verify (that is, the end user flows). The latter is planned to be tackled as part of #8915.

### Documentation

No documentation changes should be needed here.

### Additional Information

This includes changes that have been pulled from #8875.
2025-04-24 15:08:18 -07:00

177 lines
4.5 KiB
TypeScript

/**
* @license
* Copyright 2020 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
/**
* An item in the toolbox.
*
* @class
*/
// Former goog.module ID: Blockly.ToolboxItem
import type {ICollapsibleToolboxItem} from '../interfaces/i_collapsible_toolbox_item.js';
import type {IFocusableTree} from '../interfaces/i_focusable_tree.js';
import type {IToolbox} from '../interfaces/i_toolbox.js';
import type {IToolboxItem} from '../interfaces/i_toolbox_item.js';
import * as idGenerator from '../utils/idgenerator.js';
import type * as toolbox from '../utils/toolbox.js';
import type {WorkspaceSvg} from '../workspace_svg.js';
/**
* Class for an item in the toolbox.
*/
export class ToolboxItem implements IToolboxItem {
protected id_: string;
protected parent_: ICollapsibleToolboxItem | null;
protected level_: number;
protected toolboxItemDef_: toolbox.ToolboxItemInfo | null;
protected workspace_: WorkspaceSvg;
/** The toolbox this category belongs to. */
protected readonly parentToolbox_: IToolbox;
/**
* @param toolboxItemDef The JSON defining the toolbox item.
* @param parentToolbox The toolbox that holds the toolbox item.
* @param opt_parent The parent toolbox item or null if the category does not
* have a parent.
*/
constructor(
toolboxItemDef: toolbox.ToolboxItemInfo,
parentToolbox: IToolbox,
opt_parent?: ICollapsibleToolboxItem,
) {
/** The ID for the category. */
this.id_ =
(toolboxItemDef as AnyDuringMigration)['toolboxitemid'] ||
idGenerator.getNextUniqueId();
/** The parent of the category. */
this.parent_ = opt_parent || null;
/** The level that the category is nested at. */
this.level_ = this.parent_ ? this.parent_.getLevel() + 1 : 0;
/** The JSON definition of the toolbox item. */
this.toolboxItemDef_ = toolboxItemDef;
this.parentToolbox_ = parentToolbox;
/** The workspace of the parent toolbox. */
this.workspace_ = this.parentToolbox_.getWorkspace();
}
/**
* Initializes the toolbox item.
* This includes creating the DOM and updating the state of any items based
* on the info object.
*/
init() {}
// No-op by default.
/**
* Gets the div for the toolbox item.
*
* @returns The div for the toolbox item.
*/
getDiv(): Element | null {
return null;
}
/**
* Gets the HTML element that is clickable.
* The parent toolbox element receives clicks. The parent toolbox will add an
* ID to this element so it can pass the onClick event to the correct
* toolboxItem.
*
* @returns The HTML element that receives clicks, or null if this item should
* not receive clicks.
*/
getClickTarget(): Element | null {
return null;
}
/**
* Gets a unique identifier for this toolbox item.
*
* @returns The ID for the toolbox item.
*/
getId(): string {
return this.id_;
}
/**
* Gets the parent if the toolbox item is nested.
*
* @returns The parent toolbox item, or null if this toolbox item is not
* nested.
*/
getParent(): ICollapsibleToolboxItem | null {
return null;
}
/**
* Gets the nested level of the category.
*
* @returns The nested level of the category.
* @internal
*/
getLevel(): number {
return this.level_;
}
/**
* Whether the toolbox item is selectable.
*
* @returns True if the toolbox item can be selected.
*/
isSelectable(): boolean {
return false;
}
/**
* Whether the toolbox item is collapsible.
*
* @returns True if the toolbox item is collapsible.
*/
isCollapsible(): boolean {
return false;
}
/** Dispose of this toolbox item. No-op by default. */
dispose() {}
/**
* Sets whether the category is visible or not.
* For a category to be visible its parent category must also be expanded.
*
* @param _isVisible True if category should be visible.
*/
setVisible_(_isVisible: boolean) {}
/** See IFocusableNode.getFocusableElement. */
getFocusableElement(): HTMLElement | SVGElement {
const div = this.getDiv();
if (!div) {
throw Error('Trying to access toolbox item before DOM is initialized.');
}
if (!(div instanceof HTMLElement)) {
throw Error('Toolbox item div is unexpectedly not an HTML element.');
}
return div as HTMLElement;
}
/** See IFocusableNode.getFocusableTree. */
getFocusableTree(): IFocusableTree {
return this.parentToolbox_;
}
/** See IFocusableNode.onNodeFocus. */
onNodeFocus(): void {}
/** See IFocusableNode.onNodeBlur. */
onNodeBlur(): void {}
}
// nop by default