Commit defde85e authored by Connor Bär's avatar Connor Bär Committed by Joe Haddad

fix(Link): Set focus back to the body element (#7693)

* fix(Link): Set focus back to the body element

* refactor(Link): Fix typings

* Update blurring logic and add tests
parent 87acbdb5
...@@ -180,7 +180,16 @@ class Link extends Component<LinkProps> { ...@@ -180,7 +180,16 @@ class Link extends Component<LinkProps> {
if (!success) return if (!success) return
if (scroll) { if (scroll) {
window.scrollTo(0, 0) window.scrollTo(0, 0)
document.body.focus() // set focus back to the body element
const activeElement = document.activeElement as HTMLElement
const bodyElement = document.querySelector('body')
if (
activeElement &&
activeElement !== bodyElement &&
activeElement.blur
) {
activeElement.blur()
}
} }
}) })
.catch((err: any) => { .catch((err: any) => {
......
...@@ -74,6 +74,20 @@ describe('Client Navigation', () => { ...@@ -74,6 +74,20 @@ describe('Client Navigation', () => {
await browser.close() await browser.close()
}) })
it('should set focus back to body after navigating', async () => {
const browser = await webdriver(context.appPort, '/nav')
await browser
.elementByCss('#about-link')
.click()
.waitForElementByCss('.nav-about')
const result = await browser.eval(
`document.activeElement === document.querySelector('body')`
)
expect(result).toBe(true)
})
it('should navigate via the client side', async () => { it('should navigate via the client side', async () => {
const browser = await webdriver(context.appPort, '/nav') const browser = await webdriver(context.appPort, '/nav')
......
...@@ -254,6 +254,20 @@ describe('Production Usage', () => { ...@@ -254,6 +254,20 @@ describe('Production Usage', () => {
expect(newText).toBe('server') expect(newText).toBe('server')
}) })
it('should set focus back to body after navigating', async () => {
const browser = await webdriver(context.appPort, '/')
await browser
.elementByCss('a')
.click()
.waitForElementByCss('.about-page')
const result = await browser.eval(
`document.activeElement === document.querySelector('body')`
)
expect(result).toBe(true)
})
it('should change query correctly', async () => { it('should change query correctly', async () => {
const browser = await webdriver(appPort, '/query?id=0') const browser = await webdriver(appPort, '/query?id=0')
let id = await browser.elementByCss('#q0').text() let id = await browser.elementByCss('#q0').text()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment