I have a sidebar element that uses tabIndex
and onBlur
to control visibility, so when the user selects anything outside of the sidebar it automatically hides.
This works well, but I need to add a drop-down menu to the sidebar which then gets focus
and causes the sidebar to collapse (before a user could select something).
state = {
visible: true
}
componentDidMount () {
this.focusSidebar()
}
componentDidUpdate () {
if (this.state.visible) this.focusSidebar()
}
focusSidebar () {
ReactDOM.findDOMNode(this.refs.sidebarRegion).focus()
}
hideSidebar () => {
this.setState({ visible: false })
}
render () {
return (
<div
onBlur={this.hideSidebar}
tabIndex='0'
className={`sidebar ${this.state.visible ? '' : 'hidden'}`}
ref='sidebarRegion'
>
<select>
<option>Foo</option>
</select>
</div>
)
}
I'm not seeing a good way to handle this with my current implementation of the sidebar, but I'm trying to find a way to self-contain the sidebar element without needing to hoist the visible/hidden state outside of the component.