Skip to content

Commit 0958ca3

Browse files
Refactor #3965 - For Ripple
1 parent a0927bd commit 0958ca3

File tree

2 files changed

+119
-12
lines changed

2 files changed

+119
-12
lines changed

components/lib/ripple/Ripple.d.ts

+90-1
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,99 @@
88
*/
99
import { DirectiveBinding, ObjectDirective } from 'vue';
1010

11+
/**
12+
* Custom passthrough(pt) hooks options.
13+
*/
14+
export interface RipplePassThroughHooksOptions {
15+
/**
16+
* Called before bound element's attributes or event listeners are applied.
17+
*/
18+
created?: DirectiveBinding;
19+
/**
20+
* Called right before the element is inserted into the DOM.
21+
*/
22+
beforeMount?: DirectiveBinding;
23+
/**
24+
* Called when the bound element's parent component and all its children are mounted.
25+
*/
26+
mounted?: DirectiveBinding;
27+
/**
28+
* Called before the parent component is updated.
29+
*/
30+
beforeUpdate?: DirectiveBinding;
31+
/**
32+
* Called after the parent component and all of its children have updated all of its children have updated.
33+
*/
34+
updated?: DirectiveBinding;
35+
/**
36+
* Called before the parent component is unmounted.
37+
*/
38+
beforeUnmount?: DirectiveBinding;
39+
/**
40+
* Called when the parent component is unmounted.
41+
*/
42+
unmounted?: DirectiveBinding;
43+
}
44+
45+
/**
46+
* Custom passthrough(pt) css options.
47+
*/
48+
export interface RipplePassThroughCSSOptions {
49+
/**
50+
* Style class of the element.
51+
*/
52+
class?: any;
53+
/**
54+
* Inline style of the element.
55+
*/
56+
style?: any;
57+
}
58+
59+
export interface RipplePassThroughDirectiveOptions {
60+
/**
61+
* Uses to pass attributes to the life cycle hooks.
62+
* @see {@link RipplePassThroughHooksOptions}
63+
*/
64+
hooks?: RipplePassThroughHooksOptions;
65+
/**
66+
* Uses to pass attributes to the styles.
67+
* @see {@link RipplePassThroughCSSOptions}
68+
*/
69+
css?: RipplePassThroughCSSOptions;
70+
}
71+
72+
/**
73+
* Custom passthrough(pt) options.
74+
* @see {@link RippleOptions.pt}
75+
*/
76+
export interface RipplePassThroughOptions {
77+
/**
78+
* Uses to pass attributes to the root's DOM element.
79+
* @see {@link RipplePassThroughDirectiveOptions}
80+
*/
81+
root?: RipplePassThroughDirectiveOptions;
82+
}
83+
84+
/**
85+
* Defines options of Ripple.
86+
*/
87+
export interface RippleOptions {
88+
/**
89+
* Uses to pass attributes to DOM elements inside the component.
90+
* @type {RipplePassThroughOptions}
91+
*/
92+
pt?: RipplePassThroughOptions;
93+
}
94+
1195
/**
1296
* Binding of Ripple directive.
1397
*/
14-
export interface RippleDirectiveBinding extends Omit<DirectiveBinding, 'modifiers' | 'value'> {}
98+
export interface RippleDirectiveBinding extends Omit<DirectiveBinding, 'modifiers' | 'value'> {
99+
/**
100+
* Value of the Ripple.
101+
*/
102+
value?: RippleOptions | undefined;
103+
}
15104

16105
/**
17106
* **PrimeVue - Ripple**

components/lib/ripple/Ripple.js

+29-11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { BaseDirective } from 'primevue/basedirective';
12
import { DomHandler } from 'primevue/utils';
23

34
let timeout;
@@ -10,16 +11,23 @@ function unbindEvents(el) {
1011
el.removeEventListener('mousedown', onMouseDown);
1112
}
1213

13-
function create(el) {
14+
function create(el, binding) {
1415
let ink = document.createElement('span');
1516

16-
ink.className = 'p-ink';
17+
!el.$_prippleUnstyled && (ink.className = 'p-ink');
18+
ink.setAttribute('data-pc-name', 'ripple');
19+
ink.setAttribute('data-pc-section', 'root');
1720
ink.setAttribute('role', 'presentation');
1821
ink.setAttribute('aria-hidden', 'true');
1922
ink.setAttribute('data-p-ink', 'true');
23+
ink.setAttribute('data-p-ink-active', 'false');
2024
el.appendChild(ink);
25+
el.$_pDirectiveElement = ink;
2126

2227
ink.addEventListener('animationend', onAnimationEnd);
28+
29+
BaseDirective.directiveElement = ink;
30+
BaseDirective.handleCSS('ripple', ink, binding);
2331
}
2432

2533
function remove(el) {
@@ -40,7 +48,8 @@ function onMouseDown(event) {
4048
return;
4149
}
4250

43-
DomHandler.removeClass(ink, 'p-ink-active');
51+
!target.$_prippleUnstyled && DomHandler.removeClass(ink, 'p-ink-active');
52+
ink.setAttribute('data-p-ink-active', 'false');
4453

4554
if (!DomHandler.getHeight(ink) && !DomHandler.getWidth(ink)) {
4655
let d = Math.max(DomHandler.getOuterWidth(target), DomHandler.getOuterHeight(target));
@@ -55,11 +64,14 @@ function onMouseDown(event) {
5564

5665
ink.style.top = y + 'px';
5766
ink.style.left = x + 'px';
58-
DomHandler.addClass(ink, 'p-ink-active');
67+
68+
!target.$_prippleUnstyled && DomHandler.addClass(ink, 'p-ink-active');
69+
ink.setAttribute('data-p-ink-active', 'true');
5970

6071
timeout = setTimeout(() => {
6172
if (ink) {
62-
DomHandler.removeClass(ink, 'p-ink-active');
73+
!target.$_prippleUnstyled && DomHandler.removeClass(ink, 'p-ink-active');
74+
ink.setAttribute('data-p-ink-active', 'false');
6375
}
6476
}, 401);
6577
}
@@ -69,29 +81,35 @@ function onAnimationEnd(event) {
6981
clearTimeout(timeout);
7082
}
7183

72-
DomHandler.removeClass(event.currentTarget, 'p-ink-active');
84+
!event.currentTarget.$_prippleUnstyled && DomHandler.removeClass(event.currentTarget, 'p-ink-active');
85+
event.currentTarget.setAttribute('data-p-ink-active', 'false');
7386
}
7487

7588
function getInk(el) {
7689
for (let i = 0; i < el.children.length; i++) {
77-
if (typeof el.children[i].className === 'string' && el.children[i].className.indexOf('p-ink') !== -1) {
90+
if (el.children[i].getAttribute('data-pc-name') === 'ripple') {
7891
return el.children[i];
7992
}
8093
}
8194

8295
return null;
8396
}
8497

85-
const Ripple = {
98+
const Ripple = BaseDirective.extend('ripple', {
8699
mounted(el, binding) {
87-
if (binding.instance.$primevue && binding.instance.$primevue.config && binding.instance.$primevue.config.ripple) {
88-
create(el);
100+
const primevue = binding.instance.$primevue;
101+
102+
if (primevue && primevue.config && primevue.config.ripple) {
103+
el.$_prippleUnstyled = primevue.config.unstyled || false;
104+
105+
create(el, binding);
89106
bindEvents(el);
90107
}
91108
},
109+
92110
unmounted(el) {
93111
remove(el);
94112
}
95-
};
113+
});
96114

97115
export default Ripple;

0 commit comments

Comments
 (0)