Accessibility in StageXL

Branch preview. The live examples in this guide use the StageXL accessibility branch. Try them with a keyboard, pointer, or screen reader, then open the Dart source below each example.

How it works

When you opt in, StageXL mirrors accessible display objects into a native HTML tree alongside the canvas. StageXL still draws the interface and handles pointer input. The browser can expose the corresponding names, roles, and states to assistive technology. Keyboard focus follows the HTML controls, and a visible outline shows the focused control over its canvas position.

Move focus with Tab or Shift+Tab and activate a button with Enter or Space. The native control sends a bubbling StageXL MouseEvent.CLICK, so the existing onMouseClick handler can serve pointer, keyboard, and assistive-technology activation.

Run a keyboard-accessible counter

Tab to Count activation and look for the focus outline. Press Enter, press Space, then click the button. Each action should increment the counter once. With a screen reader, listen for the button’s name on focus and the new count after activation.

Counter setup and Dart source

Start with the web project in Getting Started. Replace its stagexl dependency with the Git revision below and keep its web dependency. The pinned revision matches the live examples.

dependencies:
  stagexl:
    git:
      url: https://github.com/bp74/StageXL.git
      ref: c66d64bd217e685865c33be4544fe29d17172dc1
  web: ^1.1.1

Run dart pub get. Set the canvas in web/index.html to <canvas id="stage" width="360" height="240"></canvas> and replace web/main.dart with this example.


            

Build and serve the app using the JavaScript instructions or the WebAssembly instructions.

To run another example in this project, copy its void show… function below the counter code and call it in main() in place of showCounter(stage). Keep the quick-start imports and helpers, then recompile and reload.

Name icon buttons and set their tab order

On an enabled stage, a SimpleButton can take its name from the text in its up state, as the counter did. Use accessibilityLabel to name a button that shows only a symbol or image. Use accessibilityDescription for extra context. A dynamic TextField exposes its text in the reading order without adding a tab stop.

The named group gives the two buttons context. This example adds the right button first in the display list and uses accessibilityOrder to put the left button first for reading and tabbing.

Tab to the two buttons. Focus should reach Decrease quantity before Increase quantity. Activate Increase, then Decrease, and check the visible quantity and announcement.

Dart source for the quantity buttons

        

The default reading order follows the display list. Among siblings, lower accessibilityOrder values come first, unset values count as zero, and ties keep their display-list order. For decorative content, set accessible = false to omit the object’s own node or accessibilityChildren = false to omit all of its descendants from the accessibility tree.

Edit text with a native browser input

A TextField with TextFieldType.INPUT uses a native browser editor. Give it a label even when the canvas already shows a prompt. The native editor synchronizes its value and selection with the canvas field, and user edits dispatch a bubbling Event.CHANGE.

Tab to Display name, type a name, and use Shift+Arrow keys to select and replace part of it. The preview should follow your edits. Clear the field to see Guest again. Try pasting a longer value to check the character limit.

Dart source for the name editor

        

For a notes field, set multiline = true and give it enough height for several lines. For a password field, set displayAsPassword = true and keep its value out of visible previews and announcements. For apps that accept input through an input method editor (IME), test composing and committing text in the field.

Build a toggle, a checkbox, and a disabled button

Update the semantic state in the same handler that updates your app and its artwork. This table shows the state StageXL exposes for each role.

ControlRoleState to update
Toggle buttonbuttonaccessibilityPressed
CheckboxcheckboxaccessibilityChecked
Radio buttonradio, inside a radioGroupaccessibilityChecked
List optionoption, inside a listBoxaccessibilitySelected

Keep each control’s name stable and use accessibilityPressed or accessibilityChecked to expose its current state. On the reset button, accessibilityDisabled disables the native control and removes it from the tab order, and mouseEnabled controls whether the canvas accepts a click.

Tab past the controls with the default settings. Reset settings should be skipped. Change either setting with Space and check that the preview and the reported state change together. Activate Reset and check that the defaults return and focus moves to Large text. A pointer click on the disabled Reset button should have no effect.

Dart source for the settings controls

        

Compare radio groups and list boxes

Each group contributes one tab stop. Arrow keys, Home, and End move within it. Moving to a different radio also activates it. In a list box, the focused option becomes selected when the user activates it. In both cases, your click handler sets the new state and clears the old one.

Tab to Small and press Down Arrow. Focus and the summary should both move to Large. Tab to Matte and press Down Arrow. Focus should move to Glossy, with Matte still selected in the summary. Press Space to select Glossy. Use Home and End to move to the first and last choices.

Dart source for the choice groups

        

Open and dismiss a modal dialog

Give the visible dialog container accessible = true, accessibilityRole = AccessibilityRole.dialog, and a label. Add it to the stage before calling stage.accessibility.openModal(dialog, initialFocus: cancelButton). StageXL restricts interaction outside the dialog and keeps Tab and Shift+Tab inside it. Supply a visible way to dismiss the dialog and call stage.accessibility.closeModal(dialog) from its handler. When the opener is still available, StageXL restores focus to it. This example also handles Escape in the dialog’s onKeyDown listener.

Open the dialog with Enter or a pointer click. Focus should land on Cancel. Use Tab and Shift+Tab to cycle between Cancel and Reset. Press Escape to close the dialog and return focus to the opener. Open it again and activate Reset to confirm the score change and hear the announcement.

Dart source for the modal dialog

        

Announce updates and clean up

stage.accessibility.announce(message) sends a polite announcement for feedback such as a score or a saved answer. Pass assertive: true for an urgent message that should interrupt speech. The counter announces a result after activation, while the settings example exposes the toggle and checkbox states directly. Keep feedback visible in your app when users need to refer back to it.

Call stage.dispose() when removing a stage from your app. This detaches the stage from its render loop and releases its input listeners and semantic HTML. Remove the canvas yourself if the app no longer needs it.

Test your app

  1. Use only the keyboard. Tab through the canvas controls in both directions. Verify that focus is visible and follows a logical order, and that you can leave the canvas when a modal is closed.
  2. Activate each control with a pointer and with the keyboard. Verify that both paths update the app once and that disabled actions are unavailable.
  3. Use a screen reader to read through the canvas content and interact with the controls. Check names, roles, states, group names, and announcements. Reading static text and tabbing between controls are different navigation modes.
  4. In the browser’s developer tools, look for [data-stagexl-accessibility] alongside the canvas. Inspect its controls in the accessibility panel. Compare their names and states with the canvas, then resize or scroll the page and check the focus outline again.

For more examples of the branch’s expected behavior, see the StageXL accessibility tests.