import { CodeSnippet } from '@fluentui/docs-components'; import * as React from 'react'; import { Header } from '@fluentui/react-northstar'; import DocPage from '../components/DocPage'; import { code, link } from '../utils/helpers'; import { Link } from 'react-router-dom'; export default () => (
Content
Overview

In Fluent UI, accessibility behaviors encapsulate the logic needed for keyboard navigation, focus handling and screen reading. They essentially add ARIA roles, ARIA attributes and event handlers to components' parts. The idea is to compose visual components and apply a behavior on top of them to achieve the desired keyboard navigation and screen reader support.

Each relevant component comes with its default accessibility behavior. For some components there are additional behaviors to choose from. In addition to that, user can create custom behaviors and use them instead of the standard ones.

Users are also able to add or override attributes generated by the accessibility behaviors if customization is needed.

Accessibility behavior type is a callback function which receives "props" as parameters and returns an accessibility behavior object. Type: {code('(props: any) => AccessibilityDefinition')}.

This {code('AccessibilityDefinition')} object consist of next properties which can be set in behavior:
ARIA attributes

Accessible Rich Internet Applications (ARIA) is a set of attributes that define ways to make web content more accessible to people with disabilities.

ARIA attributes are applied according to {link('ARIA specification', 'https://www.w3.org/TR/wai-aria-1.1/')} and{' '} {link('ARIA best practices', 'https://www.w3.org/TR/wai-aria-practices-1.1/')}.

For example, Menu component:

`} />

Default accessibility behavior for {code('Menu')} component is {code('menuBehavior')} ({code('menuItemBehavior')}{' '} for the {code('MenuItem')}). These behaviors add appropriate ARIA roles by default:

Rendered HTML:

`} />

Menu behavior - role "menu" is applied to root part (container) of the component:

({ //... attributes: { root: { role: 'menu', }, }, //... }) `} />

Menu item behavior - role "presentation" is applied to wrapper part of the component (which is {code('

  • ')} element by default) and role "menuitem" and other attributes applied to the root part of the component (which is{' '} {code('')} element by default):

    ({ //... attributes: { wrapper: { role: 'presentation', }, root: { role: 'menuitem', tabIndex: 0, 'aria-expanded': props.menu ? props.menuOpen || false : undefined, 'aria-haspopup': props.menu ? 'true' : undefined, 'aria-label': props['aria-label'], 'aria-labelledby': props['aria-labelledby'], 'aria-describedby': props['aria-describedby'], 'aria-disabled': !_.isNil(props['aria-disabled']) ? props['aria-disabled'] : !!props['disabled'] ? true : undefined, [IS_FOCUSABLE_ATTRIBUTE]: !props['disabled'], 'aria-posinset': props.itemPosition, 'aria-setsize': props.itemsCount, }, }, //... }) `} />

    User can override the default behavior by using the {code('accessibility')} attribute, as well as override generated attributes:

    `} />

    Apart from overriding the default behavior with an existent one, it is also possible to create your own custom behavior.

    ({ attributes: { root: { //... }, }, keyActions: { root: { //... }, }, focusZone: {}, }) `} />

    Also, if a particular behavior matches your needs but you want to change some of the props, you can create a new behavior based on an existent one and override what is needed.

    For example, using {code('menuBehavior')}, creates a new behavior with some of its FocusZone's props overridden.

    { const behavior = menuBehavior(props) behavior.focusZone.props.defaultTabbableElement = (root: HTMLElement): HTMLElement => { return root.querySelector(".ui-menu__item__wrapper:last-child") } return behavior } `} /> `} />

    All Fluent UI behaviors implementations can be found on the{' '} {link( 'GitHub', 'https://github.com/microsoft/fluentui/tree/master/packages/fluentui/accessibility/src/behaviors', )} .

    The default and other available behaviors for all the components can be found in the {link('documentation', '/')}, together with notes on other accessibility considerations for using the component. The examples show the recommended way of using the components in different variations. It is possible to edit example's code, see the rendered HTML, change themes and validate the rendering in RTL scenario, or with different behaviors.

    Read more about:

    • FocusZone
    • FocusTrapZone
    • AutoFocusZone

    );