Skip to content

Commit 3efd3cb

Browse files
committed
Fix button UX
- The past is now on the left - When you can't scroll further in one direction, the button is disabled
1 parent f83ee7b commit 3efd3cb

File tree

2 files changed

+81
-23
lines changed

2 files changed

+81
-23
lines changed

main.js

Lines changed: 75 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,85 @@ document.addEventListener('DOMContentLoaded', function() {
44
const scrollRightBtn = document.getElementById('meetups-scroll-right');
55

66
if (scrollLeftBtn && scrollRightBtn && meetupContainer) {
7-
scrollLeftBtn.addEventListener('click', () => {
8-
const containerWidth = meetupContainer.clientWidth;
9-
const scrollAmount = containerWidth * 1.0;
10-
meetupContainer.scrollBy({
11-
left: -scrollAmount,
12-
behavior: 'smooth'
13-
});
7+
const leftmostElemIsShown = () => {
8+
let { clientWidth, scrollLeft, scrollWidth } = meetupContainer
9+
scrollLeft = Math.abs(scrollLeft)
10+
errorMargin = scrollWidth * 0.1
11+
12+
res = (scrollLeft > scrollWidth - clientWidth - errorMargin)
13+
return res
14+
}
15+
const rightmostElemIsShown = () => {
16+
let { scrollLeft, scrollWidth } = meetupContainer
17+
scrollLeft = Math.abs(scrollLeft)
18+
errorMargin = scrollWidth * 0.1
19+
20+
res = (scrollLeft < errorMargin)
21+
return res
22+
}
23+
24+
const deactivate = (button) => {
25+
button.classList.add('opacity-0');
26+
button.classList.remove('opacity-100', 'hover:cursor-pointer', 'hover:scale-125');
27+
button.disabled = true;
28+
}
29+
30+
const activate = (button) => {
31+
button.classList.add('opacity-100', 'hover:cursor-pointer', 'hover:scale-125');
32+
button.classList.remove('opacity-0');
33+
button.disabled = false;
34+
}
35+
36+
// Function to update button visibility
37+
const updateButtonVisibility = () => {
38+
// Check if the container is scrolled to the left
39+
if (leftmostElemIsShown()) {
40+
deactivate(scrollLeftBtn)
41+
} else {
42+
activate(scrollLeftBtn)
43+
}
44+
45+
// Check if the container is scrolled to the right
46+
if (rightmostElemIsShown()) {
47+
deactivate(scrollRightBtn)
48+
} else {
49+
activate(scrollRightBtn)
50+
}
51+
};
52+
53+
// Initial check on page load
54+
updateButtonVisibility();
55+
56+
// Scroll Left Button
57+
scrollLeftBtn.addEventListener('click', () => {
58+
const containerWidth = meetupContainer.clientWidth;
59+
meetupContainer.scrollBy({
60+
left: -containerWidth,
61+
behavior: 'smooth'
1462
});
63+
});
1564

16-
scrollRightBtn.addEventListener('click', () => {
17-
const containerWidth = meetupContainer.clientWidth;
18-
const scrollAmount = containerWidth * 1.0;
19-
meetupContainer.scrollBy({
20-
left: scrollAmount,
21-
behavior: 'smooth'
22-
});
65+
// Scroll Right Button
66+
scrollRightBtn.addEventListener('click', () => {
67+
const containerWidth = meetupContainer.clientWidth;
68+
meetupContainer.scrollBy({
69+
left: containerWidth,
70+
behavior: 'smooth'
2371
});
72+
});
73+
74+
// Update buttons after scrolling
75+
meetupContainer.addEventListener('scroll', () => {
76+
// Throttle the updates to improve performance
77+
clearTimeout(meetupContainer.updateTimer);
78+
meetupContainer.updateTimer = setTimeout(updateButtonVisibility, 100);
79+
});
80+
81+
// Also update on window resize in case the container size changes
82+
window.addEventListener('resize', updateButtonVisibility);
83+
2484
} else {
25-
console.error('One or more required elements are missing from the DOM');
85+
console.error('One or more required elements are missing from the DOM');
2686
}
2787
});
2888

pollen.rkt

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,23 +67,21 @@
6767
`(div ([class "scroll-container mb-4 -mx-6 md:-mx-12 lg:-mx-24 md:gap-4 lg:gap-8 relative"])
6868
(div ([class "flex justify-between"])
6969
(button ([id "meetups-scroll-left"] [class "
70-
sticky -mr-6 sm:-mr-2 md:mr-0 left-0 top-0 transform
71-
text-zinc-500 bg-gradient-to-r from-white md:bg-white h-auto
72-
hover:cursor-pointer hover:scale-125
73-
duration-200 ease-out transition
70+
sticky -mr-6 sm:-mr-2 md:mr-0 left-0 top-0 transform
71+
text-zinc-500 bg-gradient-to-r from-white md:bg-white h-auto
72+
duration-200 ease-out transition
7473
z-10"])
7574
(svg ([xmlns "http://www.w3.org/2000/svg"] [fill "none"] [viewBox "0 0 24 24"] [stroke-width "1.5"] [stroke "currentColor"] [class "size-10"])
7675
(path ([stroke-linecap "round"] [stroke-linejoin "round"] [d "M15.75 19.5 8.25 12l7.5-7.5"]))
7776
)
7877
)
79-
(div ([id "meetups-container"] [class "relative flex items-center flex-grow snap-x overflow-x-auto"])
78+
(div ([id "meetups-container"] [class "relative flex flex-row-reverse items-center flex-grow snap-x overflow-x-auto"])
8079
,@body
8180
)
8281
(button ([id "meetups-scroll-right"] [class "
83-
sticky -ml-6 sm:-ml-2 md:ml-0 right-0 top-0 transform
82+
sticky -ml-6 sm:-ml-2 md:ml-0 right-0 top-0 transform
8483
text-zinc-500 bg-gradient-to-l from-white sm:bg-white h-auto
85-
hover:cursor-pointer hover:scale-125
86-
duration-200 ease-out transition
84+
duration-200 ease-out transition
8785
z-10"])
8886
(svg ([xmlns "http://www.w3.org/2000/svg"] [fill "none"] [viewBox "0 0 24 24"] [stroke-width "1.5"] [stroke "currentColor"] [class "size-10"])
8987
(path ([stroke-linecap "round"] [stroke-linejoin "round"] [d "m8.25 4.5 7.5 7.5-7.5 7.5"]))

0 commit comments

Comments
 (0)