Skip to content

Commit

Permalink
Allow disabling autofocus on error summary
Browse files Browse the repository at this point in the history
Make it possible to disable the autofocus behaviour on the error summary by setting a data attribute on the module (or by setting disableAutoFocus if using the Nunjucks macro). This is consistent with the approach currently used for the notifcation banner.

The main use case for this is on the Design System, where the auto focus is problematic for the examples provided within the guidance – it causes the browser to scroll down to the first example on the page. At the minute, we are working around this by 'monkey-patching' the init method to remove the call to $module.focus [1].

[1]: alphagov/govuk-design-system#643
  • Loading branch information
36degrees committed Jan 5, 2022
1 parent 0177f37 commit 74e6efe
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/govuk/components/error-summary/error-summary.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ ErrorSummary.prototype.init = function () {
ErrorSummary.prototype.setFocus = function () {
var $module = this.$module

if ($module.getAttribute('data-disable-auto-focus') === 'true') {
return
}

// 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')
Expand Down
18 changes: 18 additions & 0 deletions src/govuk/components/error-summary/error-summary.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,24 @@ describe('Error Summary', () => {
expect(tabindex).toBeNull()
})

describe('when auto-focus is disabled', () => {
it('does not have a tabindex attribute', async () => {
await page.goto(`${baseUrl}/components/error-summary/autofocus-disabled/preview`, { waitUntil: 'load' })

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

expect(tabindex).toBeNull()
})

it('does not focus on page load', async () => {
await page.goto(`${baseUrl}/components/error-summary/autofocus-disabled/preview`, { waitUntil: 'load' })

const activeElement = await page.evaluate(() => document.activeElement.dataset.module)

expect(activeElement).not.toBe('govuk-error-summary')
})
})

const inputTypes = [
// [description, input id, selector for label or legend]
['an input', 'input', 'label[for="input"]'],
Expand Down
9 changes: 9 additions & 0 deletions src/govuk/components/error-summary/error-summary.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ params:
type: object
required: false
description: HTML attributes (for example data attributes) to add to the error link anchor.
- name: disableAutoFocus
type: boolean
required: false
description: Prevent moving focus to the error summary when the page loads.
- name: classes
type: string
required: false
Expand Down Expand Up @@ -183,3 +187,8 @@ examples:
-
text: Descriptive link to the <b>question</b> with an error
href: '#error-1'
- name: autofocus disabled
hidden: true
data:
titleText: There is a problem
disableAutoFocus: true
1 change: 1 addition & 0 deletions src/govuk/components/error-summary/template.njk
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<div class="govuk-error-summary
{%- if params.classes %} {{ params.classes }}{% endif %}" aria-labelledby="error-summary-title" role="alert"
{%- if params.disableAutoFocus %} data-disable-auto-focus="true"{% endif %}
{%- 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

0 comments on commit 74e6efe

Please sign in to comment.