@a11y-context/mcp-server 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +62 -0
  3. package/corpus/web/react/components/accordion.basic.md +157 -0
  4. package/corpus/web/react/components/button.basic.md +91 -0
  5. package/corpus/web/react/components/button.toggle.md +109 -0
  6. package/corpus/web/react/components/carousel.dots.md +376 -0
  7. package/corpus/web/react/components/carousel.thumbnails.md +395 -0
  8. package/corpus/web/react/components/collection-row.basic.md +179 -0
  9. package/corpus/web/react/components/combobox.autocomplete.md +293 -0
  10. package/corpus/web/react/components/dialog.modal.md +202 -0
  11. package/corpus/web/react/components/dialog.nonmodal.md +184 -0
  12. package/corpus/web/react/components/disclosure.basic.md +97 -0
  13. package/corpus/web/react/components/grid.channel-guide.md +453 -0
  14. package/corpus/web/react/components/link.basic.md +105 -0
  15. package/corpus/web/react/components/listbox.basic.md +263 -0
  16. package/corpus/web/react/components/menu.basic.md +294 -0
  17. package/corpus/web/react/components/menu.menubar.md +296 -0
  18. package/corpus/web/react/components/navigation-menu.basic.md +349 -0
  19. package/corpus/web/react/components/navigation-menu.dropdown.md +220 -0
  20. package/corpus/web/react/components/select.basic.md +318 -0
  21. package/corpus/web/react/components/select.native.md +108 -0
  22. package/corpus/web/react/components/switch.basic.md +151 -0
  23. package/corpus/web/react/components/toast.basic.md +112 -0
  24. package/corpus/web/react/components/tooltip.basic.md +139 -0
  25. package/corpus/web/react/global/global_rules.md +292 -0
  26. package/corpus/web/react/patterns.json +946 -0
  27. package/dist/config.js +35 -0
  28. package/dist/contracts/v1/types.js +6 -0
  29. package/dist/http.js +103 -0
  30. package/dist/index.js +8 -0
  31. package/dist/mcp/createServer.js +122 -0
  32. package/dist/mcp/response.js +7 -0
  33. package/dist/mcp/server.js +15 -0
  34. package/dist/repo/cache.js +27 -0
  35. package/dist/repo/globalRules.js +304 -0
  36. package/dist/repo/index.js +178 -0
  37. package/dist/repo/paths.js +25 -0
  38. package/dist/repo/sections.js +166 -0
  39. package/dist/smoke/smoke-remote-mcp.js +43 -0
  40. package/dist/telemetry.js +15 -0
  41. package/dist/telemetryWrap.js +48 -0
  42. package/dist/tools/getGlobalRules.js +33 -0
  43. package/dist/tools/getPattern.js +54 -0
  44. package/dist/tools/listPatterns.js +33 -0
  45. package/dist/utils/fs.js +18 -0
  46. package/dist/utils/hash.js +4 -0
  47. package/package.json +68 -0
