diff --git a/src/govuk/components/error-summary/error-summary.js b/src/govuk/components/error-summary/error-summary.js index bbb93caae3..dfa017178c 100644 --- a/src/govuk/components/error-summary/error-summary.js +++ b/src/govuk/components/error-summary/error-summary.js @@ -11,11 +11,28 @@ ErrorSummary.prototype.init = function () { if (!$module) { return } - $module.focus() + this.setFocus() $module.addEventListener('click', this.handleClick.bind(this)) } +/** + * Focus the error summary + */ +ErrorSummary.prototype.setFocus = function () { + var $module = this.$module + + // Set tabindex to -1 to make the element programmatically focusable, but + // remove it on blur as the error summary doesn't need to be focused again. + $module.setAttribute('tabindex', '-1') + + $module.addEventListener('blur', function () { + $module.removeAttribute('tabindex') + }) + + $module.focus() +} + /** * Click event handler * diff --git a/src/govuk/components/error-summary/error-summary.test.js b/src/govuk/components/error-summary/error-summary.test.js index 906e5c26d6..4e6f561a3a 100644 --- a/src/govuk/components/error-summary/error-summary.test.js +++ b/src/govuk/components/error-summary/error-summary.test.js @@ -13,6 +13,15 @@ describe('Error Summary', () => { expect(moduleName).toBe('govuk-error-summary') }) + it('removes the tabindex attribute on blur', async () => { + await page.goto(baseUrl + '/components/error-summary/preview', { waitUntil: 'load' }) + + await page.$eval('.govuk-error-summary', el => el.blur()) + + const tabindex = await page.$eval('.govuk-error-summary', el => el.getAttribute('tabindex')) + expect(tabindex).toBeNull() + }) + const inputTypes = [ // [description, input id, selector for label or legend] ['an input', 'input', 'label[for="input"]'], diff --git a/src/govuk/components/error-summary/template.njk b/src/govuk/components/error-summary/template.njk index 44d02541a4..1ea907fbe5 100644 --- a/src/govuk/components/error-summary/template.njk +++ b/src/govuk/components/error-summary/template.njk @@ -1,5 +1,5 @@