How to create a React Modal(which is append to `<body>`) with transitions?

后端 未结 8 1049
遥遥无期
遥遥无期 2020-12-07 08:23

There is a modal in this answer https://stackoverflow.com/a/26789089/883571 which is creating a React-based Modal by appending it to . However, I fo

8条回答
  •  孤街浪徒
    2020-12-07 08:39

    Hope it helps. This is my current implementation of a transition modal based on the anwser above:

      React = require 'react/addons'
    
      keyboard = require '../util/keyboard'
      mixinLayered = require '../mixin/layered'
    
      $ = React.DOM
      T = React.PropTypes
      cx = React.addons.classSet
    
      module.exports = React.createFactory React.createClass
        displayName: 'body-modal'
        mixins: [mixinLayered]
    
        propTypes:
          # this components accepts children
          name:             T.string.isRequired
          title:            T.string
          onCloseClick:     T.func.isRequired
          showCornerClose:  T.bool
          show:             T.bool.isRequired
    
        componentDidMount: ->
          window.addEventListener 'keydown', @onWindowKeydown
    
        componentWillUnmount: ->
          window.removeEventListener 'keydown', @onWindowKeydown
    
        onWindowKeydown: (event) ->
          if event.keyCode is keyboard.esc
            @onCloseClick()
    
        onCloseClick: ->
          @props.onCloseClick()
    
        onBackdropClick: (event) ->
          unless @props.showCornerClose
            if event.target is event.currentTarget
              @onCloseClick()
    
        renderLayer: ->
          className = "body-modal is-for-#{@props.name}"
          $.div className: className, onClick: @onBackdropClick,
            if @props.showCornerClose
              $.a className: 'icon icon-remove', onClick: @onCloseClick
            $.div className: 'box',
              if @props.title?
                $.div className: 'title',
                  $.span className: 'name', @props.title
                  $.span className: 'icon icon-remove', @onCloseClick
              @props.children
    
        render: ->
          $.div()
    

提交回复
热议问题