Skip to content

Commit f5c65a5

Browse files
chore(docs): ARIA roles and labels for built-in fields (#9788)
* chore(docs): add information about ARIA for built-in fields * chore: tweak dropdown wording * chore: add accessibility information to Anatomy of a Field * chore(format): line length * chore: update section on focusability * chore: fix phrasing
1 parent 4fe25d9 commit f5c65a5

9 files changed

Lines changed: 134 additions & 15 deletions

File tree

packages/docs/docs/guides/create-custom-blocks/fields/anatomy-of-a-field.mdx

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ and may optionally be part of the [on-block display](#on-block-display).
2727

2828
In general, editable fields allow the user to make changes to the code, while
2929
non-editable fields display information to the user about the block.
30-
Editable fields may show a rich editor when clicked.
30+
Editable fields may show a rich editor when clicked or activated with the
31+
keyboard.
3132

3233
Editable fields include:
3334

@@ -43,6 +44,25 @@ Non-editable fields include:
4344
- [Label Serializable](/guides/create-custom-blocks/fields/built-in-fields/label-serializable)
4445
- [Image](/guides/create-custom-blocks/fields/built-in-fields/image)
4546

47+
## Focusable vs non-focusable fields
48+
49+
Focusable fields can be navigated to and acted on with the keyboard. A field is
50+
focusable if it is visible and clickable or currently editable. Some
51+
non-editable fields are focusable, such as images with click handlers. If a
52+
field is focusable it must specify a single focusable element to be visited when
53+
traversing blocks. Blockly ensures that the focusable element is in the
54+
accessibility tree in an appropriate location.
55+
56+
Non-focusable fields such as labels cannot be navigated to with the keyboard.
57+
58+
## ARIA label
59+
60+
All fields have an ARIA label, which is a string containing human-readable
61+
information about the field's type and value. Blockly exposes the ARIA label to
62+
screen readers by attaching it to the field's focusable element or including it
63+
in the parent block's ARIA label. All built-in fields compute their ARIA labels
64+
automatically.
65+
4666
## Serialization
4767

4868
A serializable field's value gets encoded in the save format (JSON or XML). All
@@ -93,6 +113,19 @@ complexity.
93113
| Angle | Contains a background rect, text element, and a degree symbol. |
94114
| Turtle | Contains a background rect, text element, and many SVG elements used to construct the turtle graphic. |
95115

116+
Focusable fields must designate a focusable DOM element with a unique ID for
117+
their on-block display. Blockly places this element in the accessibility tree
118+
and moves focus to it through keyboard navigation. When the field is focused its
119+
focusable DOM element will have the `blocklyActiveFocus` or
120+
`blocklyPassiveFocus` CSS class added. Blockly's built-in fields show focus
121+
with a yellow outline.
122+
123+
The focusable element must have the correct ARIA role. Most focusable fields
124+
have an ARIA role of `button` to indicate that they can be clicked or activated
125+
with the keyboard. An exception is the checkbox field, which has an ARIA role
126+
of `checkbox`. Non-focusable fields should be removed from the accessibility
127+
tree by applying `aria-hidden` to the root of the field.
128+
96129
## Editor display
97130

98131
When a user clicks on an editable field, the field may display an
@@ -107,6 +140,10 @@ complexity.
107140
| Number input | Text editor overlaid above the on-block display. Users can type; the editor may change color to indicate bad values. |
108141
| Angle picker | The angle picker has both a text editor for typing numbers and a draggable editor for selecting angles visually. |
109142

143+
Blockly automatically moves focus into the field editor when activated. The
144+
developer is responsible for the accessibility of their field editor, including
145+
setting appropriate [ARIA role and properties](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Guides/Techniques).
146+
110147
## Other display modes
111148

112149
Collapsed mode: the user collapses the block.The block displays a text representation
@@ -115,7 +152,4 @@ fields.
115152

116153
![Turtle field block collapsing](/images/fields/yertle_collapsing.gif)
117154

118-
Accessibility mode: users may be using a screenreader or similar technology to
119-
interact with Blockly. The text of the field may be read out to the user.
120-
121155
[field-generator]: /guides/create-custom-blocks/code-generation/block-code#get-field-values

packages/docs/docs/guides/create-custom-blocks/fields/built-in-fields/checkbox.mdx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import TabItem from '@theme/TabItem';
99

1010
# Checkbox fields
1111

12-
A checkbox field stores a string as its value, and a string as its text. Its
12+
A checkbox field stores a string as its value and a string as its text. Its
1313
value is either `'TRUE'` or `'FALSE'`, and its text is either `'true'` or
1414
`'false'`.
1515

@@ -99,6 +99,13 @@ The checkbox constructor takes in an optional value and an optional
9999
when diffing workspaces.
100100
</TabItem>
101101
</Tabs>
102+
103+
## Accessibility
104+
105+
A checkbox field's on-block display has the ARIA role `checkbox`. There is
106+
no editor: the on-block display simply toggles when clicked. Its ARIA value
107+
is either `'checked'` or `'unchecked'`. The ARIA label is `checkbox: <ARIA value>`.
108+
102109
## Customization
103110

104111
### Checkmark character

packages/docs/docs/guides/create-custom-blocks/fields/built-in-fields/dropdown.mdx

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,12 @@ collapsed.](/images/fields/dropdown/collapsed.png)
3434

3535
The dropdown constructor takes in a menu generator and an optional
3636
[validator](#creating-a-dropdown-validator). The menu generator is either an
37-
array of options (where each option contains a human-readable part and a
38-
language-neutral string) or a function that generates an array of options. The
39-
human-readable part of each option can be a string, an image, or an HTML element
40-
and the array can contain a mixture of options of different types.
37+
array of options or a function that generates an array of options. A dropdown
38+
can contain a mixture of options of different types. Each option
39+
contains:
40+
- A human-readable part which may be a string, image, or HTML element
41+
- A language-neutral string
42+
- An optional ARIA label
4143

4244
### Simple text dropdowns
4345

@@ -402,6 +404,25 @@ text should be a valid language-neutral option key.
402404
</TabItem>
403405
</Tabs>
404406
407+
## Accessibility
408+
409+
A dropdown field's ARIA label is `dropdown: <ARIA value>`.
410+
411+
The ARIA value is computed based on the type of the selected option.
412+
- Simple text dropdowns use the human-readable string directly.
413+
- Image dropdowns use the `ariaLabel` of the selected image or fall back to the
414+
alt text for the selected image.
415+
- HTML dropdowns use the computed `aria-label` of the `HTMLElement`. You are
416+
responsible for making sure that your element either uses appropriate
417+
[semantic HTML](https://developer.mozilla.org/en-US/docs/Learn_web_development/Core/Accessibility/HTML)
418+
or sets its [ARIA role and properties](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Guides/Techniques) appropriately.
419+
- Providing an ARIA value in the option definition overrides the automatic
420+
label computation.
421+
422+
A dropdown field’s on-block display has the ARIA role `button`. Activating the
423+
field opens a dropdown menu with the role `listbox`. If you use an HTML dropdown
424+
you are responsible for ensuring that the contents are accessible.
425+
405426
## Customization
406427

407428
### Dropdown arrow

packages/docs/docs/guides/create-custom-blocks/fields/built-in-fields/image.mdx

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ import TabItem from '@theme/TabItem';
99

1010
# Image fields
1111

12-
An image field stores a string as its value, and a string as its text. Its
12+
An image field stores a string as its value and a string as its text. Its
1313
value is the src of the image, while its text is an alt string
14-
describing/representing the image.
14+
describing/representing the image. The alt text is also displayed when the
15+
parent block is collapsed.
1516

1617
#### Image field
1718

@@ -67,14 +68,24 @@ The image constructor takes in:
6768
| `src` | A string that points to a [raster image](https://developer.mozilla.org/en-US/docs/Glossary/raster_image) file. |
6869
| `width` | Must cast to a non-zero number. |
6970
| `height` | Must cast to a non-zero number. |
70-
| `opt_alt` | (Optional) A string that accurately describes/represents the image. This is used instead of the image when the block is collapsed. If it is `null` or `undefined` an empty string will be used. |
71+
| `opt_alt` | (Optional) A string that accurately describes/represents the image. If it is `null` or `undefined` an empty string will be used. |
7172
| `opt_onClick` | (Optional) A function to call when the field is clicked. |
7273
| `opt_flipRtl` | (Optional) A boolean. If `true`, the image is flipped across the vertical axis when in right-to-left mode. Defaults to `false`. Useful for "turn left" and "turn right" icons. |
7374

7475
## Serialization
7576

7677
Image fields are not serializable.
7778

79+
## Accessibility
80+
An image field's ARIA label is `image: <alt text>`. The alt text is also
81+
displayed when the parent block is collapsed.
82+
83+
Clickable images have an ARIA role of `button`. Image fields that do not have
84+
click handlers have an ARIA role of `none`.
85+
86+
Follow [AFB guidelines](https://afb.org/digital-inclusion/accessibility-resources/writing-effective-image-descriptions)
87+
for writing good alt text. Return `null` or `undefined` to indicate a purely decorative image.
88+
7889
## Click handler
7990

8091
:::note

packages/docs/docs/guides/create-custom-blocks/fields/built-in-fields/label-serializable.mdx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@ class string. Both default to an empty string.
8686
label field, and the node's inner text is the value to apply to the field.
8787
</TabItem>
8888
</Tabs>
89+
90+
91+
## Accessibility
92+
93+
Serializable label fields cannot be focused directly with keyboard navigation.
94+
The on-block display has the ARIA role `generic`. There is no editor display.
95+
The label's text is used to construct the parent block's ARIA label.
96+
8997
## Validators
9098

9199
Serializable label fields do not support validators, because they are not

packages/docs/docs/guides/create-custom-blocks/fields/built-in-fields/label.mdx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,12 @@ If you would like your label to be serialized, because it is being changed
8989
programmatically, see the [Serializable Label](/guides/create-custom-blocks/fields/built-in-fields/label-serializable)
9090
field.
9191

92+
## Accessibility
93+
94+
Label fields cannot be focused directly with keyboard navigation. The on-block
95+
display has the ARIA role `generic`. There is no editor display. The label's
96+
text is used to construct the parent block's ARIA label.
97+
9298
## Validators
9399

94100
Label fields do not support validators, because they are not editable.

packages/docs/docs/guides/create-custom-blocks/fields/built-in-fields/number.mdx

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,25 @@ The `value` should cast to a number. If it does not 0 will be used.
102102
inner text value follows the same rules as the constructor value.
103103
</TabItem>
104104
</Tabs>
105+
106+
## Accessibility
107+
108+
A number field's ARIA label is `number: <ARIA value>`. A number fields's
109+
on-block display has the ARIA role `button`. Activating the field overlays and
110+
focuses an HTML text input.
111+
112+
:::note
113+
The HTML input type is `text` rather than `number` because the semantics of
114+
an HTML `number` input do not match Blockly's behaviour. Blockly number fields
115+
provide feedback for invalid values and handle the value `Infinity`.
116+
[According to MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/input/number#accessibility)
117+
“the implicit role for the `<input type="number">` element is `spinbutton`.
118+
[...] With `<input type="number">`, there is a risk of users accidentally
119+
incrementing a number when they're trying to do something else. Additionally,
120+
if users try to enter something that's not a number, there's no explicit
121+
feedback about what they're doing wrong.”
122+
:::
123+
105124
## Constraints
106125

107126
Constraints can be set in the field definition, or by using the
@@ -127,9 +146,7 @@ used to make the field only accept multiples of .01, 10, 42, etc.
127146

128147
### Positive numbers
129148

130-
To force your field to only accept positive numbers, set the `min` value to
131-
132-
1.
149+
To force your field to only accept positive numbers, set the `min` value to 1.
133150

134151
### Integers
135152

packages/docs/docs/guides/create-custom-blocks/fields/built-in-fields/text-input.mdx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,13 @@ The JSON definition also allows you to set the [spellcheck](#spellcheck) option.
9797
text value follows the same rules as the constructor value.
9898
</TabItem>
9999
</Tabs>
100+
101+
## Accessibility
102+
103+
A text input field's ARIA label is `text: <ARIA value>`. The on-block display
104+
has the ARIA role `button`. Activating the field overlays and focuses an HTML
105+
text input.
106+
100107
## Customization
101108

102109
### Spellcheck

packages/docs/docs/guides/create-custom-blocks/fields/built-in-fields/variable.mdx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,14 @@ types, and an optional default type.
169169
follows the same rules as the constructor's variable name parameter.
170170
</TabItem>
171171
</Tabs>
172+
173+
## Accessibility
174+
175+
A variable field's ARIA label is `variable: '<ARIA value>'`, where the ARIA
176+
value is the name of the variable. A variable field's on-block display has the
177+
ARIA role `button`. Activating the field opens a dropdown menu with the role
178+
`listbox`.
179+
172180
## Creating a variable validator
173181

174182
:::note

0 commit comments

Comments
 (0)