Skip to content

Instantly share code, notes, and snippets.

@benz2012
Created November 3, 2017 20:27
Show Gist options
  • Save benz2012/1ff5e37fceaac15e5b2abff60e9eefc0 to your computer and use it in GitHub Desktop.
Save benz2012/1ff5e37fceaac15e5b2abff60e9eefc0 to your computer and use it in GitHub Desktop.
React Portal Modal with Blurred Background
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,
)
}
}
@Stophface
Copy link

Can you share the DOM as well?

@Stophface
Copy link

@benz2012 Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment