diff --git a/addons/a11y/src/a11yRunner.ts b/addons/a11y/src/a11yRunner.ts index 73e97a6340cd..1f4fd7ddeab6 100644 --- a/addons/a11y/src/a11yRunner.ts +++ b/addons/a11y/src/a11yRunner.ts @@ -15,11 +15,6 @@ let active = false; // Holds latest story we requested a run let activeStoryId: string | undefined; -const getElement = () => { - const storyRoot = document.getElementById('story-root'); - return storyRoot ? storyRoot.childNodes : document.getElementById('root'); -}; - /** * Handle A11yContext events. * Because the event are sent without manual check, we split calls @@ -41,13 +36,14 @@ const run = async (storyId: string) => { channel.emit(EVENTS.RUNNING); const axe = (await import('axe-core')).default; - const { element = getElement(), config, options = {} } = input; + const { element = '#root', config, options = {} } = input; + const htmlElement = document.querySelector(element); axe.reset(); if (config) { axe.configure(config); } - const result = await axe.run(element, options); + const result = await axe.run(htmlElement, options); // It's possible that we requested a new run on a different story. // Unfortunately, axe doesn't support a cancel method to abort current run. // We check if the story we run against is still the current one, diff --git a/examples/official-storybook/stories/addon-a11y/parameters.stories.js b/examples/official-storybook/stories/addon-a11y/parameters.stories.js new file mode 100644 index 000000000000..77c2a22f57b6 --- /dev/null +++ b/examples/official-storybook/stories/addon-a11y/parameters.stories.js @@ -0,0 +1,38 @@ +import React from 'react'; +import BaseButton from '../../components/BaseButton'; + +export default { + title: 'Addons/A11y/Parameters', + component: BaseButton, + parameters: { + options: { selectedPanel: 'storybook/a11y/panel' }, + }, +}; + +export const ElementId = () => ( + <> +

+ The a11y.element parameter is set to the Insufficient contrast{' '} + section.{' '} +

+ +
+ No discernable button text +

This a11y violation should not be reported, as this section is not scanned.

+ +
+
+ Insufficient contrast +

This a11y issue (incomplete) should be reported.

+ +
+ +); +ElementId.parameters = { + a11y: { + element: '#insufficient-contrast', + }, +};