Test component compliance with web accessibility standards
@storybook/addon-a11y is a Storybook addon that provides comprehensive accessibility testing capabilities for Storybook stories. It integrates axe-core, a powerful accessibility testing engine, to automatically run accessibility audits on components and stories within the Storybook interface, enabling developers to identify and fix accessibility issues during development.
npx storybook add @storybook/addon-a11yMain addon functionality:
import addon, { PARAM_KEY, Setup, A11yParameters } from "@storybook/addon-a11y";Preview functionality:
import { experimental_afterEach, initialGlobals, parameters } from "@storybook/addon-a11y/preview";For CommonJS:
const { PARAM_KEY, Setup } = require("@storybook/addon-a11y");
const { experimental_afterEach } = require("@storybook/addon-a11y/preview");The addon provides both automated and manual accessibility testing capabilities. After installation, it automatically adds an accessibility panel to Storybook and runs tests on stories.
// Configure a story with accessibility parameters
export const ButtonStory = {
parameters: {
a11y: {
config: {
rules: [
{ id: 'color-contrast', enabled: true }
]
},
options: {
runOnly: ['wcag2a', 'wcag2aa']
}
}
}
};
// Configure global accessibility settings
export const globals = {
a11y: {
manual: false // Enable automatic testing
}
};The addon consists of several key components:
experimental_afterEach hookCore addon functionality providing the main export and parameter constants for integrating accessibility testing into Storybook.
declare const addon: () => any;
export default addon;
export const PARAM_KEY: "a11y";Automatic accessibility testing integration for Storybook preview with configurable test execution and reporting.
export const experimental_afterEach: AfterEach<any>;
export const initialGlobals: {
a11y: {
manual: boolean;
};
};
export const parameters: {
a11y: A11yParameters;
};Type definitions for configuring accessibility testing parameters and global settings.
export interface A11yParameters {
/**
* Accessibility configuration
*
* @see https://storybook.js.org/docs/writing-tests/accessibility-testing
*/
a11y?: {
/** Manual configuration for specific elements */
element?: ElementContext;
/**
* Configuration for the accessibility rules
*
* @see https://github.com/dequelabs/axe-core/blob/develop/doc/API.md#api-name-axeconfigure
*/
config?: Spec;
/**
* Options for the accessibility checks To learn more about the available options,
*
* @see https://github.com/dequelabs/axe-core/blob/develop/doc/API.md#options-parameter
*/
options?: RunOptions;
/** Remove the addon panel and disable the addon's behavior */
disable?: boolean;
};
}
export interface A11yGlobals {
/**
* Accessibility configuration
*
* @see https://storybook.js.org/docs/writing-tests/accessibility-testing
*/
a11y: {
/**
* Prevent the addon from executing automated accessibility checks upon visiting a story. You
* can still trigger the checks from the addon panel.
*
* @see https://storybook.js.org/docs/writing-tests/accessibility-testing#turn-off-automated-a11y-tests
*/
manual?: boolean;
};
}
export type A11YReport = AxeResults | { error: Error };Automated migration script that runs after addon installation to configure accessibility testing.
export default function postinstall(options: PostinstallOptions): Promise<void>;
interface PostinstallOptions {
packageManager: PackageManagerName;
configDir: string;
yes?: boolean;
}
type PackageManagerName = 'npm' | 'yarn' | 'pnpm' | 'bun';Usage Example:
// Typically called automatically after installation
// Can be manually invoked if needed
import postinstall from '@storybook/addon-a11y/postinstall';
await postinstall({ yes: true });The addon handles accessibility testing errors gracefully:
The addon supports multiple testing environments:
tessl i tessl/npm-storybook--addon-a11y@8.6.0