-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Link popover: prevent mouse event propagation (#11753)
* Prevent mouse event propagation in popover This could propagate through to parent handlers (such as mousedown for selection) that removes the popover, causing it to disappear. * Add e2e test to test IsolatedEventContainer Triggers problem in #11186 and ensures link popover remains * Update doc manifest with IsolatedEventContainer
- Loading branch information
1 parent
bcf55ff
commit 2e6f051
Showing
8 changed files
with
141 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
packages/components/src/isolated-event-container/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Isolated Event Container | ||
|
||
This is a container that prevents certain events from propagating outside of the container. This is used to wrap | ||
UI elements such as modals and popovers where the propagated event can cause problems. The event continues to work | ||
inside the component. | ||
|
||
For example, a `mousedown` event in a modal container can propagate to the surrounding DOM, causing UI outside of the | ||
modal to be interacted with. | ||
|
||
The current isolated events are: | ||
- mousedown - This prevents UI interaction with other `mousedown` event handlers, such as selection | ||
|
||
## Usage | ||
|
||
Creates a custom component that won't propagate `mousedown` events outside of the component. | ||
|
||
```jsx | ||
import { IsolatedEventContainer } from '@wordpress/components'; | ||
|
||
const MyModal = () => { | ||
return ( | ||
<IsolatedEventContainer | ||
className="component-some_component" | ||
onClick={ clickHandler } | ||
> | ||
<p>This is an isolated component</p> | ||
</IsolatedEventContainer> | ||
); | ||
}; | ||
``` | ||
|
||
## Props | ||
|
||
All props are passed as-is to the `<IsolatedEventContainer />` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { Component } from '@wordpress/element'; | ||
|
||
class IsolatedEventContainer extends Component { | ||
constructor( props ) { | ||
super( props ); | ||
|
||
this.stopEventPropagationOutsideContainer = this.stopEventPropagationOutsideContainer.bind( this ); | ||
} | ||
|
||
stopEventPropagationOutsideContainer( event ) { | ||
event.stopPropagation(); | ||
} | ||
|
||
render() { | ||
const { children, ... props } = this.props; | ||
|
||
// Disable reason: this stops certain events from propagating outside of the component. | ||
// - onMouseDown is disabled as this can cause interactions with other DOM elements | ||
/* eslint-disable jsx-a11y/no-static-element-interactions */ | ||
return ( | ||
<div | ||
{ ... props } | ||
onMouseDown={ this.stopEventPropagationOutsideContainer } | ||
> | ||
{ children } | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
export default IsolatedEventContainer; |
26 changes: 26 additions & 0 deletions
26
packages/components/src/isolated-event-container/test/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { shallow } from 'enzyme'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import IsolatedEventContainer from '../'; | ||
|
||
describe( 'IsolatedEventContainer', () => { | ||
it( 'should pass props to container', () => { | ||
const isolated = shallow( <IsolatedEventContainer className="test" onClick="click" /> ); | ||
|
||
expect( isolated.hasClass( 'test' ) ).toBe( true ); | ||
expect( isolated.prop( 'onClick' ) ).toBe( 'click' ); | ||
} ); | ||
|
||
it( 'should stop mousedown event propagation', () => { | ||
const isolated = shallow( <IsolatedEventContainer /> ); | ||
const event = { stopPropagation: jest.fn() }; | ||
|
||
isolated.simulate( 'mousedown', event ); | ||
expect( event.stopPropagation ).toHaveBeenCalled(); | ||
} ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters