Skip to content

Commit

Permalink
fix(Modal|Popup|Portal): fix usage of eventStack sub/unsub (#2099)
Browse files Browse the repository at this point in the history
* Fix for Portal usage of EventStack sub/unsub

* Responding to PR comments

* Fixing lint errors

* Addressing PR style comments
  • Loading branch information
austinfox authored and levithomason committed Sep 23, 2017
1 parent 8b1b02d commit de091e6
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
3 changes: 3 additions & 0 deletions src/addons/Portal/Portal.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ export interface PortalProps {
/** Initial value of open. */
defaultOpen?: boolean;

/** Event pool namespace that is used to handle component events. */
eventPool?: string;

/** The node where the portal should mount. */
mountNode?: any;

Expand Down
14 changes: 10 additions & 4 deletions src/addons/Portal/Portal.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ class Portal extends Component {
/** Initial value of open. */
defaultOpen: PropTypes.bool,

/** Event pool namespace that is used to handle component events */
eventPool: PropTypes.string,

/** The node where the portal should mount. */
mountNode: PropTypes.any,

Expand Down Expand Up @@ -125,6 +128,7 @@ class Portal extends Component {
static defaultProps = {
closeOnDocumentClick: true,
closeOnEscape: true,
eventPool: 'default',
openOnTriggerClick: true,
}

Expand Down Expand Up @@ -372,6 +376,7 @@ class Portal extends Component {
debug('mountPortal()')

const {
eventPool,
mountNode = isBrowser ? document.body : null,
prepend,
} = this.props
Expand All @@ -384,15 +389,16 @@ class Portal extends Component {
mountNode.appendChild(this.rootNode)
}

eventStack.sub('click', this.handleDocumentClick, 'Portal')
eventStack.sub('keydown', this.handleEscape, 'Portal')
eventStack.sub('click', this.handleDocumentClick, eventPool)
eventStack.sub('keydown', this.handleEscape, eventPool)
_.invoke(this.props, 'onMount', null, this.props)
}

unmountPortal = () => {
if (!isBrowser || !this.rootNode) return

debug('unmountPortal()')
const { eventPool } = this.props

ReactDOM.unmountComponentAtNode(this.rootNode)
this.rootNode.parentNode.removeChild(this.rootNode)
Expand All @@ -403,8 +409,8 @@ class Portal extends Component {
this.rootNode = null
this.portalNode = null

eventStack.unsub('click', this.handleDocumentClick, 'Portal')
eventStack.unsub('keydown', this.handleEscape, 'Portal')
eventStack.unsub('click', this.handleDocumentClick, eventPool)
eventStack.unsub('keydown', this.handleEscape, eventPool)
_.invoke(this.props, 'onUnmount', null, this.props)
}

Expand Down
3 changes: 3 additions & 0 deletions src/modules/Modal/Modal.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ export interface ModalProps extends PortalProps {
/** A modal can appear in a dimmer. */
dimmer?: boolean | 'blurring' | 'inverted';

/** Event pool namespace that is used to handle component events */
eventPool?: string;

/** A Modal can be passed header via shorthand. */
header?: SemanticShorthandItem<ModalHeaderProps>;

Expand Down
7 changes: 6 additions & 1 deletion src/modules/Modal/Modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ class Modal extends Component {
PropTypes.oneOf(['inverted', 'blurring']),
]),

/** Event pool namespace that is used to handle component events */
eventPool: PropTypes.string,

/** Modal displayed above the content in bold. */
header: customPropTypes.itemShorthand,

Expand Down Expand Up @@ -135,6 +138,7 @@ class Modal extends Component {
dimmer: true,
closeOnDimmerClick: true,
closeOnDocumentClick: false,
eventPool: 'Modal',
}

static autoControlledProps = [
Expand Down Expand Up @@ -315,7 +319,7 @@ class Modal extends Component {

render() {
const { open } = this.state
const { closeOnDimmerClick, closeOnDocumentClick, dimmer } = this.props
const { closeOnDimmerClick, closeOnDocumentClick, dimmer, eventPool } = this.props
const mountNode = this.getMountNode()

// Short circuit when server side rendering
Expand Down Expand Up @@ -353,6 +357,7 @@ class Modal extends Component {
closeOnRootNodeClick={closeOnDimmerClick}
{...portalProps}
className={dimmerClasses}
eventPool={eventPool}
mountNode={mountNode}
open={open}
onClose={this.handleClose}
Expand Down

0 comments on commit de091e6

Please sign in to comment.