-
Notifications
You must be signed in to change notification settings - Fork 336
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1994 from alphagov/add-notifications-auto-focus
- Loading branch information
Showing
6 changed files
with
143 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/govuk/components/notification-banner/notification-banner.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
function NotificationBanner ($module) { | ||
this.$module = $module | ||
} | ||
|
||
/** | ||
* Initialise the component | ||
*/ | ||
NotificationBanner.prototype.init = function () { | ||
var $module = this.$module | ||
// Check for module | ||
if (!$module) { | ||
return | ||
} | ||
|
||
this.setFocus() | ||
} | ||
|
||
/** | ||
* Focus the element | ||
* | ||
* If `role="alert"` is set, focus the element to help some assistive technologies | ||
* prioritise announcing it. | ||
* | ||
* You can turn off the auto-focus functionality by setting `data-disable-auto-focus="true"` in the | ||
* component HTML. You might wish to do this based on user research findings, or to avoid a clash | ||
* with another element which should be focused when the page loads. | ||
*/ | ||
NotificationBanner.prototype.setFocus = function () { | ||
var $module = this.$module | ||
|
||
if ($module.getAttribute('data-disable-auto-focus') === 'true') { | ||
return | ||
} | ||
|
||
if ($module.getAttribute('role') !== 'alert') { | ||
return | ||
} | ||
|
||
// Set tabindex to -1 to make the element focusable with JavaScript. | ||
if (!$module.getAttribute('tabindex')) { | ||
$module.setAttribute('tabindex', '-1') | ||
} | ||
|
||
$module.focus() | ||
} | ||
|
||
export default NotificationBanner |
64 changes: 64 additions & 0 deletions
64
src/govuk/components/notification-banner/notification-banner.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* eslint-env jest */ | ||
|
||
const configPaths = require('../../../../config/paths.json') | ||
const PORT = configPaths.ports.test | ||
|
||
const baseUrl = 'http://localhost:' + PORT | ||
|
||
describe('/components/notification-banner/with-type-as-success', () => { | ||
it('has the correct tabindex to be focused with JavaScript', async () => { | ||
await page.goto(baseUrl + '/components/notification-banner/with-type-as-success/preview', { waitUntil: 'load' }) | ||
|
||
const tabindex = await page.$eval('.govuk-notification-banner', el => el.getAttribute('tabindex')) | ||
|
||
expect(tabindex).toEqual('-1') | ||
}) | ||
|
||
it('is automatically focused when the page loads', async () => { | ||
await page.goto(baseUrl + '/components/notification-banner/with-type-as-success/preview', { waitUntil: 'load' }) | ||
|
||
const activeElement = await page.evaluate(() => document.activeElement.dataset.module) | ||
|
||
expect(activeElement).toBe('govuk-notification-banner') | ||
}) | ||
}) | ||
|
||
describe('components/notification-banner/auto-focus-disabled,-with-type-as-success/', () => { | ||
describe('when auto-focus is disabled', () => { | ||
it('does not have a tabindex attribute', async () => { | ||
await page.goto(`${baseUrl}/components/notification-banner/auto-focus-disabled,-with-type-as-success/preview`, { waitUntil: 'load' }) | ||
|
||
const tabindex = await page.$eval('.govuk-notification-banner', el => el.getAttribute('tabindex')) | ||
|
||
expect(tabindex).toBeFalsy() | ||
}) | ||
|
||
it('does not focus the notification banner', async () => { | ||
await page.goto(`${baseUrl}/components/notification-banner/auto-focus-disabled,-with-type-as-success/preview`, { waitUntil: 'load' }) | ||
|
||
const activeElement = await page.evaluate(() => document.activeElement.dataset.module) | ||
|
||
expect(activeElement).not.toBe('govuk-notification-banner') | ||
}) | ||
}) | ||
}) | ||
|
||
describe('components/notification-banner/role=alert-overridden-to-role=region,-with-type-as-success', () => { | ||
describe('when role is not alert', () => { | ||
it('does not have a tabindex attribute', async () => { | ||
await page.goto(`${baseUrl}/components/notification-banner/role=alert-overridden-to-role=region,-with-type-as-success/preview`, { waitUntil: 'load' }) | ||
|
||
const tabindex = await page.$eval('.govuk-notification-banner', el => el.getAttribute('tabindex')) | ||
|
||
expect(tabindex).toBeFalsy() | ||
}) | ||
|
||
it('does not focus the notification banner', async () => { | ||
await page.goto(`${baseUrl}/components/notification-banner/role=alert-overridden-to-role=region,-with-type-as-success/preview`, { waitUntil: 'load' }) | ||
|
||
const activeElement = await page.evaluate(() => document.activeElement.dataset.module) | ||
|
||
expect(activeElement).not.toBe('govuk-notification-banner') | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.