-
Notifications
You must be signed in to change notification settings - Fork 842
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add EuiWrappingPopover to allow non-React elements to be used as popo…
…ver anchors
- Loading branch information
1 parent
2e74893
commit 01088fe
Showing
7 changed files
with
181 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* eslint-disable react/no-multi-comp */ | ||
import React, { | ||
Component, | ||
} from 'react'; | ||
|
||
import { findDOMNode, render, unmountComponentAtNode } from 'react-dom'; | ||
|
||
import { | ||
EuiWrappingPopover, | ||
} from '../../../../src/components'; | ||
|
||
class PopoverApp extends Component { | ||
constructor(props) { | ||
super(props); | ||
|
||
this.state = { | ||
isPopoverOpen: false, | ||
}; | ||
} | ||
|
||
componentDidMount() { | ||
this.props.anchor.addEventListener('click', this.onButtonClick); | ||
} | ||
|
||
onButtonClick = () => { | ||
this.setState({ | ||
isPopoverOpen: !this.state.isPopoverOpen, | ||
}); | ||
} | ||
|
||
closePopover = () => { | ||
this.setState({ | ||
isPopoverOpen: false, | ||
}); | ||
} | ||
|
||
render() { | ||
return ( | ||
<EuiWrappingPopover | ||
id="popover" | ||
button={this.props.anchor} | ||
isOpen={this.state.isPopoverOpen} | ||
closePopover={this.closePopover} | ||
> | ||
<div>Normal JSX content populates the popover.</div> | ||
</EuiWrappingPopover> | ||
); | ||
} | ||
} | ||
|
||
export default class extends Component { | ||
componentDidMount() { | ||
const thisNode = findDOMNode(this); | ||
const thisAnchor = thisNode.querySelector('button'); | ||
|
||
// `container` can be created here or use an existing DOM element | ||
// the popover DOM is positioned independently of where the container exists | ||
this.container = document.createElement('div'); | ||
document.body.appendChild(this.container); | ||
|
||
render( | ||
<PopoverApp anchor={thisAnchor}/>, | ||
this.container | ||
); | ||
} | ||
|
||
componentWillUnmount() { | ||
unmountComponentAtNode(this.container); | ||
} | ||
|
||
render() { | ||
return ( | ||
<div dangerouslySetInnerHTML={{ __html: ` | ||
<button class="euiButton euiButton--primary"> | ||
<span class="euiButton__content">This is an HTML button</span> | ||
</button> | ||
` }} | ||
/> | ||
); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -231,6 +231,7 @@ export { | |
export { | ||
EuiPopover, | ||
EuiPopoverTitle, | ||
EuiWrappingPopover, | ||
} from './popover'; | ||
|
||
export { | ||
|
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { EuiPopover } from './popover'; | ||
export { EuiPopoverTitle } from './popover_title'; | ||
export { EuiWrappingPopover } from './wrapping_popover'; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import React, { Component } from 'react'; | ||
import { findDOMNode } from 'react-dom'; | ||
import { EuiPopover } from './popover'; | ||
import { EuiPortal } from '../portal'; | ||
|
||
/** | ||
* Injects the EuiPopover next to the button via EuiPortal | ||
* then the button element is moved into the popover dom. | ||
* On unmount, the button is moved back to its original location. | ||
*/ | ||
export class EuiWrappingPopover extends Component { | ||
constructor(...args) { | ||
super(...args); | ||
|
||
this.portal = null; | ||
this.contentParent = this.props.button.parentNode; | ||
} | ||
|
||
componentDidMount() { | ||
const thisDomNode = findDOMNode(this); | ||
const placeholderAnchor = thisDomNode.querySelector('.euiWrappingPopover__anchor'); | ||
|
||
placeholderAnchor.insertAdjacentElement( | ||
'beforebegin', | ||
this.props.button | ||
); | ||
} | ||
|
||
componentWillUnmount() { | ||
this.portal.insertAdjacentElement( | ||
'beforebegin', | ||
this.props.button | ||
); | ||
} | ||
|
||
setPortalRef = node => { | ||
this.portal = node; | ||
}; | ||
|
||
render() { | ||
const { | ||
button, // eslint-disable-line no-unused-vars | ||
...rest | ||
} = this.props; | ||
return ( | ||
<EuiPortal | ||
portalRef={this.setPortalRef} | ||
insert={{ sibling: this.props.button, position: 'after' }} | ||
> | ||
<EuiPopover | ||
{...rest} | ||
button={<div className="euiWrappingPopover__anchor"/>} | ||
/> | ||
</EuiPortal> | ||
); | ||
} | ||
} |
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