-
-
Save jameswquinn/060b3d7e185364e6d8072827e9388c0d to your computer and use it in GitHub Desktop.
Ripple Button
This file contains hidden or 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
| /** | |
| * Created by matt on 3/8/17. | |
| */ | |
| import React, { PropTypes } from 'react'; | |
| import './button.scss'; | |
| function ripple (e) { | |
| const midX = e.offsetX; | |
| const midY = e.offsetY; | |
| const { width, height } = e.target.getBoundingClientRect(); | |
| const rX = Math.max(midX, width - midX); | |
| const rY = Math.max(midY, height - midY); | |
| const radius = Math.sqrt(rX * rX + rY * rY); | |
| const $ripple = e.target.querySelector('.ripple'); | |
| const styles = { | |
| top: `${midY}px`, | |
| left: `${midX}px`, | |
| width: `${radius * 2}px`, | |
| height: `${radius * 2}px`, | |
| marginLeft: `-${radius}px`, | |
| marginTop: `-${radius}px`, | |
| }; | |
| Object.assign($ripple.style, styles); | |
| } | |
| const onClick = f => e => { | |
| ripple(e.nativeEvent); | |
| f && f(e); | |
| } | |
| const Button = (props) => | |
| <button className="btn-ripple" onClick={onClick(props.onClick)}> | |
| {props.children} | |
| <span className="ripple"></span> | |
| </button>; | |
| Button.propTypes = { | |
| onClick: React.PropTypes.func | |
| }; | |
| Button.defaultProps = { | |
| onClick: _ => {} | |
| }; | |
| export default Button; |
This file contains hidden or 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
| @function to-em($num) { | |
| @return #{($num / 16)}em; | |
| } | |
| $ripple-size: 4em; | |
| .btn { | |
| font-size: 4rem; | |
| padding: 0.25em 1em; | |
| background: none; | |
| border: to-em(1) solid; | |
| border-radius: to-em(2); | |
| outline: none; | |
| &-ripple { | |
| @extend .btn; | |
| position: relative; | |
| overflow: hidden; | |
| > .ripple { | |
| content: ''; | |
| background: rgba(0,0,0,0.5); | |
| width: $ripple-size; | |
| height: $ripple-size; | |
| display: inline-block; | |
| position: absolute; | |
| top: 50%; | |
| left: 50%; | |
| margin: { | |
| left: ($ripple-size / -2); | |
| top: ($ripple-size / -2); | |
| } | |
| border-radius: 50%; | |
| opacity: 0; | |
| //transform: scale(1); | |
| transition: | |
| opacity .6s cubic-bezier(0,0,.2,1), | |
| transform .3s cubic-bezier(0,0,.2,1); | |
| } | |
| &:active { | |
| > .ripple { | |
| opacity: 1; | |
| transform: scale(0); | |
| transition: none; | |
| } | |
| } | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment