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

[SPIKE] Fix announcement after picking files when using JAWS #5726

Closed
wants to merge 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,15 @@ export class FileUpload extends ConfigurableComponent {
this.$input.setAttribute('tabindex', '-1')
this.$input.setAttribute('aria-hidden', 'true')

// Create a hidden empty span to take focus before opening the file picker
// (the focus moves to the file picker straight away anyways)
//
// This helps JAWS announce the accessible name of the button after it gets updated
// with the new file name consistently
this.$focusMagnet = document.createElement('span')
this.$focusMagnet.setAttribute('tabindex', '-1')
this.$root.appendChild(this.$focusMagnet)

// Make all these new variables available to the module
this.$button = $button
this.$status = $status
Expand Down Expand Up @@ -340,6 +349,20 @@ export class FileUpload extends ConfigurableComponent {
* When the button is clicked, emulate clicking the actual, hidden file input
*/
onClick() {
// Move the focus out of the button before opening the picker
this.$focusMagnet.focus({ preventScroll: true })

// Once back in the document, set the focus on the button
// which lets JAWS announce the correct accessible name
// rather than the one before picking a file
document.addEventListener(
'focusin',
() => this.$button.focus({ preventScroll: true }),
{
once: true
}
)

this.$input.click()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,33 @@ describe('/components/file-upload', () => {
expect(inputElementValue).toBe(`C:\\fakepath\\${testFilename}`)
}
)

it('moves the focus after clicking to help JAWS announce the correct content', async () => {
await render(page, 'file-upload', examples.enhanced)

const [fileChooser, activeElementHTML] = await Promise.all([
page.waitForFileChooser(),
(async () => {
await page.click(buttonSelector)
return page.evaluate(() => document.activeElement.outerHTML)
})()
])

expect(activeElementHTML).toBe('<span tabindex="-1"></span>')

await fileChooser.accept(['test.txt'])
// Puppeteer does not seem to support focus events in headless mode
// so we can trigger it ourselves
// https://github.com/puppeteer/puppeteer/issues/1462
await page.evaluate(() =>
document.dispatchEvent(new CustomEvent('focusin'))
)

const activeElementClassList = await page.evaluate(() =>
Array.from(document.activeElement.classList)
)
expect(activeElementClassList).toContain('govuk-file-upload-button')
})
})

describe('when selecting a file', () => {
Expand Down
Loading