Created
November 3, 2017 20:27
-
-
Save benz2012/1ff5e37fceaac15e5b2abff60e9eefc0 to your computer and use it in GitHub Desktop.
React Portal Modal with Blurred Background
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
import React, { Component } from 'react' | |
import ReactDOM from 'react-dom' | |
const Blur = (radius) = ` | |
filter: url(#svgBlur); | |
filter: blur(${radius || 5}px); | |
` | |
const SVGBlur = ({ radius }) = ( | |
<svg style={{ position: 'absolute', top: '-99999px' }} xmlns="http://www.w3.org/2000/svg"> | |
<filter id="svgBlur" x="-5%" y="-5%" width="150%" height="150%"> | |
<feGaussianBlur in="SourceGraphic" stdDeviation={radius || 5} /> | |
</filter> | |
</svg> | |
) | |
export default class Modal extends Component { | |
constructor() { | |
super() | |
this.el = document.createElement('div') | |
this.modalRoot = document.getElementById('modal') | |
this.app = document.getElementById('app') | |
} | |
componentDidMount() { | |
this.modalRoot.appendChild(this.el) | |
this.app.setAttribute('style', Blur()) | |
} | |
componentWillUnmount() { | |
this.modalRoot.removeChild(this.el) | |
this.app.setAttribute('style', '') | |
} | |
render() { | |
const modal = ( | |
<div> | |
{this.props.children} | |
<SVGBlur /> | |
</div> | |
) | |
return ReactDOM.createPortal( | |
{modal}, | |
this.el, | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@Stophface it was from this app
https://github.com/benz2012/derby-dashboard/blob/0f2952343c4800fd63decf8354a024e4b259f8b4/src/js/containers/Modal/index.jsx