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

A11y change focus order on mobile devices #6879

Closed
wants to merge 5 commits into from
Closed
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
2 changes: 2 additions & 0 deletions app/assets/javascript/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import ClipboardController from './js_components/clipboard/clipboard';
import FormController from './js_components/form/form';
import LocationFinderController from './js_components/locationFinder/locationFinder';
import ManageQualificationsController from './js_components/manageQualifications/manageQualifications';
import MobileFocusController from './js_components/mobileFocus/mobileFocus';
import PanelController from './js_components/panel/panel';
import ShowHiddenContentController from './js_components/showHiddenContent/showHiddenContent';
import TrackedLinkController from './js_components/trackedLink/trackedLink';
Expand Down Expand Up @@ -52,6 +53,7 @@ application.register('location-finder', LocationFinderController);
application.register('manage-qualifications', ManageQualificationsController);
application.register('map', MapController);
application.register('map-sidebar', MapSidebarController);
application.register('mobile-focus', MobileFocusController);
application.register('panel', PanelController);
application.register('searchable-collection', SearchableCollectionComponent);
application.register('editor', EditorController);
Expand Down
30 changes: 30 additions & 0 deletions app/assets/javascript/js_components/mobileFocus/mobileFocus.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Controller } from '@hotwired/stimulus';

export default class MobileFocusController extends Controller {
connect() {
setTimeout(this.handleMobilePageFocus.bind(this), 100);
}

static detectMobile() {
return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
}

handleMobilePageFocus() {
if (!this.constructor.detectMobile()) {
return;
}

window.onbeforeunload = () => {
window.scrollTo(0, 0);
};

if (window.location.href.includes('profile/personal-details')) {
const skipLinkElement = document.querySelector('.govuk-skip-link');

if (skipLinkElement && skipLinkElement.length > 0) {
skipLinkElement[0].focus();
document.activeElement.blur();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

.govuk-grid-row
.govuk-grid-column-two-thirds
= form_for @step, url: { action: :update }, method: :post, as: :personal_details_form do |f|
= form_for @step, url: { action: :update }, method: :post, as: :personal_details_form, html: { data: { controller: "mobile-focus" } } do |f|
= f.govuk_error_summary

= f.govuk_fieldset legend: { text: t(".heading"), tag: "h1", size: "l" }, caption: { text: t(".caption"), size: "l" } do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

.govuk-grid-row
.govuk-grid-column-two-thirds
= form_for @step, url: { action: :update }, method: :post, as: :personal_details_form do |f|
= form_for @step, url: { action: :update }, method: :post, as: :personal_details_form, html: { data: { controller: "mobile-focus" } } do |f|
= f.govuk_error_summary

= f.govuk_radio_buttons_fieldset :phone_number_provided, legend: { text: t(".heading"), tag: "h1", size: "l" }, caption: { text: t(".caption"), size: "l" } do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

.govuk-grid-row
.govuk-grid-column-two-thirds
= form_for @step, url: { action: :update }, method: :post, as: :personal_details_form do |f|
= form_for @step, url: { action: :update }, method: :post, as: :personal_details_form, html: { data: { controller: "mobile-focus" } } do |f|
= f.govuk_error_summary

= f.govuk_radio_buttons_fieldset :right_to_work_in_uk,
Expand Down
Loading