Last active
November 7, 2016 19:10
-
-
Save dyatlov/aa357617cf0afeb5b19f to your computer and use it in GitHub Desktop.
ReactJs + Zurb Foundation modal example. Allows inner modals too
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Modal = React.createClass | |
propTypes: | |
id: React.PropTypes.string | |
show: React.PropTypes.bool | |
getInitialState: -> | |
show: false | |
getDefaultProps: -> | |
id: 'generic-modal-popup' | |
modalCode: -> | |
if [email protected] | |
return | |
`var {className, show, ...other} = this.props` | |
`className = (className || '') + ' reveal-modal'` | |
__html: React.renderToStaticMarkup ` | |
<div className={className} data-reveal aria-hidden="true" role="dialog" {...other}> | |
<div className="placeholder"></div> | |
<a className="close-reveal-modal" aria-label="Close">×</a> | |
</div>` | |
render: -> | |
`<div | |
ref={this._setModalRef} | |
className="hidden" | |
dangerouslySetInnerHTML={this.modalCode()} | |
> | |
</div>` | |
componentDidMount: -> | |
@componentDidUpdate() | |
componentDidUpdate: -> | |
if @props.show | |
@onShow() | |
else | |
modalId = '#' + @props.id + '[data-reveal]' | |
$(modalId).foundation('reveal', 'close') | |
_setModalRef: (ref) -> | |
if Object.isFrozen(@refs) && !Object.keys(@refs).length | |
@refs = {} | |
@refs.dialog = ref | |
onShow: -> | |
me = @ | |
node = $( React.findDOMNode(@refs.dialog) ) | |
modalId = '#' + @props.id + '[data-reveal]' | |
$(document).on 'open.fndtn.reveal', modalId, -> | |
$(document).off 'open.fndtn.reveal', modalId | |
holder = $(@).find('.placeholder').get(0) | |
React.render `<div>{me.props.children}</div>`, holder | |
$(document).on 'closed.fndtn.reveal', modalId, -> | |
$(document).off 'closed.fndtn.reveal', modalId | |
holder = $(@).find('.placeholder').get(0) | |
React.unmountComponentAtNode(holder) | |
me.onHide() | |
node.find('.reveal-modal').foundation('reveal', 'open') | |
onHide: -> | |
if typeof @props.onHide == 'function' | |
@props.onHide() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example use: