Skip to content
This repository has been archived by the owner on Sep 19, 2021. It is now read-only.

Commit

Permalink
don't rely on a timeout for the section focus element to be rendered
Browse files Browse the repository at this point in the history
  • Loading branch information
afeld committed Jul 6, 2018
1 parent 8f898a0 commit caf2cf9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
19 changes: 17 additions & 2 deletions src/components/Main/App.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from 'react'
import { withRouter } from 'react-router'
import { i18n } from '../../config'
import { SectionTitle, ProgressBar, ScoreCard, Navigation, NavigationToggle } from '..'
import { Introduction, Show } from '../Form'
Expand Down Expand Up @@ -38,6 +39,20 @@ class App extends React.Component {
}
this.showInstructions = this.showInstructions.bind(this)
this.dismissInstructions = this.dismissInstructions.bind(this)

// workaround for not having React.createRef(), introduced in React 16.3
// https://reactjs.org/docs/refs-and-the-dom.html#dont-overuse-refs
this.sectionFocusEl = null
this.setSectionFocusEl = (el) => {
this.sectionFocusEl = el
}
}

componentDidUpdate(prevProps) {
// for keyboard navigation accessbility, focus on the main content area after a new section is navigated to
if (this.props.location.pathname !== prevProps.location.pathname) {
this.sectionFocusEl.focus()
}
}

showInstructions (event) {
Expand Down Expand Up @@ -146,7 +161,7 @@ class App extends React.Component {
<button onClick={this.showInstructions} className="instructions mobile-visible"><span>{i18n.t('app.instructions')}</span></button>
&nbsp;
</div>
<a href="javascript:;;;" className="eapp-section-focus" title="Main content. Please press TAB to go to the next question"></a>
<a href="javascript:;;;" className="eapp-section-focus" title="Main content. Please press TAB to go to the next question" ref={this.setSectionFocusEl}></a>
<div id="main-content" className={klassCore}>
{this.props.children}
&nbsp;
Expand Down Expand Up @@ -179,4 +194,4 @@ function mapStateToProps (state) {

// Wraps the the App component with connect() which adds the dispatch()
// function to the props property for this component
export default connect(mapStateToProps)(App)
export default withRouter(connect(mapStateToProps)(App))
16 changes: 0 additions & 16 deletions src/components/Section/Section.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,6 @@ class Section extends React.Component {
this.update(this.props)
}

focusOnMainSection () {
const el = window.document.querySelector('.eapp-section-focus')
if (el) {
el.focus()
}
}

componentDidUpdate (prevProps) {
// for keyboard navigation accessbility, focus on the main content area after a new section is navigated to
if (this.props.location.pathname !== prevProps.location.pathname) {
window.setTimeout(() => {
this.focusOnMainSection()
}, 200)
}
}

update (props) {
const subsection = props.subsection || 'intro'
const path = `/form/${props.section}/${subsection}`
Expand Down

0 comments on commit caf2cf9

Please sign in to comment.