MSBA-271 · Lesson 10 of 12 · 19 min
Accessibility in custom controls
▶ Video coming soon A video version of this lesson is on the way.
Ingrith Varsamis ran accessibility services for Lumenvale's students and staff, and she reviewed every app the facilities programme released. Her reviews of model-driven and canvas apps were usually short, because the platform's standard controls are built to be accessible. Custom code was different. "When you write your own control," she told Wiremu, "you take over the job Microsoft was doing for you. Everything the standard control did for a screen reader user, a keyboard user or someone with low vision is now yours." This session covered what that job involves.
Why custom controls need special care
Standard controls in model-driven and canvas apps come with keyboard support, focus handling, labels for screen readers and tested colour contrast. A code component or code app draws its own interface with HTML, so none of that is automatic. A div with a click handler looks like a button but cannot be reached with the Tab key, is not announced as a button by a screen reader and does not respond to Enter or Space. Each of these is a barrier for someone, and each is the developer's responsibility to remove.
The standard most institutions use is the Web Content Accessibility Guidelines (WCAG) 2.1 at level AA, published by the W3C. WCAG is organized under four principles, often remembered as POUR: content must be perceivable, operable, understandable and robust. Microsoft designs its own products to WCAG and its Accessibility Conformance Reports describe how each product meets them, but those reports cover Microsoft's controls, not the controls Lumenvale writes.
| Principle | What it asks | Example in a custom control |
|---|---|---|
| Perceivable | Information can be perceived by everyone, whatever their senses | Capacity shown as text as well as a coloured bar |
| Operable | Everything can be operated in more than one way | Every action works with a keyboard, not only a mouse or touch |
| Understandable | Content and behaviour are clear and predictable | Error messages explain what to fix |
| Robust | Content works with assistive technologies | Correct roles, names and states for screen readers |
There is also a legal side, which differs between the two countries. In Canada, accessibility law depends on the jurisdiction. The Accessible Canada Act covers federally regulated organizations, and provinces have their own laws; British Columbia's Accessible British Columbia Act, for example, requires prescribed public sector organizations, including post-secondary institutions, to have an accessibility committee, an accessibility plan and a way for the public to give feedback. Institutions commonly use WCAG 2.1 AA as their technical standard for digital accessibility. Ingrith's position was simpler than the law: Lumenvale's policy required WCAG 2.1 AA for every app staff or students used, and she would not sign off on anything less.
Keyboard access
Every action in a custom control must work from the keyboard alone. Many people cannot use a mouse, and screen reader users navigate mainly by keyboard. In practice this means using real interactive elements, keeping a sensible tab order and showing where focus is.
| Rule | How to meet it |
|---|---|
| Use native elements | Use button, a, input and select rather than clickable div or span elements; they bring keyboard support and roles with them |
| Keep a logical tab order | Follow the visual order in the HTML; avoid positive tabindex values |
| Show focus | Never remove the focus outline without a clear replacement |
| Support expected keys | Enter and Space activate buttons; arrow keys move within lists, grids and menus; Escape closes popups |
| Do not trap focus | Focus must be able to leave the control, except in a modal dialog, which returns focus when it closes |
Fluent UI React components follow these rules already, which is one of the strongest reasons to build with them. A Fluent Button is a real button with focus styles and the expected keys. Wiremu's RoomCapacity bar, which only displayed information, needed no keyboard interaction at all; but the athletics loans app's item tiles did. His first version used div elements with onClick handlers. Ingrith tabbed through the counter screen and the focus jumped from the search box straight past every tile to the footer. Changing the tiles to Fluent Buttons fixed it in an afternoon.
// Not accessible: cannot be reached or activated from the keyboard
<div className="tile" onClick={() => lend(item)}>{item.name}</div>
// Accessible: a real button, with a name that includes the state
<Button
appearance="secondary"
onClick={() => lend(item)}
aria-label={item.name + ", tag " + item.tag + ", " + item.statusText}
>
{item.name}
</Button>Names, roles and states
A screen reader announces each element by its name, its role and its state: for example, "Basketball, tag 114, on loan, button". Native HTML elements provide the role and often the name from their text; custom elements need help from ARIA, the Accessible Rich Internet Applications attributes. The first rule of ARIA is to prefer native elements, because ARIA changes only what is announced, not how the element behaves. A div with role="button" is announced as a button but still does nothing when Enter is pressed unless the developer adds that.
Every input needs a label that a screen reader announces. In a model-driven form the platform shows the column label beside the code component, but the component's own inputs inside it still need accessible names. Wiremu's RoomCapacity component exposed its value with role="meter" and aria-valuenow, aria-valuemin and aria-valuemax, plus a visible text label that said "18 of 20 booked", so that screen reader users heard the same information sighted users saw.
<div
role="meter"
aria-valuemin={0}
aria-valuemax={capacity}
aria-valuenow={booked}
aria-label="Room capacity"
aria-valuetext={booked + " of " + capacity + " booked"}
>
<div className={"bar " + state.level} style={{ width: widthPercent }} />
</div>
<Text>{booked} of {capacity} booked</Text>Colour, contrast and not relying on colour
WCAG 2.1 AA requires a contrast ratio of at least 4.5 to 1 for normal text and 3 to 1 for large text, and 3 to 1 for the parts of controls and graphics needed to understand them, such as a bar against its background or a focus outline. It also requires that colour is never the only way information is shown. The original RoomCapacity bar turned amber at the warning threshold and red when the room was full; a person who cannot tell red from green, or who uses a high contrast theme, would see no difference. The fix was the text label and an icon for the warning and full states.
Model-driven apps and Windows offer high contrast modes, which replace the page's colours with the user's chosen palette. Components that draw with CSS background colours can disappear in high contrast mode. Fluent UI components handle forced colours already; custom CSS should be tested in Windows high contrast mode and use the forced-colors media query where needed. The facilities team's test checklist included a pass in high contrast mode for every custom control.
Changes, errors and announcements
When something changes without the page reloading, such as a loan saved or a search returning results, sighted users see it but screen reader users may not know. A live region, an element with aria-live="polite", makes the screen reader announce changes to its content without moving focus. The loans app used one live region for status messages: "Basketball lent to Jordan Ames, due Friday" after a loan, and "Could not save the return. Try again." after a failure. Fluent UI's toast and message bar components handle this when used as documented.
Error messages must say what went wrong and how to fix it, appear next to the field they concern, and be connected to it with aria-describedby so that a screen reader reads them with the field. Messages that disappear after a few seconds are a problem for anyone who reads slowly; WCAG requires that users can extend or turn off time limits. The loans app kept error messages until the user acted on them.
Zoom, reflow and touch
People with low vision zoom the browser to 200 or 400 percent of normal size. WCAG asks that content reflows at narrow widths without scrolling in two directions. Components should size with relative units and flexible layouts, not fixed pixel widths, and should be tested at high zoom. The counter tablet raised a related need: touch targets must be large enough to hit reliably, and the loans app's large buttons met this by design. Hamza's request for a larger tag number field in lesson 8 turned out to be an accessibility improvement as well.
Testing accessibility
No single test is enough. Automated checkers, such as the Accessibility Insights tools or axe browser extensions, catch missing names, low contrast and invalid ARIA quickly, and can run in the build. They cannot judge whether a name makes sense, whether focus moves sensibly or whether a workflow is usable, so many real problems are left for people to find: a keyboard pass, unplugging the mouse and using only the keyboard; a screen reader pass with Narrator, NVDA or JAWS on Windows, or VoiceOver on iPad; and checks at high zoom and in high contrast mode. Canvas app makers also have the App checker accessibility rules, but those cover standard controls, not the inside of a code component.
Ingrith added the step that mattered most: testing with people who use assistive technology every day. Two staff members in accessibility services, one who uses a screen reader and one who uses keyboard and voice control, tried the loans app's counter screen. They found a problem no tool had flagged: after a loan was saved, focus stayed on the button for the item just lent, which had changed its label, so the screen reader announced the change awkwardly. Moving focus back to the search box after each loan, with the live region announcing the result, made the workflow smooth.
Worked problem
Reviewing the RoomCapacity component
Ingrith reviews RoomCapacity on the Room form. The bar is green, amber or red, with no text. What should she ask for, and why?
Show solution
- Information by colour alone fails WCAG: ask for visible text such as "18 of 20 booked" and an icon for warning and full.
- A screen reader announces nothing meaningful: ask for role="meter" with value attributes and a text alternative.
- The bar against its background must meet 3 to 1 contrast: check the colours, including the amber.
- High contrast mode may hide a background-coloured bar: test in Windows high contrast mode.
Answer: Wiremu added the text, the icons, the meter role and a forced-colors style. The component passed the automated checker, the keyboard and screen reader passes and the high contrast check.
Components and code apps: who owns what
The division of responsibility differs between the two kinds of custom code. A code component lives inside a form or app that the platform draws. The host provides the column label, the tab order between fields and the page structure; the component is responsible for everything inside its own container, and for fitting into the host's order and look. A code app owns the whole page. It must provide a meaningful page title, a heading structure, a language attribute on the page, skip links for long pages, and focus management when moving between screens, none of which a component needs to think about.
Motion is one more consideration for both. Some people find animation distracting or physically uncomfortable, and operating systems let them ask for reduced motion. CSS can respect that request with the prefers-reduced-motion media query, turning off transitions and animations for those users. The loans app's sliding panels used it, and the RoomPicker design specified that the plan would not animate zoom or selection for users who asked for reduced motion.
| Responsibility | Code component | Code app |
|---|---|---|
| Field label | The host form | The developer |
| Page title, headings and language | The host | The developer |
| Focus order between fields | The host form | The developer |
| Keyboard, names and contrast inside the control | The developer | The developer |
| Reduced motion | The developer | The developer |
Common mistakes
MistakeUsing clickable div elements for actions.
InsteadUse native buttons or Fluent UI components, which bring keyboard support and roles.
MistakeShowing status only by colour.
InsteadAdd text or icons, and check contrast.
MistakeAdding ARIA roles without the matching behaviour.
InsteadPrefer native elements; if using ARIA, implement the keyboard behaviour the role promises.
MistakeRelying only on an automated checker.
InsteadAdd keyboard, screen reader, zoom and high contrast passes, and test with real users.
MistakeRemoving focus outlines for a cleaner look.
InsteadKeep a visible focus indicator with enough contrast.
At the end of the session Ingrith signed off both the RoomCapacity component and the loans app, with a note asking that every future custom control go through the same checklist. Wiremu added the checklist to the facilities repository's pull request template, so that the reviewer would see it each time.
Summary and key terms
- Custom controls take over accessibility work that standard controls do automatically.
- Lumenvale requires WCAG 2.1 AA; legal requirements differ by country and jurisdiction.
- Every action must work by keyboard, with visible focus and expected keys; prefer native elements and Fluent UI.
- Give elements correct names, roles and states, using ARIA only where native elements cannot.
- Meet contrast ratios, never rely on colour alone, and test in high contrast mode and at high zoom.
- Announce changes with live regions, connect errors to fields, and test with tools, by hand and with real users.
- WCAG 2.1 AA
- The Web Content Accessibility Guidelines version 2.1 at level AA, the common accessibility standard.
- ARIA
- Accessible Rich Internet Applications attributes that add roles, names and states for assistive technologies.
- Live region
- An element whose changes are announced by screen readers without moving focus.
- Contrast ratio
- The difference in luminance between foreground and background; 4.5 to 1 for normal text at level AA.
- High contrast mode
- An operating system setting that replaces page colours with a user-chosen palette.
- Screen reader
- Software that reads the interface aloud, such as Narrator, NVDA, JAWS or VoiceOver.
Practice problems
Work each problem on paper first, then reveal the answer to check.
Why is a div with role="button" not enough on its own?
Show answer
ARIA changes only what is announced; the developer must still make it focusable and respond to Enter and Space. A native button does all of that.
What contrast ratio does WCAG 2.1 AA require for normal text?
Show answer
At least 4.5 to 1.
How should a code app tell a screen reader user that a save succeeded?
Show answer
Update a polite live region with a status message, without moving focus unexpectedly.
Lesson quiz · 5 questions
Score 60% or more to complete this lesson.
Create a free account to save your progress, take quizzes and earn a certificate.