Skip to content

Instantly share code, notes, and snippets.

@irfanandriansyah1997
Created May 30, 2019 11:53
Show Gist options
  • Select an option

  • Save irfanandriansyah1997/e6e5fe3868948f0448bbf559bef7b778 to your computer and use it in GitHub Desktop.

Select an option

Save irfanandriansyah1997/e6e5fe3868948f0448bbf559bef7b778 to your computer and use it in GitHub Desktop.
gqraWW
<div id="app">
</div>
const {React} = window;
const {ReactDOM} = window;
const styleRipple = {
position: 'relative',
overflow: 'hidden'
};
const styleRippleContainer = {
position: 'absolute',
top: '0',
right: '0',
bottom: '0',
left: '0'
}
const styleSpan = {
transform: 'scale(0)',
borderRadius: '100%',
position: 'absolute',
opacity: '0.75',
backgroundColor: '#ffffff',
animation: 'ripple 850ms'
}
class Ripple extends React.Component {
initializeState = () => {
return {
spanStyles: {},
count: 0
}
}
state = this.initializeState();
/* Debounce Code to call the Ripple removing function */
callCleanUp = (cleanup, delay) => {
return function() {
clearTimeout(this.bounce);
this.bounce = setTimeout(() => {
cleanup();
}, delay);
}
}
showRipple = (e) => {
const rippleContainer = e.currentTarget;
const size = rippleContainer.offsetWidth;
const pos = rippleContainer.getBoundingClientRect();
const x = e.pageX - pos.x - (size / 2);
const y = e.pageY - pos.y - (size / 2);
const spanStyles = { top: y + 'px', left: x + 'px', height: size + 'px', width: size + 'px' };
const count = this.state.count + 1;
this.setState({
spanStyles: {...this.state.spanStyles, [count] : spanStyles},
count: count
});
}
cleanUp = () =>{
const initialState = this.initializeState();
this.setState({ ...initialState });
}
renderRippleSpan = () => {
const {showRipple = false, spanStyles = {}} = this.state;
const spanArray = Object.keys(spanStyles);
if (spanArray && spanArray.length > 0) {
return (
spanArray.map((key, index) => {
return <span key={'spanCount_' + index} className="" style={{ ...spanStyles[key]}}></span>
})
)
} else {
return null;
}
}
render() {
const {children= null, classes = "", onClickHandler = null} = this.props;
return (
<div ref="targetElement" className={'ripple ' + classes} onClick={onClickHandler}>
{children}
<div className="rippleContainer" onMouseDown={this.showRipple} onMouseUp={this.callCleanUp(this.cleanUp, 2000)}>
{this.renderRippleSpan()}
</div>
</div>
);
}
}
const App = (
<div className="center">
<h1 className="demo"></h1>
<Ripple classes="btn">Click Me</Ripple>
</div>
);
ReactDOM.render(
App,
document.getElementById("app")
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.7.0/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.7.0/umd/react-dom.production.min.js"></script>
.btn {
margin: 50px auto;
border-radius: 25px;
background-color: #5300e8;
box-shadow: 0 2px 4px 0 #888888;
display: inline-block;
padding: 15px 50px;
color: #ffffff;
}
.center {
text-align: center
}
/* ripple */
.ripple {
position: relative;
overflow: hidden;
}
.ripple .rippleContainer {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
}
.ripple .rippleContainer span {
transform: scale(0);
border-radius: 100%;
position: absolute;
opacity: 0.75;
background-color: #ffffff;
animation: ripple 850ms;
}
@keyframes ripple {
to {
opacity: 0;
transform: scale(2);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment