Skip to content

Commit

Permalink
Refactor #3965 - For Ripple
Browse files Browse the repository at this point in the history
  • Loading branch information
tugcekucukoglu committed Jun 22, 2023
1 parent a0927bd commit 0958ca3
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 12 deletions.
91 changes: 90 additions & 1 deletion components/lib/ripple/Ripple.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,99 @@
*/
import { DirectiveBinding, ObjectDirective } from 'vue';

/**
* Custom passthrough(pt) hooks options.
*/
export interface RipplePassThroughHooksOptions {
/**
* Called before bound element's attributes or event listeners are applied.
*/
created?: DirectiveBinding;
/**
* Called right before the element is inserted into the DOM.
*/
beforeMount?: DirectiveBinding;
/**
* Called when the bound element's parent component and all its children are mounted.
*/
mounted?: DirectiveBinding;
/**
* Called before the parent component is updated.
*/
beforeUpdate?: DirectiveBinding;
/**
* Called after the parent component and all of its children have updated all of its children have updated.
*/
updated?: DirectiveBinding;
/**
* Called before the parent component is unmounted.
*/
beforeUnmount?: DirectiveBinding;
/**
* Called when the parent component is unmounted.
*/
unmounted?: DirectiveBinding;
}

/**
* Custom passthrough(pt) css options.
*/
export interface RipplePassThroughCSSOptions {
/**
* Style class of the element.
*/
class?: any;
/**
* Inline style of the element.
*/
style?: any;
}

export interface RipplePassThroughDirectiveOptions {
/**
* Uses to pass attributes to the life cycle hooks.
* @see {@link RipplePassThroughHooksOptions}
*/
hooks?: RipplePassThroughHooksOptions;
/**
* Uses to pass attributes to the styles.
* @see {@link RipplePassThroughCSSOptions}
*/
css?: RipplePassThroughCSSOptions;
}

/**
* Custom passthrough(pt) options.
* @see {@link RippleOptions.pt}
*/
export interface RipplePassThroughOptions {
/**
* Uses to pass attributes to the root's DOM element.
* @see {@link RipplePassThroughDirectiveOptions}
*/
root?: RipplePassThroughDirectiveOptions;
}

/**
* Defines options of Ripple.
*/
export interface RippleOptions {
/**
* Uses to pass attributes to DOM elements inside the component.
* @type {RipplePassThroughOptions}
*/
pt?: RipplePassThroughOptions;
}

/**
* Binding of Ripple directive.
*/
export interface RippleDirectiveBinding extends Omit<DirectiveBinding, 'modifiers' | 'value'> {}
export interface RippleDirectiveBinding extends Omit<DirectiveBinding, 'modifiers' | 'value'> {
/**
* Value of the Ripple.
*/
value?: RippleOptions | undefined;
}

/**
* **PrimeVue - Ripple**
Expand Down
40 changes: 29 additions & 11 deletions components/lib/ripple/Ripple.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { BaseDirective } from 'primevue/basedirective';
import { DomHandler } from 'primevue/utils';

let timeout;
Expand All @@ -10,16 +11,23 @@ function unbindEvents(el) {
el.removeEventListener('mousedown', onMouseDown);
}

function create(el) {
function create(el, binding) {
let ink = document.createElement('span');

ink.className = 'p-ink';
!el.$_prippleUnstyled && (ink.className = 'p-ink');
ink.setAttribute('data-pc-name', 'ripple');
ink.setAttribute('data-pc-section', 'root');
ink.setAttribute('role', 'presentation');
ink.setAttribute('aria-hidden', 'true');
ink.setAttribute('data-p-ink', 'true');
ink.setAttribute('data-p-ink-active', 'false');
el.appendChild(ink);
el.$_pDirectiveElement = ink;

ink.addEventListener('animationend', onAnimationEnd);

BaseDirective.directiveElement = ink;
BaseDirective.handleCSS('ripple', ink, binding);
}

function remove(el) {
Expand All @@ -40,7 +48,8 @@ function onMouseDown(event) {
return;
}

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

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

ink.style.top = y + 'px';
ink.style.left = x + 'px';
DomHandler.addClass(ink, 'p-ink-active');

!target.$_prippleUnstyled && DomHandler.addClass(ink, 'p-ink-active');
ink.setAttribute('data-p-ink-active', 'true');

timeout = setTimeout(() => {
if (ink) {
DomHandler.removeClass(ink, 'p-ink-active');
!target.$_prippleUnstyled && DomHandler.removeClass(ink, 'p-ink-active');
ink.setAttribute('data-p-ink-active', 'false');
}
}, 401);
}
Expand All @@ -69,29 +81,35 @@ function onAnimationEnd(event) {
clearTimeout(timeout);
}

DomHandler.removeClass(event.currentTarget, 'p-ink-active');
!event.currentTarget.$_prippleUnstyled && DomHandler.removeClass(event.currentTarget, 'p-ink-active');
event.currentTarget.setAttribute('data-p-ink-active', 'false');
}

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

return null;
}

const Ripple = {
const Ripple = BaseDirective.extend('ripple', {
mounted(el, binding) {
if (binding.instance.$primevue && binding.instance.$primevue.config && binding.instance.$primevue.config.ripple) {
create(el);
const primevue = binding.instance.$primevue;

if (primevue && primevue.config && primevue.config.ripple) {
el.$_prippleUnstyled = primevue.config.unstyled || false;

create(el, binding);
bindEvents(el);
}
},

unmounted(el) {
remove(el);
}
};
});

export default Ripple;

0 comments on commit 0958ca3

Please sign in to comment.