From bd19b1171c3e1596ed720bca9d8ff579719d4577 Mon Sep 17 00:00:00 2001
From: Pea Tyczynska
Date: Tue, 8 Sep 2020 15:51:29 +0100
Subject: [PATCH 1/9] Autofocus now optional for live_search
This change has been made because autofocus has been confusing
to users of screenreaders because they orient themselves by
what is in focus. Moving it when the page loads without warning
can cause confusion and mean they miss parts of the page
before the point focus has moved to.
So now we will only turn on autofocus if there are no other
elements in the page that need attention than the search box.
---
app/templates/components/live-search.html | 22 +++++++++++++---------
1 file changed, 13 insertions(+), 9 deletions(-)
diff --git a/app/templates/components/live-search.html b/app/templates/components/live-search.html
index 4108ded7f..8c6641b60 100644
--- a/app/templates/components/live-search.html
+++ b/app/templates/components/live-search.html
@@ -1,21 +1,25 @@
-{% from "components/textbox.html" import textbox %}
-
{% macro live_search(
target_selector=None,
show=False,
form=None,
- label=None
+ label=None,
+ autofocus=False
) %}
{%- set search_label = label or form.search.label.text %}
+
+ {%- set param_extensions = {
+ "label": {"text": search_label},
+ "autocomplete": "off",
+ } %}
+
+ {% if autofocus %}
+ {% set x=param_extensions.__setitem__("attributes", {"data-module": "autofocus"}) %}
+ {% endif %}
+
{% if show %}
-
{% endif %}
{% endmacro %}
From ad6dc670f1b653d800b737c480c6135615757598 Mon Sep 17 00:00:00 2001
From: Pea Tyczynska
Date: Wed, 9 Sep 2020 17:57:27 +0100
Subject: [PATCH 2/9] Turn autofocus on for broadcast areas search box
Accessibility audit uncovered issues screen-reader users would
have with autofocus on Notify pages. Autofocus can lead to
confusion for those types of users and make them miss parts
of the page.
Hence, we decided that autofocus will be turned off by default
for live-search component.
We decided to keep the autofocus on for live-search text box when:
1. the page is a task page - as opposed to browsing pages, where
user wonders freely, task pages have more established flow, so
page focusing on textbox can actually be helpful.
2. page does not have actionable elements above the autofocus.
---
app/templates/views/broadcast/areas-with-sub-areas.html | 8 +++++++-
app/templates/views/broadcast/areas.html | 8 +++++++-
2 files changed, 14 insertions(+), 2 deletions(-)
diff --git a/app/templates/views/broadcast/areas-with-sub-areas.html b/app/templates/views/broadcast/areas-with-sub-areas.html
index 2f1f02a7f..640f69772 100644
--- a/app/templates/views/broadcast/areas-with-sub-areas.html
+++ b/app/templates/views/broadcast/areas-with-sub-areas.html
@@ -14,7 +14,13 @@
back_link=url_for('.choose_broadcast_library', service_id=current_service.id, broadcast_message_id=broadcast_message.id),
)}}
- {{ live_search(target_selector='.file-list-item', show=show_search_form, form=search_form, label='Search by name') }}
+ {{ live_search(
+ target_selector='.file-list-item',
+ show=show_search_form,
+ form=search_form,
+ label='Search by name',
+ autofocus=True)
+ }}
{% for area in library|sort %}
diff --git a/app/templates/views/broadcast/areas.html b/app/templates/views/broadcast/areas.html
index 9ed0a38e8..f55c364cf 100644
--- a/app/templates/views/broadcast/areas.html
+++ b/app/templates/views/broadcast/areas.html
@@ -16,7 +16,13 @@
back_link=url_for('.choose_broadcast_library', service_id=current_service.id, broadcast_message_id=broadcast_message.id),
)}}
- {{ live_search(target_selector='.govuk-checkboxes__item', show=show_search_form, form=search_form, label='Search by name') }}
+ {{ live_search(
+ target_selector='.govuk-checkboxes__item',
+ show=show_search_form,
+ form=search_form,
+ label='Search by name',
+ autofocus=True)
+ }}
{% call form_wrapper() %}
{{ form.areas }}
From d7637dcca410c80da7e8b9fc657fc3e8f8e9bf1a Mon Sep 17 00:00:00 2001
From: Pea Tyczynska
Date: Wed, 9 Sep 2020 18:05:18 +0100
Subject: [PATCH 3/9] Turn autofocus on for platform admin branding pages
search boxes
Accessibility audit uncovered issues screen-reader users would
have with autofocus on Notify pages. Autofocus can lead to
confusion for those types of users and make them miss parts
of the page.
Hence, we decided that autofocus will be turned off by default
for live-search component.
We decided to keep the autofocus on for live-search text box when:
1. the page is a task page - as opposed to browsing pages, where
user wonders freely, task pages have more established flow, so
page focusing on textbox can actually be helpful.
2. page does not have actionable elements above the autofocus.
---
app/templates/views/email-branding/select-branding.html | 8 +++++---
.../views/letter-branding/select-letter-branding.html | 8 +++++---
2 files changed, 10 insertions(+), 6 deletions(-)
diff --git a/app/templates/views/email-branding/select-branding.html b/app/templates/views/email-branding/select-branding.html
index eb6e8e3cc..bd584176c 100644
--- a/app/templates/views/email-branding/select-branding.html
+++ b/app/templates/views/email-branding/select-branding.html
@@ -3,14 +3,16 @@
{% from "components/live-search.html" import live_search %}
{% from "components/button/macro.njk" import govukButton %}
+{% set page_title = "Email branding" %}
+
{% block per_page_title %}
- Email branding
+ {{ page_title }}
{% endblock %}
{% block platform_admin_content %}
-
From a73573c165e15ef09e2d344f5f8bd1ffdafe23e6 Mon Sep 17 00:00:00 2001
From: Pea Tyczynska
Date: Thu, 10 Sep 2020 16:31:35 +0100
Subject: [PATCH 7/9] Turn autofocus on for copy template
Accessibility audit uncovered issues screen-reader users would
have with autofocus on Notify pages. Autofocus can lead to
confusion for those types of users and make them miss parts
of the page.
Hence, we decided that autofocus will be turned off by default
for live-search component.
We decided to keep the autofocus on for live-search text box when:
1. the page is a task page - as opposed to browsing pages, where
user wonders freely, task pages have more established flow, so
page focusing on textbox can actually be helpful.
2. page does not have actionable elements above the autofocus.
---
app/templates/views/templates/copy.html | 12 +++++++++---
1 file changed, 9 insertions(+), 3 deletions(-)
diff --git a/app/templates/views/templates/copy.html b/app/templates/views/templates/copy.html
index 7aac461ec..6bd5f74ba 100644
--- a/app/templates/views/templates/copy.html
+++ b/app/templates/views/templates/copy.html
@@ -2,15 +2,16 @@
{% from "components/live-search.html" import live_search %}
{% extends "withnav_template.html" %}
+{% set page_title = "Copy an existing template" %}
{% block service_page_title %}
- Copy an existing template
+ {{ page_title }}
{% endblock %}
{% block maincolumn_content %}
From 6cf7017ff5366c69796c1483852d11caadebef4f Mon Sep 17 00:00:00 2001
From: Tom Byers
Date: Tue, 15 Sep 2020 21:23:49 +0100
Subject: [PATCH 8/9] Add tests for changes to label
---
tests/javascripts/autofocus.test.js | 29 ++++++++++++++++++++++++++++-
1 file changed, 28 insertions(+), 1 deletion(-)
diff --git a/tests/javascripts/autofocus.test.js b/tests/javascripts/autofocus.test.js
index 52b516613..5ea5179a8 100644
--- a/tests/javascripts/autofocus.test.js
+++ b/tests/javascripts/autofocus.test.js
@@ -1,3 +1,5 @@
+const helpers = require('./support/helpers.js');
+
beforeAll(() => {
require('../../app/assets/javascripts/autofocus.js');
});
@@ -8,16 +10,19 @@ afterAll(() => {
describe('Autofocus', () => {
+ const labelText = 'Search by name';
let focusHandler;
let search;
beforeEach(() => {
+ document.title = 'Find services by name - GOV.UK Notify';
+
// set up DOM
document.body.innerHTML =
`
`;
@@ -45,6 +50,28 @@ describe('Autofocus', () => {
});
+ test('has a label including the page title when the module starts', () => {
+
+ // start module
+ window.GOVUK.modules.start();
+
+ expect(search.hasAttribute('aria-label')).toBe(true);
+ expect(search.getAttribute('aria-label')).toEqual(document.title + ' - ' + labelText);
+
+ });
+
+ test('gets the original label back when focus moves away', () => {
+
+ // start module
+ window.GOVUK.modules.start();
+
+ // shift focus away from textbox
+ helpers.triggerEvent(search, 'blur');
+
+ expect(search.hasAttribute('aria-label')).toBe(false);
+
+ });
+
test('is not focused if the window has scrolled', () => {
// mock the window being scrolled 25px
From 2639406ae25d57db238c4e2188f526c750dcfeb8 Mon Sep 17 00:00:00 2001
From: Tom Byers
Date: Tue, 15 Sep 2020 22:09:03 +0100
Subject: [PATCH 9/9] Simulate page load announcement in autofocus JS
Screenreaders announce the title of the page when
it loads and their users are used to it signifying
a new page.
By focusing a form control when the page loads,
this announcement is replaced with that of the
control label.
This commit prefixes the label with the page title so it
gets announced when the page loads, notifying
screenreader users that they are on a new page.
The page title prefix is removed once focus shifts
from the form control as its presence in any
further announcements could be confusing.
---
app/assets/javascripts/autofocus.js | 17 +++++++++++++++--
1 file changed, 15 insertions(+), 2 deletions(-)
diff --git a/app/assets/javascripts/autofocus.js b/app/assets/javascripts/autofocus.js
index 002a676a2..4d3d94849 100644
--- a/app/assets/javascripts/autofocus.js
+++ b/app/assets/javascripts/autofocus.js
@@ -3,13 +3,26 @@
Modules.Autofocus = function() {
this.start = function(component) {
- var forceFocus = $(component).data('forceFocus');
+ var $component = $(component),
+ forceFocus = $component.data('forceFocus'),
+ labelText = $('label[for="' + $component.attr('id') + '"]').eq(0).text().trim(),
+ clearAriaLabel = evt => {
+ $component.removeAttr('aria-label');
+ $component.off('blur', clearAriaLabel);
+ };
// if the page loads with a scroll position, we can't assume the item to focus onload
// is still where users intend to start
if (($(window).scrollTop() > 0) && !forceFocus) { return; }
- $(component).filter('input, textarea, select').eq(0).trigger('focus');
+ // screenreaders announce the page title when a new page loads
+ // this will be lost when focus is moved to our form control so add it to the label instead
+ $component.attr('aria-label', document.title + ' - ' + labelText);
+
+ $component.filter('input, textarea, select').eq(0).trigger('focus');
+
+ // the page title prefix is only needed on page load so remove once focus has shifted
+ $component.on('blur', clearAriaLabel);
};
};