Skip to content

Commit 8b4ff9a

Browse files
committed
feat: jsCssAnimation.init() accepting 'eventType' option
if eventType is not passed, it will be set to 'click' by default eventType can be any value valid to be passed to addEventListener() first argument
1 parent e8f7d71 commit 8b4ff9a

File tree

4 files changed

+33
-34
lines changed

4 files changed

+33
-34
lines changed

index.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,7 @@ <h2>Rotations</h2>
138138
name="rotation-angle"
139139
id="rotation-angle"
140140
/></label>
141-
<button class="rotation--btn">Rotate</button>
142-
<p class="rotation--input-error"></p>
141+
<p class="rotation--input-error" style="display: none"></p>
143142
<div class="rotation-area">
144143
<p class="rotation-area--text"></p>
145144
</div>

js-css-animations/animate.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -497,10 +497,11 @@ const eventHandler = (el, animationId, opts) => {
497497
* Initiate the event listener with the animation
498498
* @param {number} animationId - The ID of the animation in *_ANIMS_ID object
499499
* @param {Object} opts - All options passed by the user
500+
* @param {string} eventType - The event to attach the animation to
500501
* @see {@link module:globals.VISIBILITY_ANIMS_ID}
501502
* @see {@link module:globals.MOTION_ANIMS_ID}
502503
*/
503-
const init = (animationId, opts = {}) => {
504+
const init = (animationId, opts = {}, eventType = 'click') => {
504505
const {
505506
triggerBtn = `.${CLASS_NAMES.triggerBtn}`,
506507
targetSelector,
@@ -520,7 +521,7 @@ const init = (animationId, opts = {}) => {
520521
.querySelectorAll(getTargetSelector(btn))
521522
.forEach((el, i, queryList) => {
522523
btn.addEventListener(
523-
'click',
524+
eventType,
524525
// @ts-ignore
525526
eventHandler(el, animationId, {
526527
...opts,

js-css-animations/js-css-animations.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,9 @@ const eventBoundAnimations = (() => {
193193
* Initiate the event listener with the animation to be performed
194194
* @param {Object.<string, any>} opts - Contains all options passed by the user to customize the animation
195195
*/
196-
animations[animName] = opts =>
197-
init(animIds[animName], { animType, ...opts });
196+
animations[animName] = opts => {
197+
init(animIds[animName], { animType, ...opts }, opts.eventType);
198+
};
198199
});
199200
});
200201
return animations;

main.js

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -70,42 +70,40 @@ jsCssAnimations.init.fade({
7070
keepSpace: true,
7171
});
7272

73-
document.querySelector('.rotation--btn')?.addEventListener('click', () => {
74-
const input = document.getElementById('rotation-angle');
75-
const msgArea = document.querySelector('.rotation--input-error');
76-
// @ts-ignore
77-
const angle = input.value;
78-
// @ts-ignore
73+
const validateInput = input => {
7974
if (input.validity.patternMismatch) {
80-
jsCssAnimations.hide.fade(input, {
81-
duration: 200,
82-
keepSpace: true,
75+
const msgArea = document.querySelector('.rotation--input-error');
76+
// @ts-ignore
77+
msgArea.innerText = 'Type in a number (e.g.: 270, -22.5)';
78+
jsCssAnimations.show.fade(msgArea, {
8379
complete: () => {
84-
// @ts-ignore
85-
msgArea.innerText = 'Type in a number (e.g.: 10, -22.5, 270)';
8680
setTimeout(() => {
87-
jsCssAnimations.show.fade('#rotation-angle', {
88-
duration: 200,
89-
keepSpace: true,
90-
complete: () => {
91-
// @ts-ignore
92-
input.value = '';
93-
setTimeout(() => {
94-
// @ts-ignore
95-
msgArea.innerText = '';
96-
}, 2500);
97-
},
98-
});
81+
jsCssAnimations.hide.fade(msgArea, { delay: '2.5s' });
9982
}, 0);
10083
},
10184
});
85+
input.value = '';
86+
return false;
87+
} else {
88+
return true;
10289
}
90+
};
10391

104-
jsCssAnimations.rotate('.rotation-area', {
105-
angle: `${angle}deg`,
106-
duration: '2s',
107-
timingFunction: 'ease-in-out',
108-
});
92+
jsCssAnimations.init.rotate({
93+
triggerBtn: '#rotation-angle',
94+
targetSelector: '.rotation-area',
95+
eventType: 'change',
96+
// @ts-ignore
97+
start: () => {
98+
if (validateInput(document.querySelector('#rotation-angle'))) {
99+
// @ts-ignore
100+
const angle = `${document.getElementById('rotation-angle')?.value}deg`;
101+
jsCssAnimations.rotate('.rotation-area', { angle: angle });
102+
}
103+
},
104+
complete: () => {
105+
jsCssAnimations.rotate('.rotation-area', { angle: '0deg', delay: '1s' });
106+
},
109107
});
110108

111109
jsCssAnimations.show.fade('#anchor img', {

0 commit comments

Comments
 (0)