From e76e9c7a8e338e3147efbb4967801e699365bc7c Mon Sep 17 00:00:00 2001 From: "Manuel J. Thomsen" Date: Wed, 30 May 2018 22:22:23 +0200 Subject: [PATCH 1/2] Only run a11y analysis on demand --- addons/a11y/src/components/Panel.js | 20 +++++++++++++++++++- addons/a11y/src/index.js | 9 ++++----- addons/a11y/src/shared/index.js | 3 ++- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/addons/a11y/src/components/Panel.js b/addons/a11y/src/components/Panel.js index d4c118dea037..7fa732256f8c 100644 --- a/addons/a11y/src/components/Panel.js +++ b/addons/a11y/src/components/Panel.js @@ -3,7 +3,9 @@ import PropTypes from 'prop-types'; import styled from 'react-emotion'; -import { CHECK_EVENT_ID } from '../shared'; +import Events from '@storybook/core-events'; + +import { CHECK_EVENT_ID, RERUN_EVENT_ID, REQUEST_CHECK_EVENT_ID } from '../shared'; import Tabs from './Tabs'; import Report from './Report'; @@ -33,10 +35,20 @@ class Panel extends Component { componentDidMount() { this.props.channel.on(CHECK_EVENT_ID, this.onUpdate); + this.props.channel.on(Events.STORY_RENDERED, this.requestCheck); + this.props.channel.on(RERUN_EVENT_ID, this.requestCheck); + } + + componentDidUpdate(prevProps) { + if (!prevProps.active && this.props.active) { + this.requestCheck(); + } } componentWillUnmount() { this.props.channel.removeListener(CHECK_EVENT_ID, this.onUpdate); + this.props.channel.removeListener(Events.STORY_RENDERED, this.requestCheck); + this.props.channel.removeListener(RERUN_EVENT_ID, this.requestCheck); } onUpdate = ({ passes, violations }) => { @@ -46,6 +58,12 @@ class Panel extends Component { }); }; + requestCheck = () => { + if (this.props.active) { + this.props.channel.emit(REQUEST_CHECK_EVENT_ID); + } + }; + render() { const { passes, violations } = this.state; const { active } = this.props; diff --git a/addons/a11y/src/index.js b/addons/a11y/src/index.js index e330e75f4a95..7d579a9b66d6 100644 --- a/addons/a11y/src/index.js +++ b/addons/a11y/src/index.js @@ -4,7 +4,7 @@ import addons from '@storybook/addons'; import Events from '@storybook/core-events'; import { logger } from '@storybook/client-logger'; -import { CHECK_EVENT_ID, RERUN_EVENT_ID } from './shared'; +import { CHECK_EVENT_ID, REQUEST_CHECK_EVENT_ID } from './shared'; let axeOptions = {}; @@ -23,11 +23,10 @@ const runA11yCheck = () => { const a11ySubscription = () => { const channel = addons.getChannel(); - channel.on(Events.STORY_RENDERED, runA11yCheck); - channel.on(RERUN_EVENT_ID, runA11yCheck); + channel.on(REQUEST_CHECK_EVENT_ID, runA11yCheck); + return () => { - channel.removeListener(Events.STORY_RENDERED, runA11yCheck); - channel.removeListener(RERUN_EVENT_ID, runA11yCheck); + channel.removeListener(REQUEST_CHECK_EVENT_ID, runA11yCheck); }; }; diff --git a/addons/a11y/src/shared/index.js b/addons/a11y/src/shared/index.js index 2bcaf9d19683..5c810f2a67f6 100755 --- a/addons/a11y/src/shared/index.js +++ b/addons/a11y/src/shared/index.js @@ -3,5 +3,6 @@ const ADDON_ID = '@storybook/addon-a11y'; const PANEL_ID = `${ADDON_ID}/panel`; const CHECK_EVENT_ID = `${ADDON_ID}/check`; const RERUN_EVENT_ID = `${ADDON_ID}/rerun`; +const REQUEST_CHECK_EVENT_ID = `${ADDON_ID}/request-check`; -export { ADDON_ID, PANEL_ID, CHECK_EVENT_ID, RERUN_EVENT_ID }; +export { ADDON_ID, PANEL_ID, CHECK_EVENT_ID, RERUN_EVENT_ID, REQUEST_CHECK_EVENT_ID }; From 46cdbaceea903d5f780e441b8172c67d58313a6e Mon Sep 17 00:00:00 2001 From: "Manuel J. Thomsen" Date: Wed, 30 May 2018 22:25:20 +0200 Subject: [PATCH 2/2] Use named export for event name --- addons/a11y/src/components/Panel.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/addons/a11y/src/components/Panel.js b/addons/a11y/src/components/Panel.js index 7fa732256f8c..1a004f461c31 100644 --- a/addons/a11y/src/components/Panel.js +++ b/addons/a11y/src/components/Panel.js @@ -3,7 +3,7 @@ import PropTypes from 'prop-types'; import styled from 'react-emotion'; -import Events from '@storybook/core-events'; +import { STORY_RENDERED } from '@storybook/core-events'; import { CHECK_EVENT_ID, RERUN_EVENT_ID, REQUEST_CHECK_EVENT_ID } from '../shared'; @@ -35,7 +35,7 @@ class Panel extends Component { componentDidMount() { this.props.channel.on(CHECK_EVENT_ID, this.onUpdate); - this.props.channel.on(Events.STORY_RENDERED, this.requestCheck); + this.props.channel.on(STORY_RENDERED, this.requestCheck); this.props.channel.on(RERUN_EVENT_ID, this.requestCheck); } @@ -47,7 +47,7 @@ class Panel extends Component { componentWillUnmount() { this.props.channel.removeListener(CHECK_EVENT_ID, this.onUpdate); - this.props.channel.removeListener(Events.STORY_RENDERED, this.requestCheck); + this.props.channel.removeListener(STORY_RENDERED, this.requestCheck); this.props.channel.removeListener(RERUN_EVENT_ID, this.requestCheck); }