-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Components: Add onFocusOutside replacement to Popover onClickOutside #14851
Changes from all commits
d0a60d5
2c86e97
6231b63
738d987
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,21 @@ | ||
/** | ||
* External dependencies | ||
* WordPress dependencies | ||
*/ | ||
import clickOutside from 'react-click-outside'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unrelated question to this PR. Can we also refactor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I would like to, yes. ( #6261 (comment) 😞 ) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is still hope then :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. https://unpkg.com/[email protected]/index.js Well, it looks like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I opened a follow-up to address it – #16878. |
||
import { Component } from '@wordpress/element'; | ||
|
||
/** | ||
* WordPress dependencies | ||
* Internal dependencies | ||
*/ | ||
import { Component } from '@wordpress/element'; | ||
import withFocusOutside from '../higher-order/with-focus-outside'; | ||
|
||
class PopoverDetectOutside extends Component { | ||
handleClickOutside( event ) { | ||
const { onClickOutside } = this.props; | ||
if ( onClickOutside ) { | ||
onClickOutside( event ); | ||
} | ||
handleFocusOutside( event ) { | ||
this.props.onFocusOutside( event ); | ||
} | ||
|
||
render() { | ||
return this.props.children; | ||
} | ||
} | ||
|
||
export default clickOutside( PopoverDetectOutside ); | ||
export default withFocusOutside( PopoverDetectOutside ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could be a nice hook const |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a bit stretched use of IconButton in the case when there is no icon 😅
However, I can't think of another simple way of handling it where this doesn't trigger re-render of the button. I noticed that this behavior is broken only because we swap
IconButton
withButton
for no reason when only the icon gets removed/added.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, exactly! Switching between the two components causes React to remount the component and lose focus. Looking at
IconButton
, it seemed to me thaticon={}
is an optional prop so I felt comfortable doing it this way.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One thing we keep discussing here and there is why not just add an
icon
prop to theButton
block and deprecate theIconButton
(or make it just a shortcut)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm sure @drw158 @cburton4 have some good ideas on how to approach it :)
I'm fine with anything which makes it less confusing. I guess having more flexible
Button
andIconButton
as a backward-compatible alias makes a lot of sense.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have an issue open to discuss combining IconButton and Button in #16541
We are leaning towards keeping them separate, but I'll look at this closer to understand the disadvantage.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm assuming from the code that this is an issue because Button does not have an icon option. Switching between icon and non-icon buttons for Menu Items is causing technical problems.
Could this be solved by adding an icon option to the Button component, but still keep IconButton a separate component? That way, the unchecked and checked versions of the Menu Item could use just a single component, Button.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be fair, we already do the opposite here. Not pass the "icon" prop to the IconButton (so it behaves like a Button). If it doesn't make sense to merge the two components into one, I feel we shouldn't do anything specific about it.