Skip to content

Commit 61fca7b

Browse files
authored
Merge pull request #191 from teamEPYC/epycraghav-patch-12
Create buttons.js
2 parents 630cdb4 + c5f4a41 commit 61fca7b

File tree

1 file changed

+166
-0
lines changed

1 file changed

+166
-0
lines changed

kaviraj/buttons.js

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
console.log("Buttons.js loaded")
2+
3+
/* =============================================
4+
Global buttons
5+
================================================*/
6+
7+
/*
8+
// For Changeing the Width of the buttons
9+
10+
11+
document.querySelectorAll('[data-width]').forEach(el => {
12+
let w = el.getAttribute('data-width').trim();
13+
// if it's just a number (no px/rem/%) append "px"
14+
if (/^-?\d+(\.\d+)?$/.test(w)) {
15+
w += 'px';
16+
}
17+
// apply if non-empty
18+
if (w) {
19+
el.style.width = w;
20+
}
21+
});
22+
23+
*/
24+
25+
// For changing the color of the buttons according to the color propertity in the component
26+
27+
/*================================================
28+
Primary Button
29+
=================================================*/
30+
31+
// for changing the color of the button according to the property of the component
32+
const buttons = document.querySelectorAll('[button-color]');
33+
34+
buttons.forEach(button => {
35+
const color = button.getAttribute('button-color');
36+
if (!color) return;
37+
38+
const primaryButton = button.querySelector('.button-link');
39+
const arrow = button.querySelector('.button-icon-container.cc-hover');
40+
const content = button.querySelector('#button-content-hover')
41+
42+
if (primaryButton) {
43+
primaryButton.style.backgroundColor = color;
44+
primaryButton.style.borderColor = color;
45+
46+
}
47+
48+
if (arrow) {
49+
arrow.style.color = color;
50+
arrow.style.fill = color; // handles SVGs if any inside
51+
}
52+
53+
});
54+
55+
// Icon Animation
56+
57+
document.querySelectorAll('.button-link').forEach(link => {
58+
const iconIn = link.querySelector('.button-icon-container[data-hover="in"]');
59+
const iconOut = link.querySelector('.button-icon-container[data-hover="out"]');
60+
const text = link.querySelector('.button-text--primary');
61+
62+
const displacement = iconIn.offsetWidth / 2;
63+
64+
// Set initial states
65+
gsap.set(iconIn, { scale: 0 });
66+
gsap.set(iconOut, { scale: 1 });
67+
gsap.set(text, { x: -(displacement) });
68+
69+
let hoverInTween, hoverOutTween;
70+
71+
link.addEventListener('mouseenter', () => {
72+
if (hoverOutTween) hoverOutTween.kill();
73+
hoverInTween = gsap.timeline()
74+
.to(iconIn, { scale: 1, duration: 0.6, ease: 'expo.out' }, 0)
75+
.to(iconOut, { scale: 0, duration: 0.6, ease: 'expo.out' }, 0)
76+
.to(text, { x: displacement, duration: 0.6, ease: 'expo.out' }, 0);
77+
});
78+
79+
link.addEventListener('mouseleave', () => {
80+
if (hoverInTween) hoverInTween.kill();
81+
hoverOutTween = gsap.timeline()
82+
.to(iconIn, { scale: 0, duration: 0.3, ease: 'expo.out' }, 0)
83+
.to(iconOut, { scale: 1, duration: 0.3, ease: 'expo.out' }, 0)
84+
.to(text, { x: -(displacement), duration: 0.3, ease: 'expo.out' }, 0);
85+
});
86+
});
87+
88+
// gsap.utils.toArray(".button-link").forEach(button => {
89+
// const defArrow = button.querySelector(".button-icon-container:not(.cc-hover)");
90+
// const hovArrow = button.querySelector(".button-icon-container.cc-hover");
91+
92+
// // set your initial sizes
93+
// gsap.set(defArrow, { width: 38, height: 38 });
94+
// gsap.set(hovArrow, { width: 0, height: 0 });
95+
96+
// button.addEventListener("mouseenter", () => {
97+
// // cc-hover-in: shrink the default, grow the hover container
98+
// gsap.to(defArrow, { width: 0, height: 0, duration: 0.6, ease: "expo.out" });
99+
// gsap.to(hovArrow, { width: 38, height: 38, duration: 0.6, ease: "expo.out" });
100+
// });
101+
102+
// button.addEventListener("mouseleave", () => {
103+
// // hover-out: reverse sizes, still using expo.out
104+
// gsap.to(defArrow, { width: 38, height: 38, duration: 0.4, ease: "expo.out" });
105+
// gsap.to(hovArrow, { width: 0, height: 0, duration: 0.4, ease: "expo.out" });
106+
// });
107+
// });
108+
109+
/*================================================
110+
Secondary Button
111+
=================================================*/
112+
113+
gsap.utils.toArray('[data-scale="true"]').forEach(button => {
114+
const buttonContent = button.querySelectorAll(".button-content");
115+
116+
// Set initial position
117+
gsap.set(buttonContent, { y: 0 });
118+
119+
// hover in
120+
button.addEventListener("mouseenter", () => {
121+
gsap.to(buttonContent, {
122+
y: "-100%",
123+
duration: 0.8,
124+
ease: "expo.out"
125+
});
126+
});
127+
128+
// hover out
129+
button.addEventListener("mouseleave", () => {
130+
gsap.to(buttonContent, {
131+
y: "0%",
132+
duration: 0.5,
133+
ease: "expo.out"
134+
});
135+
});
136+
});
137+
138+
// for changing the color of the button according to the property of the component
139+
const secondaryButtonColor = document.querySelectorAll('.button-link--secondary');
140+
141+
buttons.forEach(button => {
142+
const color = button.getAttribute('button-color');
143+
if (!color) return;
144+
145+
const content = button.querySelector('.button-content.cc-hover')
146+
147+
if (content) {
148+
content.style.color = color;
149+
// arrow.style.fill = color; // handles SVGs if any inside
150+
151+
}
152+
});
153+
154+
/*================================================
155+
Teritory Button
156+
=================================================*/
157+
158+
// Change button background based on a data attribute
159+
document
160+
.querySelectorAll('.button-link--tertiary')
161+
.forEach(button => {
162+
// use the HTML5 data-* API
163+
const color = button.dataset.buttonColor; // reads data-button-color
164+
if (!color) return;
165+
button.style.backgroundColor = color;
166+
});

0 commit comments

Comments
 (0)