|
1 | | -# Accessibility: ColorSwatch must have an accessible name (`@microsoft/fluentui-jsx-a11y/colorswatch-needs-labelling`) |
| 1 | +# Accessibility: ColorSwatch must have an accessible name via aria-label, Tooltip, aria-labelledby, etc. (`@microsoft/fluentui-jsx-a11y/colorswatch-needs-labelling`) |
2 | 2 |
|
3 | 3 | 💼 This rule is enabled in the ✅ `recommended` config. |
4 | 4 |
|
5 | 5 | <!-- end auto-generated rule header --> |
6 | | - |
7 | | -All interactive elements must have an accessible name. |
8 | | - |
9 | | -`ColorSwatch` without a supported label lacks an accessible name for assistive technology users. |
10 | | - |
11 | | -## Allowed labelling strategies |
12 | | - |
13 | | -- ✅ `aria-label` **on `ColorSwatch`** |
14 | | -- ✅ `aria-labelledby` **on `ColorSwatch`** |
15 | | -- ✅ `htmlFor`/`id` |
16 | | -- ✅ Wrapping native `<label>…</label>` |
17 | | -- ✅ `Tooltip` parent with `relationship="label"` |
18 | | -- ✅ `Field` parent label |
19 | | -- ✅ Text content child |
20 | | -- ❌ Container-only label (e.g., only the surrounding `SwatchPicker` is labelled) |
21 | | - |
22 | | -## Ways to fix |
23 | | - |
24 | | -- Add `aria-label` / `aria-labelledby`. |
25 | | -- Use `<label htmlFor="…">` + `id="…"` on `ColorSwatch`. |
26 | | -- Wrap with `<label>` or use a `Tooltip (relationship="label")`. |
27 | | -- Provide a `Field label="…"` parent. |
28 | | -- Provide meaningful text as the child of `ColorSwatch`. |
29 | | - |
30 | | -## Rule Details |
31 | | - |
32 | | -This rule ensures `ColorSwatch` is labelled using **any** supported strategy above. |
33 | | - |
34 | | -### Examples of **incorrect** code |
35 | | - |
36 | | -```jsx |
37 | | -// No label |
38 | | -<SwatchPicker> |
39 | | - <ColorSwatch color="#FF1921" value="FF1921" /> |
40 | | -</SwatchPicker> |
41 | | -``` |
42 | | - |
43 | | -```jsx |
44 | | -// Container-only label: ColorSwatch itself is unnamed |
45 | | -<SwatchPicker aria-label="Color picker"> |
46 | | - <ColorSwatch color="#FF1921" value="FF1921" /> |
47 | | -</SwatchPicker> |
48 | | -``` |
49 | | - |
50 | | -### Examples of **correct** code |
51 | | - |
52 | | -```jsx |
53 | | -// aria-label |
54 | | -<SwatchPicker> |
55 | | - <ColorSwatch color="#FF1921" value="FF1921" aria-label="Red" /> |
56 | | -</SwatchPicker> |
57 | | -``` |
58 | | - |
59 | | -```jsx |
60 | | -// aria-labelledby |
61 | | -<> |
62 | | - <span id="red-swatch">Red</span> |
63 | | - <SwatchPicker> |
64 | | - <ColorSwatch color="#FF1921" value="FF1921" aria-labelledby="red-swatch" /> |
65 | | - </SwatchPicker> |
66 | | -</> |
67 | | -``` |
68 | | - |
69 | | -```jsx |
70 | | -// htmlFor/id |
71 | | -<> |
72 | | - <label htmlFor="sw-red">Red</label> |
73 | | - <SwatchPicker> |
74 | | - <ColorSwatch id="sw-red" color="#FF1921" value="FF1921" /> |
75 | | - </SwatchPicker> |
76 | | -</> |
77 | | -``` |
78 | | - |
79 | | -```jsx |
80 | | -// Wrapping native <label> |
81 | | -<label> |
82 | | - Red |
83 | | - <ColorSwatch color="#FF1921" value="FF1921" /> |
84 | | -</label> |
85 | | -``` |
86 | | - |
87 | | -```jsx |
88 | | -// Tooltip (acts as a label) |
89 | | -<SwatchPicker> |
90 | | - <Tooltip relationship="label" content="Red"> |
91 | | - <ColorSwatch color="#FF1921" value="FF1921" /> |
92 | | - </Tooltip> |
93 | | -</SwatchPicker> |
94 | | -``` |
95 | | - |
96 | | -```jsx |
97 | | -// Field parent |
98 | | -<SwatchPicker> |
99 | | - <Field label="Red"> |
100 | | - <ColorSwatch color="#FF1921" value="FF1921" /> |
101 | | - </Field> |
102 | | -</SwatchPicker> |
103 | | -``` |
104 | | - |
105 | | -```jsx |
106 | | -// Text content child |
107 | | -<SwatchPicker> |
108 | | - <ColorSwatch color="#FF1921" value="FF1921">Red</ColorSwatch> |
109 | | -</SwatchPicker> |
110 | | -``` |
0 commit comments