Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="PageHolder">
</div>
<div class="DialogHolder  is-empty">
  <div class="Backdrop"></div>
</div>
<div class="MessageHolder">
</div>

              
            
!

CSS

              
                html,
body,
.PageHolder {
  height: 100%;
}

button.close {
  padding: 0 .125em;
}

.Title {
  font-weight: bold;
}

.Page {
  min-height: 100%;
}
.Page-Body {
  padding: .5em;
}
.Page-Head {
  padding: .5em;
}

.DialogHolder {
  position: fixed;
  top: 0; left: 0;
  width: 100%; height: 100%;
  z-index: 1;
}

.DialogHolder.is-empty {
  display: none;
}

.Dialog {
  position: absolute;
  left: 30%;
  top: 10%;
  width: 40%;
  height: 80%;
  z-index: 1;
  
  background: #fff;
  border: 1px solid black;
  border-radius: .5em;
}

.Dialog-Body {
  padding: .25em;
}

.Dialog-Head {
  padding: .25em;
  font-weight: bold;
  text-align: center;
}

.Backdrop {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 0;

  background: grey;
  opacity: 0.5;
}

.Backdrop:only-child {
  display: none;
}

.MessageHolder {
  position: fixed;
  top: 0; left: 0;
  width: 100%;
  /* We need a height of 0 in order to allow events to propagate to the main part of the application. */
  z-index: 1;
}

.Message {
  position: absolute;
  left: 37.5%;
  width: 25%;
  background: khaki;
  border: 1px solid orange;
  border-radius: 0 0 .5em .5em;
}

.Message-Body {
  padding: .125em .5em .5em;
}
.Message-Head {
  padding: .25em .25em .125em;
  font-weight: bold;
  text-align: center;
}

              
            
!

JS

              
                
class Page extends React.Component {

  constructor(props) {
    super(props)
    let { dialogHolder = '.DialogHolder', messageHolder = '.MessageHolder' } = props

    this.state = {
      dialogHolder,
      dialogs: [],
      messageHolder,
      messages: [],
    }
  }
  
  addNewDialog() {
    let state = this.state,
        dialogHolder = state.dialogHolder,
        dialogs = state.dialogs,
        dialog, id

    // Need some simple way to distinquish the different dialogs.
    id = Date.now()
    dialog = ReactDOM.createPortal((
        <div className="Dialog">
          <div className="Dialog-Head">
            <span>Dialog {dialogs.length}</span>
            <button type="button" className="close" onClick={(evt) => this.onButtonClick(evt, { id: id })} aria-label="Close" data-action="close-dialog">
              <span aria-hidden="true">&times;</span>
            </button>
          </div>
          <div className="Dialog-Body">
            <div className="btn-toolbar" role="toolbar">
              <div className="btn-group" role="group">
                <button className="btn  btn-default" onClick={(evt) => this.onButtonClick(evt)} data-action="add-dialog">Add Another Dialog</button>
              </div>
              <div className="btn-group" role="group">
                <button className="btn  btn-default" onClick={(evt) => this.onButtonClick(evt)} data-action="add-message">Add Message</button>
              </div>
            </div>
          </div>
        </div>
      ),
      dialogHolder
    )
    // Note: Setting the ID on the React portal is dangerous.  This was done for
    // simplicity, and this practice should not be done in production.
    dialog.id = id
    this.setState({
      dialogs: dialogs.concat(dialog),
    })
  }
  
  addNewMessage() {
    let state = this.state,
        messageHolder = state.messageHolder,
        messages = state.messages,
        id, message

    // Need some simple way to distinquish the different messages.
    id = Date.now()
    message = ReactDOM.createPortal((
        <div className="Message">
          <div className="Message-Head">
            Title
            <button type="button" className="close" onClick={(evt) => this.onButtonClick(evt, { id: id })} aria-label="Close" data-action="close-message">
              <span aria-hidden="true">&times;</span>
            </button>
          </div>
          <div className="Message-Body">
            Message {messages.length}
          </div>
        </div>
      ),
      messageHolder
    )
    // Note: Setting the ID on the React portal is dangerous.  This was done for
    // simplicity, and this practice should not be done in production.
    message.id = id
    this.setState({
      messages: messages.concat(message),
    })
  }

  closeDialog(id) {
    let dialogs = this.state.dialogs.filter(dialog => dialog.id !== id)
    if (dialogs.length !== this.state.dialogs.length) {
      this.setState({
        dialogs: dialogs,
      })
    }
  }

  closeMessage(id) {
    let messages = this.state.messages.filter(message => message.id !== id)
    if (messages.length !== this.state.messages.length) {
      this.setState({
        messages: messages,
      })
    }
  }

  onButtonClick(evt, opts) {
    let action = evt.currentTarget.dataset.action
    
    switch (action) {
      case 'add-dialog':
        this.addNewDialog()
        break
      case 'add-message':
        this.addNewMessage()
        break
      case 'close-dialog':
        this.closeDialog(opts.id)
        break
      case 'close-message':
        this.closeMessage(opts.id)
        break
      default:
        console.log(`Unrecognized button action: ${action}`)
    }
  }
  
  onPageClick(evt) {
    console.log(`${evt.target.className} was clicked!`)
  }

  render() {
    let state = this.state,
        dialogs = state.dialogs,
        messages = state.messages

    return (
      <div className="Page" onClick={(evt) => this.onPageClick(evt)}>
        <div className="Page-Head"><span className="Title">React Portals</span></div>
        <div className="Page-Body">
          <div className="btn-toolbar" role="toolbar">
            <div className="btn-group" role="group">
              <button className="btn  btn-default" onClick={(evt) => this.onButtonClick(evt)} data-action="add-dialog">
                Add Dialog
              </button>
            </div>
            <div className="btn-group" role="group">
              <button className="btn  btn-default" onClick={(evt) => this.onButtonClick(evt)} data-action="add-message">
                Add Message
              </button>
            </div>
          </div>
          {dialogs}
          {messages}
        </div>
        <div className="Page-Foot"></div>
      </div>
    )
  }

  componentWillMount() {
    let state = this.state,
        dialogHolder = state.dialogHolder,
        messageHolder = state.messageHolder
    
    this._resolvePortalRoots(dialogHolder, messageHolder);
  }

  componentDidMount() {
    this._syncPortalParents()
  }

  componentDidUpdate() {
    this._syncPortalParents()
  }

  componentWillReceiveProps(nextProps) {
    let props = this.props,
        dialogHolder = nextProps.dialogHolder,
        messageHolder = nextProps.messageHolder

    if (props.dialogHolder !== dialogHolder ||
        props.messageHolder !== messageHolder
    ) {
      this._resolvePortalRoots(dialogHolder, messageHolder);
    }
  }

  _resolvePortalRoots(dialogHolder, messageHolder) {
    if (typeof dialogHolder === 'string') {
      dialogHolder = document.querySelector(dialogHolder)
    }
    if (typeof messageHolder === 'string') {
      messageHolder = document.querySelector(messageHolder)
    }
    this.setState({
      dialogHolder,
      messageHolder,
    })
  }

  _syncPortalParents() {
    let state = this.state,
        dialogHolder = state.dialogHolder,
        messageHolder = state.messageHolder

    if (state.dialogs.length === 0) {
      dialogHolder.classList.add('is-empty')
    }
    else {
      dialogHolder.classList.remove('is-empty')
    }
  }

}

ReactDOM.render(
  <Page/>,
  document.querySelector('.PageHolder')
)

              
            
!
999px

Console