@@ -3,49 +3,49 @@ import * as SingleSelectAccessibilityStories from './single-select.accessibility
3
3
4
4
import {OptionItem , SingleSelect } from " @khanacademy/wonder-blocks-dropdown" ;
5
5
import {View } from " @khanacademy/wonder-blocks-core" ;
6
- import {LabelLarge } from " @khanacademy/wonder-blocks-typography " ;
6
+ import {LabeledField } from " @khanacademy/wonder-blocks-labeled-field " ;
7
7
8
8
<Meta of = { SingleSelectAccessibilityStories } />
9
9
10
- export const SingleSelectAccessibility = () => (
11
- <View >
12
- <LabelLarge
13
- tag = " label"
14
- id = " label-for-single-select"
15
- htmlFor = " unique-single-select"
16
- >
17
- Associated label element
18
- </LabelLarge >
19
- <SingleSelect
20
- aria-labelledby = " label-for-single-select"
21
- id = " unique-single-select"
22
- placeholder = " Accessible SingleSelect"
23
- selectedValue = " one"
24
- >
25
- <OptionItem
26
- label = " First element"
27
- aria-label = " First element, selected"
28
- value = " one"
29
- />
30
- <OptionItem
31
- label = " Second element"
32
- aria-label = " Second element, unselelected"
33
- value = " two"
34
- />
35
- </SingleSelect >
36
- </View >
37
- );
38
-
39
10
# Accessibility
40
11
41
- If you need to associate this component with another element (e.g. ` <label> ` ),
42
- make sure to pass the ` aria-labelledby ` and/or ` id ` props to the ` SingleSelect ` component.
43
- This way, the ` opener ` will receive this value and it will associate both
44
- elements.
12
+ ## Using ` LabeledField ` with ` SingleSelect `
45
13
46
- Also, if you need screen readers to understand any relevant information on every
47
- option item, you can use ` aria-label ` on each item. e.g. You can use it to let
48
- screen readers know the current selected/unselected status of the item when it
49
- receives focus.
14
+ To associate a ` SingleSelect ` with another visible element (e.g. a ` <label> ` ),
15
+ wrap it in a ` LabeledField ` component. The label will apply to the ` SingleSelect `
16
+ opener. With ` LabeledField ` , you can supply label text (or a JSX node)
17
+ using the ` label ` prop to generate a paired ` <label> ` element. It comes with
18
+ field validation and other features baked in!
19
+
20
+ If for some reason you can't use ` LabeledField ` for a visible label, you can still
21
+ make ` SingleSelect ` accessible in a screen reader by associating it with ` <label for=""> ` .
22
+ Pass the ` id ` of the ` SingleSelect ` to the ` for ` attribute.
23
+
24
+ Alternatively, you can create an accessible name for ` SingleSelect ` using ` aria-labelledby ` .
25
+ Put ` aria-labelledby ` on ` SingleSelect ` pointing to the ` id ` of any other element.
26
+ It won't give you the same enhanced click target as a paired ` <label> ` , but it still
27
+ helps to create a more accessible experience.
50
28
51
29
<Canvas of = { SingleSelectAccessibilityStories .UsingAriaAttributes } />
30
+
31
+ ## Using ` aria-label ` for the opener and/or child options
32
+
33
+ A visible label with ` <LabeledField> ` is preferred. However, for specific cases
34
+ where the ` SingleSelect ` is not paired with a ` LabeledField ` or other
35
+ visible ` <label> ` element, you ** must** supply an ` aria-label ` attribute
36
+ for an accessible name on the opener.
37
+
38
+ This will ensure the ` SingleSelect ` has a name that describes its purpose.
39
+
40
+ For example, an ` aria-label ` for ` SingleSelect ` in a compact UI could be "Division"
41
+ while its value would be one of the selected options, such as specific division names.
42
+ It might also have a placeholder such as "e.g., Division I (D1)", which would go away
43
+ when the user selected an option.
44
+
45
+ Also, if you need screen readers to understand relevant information on
46
+ option items, you can use ` aria-label ` on each item. e.g. You can use it to let
47
+ screen readers know the current selected/unselected status of the item when it
48
+ receives focus. This can be useful when the options contain icons or other information
49
+ that would need to be omitted from the visible label.
50
+
51
+ <Canvas of = { SingleSelectAccessibilityStories .UsingOpenerAriaLabel } />
0 commit comments