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

fix(Portal): take focus on mount and restore on unmount #1154

Merged
merged 1 commit into from
Jan 17, 2017

Conversation

fracmak
Copy link
Member

@fracmak fracmak commented Jan 13, 2017

All portal based elements (modals, popups, etc) are not keyboard accessible because they're usually injected at the end of the dom and not in the flow of the document. This pull request fixes that by putting focus on the popup when open (except when closeOnTriggerBlur is true) and restores the focus to the last known focus point on close.

@codecov-io
Copy link

codecov-io commented Jan 13, 2017

Current coverage is 95.88% (diff: 100%)

Merging #1154 into master will increase coverage by <.01%

@@             master      #1154   diff @@
==========================================
  Files           879        879          
  Lines          4881       4888     +7   
  Methods           0          0          
  Messages          0          0          
  Branches          0          0          
==========================================
+ Hits           4680       4687     +7   
  Misses          201        201          
  Partials          0          0          

Powered by Codecov. Last update a0d8011...8e24170

@@ -363,6 +363,14 @@ class Portal extends Component {
)

this.portal = this.node.firstElementChild
// don't take focus away from portals that close on blur
if (!closeOnTriggerBlur) {
this.triggerNode = document.activeElement
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Could we use another name here?

If a portal is triggered programmatically, the activeElement will not necessarily be the triggerNode. These are only the same when the user clicked on a trigger, such as a button. How about something like previousActiveElement or similar?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

this.portal.setAttribute('tabindex', '-1')
this.portal.style.outline = 'none'
// wait a tick for things like popups which need to calculate where the popup shows up
setTimeout(() => this.portal && this.portal.focus())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to pull this side effect out of the render method if at all possible. I haven't the bandwidth at the moment to find a better place myself, but open to suggestions.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically this code is being called by either componentDidMount or componentDidUpdate depending on when 'open' is set to true which is after the render method. Or are you saying you'd like me to make a named helper function that's called here? There's no later life cycle function I can use to trigger this behavior

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I see, thanks for the clarification. From my initial pass I assumed this was called in a render() method itself. -1 for assumptions :)

@levithomason levithomason changed the title fix(Portal) portal should take focus when open and restore when closed fix(Portal): take focus on mount and restore on unmount Jan 17, 2017
@levithomason levithomason merged commit 6258ca1 into Semantic-Org:master Jan 17, 2017
@levithomason
Copy link
Member

Released in semantic-ui-react@0.64.1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants