Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent error summary from being refocused after it has been initially focused on page load #2491

Merged
merged 3 commits into from
Jan 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,18 @@

## Unreleased

### Recommended changes

#### Remove the tabindex attribute from the Error Summary component

If you're not using Nunjucks macros, remove the `tabindex` attribute from the HTML for the Error Summary component. This attribute is now added and removed by the JavaScript.

This change was introduced in [#2491: Prevent error summary from being re-focused after it has been initially focused on page load](https://github.com/alphagov/govuk-frontend/pull/2491).

### Fixes

- [#2475: Tweak whitespace in input component HTML for improved readability](https://github.com/alphagov/govuk-frontend/pull/2475)
- [#2491: Prevent error summary from being re-focused after it has been initially focused on page load](https://github.com/alphagov/govuk-frontend/pull/2491)

## 4.0.0 (Breaking release)

Expand Down
19 changes: 18 additions & 1 deletion src/govuk/components/error-summary/error-summary.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
16 changes: 16 additions & 0 deletions src/govuk/components/error-summary/error-summary.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,29 @@ const PORT = configPaths.ports.test
const baseUrl = 'http://localhost:' + PORT

describe('Error Summary', () => {
it('adds the tabindex attribute on page load', async () => {
await page.goto(baseUrl + '/components/error-summary/preview', { waitUntil: 'load' })

const tabindex = await page.$eval('.govuk-error-summary', el => el.getAttribute('tabindex'))
expect(tabindex).toEqual('-1')
})

it('is automatically focused when the page loads', async () => {
await page.goto(`${baseUrl}/components/error-summary/preview`, { waitUntil: 'load' })

const moduleName = await page.evaluate(() => document.activeElement.dataset.module)
expect(moduleName).toBe('govuk-error-summary')
})

it('removes the tabindex attribute on blur', async () => {
vanitabarrett marked this conversation as resolved.
Show resolved Hide resolved
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"]'],
Expand Down
2 changes: 1 addition & 1 deletion src/govuk/components/error-summary/template.njk
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div class="govuk-error-summary
{%- if params.classes %} {{ params.classes }}{% endif %}" aria-labelledby="error-summary-title" role="alert" tabindex="-1"
{%- if params.classes %} {{ params.classes }}{% endif %}" aria-labelledby="error-summary-title" role="alert"
{%- for attribute, value in params.attributes %} {{ attribute }}="{{ value }}"{% endfor %} data-module="govuk-error-summary">
<h2 class="govuk-error-summary__title" id="error-summary-title">
{{ params.titleHtml | safe if params.titleHtml else params.titleText }}
Expand Down
7 changes: 0 additions & 7 deletions src/govuk/components/error-summary/template.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,6 @@ describe('Error-summary', () => {
expect(roleAttr).toEqual('alert')
})

it('has the correct tabindex attribute to be focussed', () => {
const $ = render('error-summary', examples.default)
const tabindexAttr = $('.govuk-error-summary').attr('tabindex')

expect(tabindexAttr).toEqual('-1')
})

it('renders title text', () => {
const $ = render('error-summary', examples.default)
const summaryTitle = $('.govuk-error-summary__title').text().trim()
Expand Down