Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add disabled prop #43

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ static defaultProps = {
}
```

| Property | Description |
| -------- | ----------------------------- |
| during | The css animate duration [ms] |
| color | The ripple's background color |
| Property | Description |
|----------|------------------------------------------|
| disabled | Determines if the ripple will be played |
| during | The css animate duration [ms] |
| color | The ripple's background color |
192 changes: 99 additions & 93 deletions src/createRipples.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { CSSProperties } from 'react'
import PropTypes from 'prop-types'

export interface RipplesProps {
disabled?: boolean
during?: number
color?: string
onClick?: (ev: React.MouseEvent<HTMLDivElement>) => any
Expand All @@ -19,112 +20,117 @@ const boxStyle: CSSProperties = {
}

export const createRipples = (defaultProps: Partial<RipplesProps> = {}) =>
class extends React.PureComponent<RipplesProps, State> {
timer: number = 0
class extends React.PureComponent<RipplesProps, State> {
timer: number = 0

static displayName = 'Ripples'
static displayName = 'Ripples'

static propTypes = {
during: PropTypes.number,
color: PropTypes.string,
static propTypes = {
during: PropTypes.number,
color: PropTypes.string,

onClick: PropTypes.func,
className: PropTypes.string,
}
onClick: PropTypes.func,
className: PropTypes.string,
}

static defaultProps = {
during: 600,
color: 'rgba(0, 0, 0, .3)',
className: '',
onClick: () => {},
...defaultProps,
}
static defaultProps = {
disabled: false,
during: 600,
color: 'rgba(0, 0, 0, .3)',
className: '',
onClick: () => {},
...defaultProps,
}

constructor(props: RipplesProps) {
super(props)

this.state = {
rippleStyle: {
position: 'absolute',
borderRadius: '50%',
opacity: 0,
width: 35,
height: 35,
transform: 'translate(-50%, -50%)',
pointerEvents: 'none',
},
constructor(props: RipplesProps) {
super(props)

this.state = {
rippleStyle: {
position: 'absolute',
borderRadius: '50%',
opacity: 0,
width: 35,
height: 35,
transform: 'translate(-50%, -50%)',
pointerEvents: 'none',
},
}
}
}

componentWillUnmount() {
clearTimeout(this.timer)
}
componentWillUnmount() {
clearTimeout(this.timer)
}

onClick = (ev: React.MouseEvent<HTMLDivElement>) => {
const { during, onClick, color } = this.props
onClick = (ev: React.MouseEvent<HTMLDivElement>) => {
const { disabled, during, onClick, color } = this.props

ev.stopPropagation()
ev.stopPropagation()

const { pageX, pageY, currentTarget } = ev
if (disabled) {
return;
}

const rect = currentTarget.getBoundingClientRect()
const { pageX, pageY, currentTarget } = ev

const left = pageX - (rect.left + window.scrollX)
const top = pageY - (rect.top + window.scrollY)
const size = Math.max(rect.width, rect.height)
const rect = currentTarget.getBoundingClientRect()

this.setState(
state => {
return {
rippleStyle: {
...state.rippleStyle,
left,
top,
opacity: 1,
transform: 'translate(-50%, -50%)',
transition: 'initial',
backgroundColor: color,
const left = pageX - (rect.left + window.scrollX)
const top = pageY - (rect.top + window.scrollY)
const size = Math.max(rect.width, rect.height)

this.setState(
state => {
return {
rippleStyle: {
...state.rippleStyle,
left,
top,
opacity: 1,
transform: 'translate(-50%, -50%)',
transition: 'initial',
backgroundColor: color,
},
}
},
}
},
() => {
this.timer = setTimeout(() => {
this.setState(state => ({
rippleStyle: {
...state.rippleStyle,
opacity: 0,
transform: `scale(${size / 9})`,
transition: `all ${during}ms`,
},
}))
}, 50)
},
)

if (onClick) onClick(ev)
}
() => {
this.timer = setTimeout(() => {
this.setState(state => ({
rippleStyle: {
...state.rippleStyle,
opacity: 0,
transform: `scale(${size / 9})`,
transition: `all ${during}ms`,
},
}))
}, 50)
},
)

render() {
const {
children,
during,
color,
onClick,
className,
...props
} = this.props
const { rippleStyle } = this.state

return (
<div
{...props}
className={`react-ripples ${className}`.trim()}
style={boxStyle}
onClick={this.onClick}
>
{children}
<s style={rippleStyle} />
</div>
)
if (onClick) onClick(ev)
}

render() {
const {
children,
during,
color,
onClick,
className,
...props
} = this.props
const { rippleStyle } = this.state

return (
<div
{...props}
className={`react-ripples ${className}`.trim()}
style={boxStyle}
onClick={this.onClick}
>
{children}
<s style={rippleStyle} />
</div>
)
}
}
}