-
-
Notifications
You must be signed in to change notification settings - Fork 7k
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
enhancement/ripple #3882
enhancement/ripple #3882
Changes from 3 commits
aa93200
9bf7811
feafaf6
02a6a75
59ead24
0f54af6
e70764f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,7 +41,7 @@ const VBtn = mixins( | |
outline: Boolean, | ||
ripple: { | ||
type: [Boolean, Object], | ||
default: true | ||
default: null | ||
}, | ||
round: Boolean, | ||
small: Boolean, | ||
|
@@ -85,6 +85,11 @@ const VBtn = mixins( | |
return (!this.outline && !this.flat) | ||
? this.addBackgroundColorClassChecks(classes) | ||
: this.addTextColorClassChecks(classes) | ||
}, | ||
computedRipple (): any { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This returns |
||
const defaultRipple = this.icon || this.fab ? { circle: true } : true | ||
if (this.disabled) return false | ||
else return this.ripple || defaultRipple | ||
} | ||
}, | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,14 @@ | ||
import { VNodeDirective } from 'vue' | ||
|
||
function style (el: HTMLElement, value: string) { | ||
function transform (el: HTMLElement, value: string) { | ||
el.style['transform'] = value | ||
el.style['webkitTransform'] = value | ||
} | ||
|
||
function opacity (el: HTMLElement, value: number) { | ||
el.style['opacity'] = value.toString() | ||
} | ||
|
||
declare global { | ||
interface Element { | ||
getElementsByClassName(classNames: string): NodeListOf<HTMLElement> | ||
|
@@ -15,13 +19,39 @@ declare global { | |
enabled?: boolean | ||
centered?: boolean | ||
class?: string | ||
circle?: boolean | ||
} | ||
} | ||
} | ||
|
||
interface RippleOptions { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Either export this or move it to types/*.d.ts |
||
class?: string | ||
center?: boolean | ||
circle?: boolean | ||
} | ||
|
||
const calculate = (e: MouseEvent, el: HTMLElement, value: RippleOptions = {}) => { | ||
const offset = el.getBoundingClientRect() | ||
const localX = e.clientX - offset.left | ||
const localY = e.clientY - offset.top | ||
|
||
let radius = 0 | ||
let scale = 0.3 | ||
if (el._ripple && el._ripple.circle) { | ||
scale = 0.15 | ||
radius = el.clientWidth / 2 | ||
radius = radius + Math.sqrt((localX - radius)**2 + (localY - radius)**2) / 4 | ||
} else { | ||
radius = Math.sqrt(el.clientWidth**2 + el.clientHeight**2) / 2 | ||
} | ||
|
||
const x = value.center ? 0 : `${localX - radius}px` | ||
const y = value.center ? 0 : `${localY - radius}px` | ||
|
||
const centerX = `${(el.clientWidth - (radius * 2)) / 2}px` | ||
const centerY = `${(el.clientHeight - (radius * 2)) / 2}px` | ||
|
||
return { radius, scale, x, y, centerX, centerY } | ||
} | ||
|
||
const ripple = { | ||
|
@@ -40,28 +70,33 @@ const ripple = { | |
container.className += ` ${value.class}` | ||
} | ||
|
||
const size = Math.max(el.clientWidth, el.clientHeight) * (value.center ? 1 : 2) | ||
const halfSize = size / 2 | ||
const { radius, scale, x, y, centerX, centerY } = calculate(e, el, value) | ||
|
||
animation.className = 'v-ripple__animation' | ||
animation.style.width = `${size}px` | ||
animation.style.height = `${size}px` | ||
animation.style.width = `${radius * 2}px` | ||
animation.style.height = animation.style.width | ||
|
||
el.appendChild(container) | ||
const computed = window.getComputedStyle(el) | ||
if (computed.position !== 'absolute' && computed.position !== 'fixed') el.style.position = 'relative' | ||
|
||
const offset = el.getBoundingClientRect() | ||
const x = value.center ? 0 : e.clientX - offset.left - halfSize | ||
const y = value.center ? 0 : e.clientY - offset.top - halfSize | ||
|
||
animation.classList.add('v-ripple__animation--enter') | ||
animation.classList.add('v-ripple__animation--visible') | ||
style(animation, `translate(${x}px, ${y}px) scale3d(0, 0, 0)`) | ||
transform(animation, `translate(${x}, ${y}) scale3d(${scale},${scale},${scale})`) | ||
opacity(animation, 0) | ||
animation.dataset.activated = String(performance.now()) | ||
|
||
setTimeout(() => { | ||
animation.classList.remove('v-ripple__animation--enter') | ||
style(animation, `translate(${x}px, ${y}px) scale3d(1, 1, 1)`) | ||
animation.classList.add('v-ripple__animation--in') | ||
transform(animation, `translate(${centerX}, ${centerY}) scale3d(0.99,0.99,0.99)`) | ||
opacity(animation, 0.25) | ||
|
||
setTimeout(() => { | ||
animation.classList.remove('v-ripple__animation--in') | ||
animation.classList.add('v-ripple__animation--out') | ||
opacity(animation, 0) | ||
}, 300) | ||
}, 0) | ||
}, | ||
|
||
|
@@ -77,7 +112,7 @@ const ripple = { | |
else animation.dataset.isHiding = 'true' | ||
|
||
const diff = performance.now() - Number(animation.dataset.activated) | ||
let delay = Math.max(300 - diff, 0) | ||
let delay = Math.max(700 - diff, 0) | ||
|
||
setTimeout(() => { | ||
animation.classList.remove('v-ripple__animation--visible') | ||
|
@@ -124,6 +159,9 @@ function updateRipple (el: HTMLElement, binding: VNodeDirective, wasEnabled: boo | |
if (value.class) { | ||
el._ripple.class = binding.value.class | ||
} | ||
if (value.circle) { | ||
el._ripple.circle = value.circle | ||
} | ||
if (enabled && !wasEnabled) { | ||
if ('ontouchstart' in window) { | ||
el.addEventListener('touchend', rippleHide, false) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,12 @@ export default Vue.extend({ | |
target: String | ||
}, | ||
|
||
computed: { | ||
computedRipple (): any { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here |
||
return (this.ripple && !this.disabled) ? this.ripple : false | ||
} | ||
}, | ||
|
||
methods: { | ||
/* eslint-disable-next-line no-unused-vars */ | ||
click (e: MouseEvent): void { /**/ }, | ||
|
@@ -39,8 +45,8 @@ export default Vue.extend({ | |
props: {}, | ||
directives: [{ | ||
name: 'ripple', | ||
value: (this.ripple && !this.disabled) ? this.ripple : false | ||
}] as any, // TODO | ||
value: this.computedRipple | ||
}] as any, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I should have left a better comment, the TODO is to remove |
||
[this.to ? 'nativeOn' : 'on']: { | ||
...this.$listeners, | ||
click: this.click | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.