-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
807339d
commit c235e0a
Showing
5 changed files
with
134 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { mount } from '@vue/test-utils'; | ||
import { describe, expect, it } from 'vitest'; | ||
|
||
import RippleDirective from './ripple'; | ||
|
||
describe('Ripple directive test (ripple.ts)', () => { | ||
const createDirectiveWrapper = (bindingValue = {}) => { | ||
return mount({ | ||
template: ` | ||
<div v-ripple="options" style="width: 100px; height: 100px;"></div>`, | ||
data() { | ||
return { options: bindingValue }; | ||
}, | ||
directives: { | ||
ripple: RippleDirective, | ||
}, | ||
}); | ||
}; | ||
|
||
it('Will apply default classes when no options are provided', async () => { | ||
const wrapper = createDirectiveWrapper(); | ||
await wrapper.trigger('click', { | ||
clientX: 50, | ||
clientY: 50, | ||
}); | ||
|
||
const rippleElement = wrapper.element.querySelector('span'); | ||
expect(rippleElement).toBeTruthy(); | ||
expect(rippleElement.classList.contains('animate-ripple')).toBeTruthy(); | ||
expect(rippleElement.classList.contains('bg-foreground')).toBeTruthy(); | ||
expect(rippleElement.classList.contains('bg-opacity-25')).toBeTruthy(); | ||
}); | ||
|
||
it('Will apply provided color and opacity classes', async () => { | ||
const wrapper = createDirectiveWrapper({ | ||
color: 'bg-blue-500', | ||
opacity: 'bg-opacity-50', | ||
}); | ||
|
||
await wrapper.trigger('click', { | ||
clientX: 50, | ||
clientY: 50, | ||
}); | ||
|
||
const rippleElement = wrapper.element.querySelector('span'); | ||
expect(rippleElement).toBeTruthy(); | ||
expect(rippleElement.classList.contains('animate-ripple')).toBeTruthy(); | ||
expect(rippleElement.classList.contains('bg-blue-500')).toBeTruthy(); | ||
expect(rippleElement.classList.contains('bg-opacity-50')).toBeTruthy(); | ||
}); | ||
|
||
it('Will position the ripple correctly', async () => { | ||
const wrapper = createDirectiveWrapper(); | ||
const rect = wrapper.element.getBoundingClientRect(); | ||
const clickX = rect.left + rect.width / 2; | ||
const clickY = rect.top + rect.height / 2; | ||
|
||
await wrapper.trigger('click', { | ||
clientX: clickX, | ||
clientY: clickY, | ||
}); | ||
|
||
const rippleElement = wrapper.element.querySelector('span'); | ||
expect(rippleElement).toBeTruthy(); | ||
expect(rippleElement.style.left).toBe(`${clickX - rect.left - rect.width / 2}px`); | ||
expect(rippleElement.style.top).toBe(`${clickY - rect.top - rect.height / 2}px`); | ||
}); | ||
|
||
it('Will remove ripple after animation ends', async () => { | ||
const wrapper = createDirectiveWrapper(); | ||
await wrapper.trigger('click', { | ||
clientX: 50, | ||
clientY: 50, | ||
}); | ||
|
||
let rippleElement = wrapper.element.querySelector('span'); | ||
expect(rippleElement).toBeTruthy(); | ||
|
||
rippleElement.dispatchEvent(new Event('animationend')); | ||
rippleElement = wrapper.element.querySelector('span'); | ||
expect(rippleElement).toBeFalsy(); | ||
}); | ||
}); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { DirectiveBinding } from 'vue'; | ||
|
||
type RippleOptions = { | ||
color?: string; | ||
opacity?: string; | ||
}; | ||
|
||
export default { | ||
mounted(el: HTMLElement, binding: DirectiveBinding<RippleOptions>) { | ||
el.style.position = 'relative'; | ||
el.style.overflow = 'hidden'; | ||
|
||
el.addEventListener('click', function (e: MouseEvent) { | ||
const ripple = document.createElement('span'); | ||
const rect = el.getBoundingClientRect(); | ||
|
||
const size = Math.max(rect.width, rect.height); | ||
const x = e.clientX - rect.left - size / 2; | ||
const y = e.clientY - rect.top - size / 2; | ||
|
||
ripple.style.width = ripple.style.height = `${size}px`; | ||
ripple.style.position = 'absolute'; | ||
ripple.style.borderRadius = '50%'; | ||
ripple.style.transform = 'scale(0)'; | ||
ripple.style.opacity = '1'; | ||
ripple.style.pointerEvents = 'none'; | ||
ripple.style.left = `${x}px`; | ||
ripple.style.top = `${y}px`; | ||
|
||
const { color = 'bg-foreground', opacity = 'bg-opacity-25' } = | ||
binding.value || {}; | ||
|
||
ripple.classList.add('animate-ripple', color, opacity); | ||
|
||
el.appendChild(ripple); | ||
|
||
ripple.addEventListener('animationend', () => { | ||
ripple.remove(); | ||
}); | ||
}); | ||
}, | ||
}; |
This file contains 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