package/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or Derivative
95
+ Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and do
117
+ not modify the License. You may add Your own attribution notices
118
+ within Derivative Works that You distribute, alongside or as an
119
+ addendum to the NOTICE text from the Work, provided that such
120
+ additional attribution notices cannot be construed as modifying
121
+ the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright 2026 A11y Context
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,62 @@
1
+ # @a11y-context/mcp-server
2
+
3
+ A [Model Context Protocol](https://modelcontextprotocol.io) server that serves the [A11y Context](https://a11y-context-project.vercel.app) accessibility-pattern corpus — WCAG 2.2 AA component patterns and cross-cutting Foundations rules — to AI coding agents.
4
+
5
+ The server is the **retrieval mechanism**. Pair it with the A11y Context **MCP skill** (the brain that selects which patterns a task needs); the skill calls these tools. See the [setup guide](https://a11y-context-project.vercel.app/getting-started/ai-coding-agents/install/mcp-server).
6
+
7
+ ## Install
8
+
9
+ **Claude Code:**
10
+
11
+ ```bash
12
+ claude mcp add a11y-context -- npx -y @a11y-context/mcp-server
13
+ ```
14
+
15
+ Or add it to your MCP client config (`.mcp.json`, `.cursor/mcp.json`, etc.):
16
+
17
+ ```json
18
+ {
19
+ "mcpServers": {
20
+ "a11y-context": {
21
+ "command": "npx",
22
+ "args": ["-y", "@a11y-context/mcp-server"]
23
+ }
24
+ }
25
+ }
26
+ ```
27
+
28
+ Then run `/mcp` (in Claude Code) to confirm it's connected.
29
+
30
+ ## Tools
31
+
32
+ | Tool | Purpose |
33
+ |---|---|
34
+ | `list_patterns` | List the available component patterns for a stack. Call first to select which patterns a task needs (via each entry's `selection_excerpt`). |
35
+ | `get_pattern` | Get the full spec for one pattern by `id`: `must_haves`, `donts`, `golden_pattern`, `customizable`, `acceptance_checks`. |
36
+ | `get_foundations` | Get the cross-cutting Foundations rules (focus, landmarks, headings, contrast, page structure). Retrieve on every UI task; each rule's `scope` decides where it applies. |
37
+
38
+ All tools take a `stack` argument (defaults to `web/react`, the only fully populated stack today).
39
+
40
+ ## Corpus
41
+
42
+ The server ships a bundled snapshot of the corpus at `corpus/<stack>/`, refreshed per release. Retrieval is deterministic ID-based selection — no vector database, no embeddings.
43
+
44
+ ## Transports
45
+
46
+ - **stdio** (default, via `npx`) — runs locally in your MCP client.
47
+ - **HTTP** — `npm run start` serves the same tools over HTTP for clients that connect by URL.
48
+
49
+ ## Development
50
+
51
+ ```bash
52
+ npm install
53
+ npm run build
54
+ npm run dev # stdio, ts-node
55
+ npm run inspector # MCP Inspector
56
+ ```
57
+
58
+ Override the corpus location with `PATTERN_REPO_PATH` (absolute, or relative to the package root) to develop against a live corpus checkout.
59
+
60
+ ## License
61
+
62
+ [Apache-2.0](./LICENSE). The bundled corpus content is also Apache-2.0.
@@ -0,0 +1,157 @@
1
+ ---
2
+ id: accordion.basic
3
+ title: Accordion
4
+ stack: web/react
5
+ status: beta
6
+ latest_version: 0.1.1
7
+ tags: [accordion, disclosure, show-hide]
8
+ aliases: [accordion, disclosure group, expand collapse, collapsible panels]
9
+ summary: A set of show/hide sections with heading-wrapped buttons controlling associated panels via aria-expanded (and optionally aria-controls / region).
10
+ ---
11
+
12
+ # Accordion
13
+
14
+ Pattern ID: `accordion.basic`
15
+
16
+ A set of show/hide sections with heading-wrapped buttons controlling associated panels via `aria-expanded` (and optionally `aria-controls` / region).
17
+
18
+ ## Use When
19
+ - Use when content can be organized into collapsible sections to reduce scanning and scrolling.
20
+
21
+ ## Do Not Use When
22
+ - Do not use when content must be visible for comparison or comprehension (use static, non-collapsing sections instead).
23
+
24
+ ## Must Haves
25
+ - Accordion is composed of a series of header + panel pairs.
26
+ - Each accordion header control is a native `<button>` (or `role="button"` only when a native button cannot be used).
27
+ - If role="button" is used instead of a native `<button>`, add `tabindex="0"` and keyboard support for Enter and Space, ensuring Space prevents page scrolling while activating the control.
28
+ - Each header button is contained within a heading element (`<h2>`–`<h6>`) or an element with `role="heading"` and the appropriate `aria-level`.
29
+ - The header button uses `aria-expanded="true"` when its panel is visible and `"false"` when hidden.
30
+ - The panel is shown/hidden in the DOM (e.g., via the `hidden` attribute), so that hidden content **cannot** be accessed by screen readers.
31
+ - Users move focus between accordion headers using Tab / Shift+Tab.
32
+ - Since the header control is a button, it is activated with Enter or Space.
33
+ - If the accordion does not permit a panel to be collapsed while expanded, the expanded header button uses `aria-disabled="true"` (rare case).
34
+ - Ensure a visible focus state (e.g., a 2px solid outline offset by 1-2px) around the header controls.
35
+
36
+ ## Customizable
37
+ - `aria-controls` on the header button pointing to the panel ID (recommended, but optional).
38
+ - Panel container uses `role="region"` with `aria-labelledby` referencing the header button ID (optional).
39
+ - Avoid `role="region"` when it would create landmark proliferation (e.g., many panels can be expanded at once, especially > ~6).
40
+ - Prefer `role="region"` when panels contain headings.
41
+ - Whether multiple panels can be expanded at the same time.
42
+ - If only one panel may be expanded, expanding a new panel collapses the previously open panel.
43
+ - The heading level will usually be `h2`, but this is customizable and depends on the heading hierarchy of the surrounding page.
44
+
45
+ ## Don'ts
46
+ - Do not use non-focusable headers (e.g., `<div>` without proper button semantics) as the interactive control.
47
+ - Do not make the panel visible while leaving `aria-expanded="false"` (and vice versa).
48
+ - Do not remove panel content from the DOM in a way that breaks expected focus behavior (e.g., collapsing a panel while focus remains inside it without moving focus).
49
+ - Do not use `role="heading"` without an `aria-level`.
50
+ - Do not nest accordions within accordion panels.
51
+
52
+ ## Golden Pattern
53
+
54
+ Structural reference for AI coding assistants — semantics, focus, and keyboard behavior. Styling, copy, and demo data are illustrative.
55
+
56
+ ```jsx
57
+ "use client";
58
+
59
+ export function AccordionDemo() {
60
+ // Single-expand: exactly one panel open at a time.
61
+ const [openId, setOpenId] = useState("overview");
62
+
63
+ function toggle(id) {
64
+ setOpenId(id);
65
+ }
66
+
67
+ const overviewOpen = openId === "overview";
68
+ const detailsOpen = openId === "details";
69
+ const shippingOpen = openId === "shipping";
70
+
71
+ return (
72
+ <div>
73
+ {/* h3 assumes an h2 section heading exists above the accordion.
74
+ Match the heading level to the surrounding page hierarchy
75
+ (see Customizable). */}
76
+ <h3>
77
+ <button
78
+ id="acc-btn-overview"
79
+ type="button"
80
+ aria-expanded={overviewOpen ? "true" : "false"}
81
+ aria-controls="acc-panel-overview"
82
+ onClick={() => toggle("overview")}
83
+ >
84
+ Overview
85
+ </button>
86
+ </h3>
87
+ <div
88
+ id="acc-panel-overview"
89
+ hidden={!overviewOpen}
90
+ role="region"
91
+ aria-labelledby="acc-btn-overview"
92
+ >
93
+ <p>
94
+ This section contains summary information. Learn more in the{" "}
95
+ <a href="#details">details section</a>.
96
+ </p>
97
+ </div>
98
+
99
+ <h3>
100
+ <button
101
+ id="acc-btn-details"
102
+ type="button"
103
+ aria-expanded={detailsOpen ? "true" : "false"}
104
+ aria-controls="acc-panel-details"
105
+ onClick={() => toggle("details")}
106
+ >
107
+ Details
108
+ </button>
109
+ </h3>
110
+ <div
111
+ id="acc-panel-details"
112
+ hidden={!detailsOpen}
113
+ role="region"
114
+ aria-labelledby="acc-btn-details"
115
+ >
116
+ <p>
117
+ This panel includes supporting text and a link to{" "}
118
+ <a href="#policies">policies</a>.
119
+ </p>
120
+ </div>
121
+
122
+ <h3>
123
+ <button
124
+ id="acc-btn-shipping"
125
+ type="button"
126
+ aria-expanded={shippingOpen ? "true" : "false"}
127
+ aria-controls="acc-panel-shipping"
128
+ onClick={() => toggle("shipping")}
129
+ >
130
+ Shipping
131
+ </button>
132
+ </h3>
133
+ <div
134
+ id="acc-panel-shipping"
135
+ hidden={!shippingOpen}
136
+ role="region"
137
+ aria-labelledby="acc-btn-shipping"
138
+ >
139
+ <p>Shipping information goes here. This panel contains only text.</p>
140
+ </div>
141
+ </div>
142
+ );
143
+ }
144
+ ```
145
+
146
+ ## Acceptance Checks
147
+
148
+ - Keyboard
149
+ - Tab/Shift+Tab moves focus through accordion header buttons in order.
150
+ - Enter and Space toggle the associated panel visibility.
151
+ - Focus remains on the header button after toggling.
152
+ - If a panel is collapsed while focus is inside it (implementation choice), focus is moved to a sensible place (typically the controlling header).
153
+ - Screen Reader
154
+ - Each header is announced as a button within a heading.
155
+ - The expanded/collapsed state is announced via `aria-expanded`.
156
+ - If `role="region"` is used, the panel is announced with a name that matches the controlling header.
157
+ - Links inside expanded panels are reachable and operate normally.
@@ -0,0 +1,91 @@
1
+ ---
2
+ id: button.basic
3
+ title: Basic Button
4
+ stack: web/react
5
+ status: beta
6
+ latest_version: 0.2.1
7
+ tags: [button, control, action, icon-button]
8
+ aliases: [btn, primary button, icon button, call to action, cta]
9
+ summary: Native button that triggers an action. Supports text-only, icon+text, and icon-only labeling patterns.
10
+ ---
11
+
12
+ # Basic Button
13
+
14
+ Pattern ID: `button.basic`
15
+
16
+ Native button that triggers an action. Supports text-only, icon+text, and icon-only labeling patterns.
17
+
18
+ ## Use When
19
+ - Use when the user triggers an immediate action (e.g., "Save", "Continue", "Dismiss").
20
+
21
+ ## Do Not Use When
22
+ - Do not use when the control navigates to a new URL (use `link`).
23
+ - Do not use when the control represents an on/off pressed state (use `button.toggle`).
24
+ - Do not use when the control opens a menu (use `menu.basic`).
25
+
26
+ ## Must Haves
27
+ - Use a native `<button>` for built-in semantics and keyboard behavior.
28
+ - A custom implementation with `role="button"` is appropriate only when a native button cannot be used.
29
+ - If role="button" is used instead of a native <button>, add tabindex="0" and keyboard support for Enter and Space, ensuring Space prevents page scrolling while activating the control.
30
+ - The button has an accessible name that describes its purpose or action.
31
+ - When the button has visible text, the visible text serves as the accessible name.
32
+ - When additional context is needed beyond the visible text, add it via `aria-label`, `aria-labelledby`, or offscreen text. The visible text appears at the start of the accessible name.
33
+ - For icon-only buttons, provide an accessible name using `aria-label` or `aria-labelledby`.
34
+ - Icons within buttons must be decorative (`aria-hidden="true"`).
35
+ - If the action is unavailable, disable the button using the native `disabled` attribute. (It becomes unfocusable and non-interactive.)
36
+ - Ensure a visible focus state (e.g., a 2px solid outline offset by 1-2px) around the button.
37
+
38
+ ## Customizable
39
+ - No accessibility-relevant variations beyond the Must Haves above.
40
+
41
+ ## Don'ts
42
+ - Do not build a button out of a `<div>` or `<span>` with `role="button"` unless you absolutely must; native `<button>` is the baseline.
43
+ - Do not create icon-only buttons without an accessible name (no unlabeled icons).
44
+ - Do not use `aria-label` that conflicts with (or is wildly different from) the visible label text. Accessible names should at least begin with the visible label.
45
+ - Do not hide focus outlines without providing a strong custom focus style.
46
+
47
+ ## Golden Pattern
48
+
49
+ Structural reference for AI coding assistants — semantics, focus, and keyboard behavior. Styling, copy, and demo data are illustrative.
50
+
51
+ ```jsx
52
+ export function ButtonBasicDemo() {
53
+ return (
54
+ <div>
55
+ {/* Text-only */}
56
+ <button type="button" onClick={() => alert("Saved")}>
57
+ Save
58
+ </button>
59
+
60
+ {/* Icon + text */}
61
+ <button type="button" onClick={() => alert("Downloaded")}>
62
+ <span aria-hidden="true">[icon]</span> Download
63
+ </button>
64
+
65
+ {/* Icon-only (must have accessible name) */}
66
+ <button
67
+ type="button"
68
+ aria-label="Open settings"
69
+ onClick={() => alert("Settings")}
70
+ >
71
+ <span aria-hidden="true">[icon]</span>
72
+ </button>
73
+
74
+ {/* Disabled */}
75
+ <button type="button" disabled onClick={() => alert("Won't fire")}>
76
+ Disabled
77
+ </button>
78
+ </div>
79
+ );
80
+ }
81
+ ```
82
+
83
+ ## Acceptance Checks
84
+ - Tab to the button: a visible focus indicator is present.
85
+ - Press Space or Enter: the button activates.
86
+ - Text-only button: screen reader announces the visible label.
87
+ - Icon+text button: screen reader announces the text label (icon is not redundantly announced).
88
+ - Icon-only button: screen reader announces the `aria-label` (e.g., "Open settings").
89
+ - Disabled button:
90
+ - Cannot be activated by click/keyboard.
91
+ - Is not focusable when `disabled` is set.
@@ -0,0 +1,109 @@
1
+ ---
2
+ id: button.toggle
3
+ title: Toggle Button
4
+ stack: web/react
5
+ status: beta
6
+ latest_version: 0.2.1
7
+ tags: [button, toggle, pressed, aria-pressed, mute-button]
8
+ aliases: [toggle button, pressed button]
9
+ summary: Two- or three-state button that toggles between pressed and not pressed using aria-pressed.
10
+ ---
11
+
12
+ # Toggle Button
13
+
14
+ Pattern ID: `button.toggle`
15
+
16
+ Two- or three-state button that toggles between pressed and not pressed using `aria-pressed`.
17
+
18
+ ## Use When
19
+ - Use when a control toggles a feature or action within the current context (e.g., "Mute", "Bold", "Pin", "Enable Closed Captioning").
20
+
21
+ ## Do Not Use When
22
+ - Do not use when the control navigates to a new URL (use `link`).
23
+ - Do not use when the control represents a persistent on/off system or application setting, such as "Enable notifications", "Dark mode" (use `switch.basic`).
24
+ - Do not use when the control records a value to submit with a form rather than toggling something in the current context (use `checkbox.basic`).
25
+ - Do not use when the control opens a menu (use `menu.basic`).
26
+
27
+ ## Must Haves
28
+ - Use a native `<button>` element for built-in semantics and keyboard behavior.
29
+ - The button has an accessible name that describes its purpose or action.
30
+ - Default strategy: represent state by changing the accessible name to the next action (e.g., "Mute" ↔ "Unmute", "Pin" ↔ "Remove pin").
31
+ - When the button has visible text, the visible text serves as the accessible name.
32
+ - When additional context is needed beyond the visible text, add it via `aria-label`, `aria-labelledby`, or offscreen text. The visible text appears at the start of the accessible name.
33
+ - For icon-only buttons, provide an accessible name using `aria-label` or `aria-labelledby`.
34
+ - Icons within buttons must be decorative (`aria-hidden="true"`).
35
+ - If the action is unavailable, disable the button using the native `disabled` attribute. (It becomes unfocusable and non-interactive.)
36
+ - Ensure a visible focus state (e.g., a 2px solid outline offset by 1-2px) around the button.
37
+
38
+ ### Formatting toolbar exception
39
+ - If the control is a formatting toggle in a toolbar (e.g., Bold/Italic/Underline), use aria-pressed="true|false" to reflect whether formatting is currently applied.
40
+ - In this toolbar case, keep the accessible name stable (e.g., "Bold") and do not rename it to "Remove bold" or "Unbold".
41
+
42
+ ## Customizable
43
+ - For most toggles (non-toolbar), you may express "next action" via:
44
+ - Visible text (preferred when space allows), and/or
45
+ - `aria-label` / `aria-labelledby` (required for icon-only).
46
+ - You may add context to the accessible name when multiple similar toggles exist (e.g., "Mute Trailer", "Unmute Trailer") using `aria-label`, `aria-labelledby`, or offscreen text.
47
+
48
+ ## Don'ts
49
+ - Do not use `aria-pressed` for non-toolbar toggles **if** you are already changing the accessible name to the next action (avoid conflicting models like "Unmute, pressed").
50
+ - Do not leave `aria-pressed` incorrect, stale, or always `"true"` / always `"false"` when you choose the toolbar approach.
51
+ - Do not ship icon-only toggles without an accessible name (`aria-label` or `aria-labelledby`).
52
+ - Do not put state only in the icon (screen reader users must get state via the accessible name change or `aria-pressed`, depending on strategy).
53
+
54
+ ## Golden Pattern
55
+
56
+ Structural reference for AI coding assistants — semantics, focus, and keyboard behavior. Styling, copy, and demo data are illustrative.
57
+
58
+ ```jsx
59
+ export function ToggleButtonDemo() {
60
+ const [muted, setMuted] = useState(false);
61
+ const [iconOnlyMuted, setIconOnlyMuted] = useState(false);
62
+ const [pinned, setPinned] = useState(false);
63
+ const [bold, setBold] = useState(false);
64
+
65
+ return (
66
+ <div>
67
+ <p>Toggle state indicated by accessible name change</p>
68
+
69
+ <button type="button" onClick={() => setMuted((v) => !v)}>
70
+ <span aria-hidden="true">[icon]</span>{" "}
71
+ {muted ? "Unmute" : "Mute"}
72
+ </button>
73
+
74
+ <button type="button" onClick={() => setPinned((v) => !v)}>
75
+ <span aria-hidden="true">[icon]</span>{" "}
76
+ {pinned ? "Unpin" : "Pin"}
77
+ </button>
78
+
79
+ <button
80
+ type="button"
81
+ onClick={() => setIconOnlyMuted((v) => !v)}
82
+ aria-label={iconOnlyMuted ? "Unmute" : "Mute"}
83
+ >
84
+ <span aria-hidden="true">[icon]</span>
85
+ </button>
86
+
87
+ <hr />
88
+
89
+ <p>Toggle state indicated by aria-pressed (toolbar formatting)</p>
90
+
91
+ <button
92
+ type="button"
93
+ aria-pressed={bold ? "true" : "false"}
94
+ onClick={() => setBold((v) => !v)}
95
+ >
96
+ <span aria-hidden="true">[icon]</span>{" "}
97
+ Bold
98
+ </button>
99
+ </div>
100
+ );
101
+ }
102
+ ```
103
+
104
+ ## Acceptance Checks
105
+ - Keyboard activation
106
+ - Tab to each control: a visible focus indicator is present.
107
+ - Press Space or Enter: the control activates/toggles.
108
+ - Either the button's accessible name adjusts to reflect its state (preferred), or it remains constant and the value of `aria-pressed` reflects its state
109
+ - Icons are not announced (decorative via `aria-hidden="true"`).