【问题标题】:ReactJs Modal Using Javascript and CSS使用 Javascript 和 CSS 的 ReactJs 模态
【发布时间】:2015-01-03 09:58:38
【问题描述】:

如何在 reactjs 模态窗口附加 body 结束标签来设置带有 body 标签的模态定位绝对值。

这是在另一个组件中添加的示例。

<div className="modal">
  <p>Model Pop-up <button onClick={this.handleClick}>here</button></p>
    <div id="js-modal-inner" className="modal">
      <div className="modal__content js-dialog">
        <a className="modal__close" onClick={this.handleClose}>&times;</a>
        <header className="modal__header">
          <h2 className="modal__title">Split Ticket:</h2>
        </header>
        <div className="modal__content--inner">
          {this.props.children}
        </div>
      </div>
    </div>
    <div className="modal__overlay js-fade"></div>
  </div> 

【问题讨论】:

    标签: javascript css dialog modal-dialog reactjs


    【解决方案1】:

    这通常被称为反应中的“层”。见this fiddle

    /** @jsx React.DOM */
    
    var ReactLayeredComponentMixin = {
        componentWillUnmount: function() {
            this._unrenderLayer();
            document.body.removeChild(this._target);
        },
        componentDidUpdate: function() {
            this._renderLayer();
        },
        componentDidMount: function() {
            // Appending to the body is easier than managing the z-index of everything on the page.
            // It's also better for accessibility and makes stacking a snap (since components will stack
            // in mount order).
            this._target = document.createElement('div');
            document.body.appendChild(this._target);
            this._renderLayer();
        },
        _renderLayer: function() {
            // By calling this method in componentDidMount() and componentDidUpdate(), you're effectively
            // creating a "wormhole" that funnels React's hierarchical updates through to a DOM node on an
            // entirely different part of the page.
            React.renderComponent(this.renderLayer(), this._target);
        },
        _unrenderLayer: function() {
            React.unmountComponentAtNode(this._target);
        }
    };
    
    var Modal = React.createClass({
        killClick: function(e) {
            // clicks on the content shouldn't close the modal
            e.stopPropagation();
        },
        handleBackdropClick: function() {
            // when you click the background, the user is requesting that the modal gets closed.
            // note that the modal has no say over whether it actually gets closed. the owner of the
            // modal owns the state. this just "asks" to be closed.
            this.props.onRequestClose();
        },
        render: function() {
            return this.transferPropsTo(
                <div className="ModalBackdrop" onClick={this.handleBackdropClick}>
                    <div className="ModalContent" onClick={this.killClick}>
                        {this.props.children}
                    </div>
                </div>
            );
        }
    });
    
    var ModalLink = React.createClass({
        mixins: [ReactLayeredComponentMixin],
        handleClick: function() {
            this.setState({shown: !this.state.shown});
        },
        getInitialState: function() {
            return {shown: false, ticks: 0, modalShown: false};
        },
        componentDidMount: function() {
            setInterval(this.tick, 1000);
        },
        tick: function() {
            this.setState({ticks: this.state.ticks + 1});
        },
        renderLayer: function() {
            if (!this.state.shown) {
                return <span />;
            }
            return (
                <Modal onRequestClose={this.handleClick}>
                    <h1>Hello!</h1>
                    Look at these sweet reactive updates: {this.state.ticks}
                </Modal>
            );
        },
        render: function() {
            return <a href="javascript:;" role="button" onClick={this.handleClick}>Click to toggle modal</a>;
        }
    });
    
    React.renderComponent(<ModalLink />, document.body);
    

    【讨论】:

    • 向这个模态添加过渡效果如何?
    • 谢谢!您是从哪里了解到这种模式的?我很想找到更多这样的信息!
    • 一年多以前在 IRC。你可以在 react-bootstrap 和大多数使用模态的库中看到类似的实现。
    猜你喜欢
    • 2015-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 2018-07-12
    • 2016-06-08
    • 2015-09-08
    相关资源
    最近更新 更多