mirror of
https://github.com/google/blockly.git
synced 2026-01-04 15:40:08 +01:00
## The basics - [x] I [validated my changes](https://developers.google.com/blockly/guides/contribute/core#making_and_verifying_a_change) ## The details ### Resolves Fixes #8206 Fixes #8210 Fixes #8213 Fixes #8255 Fixes #8211 Fixes #8212 Fixes #8254 Fixes part of #9301 Fixes part of #9304 ### Proposed Changes This PR completes the remaining ARIA roles and properties needed for all core fields. Specifically: - #8206: A better name needed to be used for the checkbox value, plus there was an ARIA property missing for actually representing the checkbox state. The latter needed to be updated upon toggling the checkbox, as well. These changes bring checkbox fields in compliance with the ARIA checkbox pattern documented here: https://www.w3.org/WAI/ARIA/apg/patterns/checkbox/. - #8210: This one required a lot of changes in order to adapt to the ARIA combobox pattern documented here: https://www.w3.org/WAI/ARIA/apg/patterns/combobox/. Specifically: - Menus needed to have a unique ID that's also exposed in order to link the combobox element to its menu when open. - ARIA's `activedescendant` proved very useful in ensuring that the current dropdown selection is correctly read when the combobox has focus but its menu isn't opened. - The default properties available for options (label and value) aren't very good for readout, so a custom ARIA property was added for much clearer option readouts. This is only demonstrated for the math arithmetic block for now. - The text element is normally hidden for ARIA but it's useful in conjunction with `activedescendant` to represent the current value selection. - Images have been handled here as well (partly as part of #8255) by leveraging their alt text for readouts. This actually seems to work quite well both for current value and selection. - #8213: Much of the improvements here come from the combobox (`FieldDropdown`) improvements explained above. However one additional bit was done to provide an explicit 'Variable <name>' readout for the purpose of clarity. This demonstrates some contextualization of the value of the field which may be a generally useful pattern to copy in other field contexts. - #8255: Image fields have been refined since they were redundantly specifying 'image' when an `image` ARIA role is already being used. Now only the alt text is supplied along with the role context. Note that images need special handling since they can sometimes be navigable (such as when they have click handlers). - #8211: Text input fields have had their labeling improved like all other fields, and the field's value is now exposed via its `text` element since this will show up as a `StaticText` node in the accessibility tree and automatically be read as part of the field's value. - #8212: This gets the same benefits as the previous point since those improvements were included for both text and number input. However, existing `valuemin` and `valuemax` ARIA properties have been removed. It seems these are really only useful when introducing a slider mechanism (see https://www.w3.org/WAI/ARIA/apg/patterns/slider/) and from testing seems to not really be utilized for the basic text input that `FieldNumber` currently uses. It may be the case that this is a better pattern to use in the future, but it's more likely that other custom fields could benefit from more specific patterns like slider rather than `FieldNumber` being changed in that way. - #8254 and part of #9304: Field labels have been completely removed from the accessibility node tree since they can never be navigated to (as #8254 explains all labels will be included as part of the block's ARIA label itself for readout parity with navigation options). Note that it doesn't cover external fields (such as those supplied in blockly-samples), nor does it fully set up the infrastructure work for those. Ultimately that work needs to happen as part of #9301. Beyond the role work above, this PR also introduces some fundamental work for #9301. Specifically: - It demonstrates how block definitions could be used to introduce accessibility label customizations (in this case for the options of the arithmetic operator block's drop-down field, plus the block itself). - It sets up some central label computation for all fields, though more thought is needed on whether this is sufficient for custom fields outside of core Blockly and on how to properly contextualize labels for field values. Core Blockly's fields are fairly simple for representing values which is why that aspect of #9301 didn't need to be solved in this PR. Note that the field labeling here is being used to improve all of the fields above, but also it tries to aggressively fall back to the _next best_ label to be used (though it's possible to run out of options which is why fields still need contextually-specific fallbacks). ### Reason for Changes Generally the initial approach for implementing labels is leveraging as specific ARIA roles as exist to directly represent the element. This PR is completing that work for all of core Blockly's built-in fields, and laying some of the groundwork for generalizing this support for custom fields. Having specific roles does potentially introduce inconsistencies across screen readers (though should improve consistency across sites for a single screen reader), and expectations for behaviors (like shortcuts) that may need to be ignored or only partially supported (#9313 is discussing this). ### Test Coverage Only manual testing has been completed since this is experimental work. Video demonstrating most of the changes: [Screen recording 2025-10-01 4.05.35 PM.webm](https://github.com/user-attachments/assets/c7961caa-eae0-4585-8fd9-87d7cbe65988) ### Documentation N/A -- Experimental work. ### Additional Information This has only been tested on ChromeVox.
493 lines
13 KiB
TypeScript
493 lines
13 KiB
TypeScript
/**
|
|
* @license
|
|
* Copyright 2019 Google LLC
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
/**
|
|
* Blockly menu similar to Closure's goog.ui.Menu
|
|
*
|
|
* @class
|
|
*/
|
|
// Former goog.module ID: Blockly.Menu
|
|
|
|
import * as browserEvents from './browser_events.js';
|
|
import type {MenuSeparator} from './menu_separator.js';
|
|
import {MenuItem} from './menuitem.js';
|
|
import * as aria from './utils/aria.js';
|
|
import {Coordinate} from './utils/coordinate.js';
|
|
import * as idGenerator from './utils/idgenerator.js';
|
|
import type {Size} from './utils/size.js';
|
|
import * as style from './utils/style.js';
|
|
|
|
/**
|
|
* A basic menu class.
|
|
*/
|
|
export class Menu {
|
|
/**
|
|
* Array of menu items and separators.
|
|
*/
|
|
private readonly menuItems: Array<MenuItem | MenuSeparator> = [];
|
|
|
|
/**
|
|
* Coordinates of the pointerdown event that caused this menu to open. Used to
|
|
* prevent the consequent pointerup event due to a simple click from
|
|
* activating a menu item immediately.
|
|
*/
|
|
openingCoords: Coordinate | null = null;
|
|
|
|
/**
|
|
* This is the element that we will listen to the real focus events on.
|
|
* A value of null means no menu item is highlighted.
|
|
*/
|
|
private highlightedItem: MenuItem | null = null;
|
|
|
|
/** Pointer over event data. */
|
|
private pointerMoveHandler: browserEvents.Data | null = null;
|
|
|
|
/** Click event data. */
|
|
private clickHandler: browserEvents.Data | null = null;
|
|
|
|
/** Pointer enter event data. */
|
|
private pointerEnterHandler: browserEvents.Data | null = null;
|
|
|
|
/** Pointer leave event data. */
|
|
private pointerLeaveHandler: browserEvents.Data | null = null;
|
|
|
|
/** Key down event data. */
|
|
private onKeyDownHandler: browserEvents.Data | null = null;
|
|
|
|
/** The menu's root DOM element. */
|
|
private element: HTMLDivElement | null = null;
|
|
|
|
/** ARIA name for this menu. */
|
|
private roleName: aria.Role | null = null;
|
|
|
|
id: string;
|
|
|
|
/** Constructs a new Menu instance. */
|
|
constructor() {
|
|
this.id = idGenerator.getNextUniqueId();
|
|
}
|
|
|
|
/**
|
|
* Add a new menu item to the bottom of this menu.
|
|
*
|
|
* @param menuItem Menu item or separator to append.
|
|
* @internal
|
|
*/
|
|
addChild(menuItem: MenuItem | MenuSeparator) {
|
|
this.menuItems.push(menuItem);
|
|
}
|
|
|
|
/**
|
|
* Creates the menu DOM.
|
|
*
|
|
* @param container Element upon which to append this menu.
|
|
* @returns The menu's root DOM element.
|
|
*/
|
|
|
|
render(container: Element): HTMLDivElement {
|
|
const element = document.createElement('div');
|
|
element.className = 'blocklyMenu';
|
|
element.tabIndex = 0;
|
|
element.id = this.id;
|
|
if (this.roleName) {
|
|
aria.setRole(element, this.roleName);
|
|
}
|
|
this.element = element;
|
|
|
|
// Add menu items.
|
|
for (let i = 0, menuItem; (menuItem = this.menuItems[i]); i++) {
|
|
element.appendChild(menuItem.createDom());
|
|
}
|
|
|
|
// Add event handlers.
|
|
this.pointerMoveHandler = browserEvents.conditionalBind(
|
|
element,
|
|
'pointermove',
|
|
this,
|
|
this.handlePointerMove,
|
|
true,
|
|
);
|
|
this.clickHandler = browserEvents.conditionalBind(
|
|
element,
|
|
'pointerup',
|
|
this,
|
|
this.handleClick,
|
|
true,
|
|
);
|
|
this.pointerEnterHandler = browserEvents.conditionalBind(
|
|
element,
|
|
'pointerenter',
|
|
this,
|
|
this.handlePointerEnter,
|
|
true,
|
|
);
|
|
this.pointerLeaveHandler = browserEvents.conditionalBind(
|
|
element,
|
|
'pointerleave',
|
|
this,
|
|
this.handlePointerLeave,
|
|
true,
|
|
);
|
|
this.onKeyDownHandler = browserEvents.conditionalBind(
|
|
element,
|
|
'keydown',
|
|
this,
|
|
this.handleKeyEvent,
|
|
);
|
|
|
|
container.appendChild(element);
|
|
return element;
|
|
}
|
|
|
|
/**
|
|
* Gets the menu's element.
|
|
*
|
|
* @returns The DOM element.
|
|
* @internal
|
|
*/
|
|
getElement(): HTMLDivElement | null {
|
|
return this.element;
|
|
}
|
|
|
|
/**
|
|
* Focus the menu element.
|
|
*
|
|
* @internal
|
|
*/
|
|
focus() {
|
|
const el = this.getElement();
|
|
if (el) {
|
|
el.focus({preventScroll: true});
|
|
}
|
|
}
|
|
|
|
/** Blur the menu element. */
|
|
private blur() {
|
|
const el = this.getElement();
|
|
if (el) {
|
|
el.blur();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Set the menu accessibility role.
|
|
*
|
|
* @param roleName role name.
|
|
* @internal
|
|
*/
|
|
setRole(roleName: aria.Role) {
|
|
this.roleName = roleName;
|
|
}
|
|
|
|
/** Dispose of this menu. */
|
|
dispose() {
|
|
// Remove event handlers.
|
|
if (this.pointerMoveHandler) {
|
|
browserEvents.unbind(this.pointerMoveHandler);
|
|
this.pointerMoveHandler = null;
|
|
}
|
|
if (this.clickHandler) {
|
|
browserEvents.unbind(this.clickHandler);
|
|
this.clickHandler = null;
|
|
}
|
|
if (this.pointerEnterHandler) {
|
|
browserEvents.unbind(this.pointerEnterHandler);
|
|
this.pointerEnterHandler = null;
|
|
}
|
|
if (this.pointerLeaveHandler) {
|
|
browserEvents.unbind(this.pointerLeaveHandler);
|
|
this.pointerLeaveHandler = null;
|
|
}
|
|
if (this.onKeyDownHandler) {
|
|
browserEvents.unbind(this.onKeyDownHandler);
|
|
this.onKeyDownHandler = null;
|
|
}
|
|
|
|
// Remove menu items.
|
|
for (let i = 0, menuItem; (menuItem = this.menuItems[i]); i++) {
|
|
menuItem.dispose();
|
|
}
|
|
this.element = null;
|
|
}
|
|
|
|
// Child component management.
|
|
|
|
/**
|
|
* Returns the child menu item that owns the given DOM element,
|
|
* or null if no such menu item is found.
|
|
*
|
|
* @param elem DOM element whose owner is to be returned.
|
|
* @returns Menu item for which the DOM element belongs to.
|
|
*/
|
|
private getMenuItem(elem: Element): MenuItem | null {
|
|
const menuElem = this.getElement();
|
|
// Node might be the menu border (resulting in no associated menu item), or
|
|
// a menu item's div, or some element within the menu item.
|
|
// Walk up parents until one meets either the menu's root element, or
|
|
// a menu item's div.
|
|
let currentElement: Element | null = elem;
|
|
while (currentElement && currentElement !== menuElem) {
|
|
if (currentElement.classList.contains('blocklyMenuItem')) {
|
|
// Having found a menu item's div, locate that menu item in this menu.
|
|
const items = this.getMenuItems();
|
|
for (let i = 0, menuItem; (menuItem = items[i]); i++) {
|
|
if (menuItem.getElement() === currentElement) {
|
|
return menuItem;
|
|
}
|
|
}
|
|
}
|
|
currentElement = currentElement.parentElement;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
// Highlight management.
|
|
|
|
/**
|
|
* Highlights the given menu item, or clears highlighting if null.
|
|
*
|
|
* @param item Item to highlight, or null.
|
|
* @internal
|
|
*/
|
|
setHighlighted(item: MenuItem | null) {
|
|
const currentHighlighted = this.highlightedItem;
|
|
if (currentHighlighted) {
|
|
currentHighlighted.setHighlighted(false);
|
|
this.highlightedItem = null;
|
|
}
|
|
if (item) {
|
|
item.setHighlighted(true);
|
|
this.highlightedItem = item;
|
|
// Bring the highlighted item into view. This has no effect if the menu is
|
|
// not scrollable.
|
|
const menuElement = this.getElement();
|
|
const menuItemElement = item.getElement();
|
|
if (!menuElement || !menuItemElement) return;
|
|
|
|
style.scrollIntoContainerView(menuItemElement, menuElement);
|
|
aria.setState(menuElement, aria.State.ACTIVEDESCENDANT, item.getId());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Highlights the next highlightable item (or the first if nothing is
|
|
* currently highlighted).
|
|
*
|
|
* @internal
|
|
*/
|
|
highlightNext() {
|
|
const index = this.highlightedItem
|
|
? this.menuItems.indexOf(this.highlightedItem)
|
|
: -1;
|
|
this.highlightHelper(index, 1);
|
|
}
|
|
|
|
/**
|
|
* Highlights the previous highlightable item (or the last if nothing is
|
|
* currently highlighted).
|
|
*
|
|
* @internal
|
|
*/
|
|
highlightPrevious() {
|
|
const index = this.highlightedItem
|
|
? this.menuItems.indexOf(this.highlightedItem)
|
|
: -1;
|
|
this.highlightHelper(index < 0 ? this.menuItems.length : index, -1);
|
|
}
|
|
|
|
/** Highlights the first highlightable item. */
|
|
private highlightFirst() {
|
|
this.highlightHelper(-1, 1);
|
|
}
|
|
|
|
/** Highlights the last highlightable item. */
|
|
private highlightLast() {
|
|
this.highlightHelper(this.menuItems.length, -1);
|
|
}
|
|
|
|
/**
|
|
* Helper function that manages the details of moving the highlight among
|
|
* child menuitems in response to keyboard events.
|
|
*
|
|
* @param startIndex Start index.
|
|
* @param delta Step direction: 1 to go down, -1 to go up.
|
|
*/
|
|
private highlightHelper(startIndex: number, delta: number) {
|
|
let index = startIndex + delta;
|
|
let menuItem;
|
|
const items = this.getMenuItems();
|
|
while ((menuItem = items[index])) {
|
|
if (menuItem.isEnabled()) {
|
|
this.setHighlighted(menuItem);
|
|
break;
|
|
}
|
|
index += delta;
|
|
}
|
|
}
|
|
|
|
// Pointer events.
|
|
|
|
/**
|
|
* Handles pointermove events. Highlight menu items as the user hovers over
|
|
* them.
|
|
*
|
|
* @param e Pointer event to handle.
|
|
*/
|
|
private handlePointerMove(e: PointerEvent) {
|
|
// Check whether the pointer actually did move. Move events are triggered if
|
|
// the element underneath the pointer moves, even if the pointer itself has
|
|
// remained stationary. In the case where the pointer is hovering over
|
|
// the menu but the user is navigating through the list of items via the
|
|
// keyboard and causing items off the end of the menu to scroll into view,
|
|
// a pointermove event would be triggered due to the pointer now being over
|
|
// a new child, but we don't want to highlight the item that's now under the
|
|
// pointer.
|
|
const delta = Math.max(Math.abs(e.movementX), Math.abs(e.movementY));
|
|
if (delta === 0) return;
|
|
|
|
const menuItem = this.getMenuItem(e.target as Element);
|
|
|
|
if (menuItem) {
|
|
if (menuItem.isEnabled()) {
|
|
if (this.highlightedItem !== menuItem) {
|
|
this.setHighlighted(menuItem);
|
|
}
|
|
} else {
|
|
this.setHighlighted(null);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Handles click events. Pass the event onto the child menuitem to handle.
|
|
*
|
|
* @param e Click event to handle.
|
|
*/
|
|
private handleClick(e: PointerEvent) {
|
|
const oldCoords = this.openingCoords;
|
|
// Clear out the saved opening coords immediately so they're not used twice.
|
|
this.openingCoords = null;
|
|
if (oldCoords && typeof e.clientX === 'number') {
|
|
const newCoords = new Coordinate(e.clientX, e.clientY);
|
|
if (Coordinate.distance(oldCoords, newCoords) < 1) {
|
|
// This menu was opened by a pointerdown and we're handling the
|
|
// consequent click event. The coords haven't changed, meaning this was
|
|
// the same opening event. Don't do the usual behavior because the menu
|
|
// just popped up under the pointer and the user didn't mean to activate
|
|
// this item.
|
|
return;
|
|
}
|
|
}
|
|
|
|
const menuItem = this.getMenuItem(e.target as Element);
|
|
if (menuItem) {
|
|
menuItem.performAction(e);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Handles pointer enter events. Focus the element.
|
|
*
|
|
* @param _e Pointer event to handle.
|
|
*/
|
|
private handlePointerEnter(_e: PointerEvent) {
|
|
this.focus();
|
|
}
|
|
|
|
/**
|
|
* Handles pointer leave events by clearing the active highlight.
|
|
*
|
|
* @param _e Pointer event to handle.
|
|
*/
|
|
private handlePointerLeave(_e: PointerEvent) {
|
|
if (this.getElement()) {
|
|
this.setHighlighted(null);
|
|
}
|
|
}
|
|
|
|
// Keyboard events.
|
|
|
|
/**
|
|
* Attempts to handle a keyboard event.
|
|
*
|
|
* @param e Key event to handle.
|
|
*/
|
|
private handleKeyEvent(e: Event) {
|
|
if (!this.menuItems.length) {
|
|
// Empty menu.
|
|
return;
|
|
}
|
|
const keyboardEvent = e as KeyboardEvent;
|
|
if (
|
|
keyboardEvent.shiftKey ||
|
|
keyboardEvent.ctrlKey ||
|
|
keyboardEvent.metaKey ||
|
|
keyboardEvent.altKey
|
|
) {
|
|
// Do not handle the key event if any modifier key is pressed.
|
|
return;
|
|
}
|
|
|
|
const highlighted = this.highlightedItem;
|
|
switch (keyboardEvent.key) {
|
|
case 'Enter':
|
|
case ' ':
|
|
if (highlighted) {
|
|
highlighted.performAction(e);
|
|
}
|
|
break;
|
|
|
|
case 'ArrowUp':
|
|
this.highlightPrevious();
|
|
break;
|
|
|
|
case 'ArrowDown':
|
|
this.highlightNext();
|
|
break;
|
|
|
|
case 'PageUp':
|
|
case 'Home':
|
|
this.highlightFirst();
|
|
break;
|
|
|
|
case 'PageDown':
|
|
case 'End':
|
|
this.highlightLast();
|
|
break;
|
|
|
|
default:
|
|
// Not a key the menu is interested in.
|
|
return;
|
|
}
|
|
// The menu used this key, don't let it have secondary effects.
|
|
e.preventDefault();
|
|
e.stopPropagation();
|
|
}
|
|
|
|
/**
|
|
* Get the size of a rendered menu.
|
|
*
|
|
* @returns Object with width and height properties.
|
|
* @internal
|
|
*/
|
|
getSize(): Size {
|
|
const menuDom = this.getElement() as HTMLDivElement;
|
|
const menuSize = style.getSize(menuDom);
|
|
// Recalculate height for the total content, not only box height.
|
|
menuSize.height = menuDom.scrollHeight;
|
|
return menuSize;
|
|
}
|
|
|
|
/**
|
|
* Returns the action menu items (omitting separators) in this menu.
|
|
*
|
|
* @returns The MenuItem objects displayed in this menu.
|
|
*/
|
|
private getMenuItems(): MenuItem[] {
|
|
return this.menuItems.filter((item) => item instanceof MenuItem);
|
|
}
|
|
}
|