-
Notifications
You must be signed in to change notification settings - Fork 2
[WIP] - Portal component experiment #37
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import React from 'react' | ||
|
||
import { Button, Portal } from 'stardust' | ||
|
||
class PortalExample extends React.Component { | ||
render() { | ||
return ( | ||
<Portal | ||
trigger={ | ||
<Button | ||
onClick={() => { | ||
console.log('onClick outer') | ||
}} | ||
> | ||
Open/close portal | ||
</Button> | ||
} | ||
> | ||
<div | ||
style={{ | ||
position: 'fixed', | ||
left: '40%', | ||
top: '50%', | ||
padding: '50px', | ||
backgroundColor: 'silver', | ||
}} | ||
> | ||
portal popup | ||
</div> | ||
</Portal> | ||
) | ||
} | ||
} | ||
|
||
export default PortalExample |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import React from 'react' | ||
import ComponentExample from 'docs/src/components/ComponentDoc/ComponentExample' | ||
import ExampleSection from 'docs/src/components/ComponentDoc/ExampleSection' | ||
|
||
const Types = () => ( | ||
<ExampleSection title="Types"> | ||
<ComponentExample | ||
title="Portal" | ||
description="A portal." | ||
examplePath="components/Portal/Types/PortalExample" | ||
/> | ||
</ExampleSection> | ||
) | ||
|
||
export default Types |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import React from 'react' | ||
import Types from './Types' | ||
|
||
const PortalExamples = () => ( | ||
<div> | ||
<Types /> | ||
</div> | ||
) | ||
|
||
export default PortalExamples |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import React, { cloneElement } from 'react' | ||
import ReactDOM from 'react-dom' | ||
import PropTypes from 'prop-types' | ||
import { AutoControlledComponent, eventStack, makeDebugger } from '../../lib' | ||
|
||
const debug = makeDebugger('portal') | ||
|
||
class Portal extends AutoControlledComponent { | ||
static propTypes = { | ||
trigger: PropTypes.node, | ||
|
||
open: PropTypes.bool, | ||
|
||
defaultOpen: PropTypes.bool, | ||
} | ||
|
||
static autoControlledProps = ['open'] | ||
|
||
handleTriggerClick = () => { | ||
debug('handleTriggerClick()') | ||
this.props.trigger.props.onClick() | ||
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 will throw if the trigger does not have an handleTriggerClick = (e) => {
_.invoke(this.props.trigger, 'onClick', e)
} |
||
this.setState({ open: !this.state.open }) | ||
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. Since 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. Done. Also marked the second parameter on |
||
} | ||
|
||
handlePortalMouseEnter = () => { | ||
debug('handlePortalMouseEnter()') | ||
} | ||
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. Unused method? |
||
|
||
componentDidMount() { | ||
debug('componentDidMount()', this.state) | ||
if (this.state.open) { | ||
this.createPortal() | ||
} | ||
} | ||
|
||
componentDidUpdate() { | ||
debug('componentDidUpdate()', this.state) | ||
if (this.state.open) { | ||
this.createPortal() | ||
} | ||
} | ||
|
||
componentWillUnmount() { | ||
debug('componentWillUnmount()') | ||
} | ||
|
||
createPortal() { | ||
if (this.state.portalEl) { | ||
return | ||
} | ||
console.log('creating portalEl') | ||
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. No log calls. Use localStorage.debug = 'semanticUIReact:portal'
// refresh browser
|
||
const portalEl = document.createElement('div') | ||
document.body.appendChild(portalEl) | ||
eventStack.sub('mouseenter', this.handlePortalMouseEnter, { | ||
target: portalEl, | ||
}) | ||
this.setState({ | ||
portalEl, | ||
}) | ||
} | ||
|
||
destroyPortal() {} | ||
|
||
// To discuss: | ||
// when to create rootNode? (it is required in render, componentWillMount is deprecated) | ||
// should multiple portals share it? (how would mouseenter/mouseleave on portalEl work then?) | ||
// when to destroy it (it is too early in componentWillUnmount) | ||
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'm not certain on how we should handle the root node, however, we can use the constructor for a componentWillMount replacement. Could you explain a bit about why componentWillUnmount is too early for removing the mount node? 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. In |
||
|
||
render() { | ||
const { trigger } = this.props | ||
debug('render') | ||
|
||
if (!trigger) { | ||
return | ||
} | ||
|
||
return ( | ||
<React.Fragment> | ||
{cloneElement(trigger, { | ||
onClick: this.handleTriggerClick, | ||
})} | ||
{this.state.open && | ||
this.state.portalEl && | ||
ReactDOM.createPortal(this.props.children, this.state.portalEl)} | ||
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. Let's stick with destructuring props and state at the top of our functions. It breaks object references and makes code more readable: render() {
const { open, portalEl } = this.state
// ...
} |
||
</React.Fragment> | ||
) | ||
} | ||
} | ||
|
||
export default Portal |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from './Portal' |
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.
Avoid creating functions in the render method as it is not a best practice. Treat functions as persisted state and hoist them up to methods. This way, they are the same instance between renders and do not void any equality checks between renders.